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 and two derived versions
852 static const struct got_error *
853 install_symlink_conflict(const char *deriv_target,
854 struct got_object_id *deriv_base_commit_id, const char *orig_target,
855 const char *label_orig, const char *local_target, const char *ondisk_path)
857 const struct got_error *err;
858 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
861 err = got_object_id_str(&id_str, deriv_base_commit_id);
863 return got_error_from_errno("asprintf");
865 if (asprintf(&label_deriv, "%s: commit %s",
866 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
867 err = got_error_from_errno("asprintf");
871 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
875 if (fprintf(f, "%s: Could not install symbolic link because of merge "
876 "conflict.\nln(1) may be used to fix the situation. If this is "
877 "intended to be a\nregular file instead then its expected "
878 "contents may be filled in.\nThe following conflicting symlink "
879 "target paths were found:\n"
880 "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
881 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv, deriv_target,
882 orig_target ? label_orig : "",
883 orig_target ? "\n" : "",
884 orig_target ? orig_target : "",
885 orig_target ? "\n" : "",
886 GOT_DIFF_CONFLICT_MARKER_SEP,
887 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
888 err = got_error_from_errno2("fprintf", path);
892 if (unlink(ondisk_path) == -1) {
893 err = got_error_from_errno2("unlink", ondisk_path);
896 if (rename(path, ondisk_path) == -1) {
897 err = got_error_from_errno3("rename", path, ondisk_path);
900 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
901 err = got_error_from_errno2("chmod", ondisk_path);
905 if (f != NULL && fclose(f) == EOF && err == NULL)
906 err = got_error_from_errno2("fclose", path);
914 * Merge a symlink into the work tree, where blob_orig acts as the common
915 * ancestor, blob_deriv acts as the first derived version, and the symlink
916 * on disk acts as the second derived version.
917 * Assume that contents of both blobs represent symlinks.
919 static const struct got_error *
920 merge_symlink(struct got_worktree *worktree,
921 struct got_blob_object *blob_orig, const char *ondisk_path,
922 const char *path, uint16_t st_mode, const char *label_orig,
923 struct got_blob_object *blob_deriv,
924 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
925 got_worktree_checkout_cb progress_cb, void *progress_arg)
927 const struct got_error *err = NULL;
928 char *ancestor_target = NULL, *deriv_target = NULL;
930 ssize_t ondisk_len, deriv_len;
931 char ondisk_target[PATH_MAX];
932 int have_local_change = 0;
933 int have_incoming_change = 0;
935 if (lstat(ondisk_path, &sb) == -1)
936 return got_error_from_errno2("lstat", ondisk_path);
938 if (!S_ISLNK(sb.st_mode)) {
939 /* TODO symlink is obstructed; do something */
940 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
943 ondisk_len = readlink(ondisk_path, ondisk_target,
944 sizeof(ondisk_target));
945 if (ondisk_len == -1) {
946 err = got_error_from_errno2("readlink",
952 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
957 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
961 if (ancestor_target == NULL ||
962 (ondisk_len != strlen(ancestor_target) ||
963 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
964 have_local_change = 1;
966 deriv_len = strlen(deriv_target);
967 if (ancestor_target == NULL ||
968 (deriv_len != strlen(ancestor_target) ||
969 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
970 have_incoming_change = 1;
972 if (!have_local_change && !have_incoming_change) {
973 if (ancestor_target) {
974 /* Both sides made the same change. */
975 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
977 } else if (deriv_len == ondisk_len &&
978 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
979 /* Both sides added the same symlink. */
980 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
983 /* Both sides added symlinks which don't match. */
984 err = install_symlink_conflict(deriv_target,
985 deriv_base_commit_id, ancestor_target,
986 label_orig, ondisk_target, ondisk_path);
989 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
992 } else if (!have_local_change && have_incoming_change) {
993 /* Apply the incoming change. */
994 err = update_symlink(ondisk_path, deriv_target,
995 strlen(deriv_target));
998 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
999 } else if (have_local_change && have_incoming_change) {
1000 err = install_symlink_conflict(deriv_target,
1001 deriv_base_commit_id, ancestor_target, label_orig,
1002 ondisk_target, ondisk_path);
1005 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1010 free(ancestor_target);
1016 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1017 * blob_deriv acts as the first derived version, and the file on disk
1018 * acts as the second derived version.
1020 static const struct got_error *
1021 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1022 struct got_blob_object *blob_orig, const char *ondisk_path,
1023 const char *path, uint16_t st_mode, const char *label_orig,
1024 struct got_blob_object *blob_deriv,
1025 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1026 got_worktree_checkout_cb progress_cb, void *progress_arg)
1028 const struct got_error *err = NULL;
1029 FILE *f_deriv = NULL;
1030 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1031 char *label_deriv = NULL, *parent;
1033 *local_changes_subsumed = 0;
1035 parent = dirname(ondisk_path);
1037 return got_error_from_errno2("dirname", ondisk_path);
1040 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1041 err = got_error_from_errno("asprintf");
1046 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1049 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1054 err = got_object_id_str(&id_str, deriv_base_commit_id);
1057 if (asprintf(&label_deriv, "%s: commit %s",
1058 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1059 err = got_error_from_errno("asprintf");
1063 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1064 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1065 label_deriv, repo, progress_cb, progress_arg);
1067 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1068 err = got_error_from_errno("fclose");
1070 if (blob_deriv_path) {
1071 unlink(blob_deriv_path);
1072 free(blob_deriv_path);
1079 static const struct got_error *
1080 create_fileindex_entry(struct got_fileindex *fileindex,
1081 struct got_object_id *base_commit_id, const char *ondisk_path,
1082 const char *path, struct got_object_id *blob_id)
1084 const struct got_error *err = NULL;
1085 struct got_fileindex_entry *new_ie;
1087 err = got_fileindex_entry_alloc(&new_ie, path);
1091 err = got_fileindex_entry_update(new_ie, ondisk_path,
1092 blob_id->sha1, base_commit_id->sha1, 1);
1096 err = got_fileindex_entry_add(fileindex, new_ie);
1099 got_fileindex_entry_free(new_ie);
1104 get_ondisk_perms(int executable, mode_t st_mode)
1106 mode_t xbits = S_IXUSR;
1109 /* Map read bits to execute bits. */
1110 if (st_mode & S_IRGRP)
1112 if (st_mode & S_IROTH)
1114 return st_mode | xbits;
1117 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1120 /* forward declaration */
1121 static const struct got_error *
1122 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1123 const char *path, mode_t te_mode, mode_t st_mode,
1124 struct got_blob_object *blob, int restoring_missing_file,
1125 int reverting_versioned_file, int installing_bad_symlink,
1126 struct got_repository *repo,
1127 got_worktree_checkout_cb progress_cb, void *progress_arg);
1130 * This function assumes that the provided symlink target points at a
1131 * safe location in the work tree!
1133 static const struct got_error *
1134 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1137 const struct got_error *err = NULL;
1139 char etarget[PATH_MAX];
1143 * "Bad" symlinks (those pointing outside the work tree or into the
1144 * .got directory) are installed in the work tree as a regular file
1145 * which contains the bad symlink target path.
1146 * The new symlink target has already been checked for safety by our
1147 * caller. If we can successfully open a regular file then we simply
1148 * replace this file with a symlink below.
1150 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1153 return got_error_from_errno2("open", ondisk_path);
1155 /* We are updating an existing on-disk symlink. */
1156 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1158 return got_error_from_errno2("readlink", ondisk_path);
1160 if (elen == target_len &&
1161 memcmp(etarget, target_path, target_len) == 0)
1162 return NULL; /* nothing to do */
1165 err = update_symlink(ondisk_path, target_path, target_len);
1166 if (fd != -1 && close(fd) == -1 && err == NULL)
1167 err = got_error_from_errno2("close", ondisk_path);
1171 static const struct got_error *
1172 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1173 const char *path, mode_t te_mode, mode_t st_mode,
1174 struct got_blob_object *blob, int restoring_missing_file,
1175 int reverting_versioned_file, struct got_repository *repo,
1176 got_worktree_checkout_cb progress_cb, void *progress_arg)
1178 const struct got_error *err = NULL;
1179 char target_path[PATH_MAX];
1180 size_t len, target_len = 0;
1181 char *resolved_path = NULL, *abspath = NULL;
1182 char *path_got = NULL;
1183 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1184 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1187 * Blob object content specifies the target path of the link.
1188 * If a symbolic link cannot be installed we instead create
1189 * a regular file which contains the link target path stored
1190 * in the blob object.
1193 err = got_object_blob_read_block(&len, blob);
1194 if (len + target_len >= sizeof(target_path)) {
1195 /* Path too long; install as a regular file. */
1196 got_object_blob_rewind(blob);
1197 return install_blob(worktree, ondisk_path, path,
1198 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1199 restoring_missing_file, reverting_versioned_file,
1200 1, repo, progress_cb, progress_arg);
1203 /* Skip blob object header first time around. */
1204 memcpy(target_path + target_len, buf + hdrlen,
1206 target_len += len - hdrlen;
1210 target_path[target_len] = '\0';
1213 * Relative symlink target lookup should begin at the directory
1214 * in which the blob object is being installed.
1216 if (!got_path_is_absolute(target_path)) {
1217 char *parent = dirname(ondisk_path);
1218 if (parent == NULL) {
1219 err = got_error_from_errno2("dirname", ondisk_path);
1222 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 err = got_error_from_errno("asprintf");
1229 * unveil(2) restricts our view of paths in the filesystem.
1230 * ENOENT will occur if a link target path does not exist or
1231 * if it points outside our unveiled path space.
1233 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1234 if (resolved_path == NULL) {
1235 if (errno != ENOENT) {
1236 err = got_error_from_errno2("realpath", target_path);
1241 /* Only allow symlinks pointing at paths within the work tree. */
1242 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1243 abspath : target_path), worktree->root_path,
1244 strlen(worktree->root_path))) {
1245 /* install as a regular file */
1246 got_object_blob_rewind(blob);
1247 err = install_blob(worktree, ondisk_path, path,
1248 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1249 restoring_missing_file, reverting_versioned_file, 1,
1250 repo, progress_cb, progress_arg);
1254 /* Do not allow symlinks pointing into the .got directory. */
1255 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1256 GOT_WORKTREE_GOT_DIR) == -1) {
1257 err = got_error_from_errno("asprintf");
1260 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1261 abspath : target_path), path_got, strlen(path_got))) {
1262 /* install as a regular file */
1263 got_object_blob_rewind(blob);
1264 err = install_blob(worktree, ondisk_path, path,
1265 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1266 restoring_missing_file, reverting_versioned_file, 1,
1267 repo, progress_cb, progress_arg);
1271 if (symlink(target_path, ondisk_path) == -1) {
1272 if (errno == EEXIST) {
1273 err = replace_existing_symlink(ondisk_path,
1274 target_path, target_len);
1278 err = (*progress_cb)(progress_arg,
1279 GOT_STATUS_UPDATE, path);
1281 goto done; /* Nothing else to do. */
1284 if (errno == ENOENT) {
1285 char *parent = dirname(ondisk_path);
1286 if (parent == NULL) {
1287 err = got_error_from_errno2("dirname",
1291 err = add_dir_on_disk(worktree, parent);
1295 * Retry, and fall through to error handling
1296 * below if this second attempt fails.
1298 if (symlink(target_path, ondisk_path) != -1) {
1299 err = NULL; /* success */
1304 /* Handle errors from first or second creation attempt. */
1305 if (errno == ENAMETOOLONG) {
1306 /* bad target path; install as a regular file */
1307 got_object_blob_rewind(blob);
1308 err = install_blob(worktree, ondisk_path, path,
1309 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1310 restoring_missing_file, reverting_versioned_file, 1,
1311 repo, progress_cb, progress_arg);
1312 } else if (errno == ENOTDIR) {
1313 err = got_error_path(ondisk_path,
1314 GOT_ERR_FILE_OBSTRUCTED);
1316 err = got_error_from_errno3("symlink",
1317 target_path, ondisk_path);
1319 } else if (progress_cb)
1320 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1322 free(resolved_path);
1328 static const struct got_error *
1329 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1330 const char *path, mode_t te_mode, mode_t st_mode,
1331 struct got_blob_object *blob, int restoring_missing_file,
1332 int reverting_versioned_file, int installing_bad_symlink,
1333 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1336 const struct got_error *err = NULL;
1340 char *tmppath = NULL;
1342 if (S_ISLNK(te_mode))
1343 return install_symlink(worktree, ondisk_path, path, te_mode,
1344 st_mode, blob, restoring_missing_file,
1345 reverting_versioned_file, repo, progress_cb, progress_arg);
1347 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1348 GOT_DEFAULT_FILE_MODE);
1350 if (errno == ENOENT) {
1351 char *parent = dirname(path);
1353 return got_error_from_errno2("dirname", path);
1354 err = add_dir_on_disk(worktree, parent);
1357 fd = open(ondisk_path,
1358 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1359 GOT_DEFAULT_FILE_MODE);
1361 return got_error_from_errno2("open",
1363 } else if (errno == EEXIST) {
1364 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1365 /* TODO file is obstructed; do something */
1366 err = got_error_path(ondisk_path,
1367 GOT_ERR_FILE_OBSTRUCTED);
1370 err = got_opentemp_named_fd(&tmppath, &fd,
1377 return got_error_from_errno2("open", ondisk_path);
1381 if (restoring_missing_file)
1382 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1384 else if (reverting_versioned_file)
1385 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1388 err = (*progress_cb)(progress_arg,
1389 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1394 hdrlen = got_object_blob_get_hdrlen(blob);
1396 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1397 err = got_object_blob_read_block(&len, blob);
1401 /* Skip blob object header first time around. */
1402 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1404 err = got_error_from_errno("write");
1406 } else if (outlen != len - hdrlen) {
1407 err = got_error(GOT_ERR_IO);
1414 if (fsync(fd) != 0) {
1415 err = got_error_from_errno("fsync");
1420 if (rename(tmppath, ondisk_path) != 0) {
1421 err = got_error_from_errno3("rename", tmppath,
1428 if (chmod(ondisk_path,
1429 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1430 err = got_error_from_errno2("chmod", ondisk_path);
1435 if (fd != -1 && close(fd) != 0 && err == NULL)
1436 err = got_error_from_errno("close");
1441 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1442 static const struct got_error *
1443 get_modified_file_content_status(unsigned char *status, FILE *f)
1445 const struct got_error *err = NULL;
1446 const char *markers[3] = {
1447 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1448 GOT_DIFF_CONFLICT_MARKER_SEP,
1449 GOT_DIFF_CONFLICT_MARKER_END
1454 const char delim[3] = {'\0', '\0', '\0'};
1456 while (*status == GOT_STATUS_MODIFY) {
1457 line = fparseln(f, &len, NULL, delim, 0);
1461 err = got_ferror(f, GOT_ERR_IO);
1465 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1466 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1468 *status = GOT_STATUS_CONFLICT;
1478 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1480 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1481 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1485 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1487 return !(ie->ctime_sec == sb->st_ctime &&
1488 ie->ctime_nsec == sb->st_ctimensec &&
1489 ie->mtime_sec == sb->st_mtime &&
1490 ie->mtime_nsec == sb->st_mtimensec &&
1491 ie->size == (sb->st_size & 0xffffffff) &&
1492 !xbit_differs(ie, sb->st_mode));
1495 static unsigned char
1496 get_staged_status(struct got_fileindex_entry *ie)
1498 switch (got_fileindex_entry_stage_get(ie)) {
1499 case GOT_FILEIDX_STAGE_ADD:
1500 return GOT_STATUS_ADD;
1501 case GOT_FILEIDX_STAGE_DELETE:
1502 return GOT_STATUS_DELETE;
1503 case GOT_FILEIDX_STAGE_MODIFY:
1504 return GOT_STATUS_MODIFY;
1506 return GOT_STATUS_NO_CHANGE;
1510 static const struct got_error *
1511 get_symlink_status(unsigned char *status, struct stat *sb,
1512 struct got_fileindex_entry *ie, const char *abspath,
1513 int dirfd, const char *de_name, struct got_blob_object *blob)
1515 const struct got_error *err = NULL;
1516 char target_path[PATH_MAX];
1517 char etarget[PATH_MAX];
1519 size_t len, target_len = 0;
1520 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1521 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1523 *status = GOT_STATUS_NO_CHANGE;
1525 /* Blob object content specifies the target path of the link. */
1527 err = got_object_blob_read_block(&len, blob);
1530 if (len + target_len >= sizeof(target_path)) {
1532 * Should not happen. The blob contents were OK
1533 * when this symlink was installed.
1535 return got_error(GOT_ERR_NO_SPACE);
1538 /* Skip blob object header first time around. */
1539 memcpy(target_path + target_len, buf + hdrlen,
1541 target_len += len - hdrlen;
1545 target_path[target_len] = '\0';
1548 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1550 return got_error_from_errno2("readlinkat", abspath);
1552 elen = readlink(abspath, etarget, sizeof(etarget));
1554 return got_error_from_errno2("readlinkat", abspath);
1557 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1558 *status = GOT_STATUS_MODIFY;
1563 static const struct got_error *
1564 get_file_status(unsigned char *status, struct stat *sb,
1565 struct got_fileindex_entry *ie, const char *abspath,
1566 int dirfd, const char *de_name, struct got_repository *repo)
1568 const struct got_error *err = NULL;
1569 struct got_object_id id;
1574 struct got_blob_object *blob = NULL;
1576 unsigned char staged_status = get_staged_status(ie);
1578 *status = GOT_STATUS_NO_CHANGE;
1581 * Whenever the caller provides a directory descriptor and a
1582 * directory entry name for the file, use them! This prevents
1583 * race conditions if filesystem paths change beneath our feet.
1586 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1587 if (errno == ENOENT) {
1588 if (got_fileindex_entry_has_file_on_disk(ie))
1589 *status = GOT_STATUS_MISSING;
1591 *status = GOT_STATUS_DELETE;
1594 err = got_error_from_errno2("fstatat", abspath);
1598 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1599 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1600 return got_error_from_errno2("open", abspath);
1601 else if (fd == -1 && errno == ELOOP) {
1602 if (lstat(abspath, sb) == -1)
1603 return got_error_from_errno2("lstat", abspath);
1604 } else if (fd == -1 || fstat(fd, sb) == -1) {
1605 if (errno == ENOENT) {
1606 if (got_fileindex_entry_has_file_on_disk(ie))
1607 *status = GOT_STATUS_MISSING;
1609 *status = GOT_STATUS_DELETE;
1612 err = got_error_from_errno2("fstat", abspath);
1617 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1618 *status = GOT_STATUS_OBSTRUCTED;
1622 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1623 *status = GOT_STATUS_DELETE;
1625 } else if (!got_fileindex_entry_has_blob(ie) &&
1626 staged_status != GOT_STATUS_ADD) {
1627 *status = GOT_STATUS_ADD;
1631 if (!stat_info_differs(ie, sb))
1634 if (staged_status == GOT_STATUS_MODIFY ||
1635 staged_status == GOT_STATUS_ADD)
1636 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1638 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1640 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1644 if (S_ISLNK(sb->st_mode)) {
1645 /* Staging changes to symlinks is not yet(?) supported. */
1646 if (staged_status != GOT_STATUS_NO_CHANGE) {
1647 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1650 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1657 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1659 err = got_error_from_errno2("openat", abspath);
1664 f = fdopen(fd, "r");
1666 err = got_error_from_errno2("fdopen", abspath);
1670 hdrlen = got_object_blob_get_hdrlen(blob);
1672 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1673 err = got_object_blob_read_block(&blen, blob);
1676 /* Skip length of blob object header first time around. */
1677 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1678 if (flen == 0 && ferror(f)) {
1679 err = got_error_from_errno("fread");
1684 *status = GOT_STATUS_MODIFY;
1686 } else if (flen == 0) {
1688 *status = GOT_STATUS_MODIFY;
1690 } else if (blen - hdrlen == flen) {
1691 /* Skip blob object header first time around. */
1692 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1693 *status = GOT_STATUS_MODIFY;
1697 *status = GOT_STATUS_MODIFY;
1703 if (*status == GOT_STATUS_MODIFY) {
1705 err = get_modified_file_content_status(status, f);
1706 } else if (xbit_differs(ie, sb->st_mode))
1707 *status = GOT_STATUS_MODE_CHANGE;
1710 got_object_blob_close(blob);
1711 if (f != NULL && fclose(f) == EOF && err == NULL)
1712 err = got_error_from_errno2("fclose", abspath);
1713 if (fd != -1 && close(fd) == -1 && err == NULL)
1714 err = got_error_from_errno2("close", abspath);
1719 * Update timestamps in the file index if a file is unmodified and
1720 * we had to run a full content comparison to find out.
1722 static const struct got_error *
1723 sync_timestamps(char *ondisk_path, unsigned char status,
1724 struct got_fileindex_entry *ie, struct stat *sb)
1726 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1727 return got_fileindex_entry_update(ie, ondisk_path,
1728 ie->blob_sha1, ie->commit_sha1, 1);
1733 static const struct got_error *
1734 update_blob(struct got_worktree *worktree,
1735 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1736 struct got_tree_entry *te, const char *path,
1737 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1740 const struct got_error *err = NULL;
1741 struct got_blob_object *blob = NULL;
1743 unsigned char status = GOT_STATUS_NO_CHANGE;
1746 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1747 return got_error_from_errno("asprintf");
1750 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1751 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1754 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1758 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1759 sb.st_mode = got_fileindex_perms_to_st(ie);
1761 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1763 if (status == GOT_STATUS_OBSTRUCTED) {
1764 err = (*progress_cb)(progress_arg, status, path);
1767 if (status == GOT_STATUS_CONFLICT) {
1768 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1773 if (ie && status != GOT_STATUS_MISSING &&
1774 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1775 if (got_fileindex_entry_has_commit(ie) &&
1776 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1777 SHA1_DIGEST_LENGTH) == 0) {
1778 err = sync_timestamps(ondisk_path, status, ie, &sb);
1781 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1785 if (got_fileindex_entry_has_blob(ie) &&
1786 memcmp(ie->blob_sha1, te->id.sha1,
1787 SHA1_DIGEST_LENGTH) == 0) {
1788 err = sync_timestamps(ondisk_path, status, ie, &sb);
1793 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1797 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1798 int update_timestamps;
1799 struct got_blob_object *blob2 = NULL;
1800 char *label_orig = NULL;
1801 if (got_fileindex_entry_has_blob(ie)) {
1802 struct got_object_id id2;
1803 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1804 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1808 if (got_fileindex_entry_has_commit(ie)) {
1809 char id_str[SHA1_DIGEST_STRING_LENGTH];
1810 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1811 sizeof(id_str)) == NULL) {
1812 err = got_error_path(id_str,
1813 GOT_ERR_BAD_OBJ_ID_STR);
1816 if (asprintf(&label_orig, "%s: commit %s",
1817 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1818 err = got_error_from_errno("asprintf");
1822 err = merge_blob(&update_timestamps, worktree, blob2,
1823 ondisk_path, path, sb.st_mode, label_orig, blob,
1824 worktree->base_commit_id, repo,
1825 progress_cb, progress_arg);
1828 got_object_blob_close(blob2);
1832 * Do not update timestamps of files with local changes.
1833 * Otherwise, a future status walk would treat them as
1834 * unmodified files again.
1836 err = got_fileindex_entry_update(ie, ondisk_path,
1837 blob->id.sha1, worktree->base_commit_id->sha1,
1839 } else if (status == GOT_STATUS_MODE_CHANGE) {
1840 err = got_fileindex_entry_update(ie, ondisk_path,
1841 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1842 } else if (status == GOT_STATUS_DELETE) {
1843 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1846 err = got_fileindex_entry_update(ie, ondisk_path,
1847 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1851 err = install_blob(worktree, ondisk_path, path, te->mode,
1852 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1853 repo, progress_cb, progress_arg);
1857 err = got_fileindex_entry_update(ie, ondisk_path,
1858 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1860 err = create_fileindex_entry(fileindex,
1861 worktree->base_commit_id, ondisk_path, path,
1867 got_object_blob_close(blob);
1873 static const struct got_error *
1874 remove_ondisk_file(const char *root_path, const char *path)
1876 const struct got_error *err = NULL;
1877 char *ondisk_path = NULL;
1879 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1880 return got_error_from_errno("asprintf");
1882 if (unlink(ondisk_path) == -1) {
1883 if (errno != ENOENT)
1884 err = got_error_from_errno2("unlink", ondisk_path);
1886 char *parent = dirname(ondisk_path);
1887 while (parent && strcmp(parent, root_path) != 0) {
1888 if (rmdir(parent) == -1) {
1889 if (errno != ENOTEMPTY)
1890 err = got_error_from_errno2("rmdir",
1894 parent = dirname(parent);
1901 static const struct got_error *
1902 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1903 struct got_fileindex_entry *ie, struct got_repository *repo,
1904 got_worktree_checkout_cb progress_cb, void *progress_arg)
1906 const struct got_error *err = NULL;
1907 unsigned char status;
1911 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1912 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1914 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1916 return got_error_from_errno("asprintf");
1918 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1922 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1923 status == GOT_STATUS_ADD) {
1924 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1928 * Preserve the working file and change the deleted blob's
1929 * entry into a schedule-add entry.
1931 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1934 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1937 if (status == GOT_STATUS_NO_CHANGE) {
1938 err = remove_ondisk_file(worktree->root_path, ie->path);
1942 got_fileindex_entry_remove(fileindex, ie);
1949 struct diff_cb_arg {
1950 struct got_fileindex *fileindex;
1951 struct got_worktree *worktree;
1952 struct got_repository *repo;
1953 got_worktree_checkout_cb progress_cb;
1955 got_cancel_cb cancel_cb;
1959 static const struct got_error *
1960 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1961 struct got_tree_entry *te, const char *parent_path)
1963 struct diff_cb_arg *a = arg;
1965 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1966 return got_error(GOT_ERR_CANCELLED);
1968 return update_blob(a->worktree, a->fileindex, ie, te,
1969 ie->path, a->repo, a->progress_cb, a->progress_arg);
1972 static const struct got_error *
1973 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1975 struct diff_cb_arg *a = arg;
1977 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1978 return got_error(GOT_ERR_CANCELLED);
1980 return delete_blob(a->worktree, a->fileindex, ie,
1981 a->repo, a->progress_cb, a->progress_arg);
1984 static const struct got_error *
1985 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1987 struct diff_cb_arg *a = arg;
1988 const struct got_error *err;
1991 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1992 return got_error(GOT_ERR_CANCELLED);
1994 if (got_object_tree_entry_is_submodule(te))
1997 if (asprintf(&path, "%s%s%s", parent_path,
1998 parent_path[0] ? "/" : "", te->name)
2000 return got_error_from_errno("asprintf");
2002 if (S_ISDIR(te->mode))
2003 err = add_dir_on_disk(a->worktree, path);
2005 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2006 a->repo, a->progress_cb, a->progress_arg);
2012 static const struct got_error *
2013 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2015 const struct got_error *err = NULL;
2016 char *uuidstr = NULL;
2017 uint32_t uuid_status;
2021 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2022 if (uuid_status != uuid_s_ok)
2023 return got_error_uuid(uuid_status, "uuid_to_string");
2025 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2027 err = got_error_from_errno("asprintf");
2034 const struct got_error *
2035 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2037 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2040 static const struct got_error *
2041 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2043 return get_ref_name(refname, worktree,
2044 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2047 static const struct got_error *
2048 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2050 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2053 static const struct got_error *
2054 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2056 return get_ref_name(refname, worktree,
2057 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2060 static const struct got_error *
2061 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2063 return get_ref_name(refname, worktree,
2064 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2067 static const struct got_error *
2068 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2070 return get_ref_name(refname, worktree,
2071 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2074 static const struct got_error *
2075 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2077 return get_ref_name(refname, worktree,
2078 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2081 static const struct got_error *
2082 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2084 return get_ref_name(refname, worktree,
2085 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2088 static const struct got_error *
2089 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2091 return get_ref_name(refname, worktree,
2092 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2095 const struct got_error *
2096 got_worktree_get_histedit_script_path(char **path,
2097 struct got_worktree *worktree)
2099 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2100 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2102 return got_error_from_errno("asprintf");
2108 * Prevent Git's garbage collector from deleting our base commit by
2109 * setting a reference to our base commit's ID.
2111 static const struct got_error *
2112 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2114 const struct got_error *err = NULL;
2115 struct got_reference *ref = NULL;
2118 err = got_worktree_get_base_ref_name(&refname, worktree);
2122 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2126 err = got_ref_write(ref, repo);
2134 static const struct got_error *
2135 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2137 const struct got_error *err = NULL;
2139 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2140 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2141 err = got_error_from_errno("asprintf");
2142 *fileindex_path = NULL;
2148 static const struct got_error *
2149 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2150 struct got_worktree *worktree)
2152 const struct got_error *err = NULL;
2155 *fileindex_path = NULL;
2156 *fileindex = got_fileindex_alloc();
2157 if (*fileindex == NULL)
2158 return got_error_from_errno("got_fileindex_alloc");
2160 err = get_fileindex_path(fileindex_path, worktree);
2164 index = fopen(*fileindex_path, "rb");
2165 if (index == NULL) {
2166 if (errno != ENOENT)
2167 err = got_error_from_errno2("fopen", *fileindex_path);
2169 err = got_fileindex_read(*fileindex, index);
2170 if (fclose(index) != 0 && err == NULL)
2171 err = got_error_from_errno("fclose");
2175 free(*fileindex_path);
2176 *fileindex_path = NULL;
2177 got_fileindex_free(*fileindex);
2183 struct bump_base_commit_id_arg {
2184 struct got_object_id *base_commit_id;
2187 const char *entry_name;
2188 got_worktree_checkout_cb progress_cb;
2192 /* Bump base commit ID of all files within an updated part of the work tree. */
2193 static const struct got_error *
2194 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2196 const struct got_error *err;
2197 struct bump_base_commit_id_arg *a = arg;
2199 if (a->entry_name) {
2200 if (strcmp(ie->path, a->path) != 0)
2202 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2205 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2206 SHA1_DIGEST_LENGTH) == 0)
2209 if (a->progress_cb) {
2210 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2215 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2219 static const struct got_error *
2220 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2222 const struct got_error *err = NULL;
2223 char *new_fileindex_path = NULL;
2224 FILE *new_index = NULL;
2225 struct timespec timeout;
2227 err = got_opentemp_named(&new_fileindex_path, &new_index,
2232 err = got_fileindex_write(fileindex, new_index);
2236 if (rename(new_fileindex_path, fileindex_path) != 0) {
2237 err = got_error_from_errno3("rename", new_fileindex_path,
2239 unlink(new_fileindex_path);
2243 * Sleep for a short amount of time to ensure that files modified after
2244 * this program exits have a different time stamp from the one which
2245 * was recorded in the file index.
2248 timeout.tv_nsec = 1;
2249 nanosleep(&timeout, NULL);
2253 free(new_fileindex_path);
2257 static const struct got_error *
2258 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2259 struct got_object_id **tree_id, const char *wt_relpath,
2260 struct got_worktree *worktree, struct got_repository *repo)
2262 const struct got_error *err = NULL;
2263 struct got_object_id *id = NULL;
2264 char *in_repo_path = NULL;
2265 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2267 *entry_type = GOT_OBJ_TYPE_ANY;
2268 *tree_relpath = NULL;
2271 if (wt_relpath[0] == '\0') {
2272 /* Check out all files within the work tree. */
2273 *entry_type = GOT_OBJ_TYPE_TREE;
2274 *tree_relpath = strdup("");
2275 if (*tree_relpath == NULL) {
2276 err = got_error_from_errno("strdup");
2279 err = got_object_id_by_path(tree_id, repo,
2280 worktree->base_commit_id, worktree->path_prefix);
2286 /* Check out a subset of files in the work tree. */
2288 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2289 is_root_wt ? "" : "/", wt_relpath) == -1) {
2290 err = got_error_from_errno("asprintf");
2294 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2300 in_repo_path = NULL;
2302 err = got_object_get_type(entry_type, repo, id);
2306 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2307 /* Check out a single file. */
2308 if (strchr(wt_relpath, '/') == NULL) {
2309 /* Check out a single file in work tree's root dir. */
2310 in_repo_path = strdup(worktree->path_prefix);
2311 if (in_repo_path == NULL) {
2312 err = got_error_from_errno("strdup");
2315 *tree_relpath = strdup("");
2316 if (*tree_relpath == NULL) {
2317 err = got_error_from_errno("strdup");
2321 /* Check out a single file in a subdirectory. */
2322 err = got_path_dirname(tree_relpath, wt_relpath);
2325 if (asprintf(&in_repo_path, "%s%s%s",
2326 worktree->path_prefix, is_root_wt ? "" : "/",
2327 *tree_relpath) == -1) {
2328 err = got_error_from_errno("asprintf");
2332 err = got_object_id_by_path(tree_id, repo,
2333 worktree->base_commit_id, in_repo_path);
2335 /* Check out all files within a subdirectory. */
2336 *tree_id = got_object_id_dup(id);
2337 if (*tree_id == NULL) {
2338 err = got_error_from_errno("got_object_id_dup");
2341 *tree_relpath = strdup(wt_relpath);
2342 if (*tree_relpath == NULL) {
2343 err = got_error_from_errno("strdup");
2351 *entry_type = GOT_OBJ_TYPE_ANY;
2352 free(*tree_relpath);
2353 *tree_relpath = NULL;
2360 static const struct got_error *
2361 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2362 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2363 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2364 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2366 const struct got_error *err = NULL;
2367 struct got_commit_object *commit = NULL;
2368 struct got_tree_object *tree = NULL;
2369 struct got_fileindex_diff_tree_cb diff_cb;
2370 struct diff_cb_arg arg;
2372 err = ref_base_commit(worktree, repo);
2374 if (!(err->code == GOT_ERR_ERRNO &&
2375 (errno == EACCES || errno == EROFS)))
2377 err = (*progress_cb)(progress_arg,
2378 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2383 err = got_object_open_as_commit(&commit, repo,
2384 worktree->base_commit_id);
2388 err = got_object_open_as_tree(&tree, repo, tree_id);
2393 got_object_tree_find_entry(tree, entry_name) == NULL) {
2394 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2398 diff_cb.diff_old_new = diff_old_new;
2399 diff_cb.diff_old = diff_old;
2400 diff_cb.diff_new = diff_new;
2401 arg.fileindex = fileindex;
2402 arg.worktree = worktree;
2404 arg.progress_cb = progress_cb;
2405 arg.progress_arg = progress_arg;
2406 arg.cancel_cb = cancel_cb;
2407 arg.cancel_arg = cancel_arg;
2408 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2409 entry_name, repo, &diff_cb, &arg);
2412 got_object_tree_close(tree);
2414 got_object_commit_close(commit);
2418 const struct got_error *
2419 got_worktree_checkout_files(struct got_worktree *worktree,
2420 struct got_pathlist_head *paths, struct got_repository *repo,
2421 got_worktree_checkout_cb progress_cb, void *progress_arg,
2422 got_cancel_cb cancel_cb, void *cancel_arg)
2424 const struct got_error *err = NULL, *sync_err, *unlockerr;
2425 struct got_commit_object *commit = NULL;
2426 struct got_tree_object *tree = NULL;
2427 struct got_fileindex *fileindex = NULL;
2428 char *fileindex_path = NULL;
2429 struct got_pathlist_entry *pe;
2430 struct tree_path_data {
2431 SIMPLEQ_ENTRY(tree_path_data) entry;
2432 struct got_object_id *tree_id;
2437 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2439 SIMPLEQ_INIT(&tree_paths);
2441 err = lock_worktree(worktree, LOCK_EX);
2445 /* Map all specified paths to in-repository trees. */
2446 TAILQ_FOREACH(pe, paths, entry) {
2447 tpd = malloc(sizeof(*tpd));
2449 err = got_error_from_errno("malloc");
2453 err = find_tree_entry_for_checkout(&tpd->entry_type,
2454 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2460 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2461 err = got_path_basename(&tpd->entry_name, pe->path);
2469 tpd->entry_name = NULL;
2471 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2475 * Read the file index.
2476 * Checking out files is supposed to be an idempotent operation.
2477 * If the on-disk file index is incomplete we will try to complete it.
2479 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2483 tpd = SIMPLEQ_FIRST(&tree_paths);
2484 TAILQ_FOREACH(pe, paths, entry) {
2485 struct bump_base_commit_id_arg bbc_arg;
2487 err = checkout_files(worktree, fileindex, tpd->relpath,
2488 tpd->tree_id, tpd->entry_name, repo,
2489 progress_cb, progress_arg, cancel_cb, cancel_arg);
2493 bbc_arg.base_commit_id = worktree->base_commit_id;
2494 bbc_arg.entry_name = tpd->entry_name;
2495 bbc_arg.path = pe->path;
2496 bbc_arg.path_len = pe->path_len;
2497 bbc_arg.progress_cb = progress_cb;
2498 bbc_arg.progress_arg = progress_arg;
2499 err = got_fileindex_for_each_entry_safe(fileindex,
2500 bump_base_commit_id, &bbc_arg);
2504 tpd = SIMPLEQ_NEXT(tpd, entry);
2506 sync_err = sync_fileindex(fileindex, fileindex_path);
2507 if (sync_err && err == NULL)
2510 free(fileindex_path);
2512 got_object_tree_close(tree);
2514 got_object_commit_close(commit);
2516 got_fileindex_free(fileindex);
2517 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2518 tpd = SIMPLEQ_FIRST(&tree_paths);
2519 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2524 unlockerr = lock_worktree(worktree, LOCK_SH);
2525 if (unlockerr && err == NULL)
2530 struct merge_file_cb_arg {
2531 struct got_worktree *worktree;
2532 struct got_fileindex *fileindex;
2533 got_worktree_checkout_cb progress_cb;
2535 got_cancel_cb cancel_cb;
2537 const char *label_orig;
2538 struct got_object_id *commit_id2;
2541 static const struct got_error *
2542 merge_file_cb(void *arg, struct got_blob_object *blob1,
2543 struct got_blob_object *blob2, struct got_object_id *id1,
2544 struct got_object_id *id2, const char *path1, const char *path2,
2545 mode_t mode1, mode_t mode2, struct got_repository *repo)
2547 static const struct got_error *err = NULL;
2548 struct merge_file_cb_arg *a = arg;
2549 struct got_fileindex_entry *ie;
2550 char *ondisk_path = NULL;
2552 unsigned char status;
2553 int local_changes_subsumed;
2555 if (blob1 && blob2) {
2556 ie = got_fileindex_entry_get(a->fileindex, path2,
2559 return (*a->progress_cb)(a->progress_arg,
2560 GOT_STATUS_MISSING, path2);
2562 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2564 return got_error_from_errno("asprintf");
2566 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2571 if (status == GOT_STATUS_DELETE) {
2572 err = (*a->progress_cb)(a->progress_arg,
2573 GOT_STATUS_MERGE, path2);
2576 if (status != GOT_STATUS_NO_CHANGE &&
2577 status != GOT_STATUS_MODIFY &&
2578 status != GOT_STATUS_CONFLICT &&
2579 status != GOT_STATUS_ADD) {
2580 err = (*a->progress_cb)(a->progress_arg, status, path2);
2584 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2585 err = merge_symlink(a->worktree, blob1,
2586 ondisk_path, path2, sb.st_mode, a->label_orig,
2587 blob2, a->commit_id2, repo, a->progress_cb,
2590 err = merge_blob(&local_changes_subsumed, a->worktree,
2591 blob1, ondisk_path, path2, sb.st_mode,
2592 a->label_orig, blob2, a->commit_id2, repo,
2593 a->progress_cb, a->progress_arg);
2596 ie = got_fileindex_entry_get(a->fileindex, path1,
2599 return (*a->progress_cb)(a->progress_arg,
2600 GOT_STATUS_MISSING, path1);
2602 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2604 return got_error_from_errno("asprintf");
2606 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2612 case GOT_STATUS_NO_CHANGE:
2613 err = (*a->progress_cb)(a->progress_arg,
2614 GOT_STATUS_DELETE, path1);
2617 err = remove_ondisk_file(a->worktree->root_path, path1);
2621 got_fileindex_entry_mark_deleted_from_disk(ie);
2623 case GOT_STATUS_DELETE:
2624 case GOT_STATUS_MISSING:
2625 err = (*a->progress_cb)(a->progress_arg,
2626 GOT_STATUS_DELETE, path1);
2630 got_fileindex_entry_mark_deleted_from_disk(ie);
2632 case GOT_STATUS_ADD:
2633 case GOT_STATUS_MODIFY:
2634 case GOT_STATUS_CONFLICT:
2635 err = (*a->progress_cb)(a->progress_arg,
2636 GOT_STATUS_CANNOT_DELETE, path1);
2640 case GOT_STATUS_OBSTRUCTED:
2641 err = (*a->progress_cb)(a->progress_arg, status, path1);
2649 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2651 return got_error_from_errno("asprintf");
2652 ie = got_fileindex_entry_get(a->fileindex, path2,
2655 err = get_file_status(&status, &sb, ie, ondisk_path,
2659 if (status != GOT_STATUS_NO_CHANGE &&
2660 status != GOT_STATUS_MODIFY &&
2661 status != GOT_STATUS_CONFLICT &&
2662 status != GOT_STATUS_ADD) {
2663 err = (*a->progress_cb)(a->progress_arg,
2667 if (S_ISLNK(mode2)) {
2668 err = merge_symlink(a->worktree, NULL,
2669 ondisk_path, path2, sb.st_mode,
2670 a->label_orig, blob2, a->commit_id2,
2671 repo, a->progress_cb, a->progress_arg);
2675 err = merge_blob(&local_changes_subsumed,
2676 a->worktree, NULL, ondisk_path, path2,
2677 sb.st_mode, a->label_orig, blob2,
2678 a->commit_id2, repo, a->progress_cb,
2683 if (status == GOT_STATUS_DELETE) {
2684 err = got_fileindex_entry_update(ie,
2685 ondisk_path, blob2->id.sha1,
2686 a->worktree->base_commit_id->sha1, 0);
2691 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2692 err = install_blob(a->worktree, ondisk_path, path2,
2693 /* XXX get this from parent tree! */
2694 GOT_DEFAULT_FILE_MODE,
2695 sb.st_mode, blob2, 0, 0, 0, repo,
2696 a->progress_cb, a->progress_arg);
2699 err = got_fileindex_entry_alloc(&ie, path2);
2702 err = got_fileindex_entry_update(ie, ondisk_path,
2705 got_fileindex_entry_free(ie);
2708 err = got_fileindex_entry_add(a->fileindex, ie);
2710 got_fileindex_entry_free(ie);
2720 struct check_merge_ok_arg {
2721 struct got_worktree *worktree;
2722 struct got_repository *repo;
2725 static const struct got_error *
2726 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2728 const struct got_error *err = NULL;
2729 struct check_merge_ok_arg *a = arg;
2730 unsigned char status;
2734 /* Reject merges into a work tree with mixed base commits. */
2735 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2736 SHA1_DIGEST_LENGTH))
2737 return got_error(GOT_ERR_MIXED_COMMITS);
2739 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2741 return got_error_from_errno("asprintf");
2743 /* Reject merges into a work tree with conflicted files. */
2744 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2747 if (status == GOT_STATUS_CONFLICT)
2748 return got_error(GOT_ERR_CONFLICTS);
2753 static const struct got_error *
2754 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2755 const char *fileindex_path, struct got_object_id *commit_id1,
2756 struct got_object_id *commit_id2, struct got_repository *repo,
2757 got_worktree_checkout_cb progress_cb, void *progress_arg,
2758 got_cancel_cb cancel_cb, void *cancel_arg)
2760 const struct got_error *err = NULL, *sync_err;
2761 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2762 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2763 struct merge_file_cb_arg arg;
2764 char *label_orig = NULL;
2769 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2770 worktree->path_prefix);
2774 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2778 err = got_object_id_str(&id_str, commit_id1);
2782 if (asprintf(&label_orig, "%s: commit %s",
2783 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2784 err = got_error_from_errno("asprintf");
2791 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2792 worktree->path_prefix);
2796 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2800 arg.worktree = worktree;
2801 arg.fileindex = fileindex;
2802 arg.progress_cb = progress_cb;
2803 arg.progress_arg = progress_arg;
2804 arg.cancel_cb = cancel_cb;
2805 arg.cancel_arg = cancel_arg;
2806 arg.label_orig = label_orig;
2807 arg.commit_id2 = commit_id2;
2808 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2809 sync_err = sync_fileindex(fileindex, fileindex_path);
2810 if (sync_err && err == NULL)
2814 got_object_tree_close(tree1);
2816 got_object_tree_close(tree2);
2821 const struct got_error *
2822 got_worktree_merge_files(struct got_worktree *worktree,
2823 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2824 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2825 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2827 const struct got_error *err, *unlockerr;
2828 char *fileindex_path = NULL;
2829 struct got_fileindex *fileindex = NULL;
2830 struct check_merge_ok_arg mok_arg;
2832 err = lock_worktree(worktree, LOCK_EX);
2836 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2840 mok_arg.worktree = worktree;
2841 mok_arg.repo = repo;
2842 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2847 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2848 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2851 got_fileindex_free(fileindex);
2852 free(fileindex_path);
2853 unlockerr = lock_worktree(worktree, LOCK_SH);
2854 if (unlockerr && err == NULL)
2859 struct diff_dir_cb_arg {
2860 struct got_fileindex *fileindex;
2861 struct got_worktree *worktree;
2862 const char *status_path;
2863 size_t status_path_len;
2864 struct got_repository *repo;
2865 got_worktree_status_cb status_cb;
2867 got_cancel_cb cancel_cb;
2869 /* A pathlist containing per-directory pathlists of ignore patterns. */
2870 struct got_pathlist_head ignores;
2871 int report_unchanged;
2875 static const struct got_error *
2876 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2877 int dirfd, const char *de_name,
2878 got_worktree_status_cb status_cb, void *status_arg,
2879 struct got_repository *repo, int report_unchanged)
2881 const struct got_error *err = NULL;
2882 unsigned char status = GOT_STATUS_NO_CHANGE;
2883 unsigned char staged_status = get_staged_status(ie);
2885 struct got_object_id blob_id, commit_id, staged_blob_id;
2886 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2887 struct got_object_id *staged_blob_idp = NULL;
2889 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2893 if (status == GOT_STATUS_NO_CHANGE &&
2894 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2897 if (got_fileindex_entry_has_blob(ie)) {
2898 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2899 blob_idp = &blob_id;
2901 if (got_fileindex_entry_has_commit(ie)) {
2902 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2903 commit_idp = &commit_id;
2905 if (staged_status == GOT_STATUS_ADD ||
2906 staged_status == GOT_STATUS_MODIFY) {
2907 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2908 SHA1_DIGEST_LENGTH);
2909 staged_blob_idp = &staged_blob_id;
2912 return (*status_cb)(status_arg, status, staged_status,
2913 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2916 static const struct got_error *
2917 status_old_new(void *arg, struct got_fileindex_entry *ie,
2918 struct dirent *de, const char *parent_path, int dirfd)
2920 const struct got_error *err = NULL;
2921 struct diff_dir_cb_arg *a = arg;
2924 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2925 return got_error(GOT_ERR_CANCELLED);
2927 if (got_path_cmp(parent_path, a->status_path,
2928 strlen(parent_path), a->status_path_len) != 0 &&
2929 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2932 if (parent_path[0]) {
2933 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2934 parent_path, de->d_name) == -1)
2935 return got_error_from_errno("asprintf");
2937 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2939 return got_error_from_errno("asprintf");
2942 err = report_file_status(ie, abspath, dirfd, de->d_name,
2943 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2948 static const struct got_error *
2949 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2951 struct diff_dir_cb_arg *a = arg;
2952 struct got_object_id blob_id, commit_id;
2953 unsigned char status;
2955 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2956 return got_error(GOT_ERR_CANCELLED);
2958 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2961 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2962 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2963 if (got_fileindex_entry_has_file_on_disk(ie))
2964 status = GOT_STATUS_MISSING;
2966 status = GOT_STATUS_DELETE;
2967 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2968 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2972 free_ignorelist(struct got_pathlist_head *ignorelist)
2974 struct got_pathlist_entry *pe;
2976 TAILQ_FOREACH(pe, ignorelist, entry)
2977 free((char *)pe->path);
2978 got_pathlist_free(ignorelist);
2982 free_ignores(struct got_pathlist_head *ignores)
2984 struct got_pathlist_entry *pe;
2986 TAILQ_FOREACH(pe, ignores, entry) {
2987 struct got_pathlist_head *ignorelist = pe->data;
2988 free_ignorelist(ignorelist);
2989 free((char *)pe->path);
2991 got_pathlist_free(ignores);
2994 static const struct got_error *
2995 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2997 const struct got_error *err = NULL;
2998 struct got_pathlist_entry *pe = NULL;
2999 struct got_pathlist_head *ignorelist;
3000 char *line = NULL, *pattern, *dirpath = NULL;
3001 size_t linesize = 0;
3004 ignorelist = calloc(1, sizeof(*ignorelist));
3005 if (ignorelist == NULL)
3006 return got_error_from_errno("calloc");
3007 TAILQ_INIT(ignorelist);
3009 while ((linelen = getline(&line, &linesize, f)) != -1) {
3010 if (linelen > 0 && line[linelen - 1] == '\n')
3011 line[linelen - 1] = '\0';
3013 /* Git's ignores may contain comments. */
3017 /* Git's negated patterns are not (yet?) supported. */
3021 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3023 err = got_error_from_errno("asprintf");
3026 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3031 err = got_error_from_errno("getline");
3035 dirpath = strdup(path);
3036 if (dirpath == NULL) {
3037 err = got_error_from_errno("strdup");
3040 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3043 if (err || pe == NULL) {
3045 free_ignorelist(ignorelist);
3051 match_ignores(struct got_pathlist_head *ignores, const char *path)
3053 struct got_pathlist_entry *pe;
3055 /* Handle patterns which match in all directories. */
3056 TAILQ_FOREACH(pe, ignores, entry) {
3057 struct got_pathlist_head *ignorelist = pe->data;
3058 struct got_pathlist_entry *pi;
3060 TAILQ_FOREACH(pi, ignorelist, entry) {
3061 const char *p, *pattern = pi->path;
3063 if (strncmp(pattern, "**/", 3) != 0)
3068 if (fnmatch(pattern, p,
3069 FNM_PATHNAME | FNM_LEADING_DIR)) {
3070 /* Retry in next directory. */
3071 while (*p && *p != '/')
3083 * The ignores pathlist contains ignore lists from children before
3084 * parents, so we can find the most specific ignorelist by walking
3085 * ignores backwards.
3087 pe = TAILQ_LAST(ignores, got_pathlist_head);
3089 if (got_path_is_child(path, pe->path, pe->path_len)) {
3090 struct got_pathlist_head *ignorelist = pe->data;
3091 struct got_pathlist_entry *pi;
3092 TAILQ_FOREACH(pi, ignorelist, entry) {
3093 const char *pattern = pi->path;
3094 int flags = FNM_LEADING_DIR;
3095 if (strstr(pattern, "/**/") == NULL)
3096 flags |= FNM_PATHNAME;
3097 if (fnmatch(pattern, path, flags))
3102 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3108 static const struct got_error *
3109 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3110 const char *path, int dirfd, const char *ignores_filename)
3112 const struct got_error *err = NULL;
3115 FILE *ignoresfile = NULL;
3117 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3118 path[0] ? "/" : "", ignores_filename) == -1)
3119 return got_error_from_errno("asprintf");
3122 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3124 if (errno != ENOENT && errno != EACCES)
3125 err = got_error_from_errno2("openat",
3128 ignoresfile = fdopen(fd, "r");
3129 if (ignoresfile == NULL)
3130 err = got_error_from_errno2("fdopen",
3134 err = read_ignores(ignores, path, ignoresfile);
3138 ignoresfile = fopen(ignorespath, "r");
3139 if (ignoresfile == NULL) {
3140 if (errno != ENOENT && errno != EACCES)
3141 err = got_error_from_errno2("fopen",
3144 err = read_ignores(ignores, path, ignoresfile);
3147 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3148 err = got_error_from_errno2("fclose", path);
3149 if (fd != -1 && close(fd) == -1 && err == NULL)
3150 err = got_error_from_errno2("close", path);
3155 static const struct got_error *
3156 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3158 const struct got_error *err = NULL;
3159 struct diff_dir_cb_arg *a = arg;
3162 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3163 return got_error(GOT_ERR_CANCELLED);
3165 if (parent_path[0]) {
3166 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3167 return got_error_from_errno("asprintf");
3172 if (de->d_type != DT_DIR &&
3173 got_path_is_child(path, a->status_path, a->status_path_len)
3174 && !match_ignores(&a->ignores, path))
3175 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3176 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3182 static const struct got_error *
3183 status_traverse(void *arg, const char *path, int dirfd)
3185 const struct got_error *err = NULL;
3186 struct diff_dir_cb_arg *a = arg;
3191 err = add_ignores(&a->ignores, a->worktree->root_path,
3192 path, dirfd, ".cvsignore");
3196 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3197 dirfd, ".gitignore");
3202 static const struct got_error *
3203 report_single_file_status(const char *path, const char *ondisk_path,
3204 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3205 void *status_arg, struct got_repository *repo, int report_unchanged)
3207 struct got_fileindex_entry *ie;
3210 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3212 return report_file_status(ie, ondisk_path, -1, NULL,
3213 status_cb, status_arg, repo, report_unchanged);
3215 if (lstat(ondisk_path, &sb) == -1) {
3216 if (errno != ENOENT)
3217 return got_error_from_errno2("lstat", ondisk_path);
3218 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3219 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3223 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3224 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3225 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3230 static const struct got_error *
3231 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3232 const char *root_path, const char *path)
3234 const struct got_error *err;
3235 char *parent_path, *next_parent_path = NULL;
3237 err = add_ignores(ignores, root_path, "", -1,
3242 err = add_ignores(ignores, root_path, "", -1,
3247 err = got_path_dirname(&parent_path, path);
3249 if (err->code == GOT_ERR_BAD_PATH)
3250 return NULL; /* cannot traverse parent */
3254 err = add_ignores(ignores, root_path, parent_path, -1,
3258 err = add_ignores(ignores, root_path, parent_path, -1,
3262 err = got_path_dirname(&next_parent_path, parent_path);
3264 if (err->code == GOT_ERR_BAD_PATH)
3265 err = NULL; /* traversed everything */
3269 parent_path = next_parent_path;
3270 next_parent_path = NULL;
3274 free(next_parent_path);
3278 static const struct got_error *
3279 worktree_status(struct got_worktree *worktree, const char *path,
3280 struct got_fileindex *fileindex, struct got_repository *repo,
3281 got_worktree_status_cb status_cb, void *status_arg,
3282 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3283 int report_unchanged)
3285 const struct got_error *err = NULL;
3287 struct got_fileindex_diff_dir_cb fdiff_cb;
3288 struct diff_dir_cb_arg arg;
3289 char *ondisk_path = NULL;
3291 TAILQ_INIT(&arg.ignores);
3293 if (asprintf(&ondisk_path, "%s%s%s",
3294 worktree->root_path, path[0] ? "/" : "", path) == -1)
3295 return got_error_from_errno("asprintf");
3297 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3299 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3301 err = got_error_from_errno2("open", ondisk_path);
3303 err = report_single_file_status(path, ondisk_path,
3304 fileindex, status_cb, status_arg, repo,
3307 fdiff_cb.diff_old_new = status_old_new;
3308 fdiff_cb.diff_old = status_old;
3309 fdiff_cb.diff_new = status_new;
3310 fdiff_cb.diff_traverse = status_traverse;
3311 arg.fileindex = fileindex;
3312 arg.worktree = worktree;
3313 arg.status_path = path;
3314 arg.status_path_len = strlen(path);
3316 arg.status_cb = status_cb;
3317 arg.status_arg = status_arg;
3318 arg.cancel_cb = cancel_cb;
3319 arg.cancel_arg = cancel_arg;
3320 arg.report_unchanged = report_unchanged;
3321 arg.no_ignores = no_ignores;
3323 err = add_ignores_from_parent_paths(&arg.ignores,
3324 worktree->root_path, path);
3328 err = got_fileindex_diff_dir(fileindex, fd,
3329 worktree->root_path, path, repo, &fdiff_cb, &arg);
3332 free_ignores(&arg.ignores);
3333 if (fd != -1 && close(fd) != 0 && err == NULL)
3334 err = got_error_from_errno("close");
3339 const struct got_error *
3340 got_worktree_status(struct got_worktree *worktree,
3341 struct got_pathlist_head *paths, struct got_repository *repo,
3342 got_worktree_status_cb status_cb, void *status_arg,
3343 got_cancel_cb cancel_cb, void *cancel_arg)
3345 const struct got_error *err = NULL;
3346 char *fileindex_path = NULL;
3347 struct got_fileindex *fileindex = NULL;
3348 struct got_pathlist_entry *pe;
3350 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3354 TAILQ_FOREACH(pe, paths, entry) {
3355 err = worktree_status(worktree, pe->path, fileindex, repo,
3356 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3360 free(fileindex_path);
3361 got_fileindex_free(fileindex);
3365 const struct got_error *
3366 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3369 const struct got_error *err = NULL;
3370 char *resolved = NULL, *cwd = NULL, *path = NULL;
3376 cwd = getcwd(NULL, 0);
3378 return got_error_from_errno("getcwd");
3380 if (lstat(arg, &sb) == -1) {
3381 if (errno != ENOENT) {
3382 err = got_error_from_errno2("lstat", arg);
3386 if (S_ISLNK(sb.st_mode)) {
3388 * We cannot use realpath(3) with symlinks since we want to
3389 * operate on the symlink itself.
3390 * But we can make the path absolute, assuming it is relative
3391 * to the current working directory, and then canonicalize it.
3393 char *abspath = NULL;
3394 char canonpath[PATH_MAX];
3395 if (!got_path_is_absolute(arg)) {
3396 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3397 err = got_error_from_errno("asprintf");
3402 err = got_canonpath(abspath ? abspath : arg, canonpath,
3406 resolved = strdup(canonpath);
3407 if (resolved == NULL) {
3408 err = got_error_from_errno("strdup");
3412 resolved = realpath(arg, NULL);
3413 if (resolved == NULL) {
3414 if (errno != ENOENT) {
3415 err = got_error_from_errno2("realpath", arg);
3418 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3419 err = got_error_from_errno("asprintf");
3425 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3426 strlen(got_worktree_get_root_path(worktree)))) {
3427 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3431 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3432 err = got_path_skip_common_ancestor(&path,
3433 got_worktree_get_root_path(worktree), resolved);
3439 err = got_error_from_errno("strdup");
3444 /* XXX status walk can't deal with trailing slash! */
3446 while (len > 0 && path[len - 1] == '/') {
3447 path[len - 1] = '\0';
3460 struct schedule_addition_args {
3461 struct got_worktree *worktree;
3462 struct got_fileindex *fileindex;
3463 got_worktree_checkout_cb progress_cb;
3465 struct got_repository *repo;
3468 static const struct got_error *
3469 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3470 const char *relpath, struct got_object_id *blob_id,
3471 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3472 int dirfd, const char *de_name)
3474 struct schedule_addition_args *a = arg;
3475 const struct got_error *err = NULL;
3476 struct got_fileindex_entry *ie;
3480 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3482 return got_error_from_errno("asprintf");
3484 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3486 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3490 /* Re-adding an existing entry is a no-op. */
3491 if (status == GOT_STATUS_ADD)
3493 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3498 if (status != GOT_STATUS_UNVERSIONED) {
3499 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3503 err = got_fileindex_entry_alloc(&ie, relpath);
3506 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3508 got_fileindex_entry_free(ie);
3511 err = got_fileindex_entry_add(a->fileindex, ie);
3513 got_fileindex_entry_free(ie);
3520 if (status == GOT_STATUS_ADD)
3522 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3525 const struct got_error *
3526 got_worktree_schedule_add(struct got_worktree *worktree,
3527 struct got_pathlist_head *paths,
3528 got_worktree_checkout_cb progress_cb, void *progress_arg,
3529 struct got_repository *repo, int no_ignores)
3531 struct got_fileindex *fileindex = NULL;
3532 char *fileindex_path = NULL;
3533 const struct got_error *err = NULL, *sync_err, *unlockerr;
3534 struct got_pathlist_entry *pe;
3535 struct schedule_addition_args saa;
3537 err = lock_worktree(worktree, LOCK_EX);
3541 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3545 saa.worktree = worktree;
3546 saa.fileindex = fileindex;
3547 saa.progress_cb = progress_cb;
3548 saa.progress_arg = progress_arg;
3551 TAILQ_FOREACH(pe, paths, entry) {
3552 err = worktree_status(worktree, pe->path, fileindex, repo,
3553 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3557 sync_err = sync_fileindex(fileindex, fileindex_path);
3558 if (sync_err && err == NULL)
3561 free(fileindex_path);
3563 got_fileindex_free(fileindex);
3564 unlockerr = lock_worktree(worktree, LOCK_SH);
3565 if (unlockerr && err == NULL)
3570 struct schedule_deletion_args {
3571 struct got_worktree *worktree;
3572 struct got_fileindex *fileindex;
3573 got_worktree_delete_cb progress_cb;
3575 struct got_repository *repo;
3576 int delete_local_mods;
3580 static const struct got_error *
3581 schedule_for_deletion(void *arg, unsigned char status,
3582 unsigned char staged_status, const char *relpath,
3583 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3584 struct got_object_id *commit_id, int dirfd, const char *de_name)
3586 struct schedule_deletion_args *a = arg;
3587 const struct got_error *err = NULL;
3588 struct got_fileindex_entry *ie = NULL;
3590 char *ondisk_path, *parent = NULL;
3592 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3594 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3596 staged_status = get_staged_status(ie);
3597 if (staged_status != GOT_STATUS_NO_CHANGE) {
3598 if (staged_status == GOT_STATUS_DELETE)
3600 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3603 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3605 return got_error_from_errno("asprintf");
3607 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3612 if (status != GOT_STATUS_NO_CHANGE) {
3613 if (status == GOT_STATUS_DELETE)
3615 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3616 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3619 if (status != GOT_STATUS_MODIFY &&
3620 status != GOT_STATUS_MISSING) {
3621 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3626 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3628 if (unlinkat(dirfd, de_name, 0) != 0) {
3629 err = got_error_from_errno2("unlinkat",
3633 } else if (unlink(ondisk_path) != 0) {
3634 err = got_error_from_errno2("unlink", ondisk_path);
3638 parent = dirname(ondisk_path);
3640 if (parent == NULL) {
3641 err = got_error_from_errno2("dirname", ondisk_path);
3644 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3645 if (rmdir(parent) == -1) {
3646 if (errno != ENOTEMPTY)
3647 err = got_error_from_errno2("rmdir",
3651 parent = dirname(parent);
3652 if (parent == NULL) {
3653 err = got_error_from_errno2("dirname", parent);
3659 got_fileindex_entry_mark_deleted_from_disk(ie);
3664 if (status == GOT_STATUS_DELETE)
3666 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3667 staged_status, relpath);
3670 const struct got_error *
3671 got_worktree_schedule_delete(struct got_worktree *worktree,
3672 struct got_pathlist_head *paths, int delete_local_mods,
3673 got_worktree_delete_cb progress_cb, void *progress_arg,
3674 struct got_repository *repo, int keep_on_disk)
3676 struct got_fileindex *fileindex = NULL;
3677 char *fileindex_path = NULL;
3678 const struct got_error *err = NULL, *sync_err, *unlockerr;
3679 struct got_pathlist_entry *pe;
3680 struct schedule_deletion_args sda;
3682 err = lock_worktree(worktree, LOCK_EX);
3686 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3690 sda.worktree = worktree;
3691 sda.fileindex = fileindex;
3692 sda.progress_cb = progress_cb;
3693 sda.progress_arg = progress_arg;
3695 sda.delete_local_mods = delete_local_mods;
3696 sda.keep_on_disk = keep_on_disk;
3698 TAILQ_FOREACH(pe, paths, entry) {
3699 err = worktree_status(worktree, pe->path, fileindex, repo,
3700 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3704 sync_err = sync_fileindex(fileindex, fileindex_path);
3705 if (sync_err && err == NULL)
3708 free(fileindex_path);
3710 got_fileindex_free(fileindex);
3711 unlockerr = lock_worktree(worktree, LOCK_SH);
3712 if (unlockerr && err == NULL)
3717 static const struct got_error *
3718 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3720 const struct got_error *err = NULL;
3722 size_t linesize = 0, n;
3725 linelen = getline(&line, &linesize, infile);
3726 if (linelen == -1) {
3727 if (ferror(infile)) {
3728 err = got_error_from_errno("getline");
3734 n = fwrite(line, 1, linelen, outfile);
3736 err = got_ferror(outfile, GOT_ERR_IO);
3741 n = fwrite(line, 1, linelen, rejectfile);
3743 err = got_ferror(outfile, GOT_ERR_IO);
3750 static const struct got_error *
3751 skip_one_line(FILE *f)
3754 size_t linesize = 0;
3757 linelen = getline(&line, &linesize, f);
3758 if (linelen == -1) {
3760 return got_error_from_errno("getline");
3767 static const struct got_error *
3768 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3769 int start_old, int end_old, int start_new, int end_new,
3770 FILE *outfile, FILE *rejectfile)
3772 const struct got_error *err;
3774 /* Copy old file's lines leading up to patch. */
3775 while (!feof(f1) && *line_cur1 < start_old) {
3776 err = copy_one_line(f1, outfile, NULL);
3781 /* Skip new file's lines leading up to patch. */
3782 while (!feof(f2) && *line_cur2 < start_new) {
3784 err = copy_one_line(f2, NULL, rejectfile);
3786 err = skip_one_line(f2);
3791 /* Copy patched lines. */
3792 while (!feof(f2) && *line_cur2 <= end_new) {
3793 err = copy_one_line(f2, outfile, NULL);
3798 /* Skip over old file's replaced lines. */
3799 while (!feof(f1) && *line_cur1 <= end_old) {
3801 err = copy_one_line(f1, NULL, rejectfile);
3803 err = skip_one_line(f1);
3812 static const struct got_error *
3813 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3814 FILE *outfile, FILE *rejectfile)
3816 const struct got_error *err;
3819 /* Copy old file's lines until EOF. */
3821 err = copy_one_line(f1, outfile, NULL);
3828 /* Copy new file's lines until EOF. */
3830 err = copy_one_line(f2, NULL, rejectfile);
3840 static const struct got_error *
3841 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3842 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3843 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3844 int *line_cur2, FILE *outfile, FILE *rejectfile,
3845 got_worktree_patch_cb patch_cb, void *patch_arg)
3847 const struct got_error *err = NULL;
3848 int start_old = change->cv.a;
3849 int end_old = change->cv.b;
3850 int start_new = change->cv.c;
3851 int end_new = change->cv.d;
3855 *choice = GOT_PATCH_CHOICE_NONE;
3857 hunkfile = got_opentemp();
3858 if (hunkfile == NULL)
3859 return got_error_from_errno("got_opentemp");
3864 /* XXX TODO needs error checking */
3865 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3867 if (fseek(f1, pos1, SEEK_SET) == -1) {
3868 err = got_ferror(f1, GOT_ERR_IO);
3871 if (fseek(f2, pos2, SEEK_SET) == -1) {
3872 err = got_ferror(f1, GOT_ERR_IO);
3875 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3876 err = got_ferror(hunkfile, GOT_ERR_IO);
3880 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3881 hunkfile, n, nchanges);
3886 case GOT_PATCH_CHOICE_YES:
3887 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3888 end_old, start_new, end_new, outfile, rejectfile);
3890 case GOT_PATCH_CHOICE_NO:
3891 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3892 end_old, start_new, end_new, rejectfile, outfile);
3894 case GOT_PATCH_CHOICE_QUIT:
3897 err = got_error(GOT_ERR_PATCH_CHOICE);
3901 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3902 err = got_error_from_errno("fclose");
3906 struct revert_file_args {
3907 struct got_worktree *worktree;
3908 struct got_fileindex *fileindex;
3909 got_worktree_checkout_cb progress_cb;
3911 got_worktree_patch_cb patch_cb;
3913 struct got_repository *repo;
3916 static const struct got_error *
3917 create_patched_content(char **path_outfile, int reverse_patch,
3918 struct got_object_id *blob_id, const char *path2,
3919 int dirfd2, const char *de_name2,
3920 const char *relpath, struct got_repository *repo,
3921 got_worktree_patch_cb patch_cb, void *patch_arg)
3923 const struct got_error *err;
3924 struct got_blob_object *blob = NULL;
3925 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3927 char *path1 = NULL, *id_str = NULL;
3928 struct stat sb1, sb2;
3929 struct got_diff_changes *changes = NULL;
3930 struct got_diff_state *ds = NULL;
3931 struct got_diff_args *args = NULL;
3932 struct got_diff_change *change;
3933 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3936 *path_outfile = NULL;
3938 err = got_object_id_str(&id_str, blob_id);
3943 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3945 err = got_error_from_errno2("openat", path2);
3949 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3951 err = got_error_from_errno2("open", path2);
3955 if (fstat(fd2, &sb2) == -1) {
3956 err = got_error_from_errno2("fstat", path2);
3960 f2 = fdopen(fd2, "r");
3962 err = got_error_from_errno2("fdopen", path2);
3967 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3971 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3975 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3979 if (stat(path1, &sb1) == -1) {
3980 err = got_error_from_errno2("stat", path1);
3984 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3985 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3989 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3993 if (fseek(f1, 0L, SEEK_SET) == -1)
3994 return got_ferror(f1, GOT_ERR_IO);
3995 if (fseek(f2, 0L, SEEK_SET) == -1)
3996 return got_ferror(f2, GOT_ERR_IO);
3997 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3999 err = apply_or_reject_change(&choice, change, ++n,
4000 changes->nchanges, ds, args, diff_flags, relpath,
4001 f1, f2, &line_cur1, &line_cur2,
4002 reverse_patch ? NULL : outfile,
4003 reverse_patch ? outfile : NULL,
4004 patch_cb, patch_arg);
4007 if (choice == GOT_PATCH_CHOICE_YES)
4009 else if (choice == GOT_PATCH_CHOICE_QUIT)
4013 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4014 reverse_patch ? NULL : outfile,
4015 reverse_patch ? outfile : NULL);
4019 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4020 err = got_error_from_errno2("chmod", path2);
4027 got_object_blob_close(blob);
4028 if (f1 && fclose(f1) == EOF && err == NULL)
4029 err = got_error_from_errno2("fclose", path1);
4030 if (f2 && fclose(f2) == EOF && err == NULL)
4031 err = got_error_from_errno2("fclose", path2);
4032 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4033 err = got_error_from_errno2("close", path2);
4034 if (outfile && fclose(outfile) == EOF && err == NULL)
4035 err = got_error_from_errno2("fclose", *path_outfile);
4036 if (path1 && unlink(path1) == -1 && err == NULL)
4037 err = got_error_from_errno2("unlink", path1);
4038 if (err || !have_content) {
4039 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4040 err = got_error_from_errno2("unlink", *path_outfile);
4041 free(*path_outfile);
4042 *path_outfile = NULL;
4046 got_diff_state_free(ds);
4050 got_diff_free_changes(changes);
4055 static const struct got_error *
4056 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4057 const char *relpath, struct got_object_id *blob_id,
4058 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4059 int dirfd, const char *de_name)
4061 struct revert_file_args *a = arg;
4062 const struct got_error *err = NULL;
4063 char *parent_path = NULL;
4064 struct got_fileindex_entry *ie;
4065 struct got_tree_object *tree = NULL;
4066 struct got_object_id *tree_id = NULL;
4067 const struct got_tree_entry *te = NULL;
4068 char *tree_path = NULL, *te_name;
4069 char *ondisk_path = NULL, *path_content = NULL;
4070 struct got_blob_object *blob = NULL;
4072 /* Reverting a staged deletion is a no-op. */
4073 if (status == GOT_STATUS_DELETE &&
4074 staged_status != GOT_STATUS_NO_CHANGE)
4077 if (status == GOT_STATUS_UNVERSIONED)
4078 return (*a->progress_cb)(a->progress_arg,
4079 GOT_STATUS_UNVERSIONED, relpath);
4081 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4083 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4085 /* Construct in-repository path of tree which contains this blob. */
4086 err = got_path_dirname(&parent_path, ie->path);
4088 if (err->code != GOT_ERR_BAD_PATH)
4090 parent_path = strdup("/");
4091 if (parent_path == NULL) {
4092 err = got_error_from_errno("strdup");
4096 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4097 tree_path = strdup(parent_path);
4098 if (tree_path == NULL) {
4099 err = got_error_from_errno("strdup");
4103 if (got_path_is_root_dir(parent_path)) {
4104 tree_path = strdup(a->worktree->path_prefix);
4105 if (tree_path == NULL) {
4106 err = got_error_from_errno("strdup");
4110 if (asprintf(&tree_path, "%s/%s",
4111 a->worktree->path_prefix, parent_path) == -1) {
4112 err = got_error_from_errno("asprintf");
4118 err = got_object_id_by_path(&tree_id, a->repo,
4119 a->worktree->base_commit_id, tree_path);
4121 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4122 (status == GOT_STATUS_ADD ||
4123 staged_status == GOT_STATUS_ADD)))
4126 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4130 te_name = basename(ie->path);
4131 if (te_name == NULL) {
4132 err = got_error_from_errno2("basename", ie->path);
4136 te = got_object_tree_find_entry(tree, te_name);
4137 if (te == NULL && status != GOT_STATUS_ADD &&
4138 staged_status != GOT_STATUS_ADD) {
4139 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4145 case GOT_STATUS_ADD:
4147 int choice = GOT_PATCH_CHOICE_NONE;
4148 err = (*a->patch_cb)(&choice, a->patch_arg,
4149 status, ie->path, NULL, 1, 1);
4152 if (choice != GOT_PATCH_CHOICE_YES)
4155 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4159 got_fileindex_entry_remove(a->fileindex, ie);
4161 case GOT_STATUS_DELETE:
4163 int choice = GOT_PATCH_CHOICE_NONE;
4164 err = (*a->patch_cb)(&choice, a->patch_arg,
4165 status, ie->path, NULL, 1, 1);
4168 if (choice != GOT_PATCH_CHOICE_YES)
4172 case GOT_STATUS_MODIFY:
4173 case GOT_STATUS_MODE_CHANGE:
4174 case GOT_STATUS_CONFLICT:
4175 case GOT_STATUS_MISSING: {
4176 struct got_object_id id;
4177 if (staged_status == GOT_STATUS_ADD ||
4178 staged_status == GOT_STATUS_MODIFY) {
4179 memcpy(id.sha1, ie->staged_blob_sha1,
4180 SHA1_DIGEST_LENGTH);
4182 memcpy(id.sha1, ie->blob_sha1,
4183 SHA1_DIGEST_LENGTH);
4184 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4188 if (asprintf(&ondisk_path, "%s/%s",
4189 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4190 err = got_error_from_errno("asprintf");
4194 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4195 status == GOT_STATUS_CONFLICT)) {
4196 err = create_patched_content(&path_content, 1, &id,
4197 ondisk_path, dirfd, de_name, ie->path, a->repo,
4198 a->patch_cb, a->patch_arg);
4199 if (err || path_content == NULL)
4201 if (rename(path_content, ondisk_path) == -1) {
4202 err = got_error_from_errno3("rename",
4203 path_content, ondisk_path);
4207 err = install_blob(a->worktree, ondisk_path, ie->path,
4208 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4209 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4210 a->repo, a->progress_cb, a->progress_arg);
4213 if (status == GOT_STATUS_DELETE ||
4214 status == GOT_STATUS_MODE_CHANGE) {
4215 err = got_fileindex_entry_update(ie,
4216 ondisk_path, blob->id.sha1,
4217 a->worktree->base_commit_id->sha1, 1);
4233 got_object_blob_close(blob);
4235 got_object_tree_close(tree);
4240 const struct got_error *
4241 got_worktree_revert(struct got_worktree *worktree,
4242 struct got_pathlist_head *paths,
4243 got_worktree_checkout_cb progress_cb, void *progress_arg,
4244 got_worktree_patch_cb patch_cb, void *patch_arg,
4245 struct got_repository *repo)
4247 struct got_fileindex *fileindex = NULL;
4248 char *fileindex_path = NULL;
4249 const struct got_error *err = NULL, *unlockerr = NULL;
4250 const struct got_error *sync_err = NULL;
4251 struct got_pathlist_entry *pe;
4252 struct revert_file_args rfa;
4254 err = lock_worktree(worktree, LOCK_EX);
4258 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4262 rfa.worktree = worktree;
4263 rfa.fileindex = fileindex;
4264 rfa.progress_cb = progress_cb;
4265 rfa.progress_arg = progress_arg;
4266 rfa.patch_cb = patch_cb;
4267 rfa.patch_arg = patch_arg;
4269 TAILQ_FOREACH(pe, paths, entry) {
4270 err = worktree_status(worktree, pe->path, fileindex, repo,
4271 revert_file, &rfa, NULL, NULL, 0, 0);
4275 sync_err = sync_fileindex(fileindex, fileindex_path);
4276 if (sync_err && err == NULL)
4279 free(fileindex_path);
4281 got_fileindex_free(fileindex);
4282 unlockerr = lock_worktree(worktree, LOCK_SH);
4283 if (unlockerr && err == NULL)
4289 free_commitable(struct got_commitable *ct)
4292 free(ct->in_repo_path);
4293 free(ct->ondisk_path);
4295 free(ct->base_blob_id);
4296 free(ct->staged_blob_id);
4297 free(ct->base_commit_id);
4301 struct collect_commitables_arg {
4302 struct got_pathlist_head *commitable_paths;
4303 struct got_repository *repo;
4304 struct got_worktree *worktree;
4305 int have_staged_files;
4308 static const struct got_error *
4309 collect_commitables(void *arg, unsigned char status,
4310 unsigned char staged_status, const char *relpath,
4311 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4312 struct got_object_id *commit_id, int dirfd, const char *de_name)
4314 struct collect_commitables_arg *a = arg;
4315 const struct got_error *err = NULL;
4316 struct got_commitable *ct = NULL;
4317 struct got_pathlist_entry *new = NULL;
4318 char *parent_path = NULL, *path = NULL;
4321 if (a->have_staged_files) {
4322 if (staged_status != GOT_STATUS_MODIFY &&
4323 staged_status != GOT_STATUS_ADD &&
4324 staged_status != GOT_STATUS_DELETE)
4327 if (status == GOT_STATUS_CONFLICT)
4328 return got_error(GOT_ERR_COMMIT_CONFLICT);
4330 if (status != GOT_STATUS_MODIFY &&
4331 status != GOT_STATUS_MODE_CHANGE &&
4332 status != GOT_STATUS_ADD &&
4333 status != GOT_STATUS_DELETE)
4337 if (asprintf(&path, "/%s", relpath) == -1) {
4338 err = got_error_from_errno("asprintf");
4341 if (strcmp(path, "/") == 0) {
4342 parent_path = strdup("");
4343 if (parent_path == NULL)
4344 return got_error_from_errno("strdup");
4346 err = got_path_dirname(&parent_path, path);
4351 ct = calloc(1, sizeof(*ct));
4353 err = got_error_from_errno("calloc");
4357 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4359 err = got_error_from_errno("asprintf");
4362 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4363 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4366 if (fstatat(dirfd, de_name, &sb,
4367 AT_SYMLINK_NOFOLLOW) == -1) {
4368 err = got_error_from_errno2("fstatat",
4372 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4373 err = got_error_from_errno2("lstat", ct->ondisk_path);
4376 ct->mode = sb.st_mode;
4379 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4380 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4382 err = got_error_from_errno("asprintf");
4386 ct->status = status;
4387 ct->staged_status = staged_status;
4388 ct->blob_id = NULL; /* will be filled in when blob gets created */
4389 if (ct->status != GOT_STATUS_ADD &&
4390 ct->staged_status != GOT_STATUS_ADD) {
4391 ct->base_blob_id = got_object_id_dup(blob_id);
4392 if (ct->base_blob_id == NULL) {
4393 err = got_error_from_errno("got_object_id_dup");
4396 ct->base_commit_id = got_object_id_dup(commit_id);
4397 if (ct->base_commit_id == NULL) {
4398 err = got_error_from_errno("got_object_id_dup");
4402 if (ct->staged_status == GOT_STATUS_ADD ||
4403 ct->staged_status == GOT_STATUS_MODIFY) {
4404 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4405 if (ct->staged_blob_id == NULL) {
4406 err = got_error_from_errno("got_object_id_dup");
4410 ct->path = strdup(path);
4411 if (ct->path == NULL) {
4412 err = got_error_from_errno("strdup");
4415 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4417 if (ct && (err || new == NULL))
4418 free_commitable(ct);
4424 static const struct got_error *write_tree(struct got_object_id **, int *,
4425 struct got_tree_object *, const char *, struct got_pathlist_head *,
4426 got_worktree_status_cb status_cb, void *status_arg,
4427 struct got_repository *);
4429 static const struct got_error *
4430 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4431 struct got_tree_entry *te, const char *parent_path,
4432 struct got_pathlist_head *commitable_paths,
4433 got_worktree_status_cb status_cb, void *status_arg,
4434 struct got_repository *repo)
4436 const struct got_error *err = NULL;
4437 struct got_tree_object *subtree;
4440 if (asprintf(&subpath, "%s%s%s", parent_path,
4441 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4442 return got_error_from_errno("asprintf");
4444 err = got_object_open_as_tree(&subtree, repo, &te->id);
4448 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4449 commitable_paths, status_cb, status_arg, repo);
4450 got_object_tree_close(subtree);
4455 static const struct got_error *
4456 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4458 const struct got_error *err = NULL;
4459 char *ct_parent_path = NULL;
4463 if (strchr(ct->in_repo_path, '/') == NULL) {
4464 *match = got_path_is_root_dir(path);
4468 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4471 *match = (strcmp(path, ct_parent_path) == 0);
4472 free(ct_parent_path);
4477 get_ct_file_mode(struct got_commitable *ct)
4479 if (S_ISLNK(ct->mode))
4482 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4485 static const struct got_error *
4486 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4487 struct got_tree_entry *te, struct got_commitable *ct)
4489 const struct got_error *err = NULL;
4493 err = got_object_tree_entry_dup(new_te, te);
4497 (*new_te)->mode = get_ct_file_mode(ct);
4499 if (ct->staged_status == GOT_STATUS_MODIFY)
4500 memcpy(&(*new_te)->id, ct->staged_blob_id,
4501 sizeof((*new_te)->id));
4503 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4505 if (err && *new_te) {
4512 static const struct got_error *
4513 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4514 struct got_commitable *ct)
4516 const struct got_error *err = NULL;
4521 *new_te = calloc(1, sizeof(**new_te));
4522 if (*new_te == NULL)
4523 return got_error_from_errno("calloc");
4525 ct_name = basename(ct->path);
4526 if (ct_name == NULL) {
4527 err = got_error_from_errno2("basename", ct->path);
4530 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4531 sizeof((*new_te)->name)) {
4532 err = got_error(GOT_ERR_NO_SPACE);
4536 (*new_te)->mode = get_ct_file_mode(ct);
4538 if (ct->staged_status == GOT_STATUS_ADD)
4539 memcpy(&(*new_te)->id, ct->staged_blob_id,
4540 sizeof((*new_te)->id));
4542 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4544 if (err && *new_te) {
4551 static const struct got_error *
4552 insert_tree_entry(struct got_tree_entry *new_te,
4553 struct got_pathlist_head *paths)
4555 const struct got_error *err = NULL;
4556 struct got_pathlist_entry *new_pe;
4558 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4562 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4566 static const struct got_error *
4567 report_ct_status(struct got_commitable *ct,
4568 got_worktree_status_cb status_cb, void *status_arg)
4570 const char *ct_path = ct->path;
4571 unsigned char status;
4573 while (ct_path[0] == '/')
4576 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4577 status = ct->staged_status;
4579 status = ct->status;
4581 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4582 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4585 static const struct got_error *
4586 match_modified_subtree(int *modified, struct got_tree_entry *te,
4587 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4589 const struct got_error *err = NULL;
4590 struct got_pathlist_entry *pe;
4595 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4596 got_path_is_root_dir(base_tree_path) ? "" : "/",
4598 return got_error_from_errno("asprintf");
4600 TAILQ_FOREACH(pe, commitable_paths, entry) {
4601 struct got_commitable *ct = pe->data;
4602 *modified = got_path_is_child(ct->in_repo_path, te_path,
4612 static const struct got_error *
4613 match_deleted_or_modified_ct(struct got_commitable **ctp,
4614 struct got_tree_entry *te, const char *base_tree_path,
4615 struct got_pathlist_head *commitable_paths)
4617 const struct got_error *err = NULL;
4618 struct got_pathlist_entry *pe;
4622 TAILQ_FOREACH(pe, commitable_paths, entry) {
4623 struct got_commitable *ct = pe->data;
4624 char *ct_name = NULL;
4627 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4628 if (ct->status != GOT_STATUS_MODIFY &&
4629 ct->status != GOT_STATUS_MODE_CHANGE &&
4630 ct->status != GOT_STATUS_DELETE)
4633 if (ct->staged_status != GOT_STATUS_MODIFY &&
4634 ct->staged_status != GOT_STATUS_DELETE)
4638 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4641 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4647 ct_name = basename(pe->path);
4648 if (ct_name == NULL)
4649 return got_error_from_errno2("basename", pe->path);
4651 if (strcmp(te->name, ct_name) != 0)
4661 static const struct got_error *
4662 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4663 const char *child_path, const char *path_base_tree,
4664 struct got_pathlist_head *commitable_paths,
4665 got_worktree_status_cb status_cb, void *status_arg,
4666 struct got_repository *repo)
4668 const struct got_error *err = NULL;
4669 struct got_tree_entry *new_te;
4671 struct got_object_id *id = NULL;
4676 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4677 got_path_is_root_dir(path_base_tree) ? "" : "/",
4679 return got_error_from_errno("asprintf");
4681 new_te = calloc(1, sizeof(*new_te));
4683 return got_error_from_errno("calloc");
4684 new_te->mode = S_IFDIR;
4686 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4687 sizeof(new_te->name)) {
4688 err = got_error(GOT_ERR_NO_SPACE);
4691 err = write_tree(&id, &nentries, NULL, subtree_path,
4692 commitable_paths, status_cb, status_arg, repo);
4697 memcpy(&new_te->id, id, sizeof(new_te->id));
4706 static const struct got_error *
4707 write_tree(struct got_object_id **new_tree_id, int *nentries,
4708 struct got_tree_object *base_tree, const char *path_base_tree,
4709 struct got_pathlist_head *commitable_paths,
4710 got_worktree_status_cb status_cb, void *status_arg,
4711 struct got_repository *repo)
4713 const struct got_error *err = NULL;
4714 struct got_pathlist_head paths;
4715 struct got_tree_entry *te, *new_te = NULL;
4716 struct got_pathlist_entry *pe;
4721 /* Insert, and recurse into, newly added entries first. */
4722 TAILQ_FOREACH(pe, commitable_paths, entry) {
4723 struct got_commitable *ct = pe->data;
4724 char *child_path = NULL, *slash;
4726 if ((ct->status != GOT_STATUS_ADD &&
4727 ct->staged_status != GOT_STATUS_ADD) ||
4728 (ct->flags & GOT_COMMITABLE_ADDED))
4731 if (!got_path_is_child(pe->path, path_base_tree,
4732 strlen(path_base_tree)))
4735 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4740 slash = strchr(child_path, '/');
4741 if (slash == NULL) {
4742 err = alloc_added_blob_tree_entry(&new_te, ct);
4745 err = report_ct_status(ct, status_cb, status_arg);
4748 ct->flags |= GOT_COMMITABLE_ADDED;
4749 err = insert_tree_entry(new_te, &paths);
4754 *slash = '\0'; /* trim trailing path components */
4755 if (base_tree == NULL ||
4756 got_object_tree_find_entry(base_tree, child_path)
4758 err = make_subtree_for_added_blob(&new_te,
4759 child_path, path_base_tree,
4760 commitable_paths, status_cb, status_arg,
4764 err = insert_tree_entry(new_te, &paths);
4773 int i, nbase_entries;
4774 /* Handle modified and deleted entries. */
4775 nbase_entries = got_object_tree_get_nentries(base_tree);
4776 for (i = 0; i < nbase_entries; i++) {
4777 struct got_commitable *ct = NULL;
4779 te = got_object_tree_get_entry(base_tree, i);
4780 if (got_object_tree_entry_is_submodule(te)) {
4781 /* Entry is a submodule; just copy it. */
4782 err = got_object_tree_entry_dup(&new_te, te);
4785 err = insert_tree_entry(new_te, &paths);
4792 if (S_ISDIR(te->mode)) {
4794 err = got_object_tree_entry_dup(&new_te, te);
4797 err = match_modified_subtree(&modified, te,
4798 path_base_tree, commitable_paths);
4801 /* Avoid recursion into unmodified subtrees. */
4803 struct got_object_id *new_id;
4805 err = write_subtree(&new_id,
4807 path_base_tree, commitable_paths,
4808 status_cb, status_arg, repo);
4811 if (nsubentries == 0) {
4812 /* All entries were deleted. */
4816 memcpy(&new_te->id, new_id,
4817 sizeof(new_te->id));
4820 err = insert_tree_entry(new_te, &paths);
4827 err = match_deleted_or_modified_ct(&ct, te,
4828 path_base_tree, commitable_paths);
4832 /* NB: Deleted entries get dropped here. */
4833 if (ct->status == GOT_STATUS_MODIFY ||
4834 ct->status == GOT_STATUS_MODE_CHANGE ||
4835 ct->staged_status == GOT_STATUS_MODIFY) {
4836 err = alloc_modified_blob_tree_entry(
4840 err = insert_tree_entry(new_te, &paths);
4845 err = report_ct_status(ct, status_cb,
4850 /* Entry is unchanged; just copy it. */
4851 err = got_object_tree_entry_dup(&new_te, te);
4854 err = insert_tree_entry(new_te, &paths);
4862 /* Write new list of entries; deleted entries have been dropped. */
4863 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4865 got_pathlist_free(&paths);
4869 static const struct got_error *
4870 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4871 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4872 int have_staged_files)
4874 const struct got_error *err = NULL;
4875 struct got_pathlist_entry *pe;
4877 TAILQ_FOREACH(pe, commitable_paths, entry) {
4878 struct got_fileindex_entry *ie;
4879 struct got_commitable *ct = pe->data;
4881 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4883 if (ct->status == GOT_STATUS_DELETE ||
4884 ct->staged_status == GOT_STATUS_DELETE) {
4885 got_fileindex_entry_remove(fileindex, ie);
4886 } else if (ct->staged_status == GOT_STATUS_ADD ||
4887 ct->staged_status == GOT_STATUS_MODIFY) {
4888 got_fileindex_entry_stage_set(ie,
4889 GOT_FILEIDX_STAGE_NONE);
4890 err = got_fileindex_entry_update(ie,
4891 ct->ondisk_path, ct->staged_blob_id->sha1,
4892 new_base_commit_id->sha1,
4893 !have_staged_files);
4895 err = got_fileindex_entry_update(ie,
4896 ct->ondisk_path, ct->blob_id->sha1,
4897 new_base_commit_id->sha1,
4898 !have_staged_files);
4900 err = got_fileindex_entry_alloc(&ie, pe->path);
4903 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4904 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4906 got_fileindex_entry_free(ie);
4909 err = got_fileindex_entry_add(fileindex, ie);
4911 got_fileindex_entry_free(ie);
4920 static const struct got_error *
4921 check_out_of_date(const char *in_repo_path, unsigned char status,
4922 unsigned char staged_status, struct got_object_id *base_blob_id,
4923 struct got_object_id *base_commit_id,
4924 struct got_object_id *head_commit_id, struct got_repository *repo,
4927 const struct got_error *err = NULL;
4928 struct got_object_id *id = NULL;
4930 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4931 /* Trivial case: base commit == head commit */
4932 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4935 * Ensure file content which local changes were based
4936 * on matches file content in the branch head.
4938 err = got_object_id_by_path(&id, repo, head_commit_id,
4941 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4942 err = got_error(ood_errcode);
4944 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4945 err = got_error(ood_errcode);
4947 /* Require that added files don't exist in the branch head. */
4948 err = got_object_id_by_path(&id, repo, head_commit_id,
4950 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4952 err = id ? got_error(ood_errcode) : NULL;
4959 const struct got_error *
4960 commit_worktree(struct got_object_id **new_commit_id,
4961 struct got_pathlist_head *commitable_paths,
4962 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4963 const char *author, const char *committer,
4964 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4965 got_worktree_status_cb status_cb, void *status_arg,
4966 struct got_repository *repo)
4968 const struct got_error *err = NULL, *unlockerr = NULL;
4969 struct got_pathlist_entry *pe;
4970 const char *head_ref_name = NULL;
4971 struct got_commit_object *head_commit = NULL;
4972 struct got_reference *head_ref2 = NULL;
4973 struct got_object_id *head_commit_id2 = NULL;
4974 struct got_tree_object *head_tree = NULL;
4975 struct got_object_id *new_tree_id = NULL;
4977 struct got_object_id_queue parent_ids;
4978 struct got_object_qid *pid = NULL;
4979 char *logmsg = NULL;
4981 *new_commit_id = NULL;
4983 SIMPLEQ_INIT(&parent_ids);
4985 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4989 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4993 if (commit_msg_cb != NULL) {
4994 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4999 if (logmsg == NULL || strlen(logmsg) == 0) {
5000 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5004 /* Create blobs from added and modified files and record their IDs. */
5005 TAILQ_FOREACH(pe, commitable_paths, entry) {
5006 struct got_commitable *ct = pe->data;
5009 /* Blobs for staged files already exist. */
5010 if (ct->staged_status == GOT_STATUS_ADD ||
5011 ct->staged_status == GOT_STATUS_MODIFY)
5014 if (ct->status != GOT_STATUS_ADD &&
5015 ct->status != GOT_STATUS_MODIFY &&
5016 ct->status != GOT_STATUS_MODE_CHANGE)
5019 if (asprintf(&ondisk_path, "%s/%s",
5020 worktree->root_path, pe->path) == -1) {
5021 err = got_error_from_errno("asprintf");
5024 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5031 * When comitting a symlink we convert "bad" symlinks (those
5032 * which point outside the work tree or into .got) to regular
5033 * files. This way, the post-commit work tree state matches
5034 * a fresh checkout of the tree which was committed.
5036 if (S_ISLNK(get_ct_file_mode(ct))) {
5037 struct got_blob_object *blob;
5038 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5044 err = install_symlink(worktree, ondisk_path, ct->path,
5045 get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
5046 0, 0, repo, NULL, NULL);
5047 got_object_blob_close(blob);
5056 /* Recursively write new tree objects. */
5057 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5058 commitable_paths, status_cb, status_arg, repo);
5062 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5065 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5066 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5067 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5068 got_object_qid_free(pid);
5074 /* Check if a concurrent commit to our branch has occurred. */
5075 head_ref_name = got_worktree_get_head_ref_name(worktree);
5076 if (head_ref_name == NULL) {
5077 err = got_error_from_errno("got_worktree_get_head_ref_name");
5080 /* Lock the reference here to prevent concurrent modification. */
5081 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5084 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5087 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5088 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5091 /* Update branch head in repository. */
5092 err = got_ref_change_ref(head_ref2, *new_commit_id);
5095 err = got_ref_write(head_ref2, repo);
5099 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5103 err = ref_base_commit(worktree, repo);
5108 got_object_tree_close(head_tree);
5110 got_object_commit_close(head_commit);
5111 free(head_commit_id2);
5113 unlockerr = got_ref_unlock(head_ref2);
5114 if (unlockerr && err == NULL)
5116 got_ref_close(head_ref2);
5121 static const struct got_error *
5122 check_path_is_commitable(const char *path,
5123 struct got_pathlist_head *commitable_paths)
5125 struct got_pathlist_entry *cpe = NULL;
5126 size_t path_len = strlen(path);
5128 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5129 struct got_commitable *ct = cpe->data;
5130 const char *ct_path = ct->path;
5132 while (ct_path[0] == '/')
5135 if (strcmp(path, ct_path) == 0 ||
5136 got_path_is_child(ct_path, path, path_len))
5141 return got_error_path(path, GOT_ERR_BAD_PATH);
5146 static const struct got_error *
5147 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5149 int *have_staged_files = arg;
5151 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5152 *have_staged_files = 1;
5153 return got_error(GOT_ERR_CANCELLED);
5159 static const struct got_error *
5160 check_non_staged_files(struct got_fileindex *fileindex,
5161 struct got_pathlist_head *paths)
5163 struct got_pathlist_entry *pe;
5164 struct got_fileindex_entry *ie;
5166 TAILQ_FOREACH(pe, paths, entry) {
5167 if (pe->path[0] == '\0')
5169 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5171 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5172 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5173 return got_error_path(pe->path,
5174 GOT_ERR_FILE_NOT_STAGED);
5180 const struct got_error *
5181 got_worktree_commit(struct got_object_id **new_commit_id,
5182 struct got_worktree *worktree, struct got_pathlist_head *paths,
5183 const char *author, const char *committer,
5184 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5185 got_worktree_status_cb status_cb, void *status_arg,
5186 struct got_repository *repo)
5188 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5189 struct got_fileindex *fileindex = NULL;
5190 char *fileindex_path = NULL;
5191 struct got_pathlist_head commitable_paths;
5192 struct collect_commitables_arg cc_arg;
5193 struct got_pathlist_entry *pe;
5194 struct got_reference *head_ref = NULL;
5195 struct got_object_id *head_commit_id = NULL;
5196 int have_staged_files = 0;
5198 *new_commit_id = NULL;
5200 TAILQ_INIT(&commitable_paths);
5202 err = lock_worktree(worktree, LOCK_EX);
5206 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5210 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5214 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5218 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5219 &have_staged_files);
5220 if (err && err->code != GOT_ERR_CANCELLED)
5222 if (have_staged_files) {
5223 err = check_non_staged_files(fileindex, paths);
5228 cc_arg.commitable_paths = &commitable_paths;
5229 cc_arg.worktree = worktree;
5231 cc_arg.have_staged_files = have_staged_files;
5232 TAILQ_FOREACH(pe, paths, entry) {
5233 err = worktree_status(worktree, pe->path, fileindex, repo,
5234 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5239 if (TAILQ_EMPTY(&commitable_paths)) {
5240 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5244 TAILQ_FOREACH(pe, paths, entry) {
5245 err = check_path_is_commitable(pe->path, &commitable_paths);
5250 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5251 struct got_commitable *ct = pe->data;
5252 const char *ct_path = ct->in_repo_path;
5254 while (ct_path[0] == '/')
5256 err = check_out_of_date(ct_path, ct->status,
5257 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5258 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5264 err = commit_worktree(new_commit_id, &commitable_paths,
5265 head_commit_id, worktree, author, committer,
5266 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5270 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5271 fileindex, have_staged_files);
5272 sync_err = sync_fileindex(fileindex, fileindex_path);
5273 if (sync_err && err == NULL)
5277 got_fileindex_free(fileindex);
5278 free(fileindex_path);
5279 unlockerr = lock_worktree(worktree, LOCK_SH);
5280 if (unlockerr && err == NULL)
5282 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5283 struct got_commitable *ct = pe->data;
5284 free_commitable(ct);
5286 got_pathlist_free(&commitable_paths);
5291 got_commitable_get_path(struct got_commitable *ct)
5297 got_commitable_get_status(struct got_commitable *ct)
5302 struct check_rebase_ok_arg {
5303 struct got_worktree *worktree;
5304 struct got_repository *repo;
5307 static const struct got_error *
5308 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5310 const struct got_error *err = NULL;
5311 struct check_rebase_ok_arg *a = arg;
5312 unsigned char status;
5316 /* Reject rebase of a work tree with mixed base commits. */
5317 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5318 SHA1_DIGEST_LENGTH))
5319 return got_error(GOT_ERR_MIXED_COMMITS);
5321 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5323 return got_error_from_errno("asprintf");
5325 /* Reject rebase of a work tree with modified or staged files. */
5326 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5331 if (status != GOT_STATUS_NO_CHANGE)
5332 return got_error(GOT_ERR_MODIFIED);
5333 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5334 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5339 const struct got_error *
5340 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5341 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5342 struct got_worktree *worktree, struct got_reference *branch,
5343 struct got_repository *repo)
5345 const struct got_error *err = NULL;
5346 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5347 char *branch_ref_name = NULL;
5348 char *fileindex_path = NULL;
5349 struct check_rebase_ok_arg ok_arg;
5350 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5351 struct got_object_id *wt_branch_tip = NULL;
5353 *new_base_branch_ref = NULL;
5357 err = lock_worktree(worktree, LOCK_EX);
5361 err = open_fileindex(fileindex, &fileindex_path, worktree);
5365 ok_arg.worktree = worktree;
5367 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5372 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5376 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5380 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5384 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5389 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5392 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5393 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5397 err = got_ref_alloc_symref(new_base_branch_ref,
5398 new_base_branch_ref_name, wt_branch);
5401 err = got_ref_write(*new_base_branch_ref, repo);
5405 /* TODO Lock original branch's ref while rebasing? */
5407 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5411 err = got_ref_write(branch_ref, repo);
5415 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5416 worktree->base_commit_id);
5419 err = got_ref_write(*tmp_branch, repo);
5423 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5427 free(fileindex_path);
5428 free(tmp_branch_name);
5429 free(new_base_branch_ref_name);
5430 free(branch_ref_name);
5432 got_ref_close(branch_ref);
5434 got_ref_close(wt_branch);
5435 free(wt_branch_tip);
5437 if (*new_base_branch_ref) {
5438 got_ref_close(*new_base_branch_ref);
5439 *new_base_branch_ref = NULL;
5442 got_ref_close(*tmp_branch);
5446 got_fileindex_free(*fileindex);
5449 lock_worktree(worktree, LOCK_SH);
5454 const struct got_error *
5455 got_worktree_rebase_continue(struct got_object_id **commit_id,
5456 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5457 struct got_reference **branch, struct got_fileindex **fileindex,
5458 struct got_worktree *worktree, struct got_repository *repo)
5460 const struct got_error *err;
5461 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5462 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5463 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5464 char *fileindex_path = NULL;
5465 int have_staged_files = 0;
5468 *new_base_branch = NULL;
5473 err = lock_worktree(worktree, LOCK_EX);
5477 err = open_fileindex(fileindex, &fileindex_path, worktree);
5481 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5482 &have_staged_files);
5483 if (err && err->code != GOT_ERR_CANCELLED)
5485 if (have_staged_files) {
5486 err = got_error(GOT_ERR_STAGED_PATHS);
5490 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5494 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5498 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5502 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5506 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5510 err = got_ref_open(branch, repo,
5511 got_ref_get_symref_target(branch_ref), 0);
5515 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5519 err = got_ref_resolve(commit_id, repo, commit_ref);
5523 err = got_ref_open(new_base_branch, repo,
5524 new_base_branch_ref_name, 0);
5528 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5532 free(commit_ref_name);
5533 free(branch_ref_name);
5534 free(fileindex_path);
5536 got_ref_close(commit_ref);
5538 got_ref_close(branch_ref);
5543 got_ref_close(*tmp_branch);
5546 if (*new_base_branch) {
5547 got_ref_close(*new_base_branch);
5548 *new_base_branch = NULL;
5551 got_ref_close(*branch);
5555 got_fileindex_free(*fileindex);
5558 lock_worktree(worktree, LOCK_SH);
5563 const struct got_error *
5564 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5566 const struct got_error *err;
5567 char *tmp_branch_name = NULL;
5569 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5573 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5574 free(tmp_branch_name);
5578 static const struct got_error *
5579 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5580 char **logmsg, void *arg)
5586 static const struct got_error *
5587 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5588 const char *path, struct got_object_id *blob_id,
5589 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5590 int dirfd, const char *de_name)
5595 struct collect_merged_paths_arg {
5596 got_worktree_checkout_cb progress_cb;
5598 struct got_pathlist_head *merged_paths;
5601 static const struct got_error *
5602 collect_merged_paths(void *arg, unsigned char status, const char *path)
5604 const struct got_error *err;
5605 struct collect_merged_paths_arg *a = arg;
5607 struct got_pathlist_entry *new;
5609 err = (*a->progress_cb)(a->progress_arg, status, path);
5613 if (status != GOT_STATUS_MERGE &&
5614 status != GOT_STATUS_ADD &&
5615 status != GOT_STATUS_DELETE &&
5616 status != GOT_STATUS_CONFLICT)
5621 return got_error_from_errno("strdup");
5623 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5624 if (err || new == NULL)
5630 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5632 struct got_pathlist_entry *pe;
5634 TAILQ_FOREACH(pe, merged_paths, entry)
5635 free((char *)pe->path);
5637 got_pathlist_free(merged_paths);
5640 static const struct got_error *
5641 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5642 int is_rebase, struct got_repository *repo)
5644 const struct got_error *err;
5645 struct got_reference *commit_ref = NULL;
5647 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5649 if (err->code != GOT_ERR_NOT_REF)
5651 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5654 err = got_ref_write(commit_ref, repo);
5657 } else if (is_rebase) {
5658 struct got_object_id *stored_id;
5661 err = got_ref_resolve(&stored_id, repo, commit_ref);
5664 cmp = got_object_id_cmp(commit_id, stored_id);
5667 err = got_error(GOT_ERR_REBASE_COMMITID);
5673 got_ref_close(commit_ref);
5677 static const struct got_error *
5678 rebase_merge_files(struct got_pathlist_head *merged_paths,
5679 const char *commit_ref_name, struct got_worktree *worktree,
5680 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5681 struct got_object_id *commit_id, struct got_repository *repo,
5682 got_worktree_checkout_cb progress_cb, void *progress_arg,
5683 got_cancel_cb cancel_cb, void *cancel_arg)
5685 const struct got_error *err;
5686 struct got_reference *commit_ref = NULL;
5687 struct collect_merged_paths_arg cmp_arg;
5688 char *fileindex_path;
5690 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5692 err = get_fileindex_path(&fileindex_path, worktree);
5696 cmp_arg.progress_cb = progress_cb;
5697 cmp_arg.progress_arg = progress_arg;
5698 cmp_arg.merged_paths = merged_paths;
5699 err = merge_files(worktree, fileindex, fileindex_path,
5700 parent_commit_id, commit_id, repo, collect_merged_paths,
5701 &cmp_arg, cancel_cb, cancel_arg);
5703 got_ref_close(commit_ref);
5707 const struct got_error *
5708 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5709 struct got_worktree *worktree, struct got_fileindex *fileindex,
5710 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5711 struct got_repository *repo,
5712 got_worktree_checkout_cb progress_cb, void *progress_arg,
5713 got_cancel_cb cancel_cb, void *cancel_arg)
5715 const struct got_error *err;
5716 char *commit_ref_name;
5718 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5722 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5726 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5727 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5728 progress_arg, cancel_cb, cancel_arg);
5730 free(commit_ref_name);
5734 const struct got_error *
5735 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5736 struct got_worktree *worktree, struct got_fileindex *fileindex,
5737 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5738 struct got_repository *repo,
5739 got_worktree_checkout_cb progress_cb, void *progress_arg,
5740 got_cancel_cb cancel_cb, void *cancel_arg)
5742 const struct got_error *err;
5743 char *commit_ref_name;
5745 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5749 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5753 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5754 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5755 progress_arg, cancel_cb, cancel_arg);
5757 free(commit_ref_name);
5761 static const struct got_error *
5762 rebase_commit(struct got_object_id **new_commit_id,
5763 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5764 struct got_worktree *worktree, struct got_fileindex *fileindex,
5765 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5766 const char *new_logmsg, struct got_repository *repo)
5768 const struct got_error *err, *sync_err;
5769 struct got_pathlist_head commitable_paths;
5770 struct collect_commitables_arg cc_arg;
5771 char *fileindex_path = NULL;
5772 struct got_reference *head_ref = NULL;
5773 struct got_object_id *head_commit_id = NULL;
5774 char *logmsg = NULL;
5776 TAILQ_INIT(&commitable_paths);
5777 *new_commit_id = NULL;
5779 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5781 err = get_fileindex_path(&fileindex_path, worktree);
5785 cc_arg.commitable_paths = &commitable_paths;
5786 cc_arg.worktree = worktree;
5788 cc_arg.have_staged_files = 0;
5790 * If possible get the status of individual files directly to
5791 * avoid crawling the entire work tree once per rebased commit.
5792 * TODO: Ideally, merged_paths would contain a list of commitables
5793 * we could use so we could skip worktree_status() entirely.
5796 struct got_pathlist_entry *pe;
5797 TAILQ_FOREACH(pe, merged_paths, entry) {
5798 err = worktree_status(worktree, pe->path, fileindex,
5799 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5805 err = worktree_status(worktree, "", fileindex, repo,
5806 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5811 if (TAILQ_EMPTY(&commitable_paths)) {
5812 /* No-op change; commit will be elided. */
5813 err = got_ref_delete(commit_ref, repo);
5816 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5820 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5824 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5829 logmsg = strdup(new_logmsg);
5830 if (logmsg == NULL) {
5831 err = got_error_from_errno("strdup");
5835 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5840 /* NB: commit_worktree will call free(logmsg) */
5841 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5842 worktree, got_object_commit_get_author(orig_commit),
5843 got_object_commit_get_committer(orig_commit),
5844 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5848 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5852 err = got_ref_delete(commit_ref, repo);
5856 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5858 sync_err = sync_fileindex(fileindex, fileindex_path);
5859 if (sync_err && err == NULL)
5862 free(fileindex_path);
5863 free(head_commit_id);
5865 got_ref_close(head_ref);
5867 free(*new_commit_id);
5868 *new_commit_id = NULL;
5873 const struct got_error *
5874 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5875 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5876 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5877 struct got_commit_object *orig_commit,
5878 struct got_object_id *orig_commit_id, struct got_repository *repo)
5880 const struct got_error *err;
5881 char *commit_ref_name;
5882 struct got_reference *commit_ref = NULL;
5883 struct got_object_id *commit_id = NULL;
5885 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5889 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5892 err = got_ref_resolve(&commit_id, repo, commit_ref);
5895 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5896 err = got_error(GOT_ERR_REBASE_COMMITID);
5900 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5901 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5904 got_ref_close(commit_ref);
5905 free(commit_ref_name);
5910 const struct got_error *
5911 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5912 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5913 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5914 struct got_commit_object *orig_commit,
5915 struct got_object_id *orig_commit_id, const char *new_logmsg,
5916 struct got_repository *repo)
5918 const struct got_error *err;
5919 char *commit_ref_name;
5920 struct got_reference *commit_ref = NULL;
5922 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5926 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5930 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5931 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5934 got_ref_close(commit_ref);
5935 free(commit_ref_name);
5939 const struct got_error *
5940 got_worktree_rebase_postpone(struct got_worktree *worktree,
5941 struct got_fileindex *fileindex)
5944 got_fileindex_free(fileindex);
5945 return lock_worktree(worktree, LOCK_SH);
5948 static const struct got_error *
5949 delete_ref(const char *name, struct got_repository *repo)
5951 const struct got_error *err;
5952 struct got_reference *ref;
5954 err = got_ref_open(&ref, repo, name, 0);
5956 if (err->code == GOT_ERR_NOT_REF)
5961 err = got_ref_delete(ref, repo);
5966 static const struct got_error *
5967 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5969 const struct got_error *err;
5970 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5971 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5973 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5976 err = delete_ref(tmp_branch_name, repo);
5980 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5983 err = delete_ref(new_base_branch_ref_name, repo);
5987 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5990 err = delete_ref(branch_ref_name, repo);
5994 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5997 err = delete_ref(commit_ref_name, repo);
6002 free(tmp_branch_name);
6003 free(new_base_branch_ref_name);
6004 free(branch_ref_name);
6005 free(commit_ref_name);
6009 const struct got_error *
6010 got_worktree_rebase_complete(struct got_worktree *worktree,
6011 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6012 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6013 struct got_repository *repo)
6015 const struct got_error *err, *unlockerr;
6016 struct got_object_id *new_head_commit_id = NULL;
6018 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6022 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6026 err = got_ref_write(rebased_branch, repo);
6030 err = got_worktree_set_head_ref(worktree, rebased_branch);
6034 err = delete_rebase_refs(worktree, repo);
6037 got_fileindex_free(fileindex);
6038 free(new_head_commit_id);
6039 unlockerr = lock_worktree(worktree, LOCK_SH);
6040 if (unlockerr && err == NULL)
6045 const struct got_error *
6046 got_worktree_rebase_abort(struct got_worktree *worktree,
6047 struct got_fileindex *fileindex, struct got_repository *repo,
6048 struct got_reference *new_base_branch,
6049 got_worktree_checkout_cb progress_cb, void *progress_arg)
6051 const struct got_error *err, *unlockerr, *sync_err;
6052 struct got_reference *resolved = NULL;
6053 struct got_object_id *commit_id = NULL;
6054 char *fileindex_path = NULL;
6055 struct revert_file_args rfa;
6056 struct got_object_id *tree_id = NULL;
6058 err = lock_worktree(worktree, LOCK_EX);
6062 err = got_ref_open(&resolved, repo,
6063 got_ref_get_symref_target(new_base_branch), 0);
6067 err = got_worktree_set_head_ref(worktree, resolved);
6072 * XXX commits to the base branch could have happened while
6073 * we were busy rebasing; should we store the original commit ID
6074 * when rebase begins and read it back here?
6076 err = got_ref_resolve(&commit_id, repo, resolved);
6080 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6084 err = got_object_id_by_path(&tree_id, repo,
6085 worktree->base_commit_id, worktree->path_prefix);
6089 err = delete_rebase_refs(worktree, repo);
6093 err = get_fileindex_path(&fileindex_path, worktree);
6097 rfa.worktree = worktree;
6098 rfa.fileindex = fileindex;
6099 rfa.progress_cb = progress_cb;
6100 rfa.progress_arg = progress_arg;
6101 rfa.patch_cb = NULL;
6102 rfa.patch_arg = NULL;
6104 err = worktree_status(worktree, "", fileindex, repo,
6105 revert_file, &rfa, NULL, NULL, 0, 0);
6109 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6110 repo, progress_cb, progress_arg, NULL, NULL);
6112 sync_err = sync_fileindex(fileindex, fileindex_path);
6113 if (sync_err && err == NULL)
6116 got_ref_close(resolved);
6120 got_fileindex_free(fileindex);
6121 free(fileindex_path);
6123 unlockerr = lock_worktree(worktree, LOCK_SH);
6124 if (unlockerr && err == NULL)
6129 const struct got_error *
6130 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6131 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6132 struct got_fileindex **fileindex, struct got_worktree *worktree,
6133 struct got_repository *repo)
6135 const struct got_error *err = NULL;
6136 char *tmp_branch_name = NULL;
6137 char *branch_ref_name = NULL;
6138 char *base_commit_ref_name = NULL;
6139 char *fileindex_path = NULL;
6140 struct check_rebase_ok_arg ok_arg;
6141 struct got_reference *wt_branch = NULL;
6142 struct got_reference *base_commit_ref = NULL;
6146 *base_commit_id = NULL;
6149 err = lock_worktree(worktree, LOCK_EX);
6153 err = open_fileindex(fileindex, &fileindex_path, worktree);
6157 ok_arg.worktree = worktree;
6159 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6164 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6168 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6172 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6177 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6182 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6186 err = got_ref_write(*branch_ref, repo);
6190 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6191 worktree->base_commit_id);
6194 err = got_ref_write(base_commit_ref, repo);
6197 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6198 if (*base_commit_id == NULL) {
6199 err = got_error_from_errno("got_object_id_dup");
6203 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6204 worktree->base_commit_id);
6207 err = got_ref_write(*tmp_branch, repo);
6211 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6215 free(fileindex_path);
6216 free(tmp_branch_name);
6217 free(branch_ref_name);
6218 free(base_commit_ref_name);
6220 got_ref_close(wt_branch);
6223 got_ref_close(*branch_ref);
6227 got_ref_close(*tmp_branch);
6230 free(*base_commit_id);
6232 got_fileindex_free(*fileindex);
6235 lock_worktree(worktree, LOCK_SH);
6240 const struct got_error *
6241 got_worktree_histedit_postpone(struct got_worktree *worktree,
6242 struct got_fileindex *fileindex)
6245 got_fileindex_free(fileindex);
6246 return lock_worktree(worktree, LOCK_SH);
6249 const struct got_error *
6250 got_worktree_histedit_in_progress(int *in_progress,
6251 struct got_worktree *worktree)
6253 const struct got_error *err;
6254 char *tmp_branch_name = NULL;
6256 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6260 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6261 free(tmp_branch_name);
6265 const struct got_error *
6266 got_worktree_histedit_continue(struct got_object_id **commit_id,
6267 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6268 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6269 struct got_worktree *worktree, struct got_repository *repo)
6271 const struct got_error *err;
6272 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6273 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6274 struct got_reference *commit_ref = NULL;
6275 struct got_reference *base_commit_ref = NULL;
6276 char *fileindex_path = NULL;
6277 int have_staged_files = 0;
6281 *base_commit_id = NULL;
6284 err = lock_worktree(worktree, LOCK_EX);
6288 err = open_fileindex(fileindex, &fileindex_path, worktree);
6292 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6293 &have_staged_files);
6294 if (err && err->code != GOT_ERR_CANCELLED)
6296 if (have_staged_files) {
6297 err = got_error(GOT_ERR_STAGED_PATHS);
6301 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6305 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6309 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6313 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6318 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6322 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6325 err = got_ref_resolve(commit_id, repo, commit_ref);
6329 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6332 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6336 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6340 free(commit_ref_name);
6341 free(branch_ref_name);
6342 free(fileindex_path);
6344 got_ref_close(commit_ref);
6345 if (base_commit_ref)
6346 got_ref_close(base_commit_ref);
6350 free(*base_commit_id);
6351 *base_commit_id = NULL;
6353 got_ref_close(*tmp_branch);
6357 got_fileindex_free(*fileindex);
6360 lock_worktree(worktree, LOCK_EX);
6365 static const struct got_error *
6366 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6368 const struct got_error *err;
6369 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6370 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6372 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6375 err = delete_ref(tmp_branch_name, repo);
6379 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6383 err = delete_ref(base_commit_ref_name, repo);
6387 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6390 err = delete_ref(branch_ref_name, repo);
6394 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6397 err = delete_ref(commit_ref_name, repo);
6401 free(tmp_branch_name);
6402 free(base_commit_ref_name);
6403 free(branch_ref_name);
6404 free(commit_ref_name);
6408 const struct got_error *
6409 got_worktree_histedit_abort(struct got_worktree *worktree,
6410 struct got_fileindex *fileindex, struct got_repository *repo,
6411 struct got_reference *branch, struct got_object_id *base_commit_id,
6412 got_worktree_checkout_cb progress_cb, void *progress_arg)
6414 const struct got_error *err, *unlockerr, *sync_err;
6415 struct got_reference *resolved = NULL;
6416 char *fileindex_path = NULL;
6417 struct got_object_id *tree_id = NULL;
6418 struct revert_file_args rfa;
6420 err = lock_worktree(worktree, LOCK_EX);
6424 err = got_ref_open(&resolved, repo,
6425 got_ref_get_symref_target(branch), 0);
6429 err = got_worktree_set_head_ref(worktree, resolved);
6433 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6437 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6438 worktree->path_prefix);
6442 err = delete_histedit_refs(worktree, repo);
6446 err = get_fileindex_path(&fileindex_path, worktree);
6450 rfa.worktree = worktree;
6451 rfa.fileindex = fileindex;
6452 rfa.progress_cb = progress_cb;
6453 rfa.progress_arg = progress_arg;
6454 rfa.patch_cb = NULL;
6455 rfa.patch_arg = NULL;
6457 err = worktree_status(worktree, "", fileindex, repo,
6458 revert_file, &rfa, NULL, NULL, 0, 0);
6462 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6463 repo, progress_cb, progress_arg, NULL, NULL);
6465 sync_err = sync_fileindex(fileindex, fileindex_path);
6466 if (sync_err && err == NULL)
6469 got_ref_close(resolved);
6471 free(fileindex_path);
6473 unlockerr = lock_worktree(worktree, LOCK_SH);
6474 if (unlockerr && err == NULL)
6479 const struct got_error *
6480 got_worktree_histedit_complete(struct got_worktree *worktree,
6481 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6482 struct got_reference *edited_branch, struct got_repository *repo)
6484 const struct got_error *err, *unlockerr;
6485 struct got_object_id *new_head_commit_id = NULL;
6486 struct got_reference *resolved = NULL;
6488 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6492 err = got_ref_open(&resolved, repo,
6493 got_ref_get_symref_target(edited_branch), 0);
6497 err = got_ref_change_ref(resolved, new_head_commit_id);
6501 err = got_ref_write(resolved, repo);
6505 err = got_worktree_set_head_ref(worktree, resolved);
6509 err = delete_histedit_refs(worktree, repo);
6512 got_fileindex_free(fileindex);
6513 free(new_head_commit_id);
6514 unlockerr = lock_worktree(worktree, LOCK_SH);
6515 if (unlockerr && err == NULL)
6520 const struct got_error *
6521 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6522 struct got_object_id *commit_id, struct got_repository *repo)
6524 const struct got_error *err;
6525 char *commit_ref_name;
6527 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6531 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6535 err = delete_ref(commit_ref_name, repo);
6537 free(commit_ref_name);
6541 const struct got_error *
6542 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6543 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6544 struct got_worktree *worktree, const char *refname,
6545 struct got_repository *repo)
6547 const struct got_error *err = NULL;
6548 char *fileindex_path = NULL;
6549 struct check_rebase_ok_arg ok_arg;
6553 *base_branch_ref = NULL;
6555 err = lock_worktree(worktree, LOCK_EX);
6559 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6560 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6561 "cannot integrate a branch into itself; "
6562 "update -b or different branch name required");
6566 err = open_fileindex(fileindex, &fileindex_path, worktree);
6570 /* Preconditions are the same as for rebase. */
6571 ok_arg.worktree = worktree;
6573 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6578 err = got_ref_open(branch_ref, repo, refname, 1);
6582 err = got_ref_open(base_branch_ref, repo,
6583 got_worktree_get_head_ref_name(worktree), 1);
6587 got_ref_close(*branch_ref);
6590 if (*base_branch_ref) {
6591 got_ref_close(*base_branch_ref);
6592 *base_branch_ref = NULL;
6595 got_fileindex_free(*fileindex);
6598 lock_worktree(worktree, LOCK_SH);
6603 const struct got_error *
6604 got_worktree_integrate_continue(struct got_worktree *worktree,
6605 struct got_fileindex *fileindex, struct got_repository *repo,
6606 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6607 got_worktree_checkout_cb progress_cb, void *progress_arg,
6608 got_cancel_cb cancel_cb, void *cancel_arg)
6610 const struct got_error *err = NULL, *sync_err, *unlockerr;
6611 char *fileindex_path = NULL;
6612 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6614 err = get_fileindex_path(&fileindex_path, worktree);
6618 err = got_ref_resolve(&commit_id, repo, branch_ref);
6622 err = got_object_id_by_path(&tree_id, repo, commit_id,
6623 worktree->path_prefix);
6627 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6631 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6632 progress_cb, progress_arg, cancel_cb, cancel_arg);
6636 err = got_ref_change_ref(base_branch_ref, commit_id);
6640 err = got_ref_write(base_branch_ref, repo);
6642 sync_err = sync_fileindex(fileindex, fileindex_path);
6643 if (sync_err && err == NULL)
6647 unlockerr = got_ref_unlock(branch_ref);
6648 if (unlockerr && err == NULL)
6650 got_ref_close(branch_ref);
6652 unlockerr = got_ref_unlock(base_branch_ref);
6653 if (unlockerr && err == NULL)
6655 got_ref_close(base_branch_ref);
6657 got_fileindex_free(fileindex);
6658 free(fileindex_path);
6661 unlockerr = lock_worktree(worktree, LOCK_SH);
6662 if (unlockerr && err == NULL)
6667 const struct got_error *
6668 got_worktree_integrate_abort(struct got_worktree *worktree,
6669 struct got_fileindex *fileindex, struct got_repository *repo,
6670 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6672 const struct got_error *err = NULL, *unlockerr = NULL;
6674 got_fileindex_free(fileindex);
6676 err = lock_worktree(worktree, LOCK_SH);
6678 unlockerr = got_ref_unlock(branch_ref);
6679 if (unlockerr && err == NULL)
6681 got_ref_close(branch_ref);
6683 unlockerr = got_ref_unlock(base_branch_ref);
6684 if (unlockerr && err == NULL)
6686 got_ref_close(base_branch_ref);
6691 struct check_stage_ok_arg {
6692 struct got_object_id *head_commit_id;
6693 struct got_worktree *worktree;
6694 struct got_fileindex *fileindex;
6695 struct got_repository *repo;
6699 const struct got_error *
6700 check_stage_ok(void *arg, unsigned char status,
6701 unsigned char staged_status, const char *relpath,
6702 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6703 struct got_object_id *commit_id, int dirfd, const char *de_name)
6705 struct check_stage_ok_arg *a = arg;
6706 const struct got_error *err = NULL;
6707 struct got_fileindex_entry *ie;
6708 struct got_object_id base_commit_id;
6709 struct got_object_id *base_commit_idp = NULL;
6710 char *in_repo_path = NULL, *p;
6712 if (status == GOT_STATUS_UNVERSIONED ||
6713 status == GOT_STATUS_NO_CHANGE)
6715 if (status == GOT_STATUS_NONEXISTENT)
6716 return got_error_set_errno(ENOENT, relpath);
6718 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6720 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6722 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6723 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6725 return got_error_from_errno("asprintf");
6727 if (got_fileindex_entry_has_commit(ie)) {
6728 memcpy(base_commit_id.sha1, ie->commit_sha1,
6729 SHA1_DIGEST_LENGTH);
6730 base_commit_idp = &base_commit_id;
6733 if (status == GOT_STATUS_CONFLICT) {
6734 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6736 } else if (status != GOT_STATUS_ADD &&
6737 status != GOT_STATUS_MODIFY &&
6738 status != GOT_STATUS_DELETE) {
6739 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6743 a->have_changes = 1;
6748 err = check_out_of_date(p, status, staged_status,
6749 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6750 GOT_ERR_STAGE_OUT_OF_DATE);
6756 struct stage_path_arg {
6757 struct got_worktree *worktree;
6758 struct got_fileindex *fileindex;
6759 struct got_repository *repo;
6760 got_worktree_status_cb status_cb;
6762 got_worktree_patch_cb patch_cb;
6764 int staged_something;
6767 static const struct got_error *
6768 stage_path(void *arg, unsigned char status,
6769 unsigned char staged_status, const char *relpath,
6770 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6771 struct got_object_id *commit_id, int dirfd, const char *de_name)
6773 struct stage_path_arg *a = arg;
6774 const struct got_error *err = NULL;
6775 struct got_fileindex_entry *ie;
6776 char *ondisk_path = NULL, *path_content = NULL;
6778 struct got_object_id *new_staged_blob_id = NULL;
6780 if (status == GOT_STATUS_UNVERSIONED)
6783 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6785 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6787 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6789 return got_error_from_errno("asprintf");
6792 case GOT_STATUS_ADD:
6793 case GOT_STATUS_MODIFY:
6795 if (status == GOT_STATUS_ADD) {
6796 int choice = GOT_PATCH_CHOICE_NONE;
6797 err = (*a->patch_cb)(&choice, a->patch_arg,
6798 status, ie->path, NULL, 1, 1);
6801 if (choice != GOT_PATCH_CHOICE_YES)
6804 err = create_patched_content(&path_content, 0,
6805 staged_blob_id ? staged_blob_id : blob_id,
6806 ondisk_path, dirfd, de_name, ie->path,
6807 a->repo, a->patch_cb, a->patch_arg);
6808 if (err || path_content == NULL)
6812 err = got_object_blob_create(&new_staged_blob_id,
6813 path_content ? path_content : ondisk_path, a->repo);
6816 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6817 SHA1_DIGEST_LENGTH);
6818 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6819 stage = GOT_FILEIDX_STAGE_ADD;
6821 stage = GOT_FILEIDX_STAGE_MODIFY;
6822 got_fileindex_entry_stage_set(ie, stage);
6823 a->staged_something = 1;
6824 if (a->status_cb == NULL)
6826 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6827 get_staged_status(ie), relpath, blob_id,
6828 new_staged_blob_id, NULL, dirfd, de_name);
6830 case GOT_STATUS_DELETE:
6831 if (staged_status == GOT_STATUS_DELETE)
6834 int choice = GOT_PATCH_CHOICE_NONE;
6835 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6836 ie->path, NULL, 1, 1);
6839 if (choice == GOT_PATCH_CHOICE_NO)
6841 if (choice != GOT_PATCH_CHOICE_YES) {
6842 err = got_error(GOT_ERR_PATCH_CHOICE);
6846 stage = GOT_FILEIDX_STAGE_DELETE;
6847 got_fileindex_entry_stage_set(ie, stage);
6848 a->staged_something = 1;
6849 if (a->status_cb == NULL)
6851 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6852 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6855 case GOT_STATUS_NO_CHANGE:
6857 case GOT_STATUS_CONFLICT:
6858 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6860 case GOT_STATUS_NONEXISTENT:
6861 err = got_error_set_errno(ENOENT, relpath);
6864 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6868 if (path_content && unlink(path_content) == -1 && err == NULL)
6869 err = got_error_from_errno2("unlink", path_content);
6872 free(new_staged_blob_id);
6876 const struct got_error *
6877 got_worktree_stage(struct got_worktree *worktree,
6878 struct got_pathlist_head *paths,
6879 got_worktree_status_cb status_cb, void *status_arg,
6880 got_worktree_patch_cb patch_cb, void *patch_arg,
6881 struct got_repository *repo)
6883 const struct got_error *err = NULL, *sync_err, *unlockerr;
6884 struct got_pathlist_entry *pe;
6885 struct got_fileindex *fileindex = NULL;
6886 char *fileindex_path = NULL;
6887 struct got_reference *head_ref = NULL;
6888 struct got_object_id *head_commit_id = NULL;
6889 struct check_stage_ok_arg oka;
6890 struct stage_path_arg spa;
6892 err = lock_worktree(worktree, LOCK_EX);
6896 err = got_ref_open(&head_ref, repo,
6897 got_worktree_get_head_ref_name(worktree), 0);
6900 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6903 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6907 /* Check pre-conditions before staging anything. */
6908 oka.head_commit_id = head_commit_id;
6909 oka.worktree = worktree;
6910 oka.fileindex = fileindex;
6912 oka.have_changes = 0;
6913 TAILQ_FOREACH(pe, paths, entry) {
6914 err = worktree_status(worktree, pe->path, fileindex, repo,
6915 check_stage_ok, &oka, NULL, NULL, 0, 0);
6919 if (!oka.have_changes) {
6920 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6924 spa.worktree = worktree;
6925 spa.fileindex = fileindex;
6927 spa.patch_cb = patch_cb;
6928 spa.patch_arg = patch_arg;
6929 spa.status_cb = status_cb;
6930 spa.status_arg = status_arg;
6931 spa.staged_something = 0;
6932 TAILQ_FOREACH(pe, paths, entry) {
6933 err = worktree_status(worktree, pe->path, fileindex, repo,
6934 stage_path, &spa, NULL, NULL, 0, 0);
6938 if (!spa.staged_something) {
6939 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6943 sync_err = sync_fileindex(fileindex, fileindex_path);
6944 if (sync_err && err == NULL)
6948 got_ref_close(head_ref);
6949 free(head_commit_id);
6950 free(fileindex_path);
6952 got_fileindex_free(fileindex);
6953 unlockerr = lock_worktree(worktree, LOCK_SH);
6954 if (unlockerr && err == NULL)
6959 struct unstage_path_arg {
6960 struct got_worktree *worktree;
6961 struct got_fileindex *fileindex;
6962 struct got_repository *repo;
6963 got_worktree_checkout_cb progress_cb;
6965 got_worktree_patch_cb patch_cb;
6969 static const struct got_error *
6970 create_unstaged_content(char **path_unstaged_content,
6971 char **path_new_staged_content, struct got_object_id *blob_id,
6972 struct got_object_id *staged_blob_id, const char *relpath,
6973 struct got_repository *repo,
6974 got_worktree_patch_cb patch_cb, void *patch_arg)
6976 const struct got_error *err;
6977 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6978 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6979 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6980 struct stat sb1, sb2;
6981 struct got_diff_changes *changes = NULL;
6982 struct got_diff_state *ds = NULL;
6983 struct got_diff_args *args = NULL;
6984 struct got_diff_change *change;
6985 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6986 int have_content = 0, have_rejected_content = 0;
6988 *path_unstaged_content = NULL;
6989 *path_new_staged_content = NULL;
6991 err = got_object_id_str(&label1, blob_id);
6994 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6998 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7002 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7006 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7010 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7014 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7018 if (stat(path1, &sb1) == -1) {
7019 err = got_error_from_errno2("stat", path1);
7023 if (stat(path2, &sb2) == -1) {
7024 err = got_error_from_errno2("stat", path2);
7028 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7029 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7033 err = got_opentemp_named(path_unstaged_content, &outfile,
7034 "got-unstaged-content");
7037 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7038 "got-new-staged-content");
7042 if (fseek(f1, 0L, SEEK_SET) == -1) {
7043 err = got_ferror(f1, GOT_ERR_IO);
7046 if (fseek(f2, 0L, SEEK_SET) == -1) {
7047 err = got_ferror(f2, GOT_ERR_IO);
7050 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7052 err = apply_or_reject_change(&choice, change, ++n,
7053 changes->nchanges, ds, args, diff_flags, relpath,
7054 f1, f2, &line_cur1, &line_cur2,
7055 outfile, rejectfile, patch_cb, patch_arg);
7058 if (choice == GOT_PATCH_CHOICE_YES)
7061 have_rejected_content = 1;
7062 if (choice == GOT_PATCH_CHOICE_QUIT)
7065 if (have_content || have_rejected_content)
7066 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7067 outfile, rejectfile);
7071 got_object_blob_close(blob);
7073 got_object_blob_close(staged_blob);
7074 if (f1 && fclose(f1) == EOF && err == NULL)
7075 err = got_error_from_errno2("fclose", path1);
7076 if (f2 && fclose(f2) == EOF && err == NULL)
7077 err = got_error_from_errno2("fclose", path2);
7078 if (outfile && fclose(outfile) == EOF && err == NULL)
7079 err = got_error_from_errno2("fclose", *path_unstaged_content);
7080 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7081 err = got_error_from_errno2("fclose", *path_new_staged_content);
7082 if (path1 && unlink(path1) == -1 && err == NULL)
7083 err = got_error_from_errno2("unlink", path1);
7084 if (path2 && unlink(path2) == -1 && err == NULL)
7085 err = got_error_from_errno2("unlink", path2);
7086 if (err || !have_content) {
7087 if (*path_unstaged_content &&
7088 unlink(*path_unstaged_content) == -1 && err == NULL)
7089 err = got_error_from_errno2("unlink",
7090 *path_unstaged_content);
7091 free(*path_unstaged_content);
7092 *path_unstaged_content = NULL;
7094 if (err || !have_rejected_content) {
7095 if (*path_new_staged_content &&
7096 unlink(*path_new_staged_content) == -1 && err == NULL)
7097 err = got_error_from_errno2("unlink",
7098 *path_new_staged_content);
7099 free(*path_new_staged_content);
7100 *path_new_staged_content = NULL;
7104 got_diff_state_free(ds);
7108 got_diff_free_changes(changes);
7114 static const struct got_error *
7115 unstage_path(void *arg, unsigned char status,
7116 unsigned char staged_status, const char *relpath,
7117 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7118 struct got_object_id *commit_id, int dirfd, const char *de_name)
7120 const struct got_error *err = NULL;
7121 struct unstage_path_arg *a = arg;
7122 struct got_fileindex_entry *ie;
7123 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7124 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7125 char *path_new_staged_content = NULL;
7126 char *id_str = NULL, *label_orig = NULL;
7127 int local_changes_subsumed;
7130 if (staged_status != GOT_STATUS_ADD &&
7131 staged_status != GOT_STATUS_MODIFY &&
7132 staged_status != GOT_STATUS_DELETE)
7135 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7137 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7139 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7141 return got_error_from_errno("asprintf");
7143 err = got_object_id_str(&id_str,
7144 commit_id ? commit_id : a->worktree->base_commit_id);
7147 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7149 err = got_error_from_errno("asprintf");
7153 switch (staged_status) {
7154 case GOT_STATUS_MODIFY:
7155 err = got_object_open_as_blob(&blob_base, a->repo,
7160 case GOT_STATUS_ADD:
7162 if (staged_status == GOT_STATUS_ADD) {
7163 int choice = GOT_PATCH_CHOICE_NONE;
7164 err = (*a->patch_cb)(&choice, a->patch_arg,
7165 staged_status, ie->path, NULL, 1, 1);
7168 if (choice != GOT_PATCH_CHOICE_YES)
7171 err = create_unstaged_content(
7172 &path_unstaged_content,
7173 &path_new_staged_content, blob_id,
7174 staged_blob_id, ie->path, a->repo,
7175 a->patch_cb, a->patch_arg);
7176 if (err || path_unstaged_content == NULL)
7178 if (path_new_staged_content) {
7179 err = got_object_blob_create(
7181 path_new_staged_content,
7185 memcpy(ie->staged_blob_sha1,
7186 staged_blob_id->sha1,
7187 SHA1_DIGEST_LENGTH);
7189 err = merge_file(&local_changes_subsumed,
7190 a->worktree, blob_base, ondisk_path,
7191 relpath, got_fileindex_perms_to_st(ie),
7192 path_unstaged_content, label_orig,
7193 "unstaged", a->repo, a->progress_cb,
7196 path_new_staged_content == NULL)
7197 got_fileindex_entry_stage_set(ie,
7198 GOT_FILEIDX_STAGE_NONE);
7199 break; /* Done with this file. */
7202 err = got_object_open_as_blob(&blob_staged, a->repo,
7203 staged_blob_id, 8192);
7206 err = merge_blob(&local_changes_subsumed, a->worktree,
7207 blob_base, ondisk_path, relpath,
7208 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7209 commit_id ? commit_id : a->worktree->base_commit_id,
7210 a->repo, a->progress_cb, a->progress_arg);
7212 got_fileindex_entry_stage_set(ie,
7213 GOT_FILEIDX_STAGE_NONE);
7215 case GOT_STATUS_DELETE:
7217 int choice = GOT_PATCH_CHOICE_NONE;
7218 err = (*a->patch_cb)(&choice, a->patch_arg,
7219 staged_status, ie->path, NULL, 1, 1);
7222 if (choice == GOT_PATCH_CHOICE_NO)
7224 if (choice != GOT_PATCH_CHOICE_YES) {
7225 err = got_error(GOT_ERR_PATCH_CHOICE);
7229 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7230 err = get_file_status(&status, &sb, ie, ondisk_path,
7231 dirfd, de_name, a->repo);
7234 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7239 if (path_unstaged_content &&
7240 unlink(path_unstaged_content) == -1 && err == NULL)
7241 err = got_error_from_errno2("unlink", path_unstaged_content);
7242 if (path_new_staged_content &&
7243 unlink(path_new_staged_content) == -1 && err == NULL)
7244 err = got_error_from_errno2("unlink", path_new_staged_content);
7245 free(path_unstaged_content);
7246 free(path_new_staged_content);
7248 got_object_blob_close(blob_base);
7250 got_object_blob_close(blob_staged);
7256 const struct got_error *
7257 got_worktree_unstage(struct got_worktree *worktree,
7258 struct got_pathlist_head *paths,
7259 got_worktree_checkout_cb progress_cb, void *progress_arg,
7260 got_worktree_patch_cb patch_cb, void *patch_arg,
7261 struct got_repository *repo)
7263 const struct got_error *err = NULL, *sync_err, *unlockerr;
7264 struct got_pathlist_entry *pe;
7265 struct got_fileindex *fileindex = NULL;
7266 char *fileindex_path = NULL;
7267 struct unstage_path_arg upa;
7269 err = lock_worktree(worktree, LOCK_EX);
7273 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7277 upa.worktree = worktree;
7278 upa.fileindex = fileindex;
7280 upa.progress_cb = progress_cb;
7281 upa.progress_arg = progress_arg;
7282 upa.patch_cb = patch_cb;
7283 upa.patch_arg = patch_arg;
7284 TAILQ_FOREACH(pe, paths, entry) {
7285 err = worktree_status(worktree, pe->path, fileindex, repo,
7286 unstage_path, &upa, NULL, NULL, 0, 0);
7291 sync_err = sync_fileindex(fileindex, fileindex_path);
7292 if (sync_err && err == NULL)
7295 free(fileindex_path);
7297 got_fileindex_free(fileindex);
7298 unlockerr = lock_worktree(worktree, LOCK_SH);
7299 if (unlockerr && err == NULL)