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 = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
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(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);
834 * Perform a 3-way merge where blob_orig acts as the common ancestor,
835 * blob_deriv acts as the first derived version, and the file on disk
836 * acts as the second derived version.
838 static const struct got_error *
839 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
840 struct got_blob_object *blob_orig, const char *ondisk_path,
841 const char *path, uint16_t st_mode, const char *label_orig,
842 struct got_blob_object *blob_deriv,
843 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
844 got_worktree_checkout_cb progress_cb, void *progress_arg)
846 const struct got_error *err = NULL;
847 FILE *f_deriv = NULL;
848 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
849 char *label_deriv = NULL, *parent;
851 *local_changes_subsumed = 0;
853 parent = dirname(ondisk_path);
855 return got_error_from_errno2("dirname", ondisk_path);
858 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
859 err = got_error_from_errno("asprintf");
864 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
867 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
872 err = got_object_id_str(&id_str, deriv_base_commit_id);
875 if (asprintf(&label_deriv, "%s: commit %s",
876 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
877 err = got_error_from_errno("asprintf");
881 err = merge_file(local_changes_subsumed, worktree, blob_orig,
882 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
883 label_deriv, repo, progress_cb, progress_arg);
885 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
886 err = got_error_from_errno("fclose");
888 if (blob_deriv_path) {
889 unlink(blob_deriv_path);
890 free(blob_deriv_path);
897 static const struct got_error *
898 update_blob_fileindex_entry(struct got_worktree *worktree,
899 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
900 const char *ondisk_path, const char *path, struct got_blob_object *blob,
901 int update_timestamps)
903 const struct got_error *err = NULL;
906 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
908 err = got_fileindex_entry_update(ie, ondisk_path,
909 blob->id.sha1, worktree->base_commit_id->sha1,
912 struct got_fileindex_entry *new_ie;
913 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
914 path, blob->id.sha1, worktree->base_commit_id->sha1);
916 err = got_fileindex_entry_add(fileindex, new_ie);
922 get_ondisk_perms(int executable, mode_t st_mode)
924 mode_t xbits = S_IXUSR;
927 /* Map read bits to execute bits. */
928 if (st_mode & S_IRGRP)
930 if (st_mode & S_IROTH)
932 return st_mode | xbits;
935 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
938 static const struct got_error *
939 install_blob(struct got_worktree *worktree, const char *ondisk_path,
940 const char *path, mode_t te_mode, mode_t st_mode,
941 struct got_blob_object *blob, int restoring_missing_file,
942 int reverting_versioned_file, struct got_repository *repo,
943 got_worktree_checkout_cb progress_cb, void *progress_arg)
945 const struct got_error *err = NULL;
949 char *tmppath = NULL;
951 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
952 GOT_DEFAULT_FILE_MODE);
954 if (errno == ENOENT) {
955 char *parent = dirname(path);
957 return got_error_from_errno2("dirname", path);
958 err = add_dir_on_disk(worktree, parent);
961 fd = open(ondisk_path,
962 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
963 GOT_DEFAULT_FILE_MODE);
965 return got_error_from_errno2("open",
967 } else if (errno == EEXIST) {
968 if (!S_ISREG(st_mode)) {
969 /* TODO file is obstructed; do something */
970 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
973 err = got_opentemp_named_fd(&tmppath, &fd,
980 return got_error_from_errno2("open", ondisk_path);
983 if (restoring_missing_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
985 else if (reverting_versioned_file)
986 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
988 err = (*progress_cb)(progress_arg,
989 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
993 hdrlen = got_object_blob_get_hdrlen(blob);
995 const uint8_t *buf = got_object_blob_get_read_buf(blob);
996 err = got_object_blob_read_block(&len, blob);
1000 /* Skip blob object header first time around. */
1001 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1003 err = got_error_from_errno("write");
1005 } else if (outlen != len - hdrlen) {
1006 err = got_error(GOT_ERR_IO);
1013 if (fsync(fd) != 0) {
1014 err = got_error_from_errno("fsync");
1019 if (rename(tmppath, ondisk_path) != 0) {
1020 err = got_error_from_errno3("rename", tmppath,
1027 if (chmod(ondisk_path,
1028 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1029 err = got_error_from_errno2("chmod", ondisk_path);
1034 if (fd != -1 && close(fd) != 0 && err == NULL)
1035 err = got_error_from_errno("close");
1040 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1041 static const struct got_error *
1042 get_modified_file_content_status(unsigned char *status, FILE *f)
1044 const struct got_error *err = NULL;
1045 const char *markers[3] = {
1046 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1047 GOT_DIFF_CONFLICT_MARKER_SEP,
1048 GOT_DIFF_CONFLICT_MARKER_END
1053 const char delim[3] = {'\0', '\0', '\0'};
1055 while (*status == GOT_STATUS_MODIFY) {
1056 line = fparseln(f, &len, NULL, delim, 0);
1060 err = got_ferror(f, GOT_ERR_IO);
1064 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1065 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1067 *status = GOT_STATUS_CONFLICT;
1077 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1079 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1080 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1084 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1086 return !(ie->ctime_sec == sb->st_ctime &&
1087 ie->ctime_nsec == sb->st_ctimensec &&
1088 ie->mtime_sec == sb->st_mtime &&
1089 ie->mtime_nsec == sb->st_mtimensec &&
1090 ie->size == (sb->st_size & 0xffffffff) &&
1091 !xbit_differs(ie, sb->st_mode));
1094 static unsigned char
1095 get_staged_status(struct got_fileindex_entry *ie)
1097 switch (got_fileindex_entry_stage_get(ie)) {
1098 case GOT_FILEIDX_STAGE_ADD:
1099 return GOT_STATUS_ADD;
1100 case GOT_FILEIDX_STAGE_DELETE:
1101 return GOT_STATUS_DELETE;
1102 case GOT_FILEIDX_STAGE_MODIFY:
1103 return GOT_STATUS_MODIFY;
1105 return GOT_STATUS_NO_CHANGE;
1109 static const struct got_error *
1110 get_file_status(unsigned char *status, struct stat *sb,
1111 struct got_fileindex_entry *ie, const char *abspath,
1112 int dirfd, const char *de_name, struct got_repository *repo)
1114 const struct got_error *err = NULL;
1115 struct got_object_id id;
1120 struct got_blob_object *blob = NULL;
1122 unsigned char staged_status = get_staged_status(ie);
1124 *status = GOT_STATUS_NO_CHANGE;
1127 * Whenever the caller provides a directory descriptor and a
1128 * directory entry name for the file, use them! This prevents
1129 * race conditions if filesystem paths change beneath our feet.
1132 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1133 if (errno == ENOENT) {
1134 if (got_fileindex_entry_has_file_on_disk(ie))
1135 *status = GOT_STATUS_MISSING;
1137 *status = GOT_STATUS_DELETE;
1140 err = got_error_from_errno2("fstatat", abspath);
1144 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1145 if (fd == -1 && errno != ENOENT)
1146 return got_error_from_errno2("open", abspath);
1147 if (fd == -1 || fstat(fd, sb) == -1) {
1148 if (errno == ENOENT) {
1149 if (got_fileindex_entry_has_file_on_disk(ie))
1150 *status = GOT_STATUS_MISSING;
1152 *status = GOT_STATUS_DELETE;
1155 err = got_error_from_errno2("fstat", abspath);
1160 if (!S_ISREG(sb->st_mode)) {
1161 *status = GOT_STATUS_OBSTRUCTED;
1165 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1166 *status = GOT_STATUS_DELETE;
1168 } else if (!got_fileindex_entry_has_blob(ie) &&
1169 staged_status != GOT_STATUS_ADD) {
1170 *status = GOT_STATUS_ADD;
1174 if (!stat_info_differs(ie, sb))
1177 if (staged_status == GOT_STATUS_MODIFY ||
1178 staged_status == GOT_STATUS_ADD)
1179 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1181 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1183 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1188 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1190 err = got_error_from_errno2("openat", abspath);
1195 f = fdopen(fd, "r");
1197 err = got_error_from_errno2("fdopen", abspath);
1201 hdrlen = got_object_blob_get_hdrlen(blob);
1203 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1204 err = got_object_blob_read_block(&blen, blob);
1207 /* Skip length of blob object header first time around. */
1208 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1209 if (flen == 0 && ferror(f)) {
1210 err = got_error_from_errno("fread");
1215 *status = GOT_STATUS_MODIFY;
1217 } else if (flen == 0) {
1219 *status = GOT_STATUS_MODIFY;
1221 } else if (blen - hdrlen == flen) {
1222 /* Skip blob object header first time around. */
1223 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1224 *status = GOT_STATUS_MODIFY;
1228 *status = GOT_STATUS_MODIFY;
1234 if (*status == GOT_STATUS_MODIFY) {
1236 err = get_modified_file_content_status(status, f);
1237 } else if (xbit_differs(ie, sb->st_mode))
1238 *status = GOT_STATUS_MODE_CHANGE;
1241 got_object_blob_close(blob);
1242 if (f != NULL && fclose(f) == EOF && err == NULL)
1243 err = got_error_from_errno2("fclose", abspath);
1244 if (fd != -1 && close(fd) == -1 && err == NULL)
1245 err = got_error_from_errno2("close", abspath);
1250 * Update timestamps in the file index if a file is unmodified and
1251 * we had to run a full content comparison to find out.
1253 static const struct got_error *
1254 sync_timestamps(char *ondisk_path, unsigned char status,
1255 struct got_fileindex_entry *ie, struct stat *sb)
1257 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1258 return got_fileindex_entry_update(ie, ondisk_path,
1259 ie->blob_sha1, ie->commit_sha1, 1);
1264 static const struct got_error *
1265 update_blob(struct got_worktree *worktree,
1266 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1267 struct got_tree_entry *te, const char *path,
1268 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1271 const struct got_error *err = NULL;
1272 struct got_blob_object *blob = NULL;
1274 unsigned char status = GOT_STATUS_NO_CHANGE;
1277 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1278 return got_error_from_errno("asprintf");
1281 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1282 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1285 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1289 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1290 sb.st_mode = got_fileindex_perms_to_st(ie);
1292 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1294 if (status == GOT_STATUS_OBSTRUCTED) {
1295 err = (*progress_cb)(progress_arg, status, path);
1299 if (ie && status != GOT_STATUS_MISSING &&
1300 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1301 if (got_fileindex_entry_has_commit(ie) &&
1302 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1303 SHA1_DIGEST_LENGTH) == 0) {
1304 err = sync_timestamps(ondisk_path, status, ie, &sb);
1307 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1311 if (got_fileindex_entry_has_blob(ie) &&
1312 memcmp(ie->blob_sha1, te->id.sha1,
1313 SHA1_DIGEST_LENGTH) == 0) {
1314 err = sync_timestamps(ondisk_path, status, ie, &sb);
1319 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1323 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1324 int update_timestamps;
1325 struct got_blob_object *blob2 = NULL;
1326 char *label_orig = NULL;
1327 if (got_fileindex_entry_has_blob(ie)) {
1328 struct got_object_id id2;
1329 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1330 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1334 if (got_fileindex_entry_has_commit(ie)) {
1335 char id_str[SHA1_DIGEST_STRING_LENGTH];
1336 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1337 sizeof(id_str)) == NULL) {
1338 err = got_error_path(id_str,
1339 GOT_ERR_BAD_OBJ_ID_STR);
1342 if (asprintf(&label_orig, "%s: commit %s",
1343 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1344 err = got_error_from_errno("asprintf");
1348 err = merge_blob(&update_timestamps, worktree, blob2,
1349 ondisk_path, path, sb.st_mode, label_orig, blob,
1350 worktree->base_commit_id, repo,
1351 progress_cb, progress_arg);
1354 got_object_blob_close(blob2);
1358 * Do not update timestamps of files with local changes.
1359 * Otherwise, a future status walk would treat them as
1360 * unmodified files again.
1362 err = got_fileindex_entry_update(ie, ondisk_path,
1363 blob->id.sha1, worktree->base_commit_id->sha1,
1365 } else if (status == GOT_STATUS_MODE_CHANGE) {
1366 err = got_fileindex_entry_update(ie, ondisk_path,
1367 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1368 } else if (status == GOT_STATUS_DELETE) {
1369 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1372 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1373 ondisk_path, path, blob, 0);
1377 err = install_blob(worktree, ondisk_path, path, te->mode,
1378 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1379 repo, progress_cb, progress_arg);
1382 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1383 ondisk_path, path, blob, 1);
1387 got_object_blob_close(blob);
1393 static const struct got_error *
1394 remove_ondisk_file(const char *root_path, const char *path)
1396 const struct got_error *err = NULL;
1397 char *ondisk_path = NULL;
1399 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1400 return got_error_from_errno("asprintf");
1402 if (unlink(ondisk_path) == -1) {
1403 if (errno != ENOENT)
1404 err = got_error_from_errno2("unlink", ondisk_path);
1406 char *parent = dirname(ondisk_path);
1407 while (parent && strcmp(parent, root_path) != 0) {
1408 if (rmdir(parent) == -1) {
1409 if (errno != ENOTEMPTY)
1410 err = got_error_from_errno2("rmdir",
1414 parent = dirname(parent);
1421 static const struct got_error *
1422 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1423 struct got_fileindex_entry *ie, struct got_repository *repo,
1424 got_worktree_checkout_cb progress_cb, void *progress_arg)
1426 const struct got_error *err = NULL;
1427 unsigned char status;
1431 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1432 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1434 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1436 return got_error_from_errno("asprintf");
1438 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1442 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1443 status == GOT_STATUS_ADD) {
1444 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1448 * Preserve the working file and change the deleted blob's
1449 * entry into a schedule-add entry.
1451 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1459 if (status == GOT_STATUS_NO_CHANGE) {
1460 err = remove_ondisk_file(worktree->root_path, ie->path);
1464 got_fileindex_entry_remove(fileindex, ie);
1470 struct diff_cb_arg {
1471 struct got_fileindex *fileindex;
1472 struct got_worktree *worktree;
1473 struct got_repository *repo;
1474 got_worktree_checkout_cb progress_cb;
1476 got_cancel_cb cancel_cb;
1480 static const struct got_error *
1481 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1482 struct got_tree_entry *te, const char *parent_path)
1484 struct diff_cb_arg *a = arg;
1486 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1487 return got_error(GOT_ERR_CANCELLED);
1489 return update_blob(a->worktree, a->fileindex, ie, te,
1490 ie->path, a->repo, a->progress_cb, a->progress_arg);
1493 static const struct got_error *
1494 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1496 struct diff_cb_arg *a = arg;
1498 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1499 return got_error(GOT_ERR_CANCELLED);
1501 return delete_blob(a->worktree, a->fileindex, ie,
1502 a->repo, a->progress_cb, a->progress_arg);
1505 static const struct got_error *
1506 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1508 struct diff_cb_arg *a = arg;
1509 const struct got_error *err;
1512 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1513 return got_error(GOT_ERR_CANCELLED);
1515 if (got_object_tree_entry_is_submodule(te))
1518 if (asprintf(&path, "%s%s%s", parent_path,
1519 parent_path[0] ? "/" : "", te->name)
1521 return got_error_from_errno("asprintf");
1523 if (S_ISDIR(te->mode))
1524 err = add_dir_on_disk(a->worktree, path);
1526 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1527 a->repo, a->progress_cb, a->progress_arg);
1533 static const struct got_error *
1534 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1536 const struct got_error *err = NULL;
1537 char *uuidstr = NULL;
1538 uint32_t uuid_status;
1542 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1543 if (uuid_status != uuid_s_ok)
1544 return got_error_uuid(uuid_status, "uuid_to_string");
1546 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1548 err = got_error_from_errno("asprintf");
1555 const struct got_error *
1556 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1558 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1561 static const struct got_error *
1562 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1564 return get_ref_name(refname, worktree,
1565 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1568 static const struct got_error *
1569 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1571 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1574 static const struct got_error *
1575 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1577 return get_ref_name(refname, worktree,
1578 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1581 static const struct got_error *
1582 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1584 return get_ref_name(refname, worktree,
1585 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1588 static const struct got_error *
1589 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1591 return get_ref_name(refname, worktree,
1592 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1595 static const struct got_error *
1596 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1598 return get_ref_name(refname, worktree,
1599 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1602 static const struct got_error *
1603 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1605 return get_ref_name(refname, worktree,
1606 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1609 static const struct got_error *
1610 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1612 return get_ref_name(refname, worktree,
1613 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1616 const struct got_error *
1617 got_worktree_get_histedit_script_path(char **path,
1618 struct got_worktree *worktree)
1620 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1621 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1623 return got_error_from_errno("asprintf");
1629 * Prevent Git's garbage collector from deleting our base commit by
1630 * setting a reference to our base commit's ID.
1632 static const struct got_error *
1633 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1635 const struct got_error *err = NULL;
1636 struct got_reference *ref = NULL;
1639 err = got_worktree_get_base_ref_name(&refname, worktree);
1643 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1647 err = got_ref_write(ref, repo);
1655 static const struct got_error *
1656 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1658 const struct got_error *err = NULL;
1660 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1661 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1662 err = got_error_from_errno("asprintf");
1663 *fileindex_path = NULL;
1669 static const struct got_error *
1670 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1671 struct got_worktree *worktree)
1673 const struct got_error *err = NULL;
1676 *fileindex_path = NULL;
1677 *fileindex = got_fileindex_alloc();
1678 if (*fileindex == NULL)
1679 return got_error_from_errno("got_fileindex_alloc");
1681 err = get_fileindex_path(fileindex_path, worktree);
1685 index = fopen(*fileindex_path, "rb");
1686 if (index == NULL) {
1687 if (errno != ENOENT)
1688 err = got_error_from_errno2("fopen", *fileindex_path);
1690 err = got_fileindex_read(*fileindex, index);
1691 if (fclose(index) != 0 && err == NULL)
1692 err = got_error_from_errno("fclose");
1696 free(*fileindex_path);
1697 *fileindex_path = NULL;
1698 got_fileindex_free(*fileindex);
1704 struct bump_base_commit_id_arg {
1705 struct got_object_id *base_commit_id;
1708 const char *entry_name;
1709 got_worktree_checkout_cb progress_cb;
1713 /* Bump base commit ID of all files within an updated part of the work tree. */
1714 static const struct got_error *
1715 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1717 const struct got_error *err;
1718 struct bump_base_commit_id_arg *a = arg;
1720 if (a->entry_name) {
1721 if (strcmp(ie->path, a->path) != 0)
1723 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1726 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1727 SHA1_DIGEST_LENGTH) == 0)
1730 if (a->progress_cb) {
1731 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1736 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1740 static const struct got_error *
1741 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1743 const struct got_error *err = NULL;
1744 char *new_fileindex_path = NULL;
1745 FILE *new_index = NULL;
1747 err = got_opentemp_named(&new_fileindex_path, &new_index,
1752 err = got_fileindex_write(fileindex, new_index);
1756 if (rename(new_fileindex_path, fileindex_path) != 0) {
1757 err = got_error_from_errno3("rename", new_fileindex_path,
1759 unlink(new_fileindex_path);
1764 free(new_fileindex_path);
1768 static const struct got_error *
1769 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1770 struct got_object_id **tree_id, const char *wt_relpath,
1771 struct got_worktree *worktree, struct got_repository *repo)
1773 const struct got_error *err = NULL;
1774 struct got_object_id *id = NULL;
1775 char *in_repo_path = NULL;
1776 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1778 *entry_type = GOT_OBJ_TYPE_ANY;
1779 *tree_relpath = NULL;
1782 if (wt_relpath[0] == '\0') {
1783 /* Check out all files within the work tree. */
1784 *entry_type = GOT_OBJ_TYPE_TREE;
1785 *tree_relpath = strdup("");
1786 if (*tree_relpath == NULL) {
1787 err = got_error_from_errno("strdup");
1790 err = got_object_id_by_path(tree_id, repo,
1791 worktree->base_commit_id, worktree->path_prefix);
1797 /* Check out a subset of files in the work tree. */
1799 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1800 is_root_wt ? "" : "/", wt_relpath) == -1) {
1801 err = got_error_from_errno("asprintf");
1805 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1811 in_repo_path = NULL;
1813 err = got_object_get_type(entry_type, repo, id);
1817 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1818 /* Check out a single file. */
1819 if (strchr(wt_relpath, '/') == NULL) {
1820 /* Check out a single file in work tree's root dir. */
1821 in_repo_path = strdup(worktree->path_prefix);
1822 if (in_repo_path == NULL) {
1823 err = got_error_from_errno("strdup");
1826 *tree_relpath = strdup("");
1827 if (*tree_relpath == NULL) {
1828 err = got_error_from_errno("strdup");
1832 /* Check out a single file in a subdirectory. */
1833 err = got_path_dirname(tree_relpath, wt_relpath);
1836 if (asprintf(&in_repo_path, "%s%s%s",
1837 worktree->path_prefix, is_root_wt ? "" : "/",
1838 *tree_relpath) == -1) {
1839 err = got_error_from_errno("asprintf");
1843 err = got_object_id_by_path(tree_id, repo,
1844 worktree->base_commit_id, in_repo_path);
1846 /* Check out all files within a subdirectory. */
1847 *tree_id = got_object_id_dup(id);
1848 if (*tree_id == NULL) {
1849 err = got_error_from_errno("got_object_id_dup");
1852 *tree_relpath = strdup(wt_relpath);
1853 if (*tree_relpath == NULL) {
1854 err = got_error_from_errno("strdup");
1862 *entry_type = GOT_OBJ_TYPE_ANY;
1863 free(*tree_relpath);
1864 *tree_relpath = NULL;
1871 static const struct got_error *
1872 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1873 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1874 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1875 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1877 const struct got_error *err = NULL;
1878 struct got_commit_object *commit = NULL;
1879 struct got_tree_object *tree = NULL;
1880 struct got_fileindex_diff_tree_cb diff_cb;
1881 struct diff_cb_arg arg;
1883 err = ref_base_commit(worktree, repo);
1885 if (!(err->code == GOT_ERR_ERRNO && errno == EACCES))
1887 err = (*progress_cb)(progress_arg,
1888 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1893 err = got_object_open_as_commit(&commit, repo,
1894 worktree->base_commit_id);
1898 err = got_object_open_as_tree(&tree, repo, tree_id);
1903 got_object_tree_find_entry(tree, entry_name) == NULL) {
1904 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1908 diff_cb.diff_old_new = diff_old_new;
1909 diff_cb.diff_old = diff_old;
1910 diff_cb.diff_new = diff_new;
1911 arg.fileindex = fileindex;
1912 arg.worktree = worktree;
1914 arg.progress_cb = progress_cb;
1915 arg.progress_arg = progress_arg;
1916 arg.cancel_cb = cancel_cb;
1917 arg.cancel_arg = cancel_arg;
1918 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1919 entry_name, repo, &diff_cb, &arg);
1922 got_object_tree_close(tree);
1924 got_object_commit_close(commit);
1928 const struct got_error *
1929 got_worktree_checkout_files(struct got_worktree *worktree,
1930 struct got_pathlist_head *paths, struct got_repository *repo,
1931 got_worktree_checkout_cb progress_cb, void *progress_arg,
1932 got_cancel_cb cancel_cb, void *cancel_arg)
1934 const struct got_error *err = NULL, *sync_err, *unlockerr;
1935 struct got_commit_object *commit = NULL;
1936 struct got_tree_object *tree = NULL;
1937 struct got_fileindex *fileindex = NULL;
1938 char *fileindex_path = NULL;
1939 struct got_pathlist_entry *pe;
1940 struct tree_path_data {
1941 SIMPLEQ_ENTRY(tree_path_data) entry;
1942 struct got_object_id *tree_id;
1947 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1949 SIMPLEQ_INIT(&tree_paths);
1951 err = lock_worktree(worktree, LOCK_EX);
1955 /* Map all specified paths to in-repository trees. */
1956 TAILQ_FOREACH(pe, paths, entry) {
1957 tpd = malloc(sizeof(*tpd));
1959 err = got_error_from_errno("malloc");
1963 err = find_tree_entry_for_checkout(&tpd->entry_type,
1964 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1970 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1971 err = got_path_basename(&tpd->entry_name, pe->path);
1979 tpd->entry_name = NULL;
1981 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1985 * Read the file index.
1986 * Checking out files is supposed to be an idempotent operation.
1987 * If the on-disk file index is incomplete we will try to complete it.
1989 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1993 tpd = SIMPLEQ_FIRST(&tree_paths);
1994 TAILQ_FOREACH(pe, paths, entry) {
1995 struct bump_base_commit_id_arg bbc_arg;
1997 err = checkout_files(worktree, fileindex, tpd->relpath,
1998 tpd->tree_id, tpd->entry_name, repo,
1999 progress_cb, progress_arg, cancel_cb, cancel_arg);
2003 bbc_arg.base_commit_id = worktree->base_commit_id;
2004 bbc_arg.entry_name = tpd->entry_name;
2005 bbc_arg.path = pe->path;
2006 bbc_arg.path_len = pe->path_len;
2007 bbc_arg.progress_cb = progress_cb;
2008 bbc_arg.progress_arg = progress_arg;
2009 err = got_fileindex_for_each_entry_safe(fileindex,
2010 bump_base_commit_id, &bbc_arg);
2014 tpd = SIMPLEQ_NEXT(tpd, entry);
2016 sync_err = sync_fileindex(fileindex, fileindex_path);
2017 if (sync_err && err == NULL)
2020 free(fileindex_path);
2022 got_object_tree_close(tree);
2024 got_object_commit_close(commit);
2026 got_fileindex_free(fileindex);
2027 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2028 tpd = SIMPLEQ_FIRST(&tree_paths);
2029 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2034 unlockerr = lock_worktree(worktree, LOCK_SH);
2035 if (unlockerr && err == NULL)
2040 struct merge_file_cb_arg {
2041 struct got_worktree *worktree;
2042 struct got_fileindex *fileindex;
2043 got_worktree_checkout_cb progress_cb;
2045 got_cancel_cb cancel_cb;
2047 const char *label_orig;
2048 struct got_object_id *commit_id2;
2051 static const struct got_error *
2052 merge_file_cb(void *arg, struct got_blob_object *blob1,
2053 struct got_blob_object *blob2, struct got_object_id *id1,
2054 struct got_object_id *id2, const char *path1, const char *path2,
2055 mode_t mode1, mode_t mode2, struct got_repository *repo)
2057 static const struct got_error *err = NULL;
2058 struct merge_file_cb_arg *a = arg;
2059 struct got_fileindex_entry *ie;
2060 char *ondisk_path = NULL;
2062 unsigned char status;
2063 int local_changes_subsumed;
2065 if (blob1 && blob2) {
2066 ie = got_fileindex_entry_get(a->fileindex, path2,
2069 return (*a->progress_cb)(a->progress_arg,
2070 GOT_STATUS_MISSING, path2);
2072 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2074 return got_error_from_errno("asprintf");
2076 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2081 if (status == GOT_STATUS_DELETE) {
2082 err = (*a->progress_cb)(a->progress_arg,
2083 GOT_STATUS_MERGE, path2);
2086 if (status != GOT_STATUS_NO_CHANGE &&
2087 status != GOT_STATUS_MODIFY &&
2088 status != GOT_STATUS_CONFLICT &&
2089 status != GOT_STATUS_ADD) {
2090 err = (*a->progress_cb)(a->progress_arg, status, path2);
2094 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2095 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2096 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2098 ie = got_fileindex_entry_get(a->fileindex, path1,
2101 return (*a->progress_cb)(a->progress_arg,
2102 GOT_STATUS_MISSING, path2);
2104 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2106 return got_error_from_errno("asprintf");
2108 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2114 case GOT_STATUS_NO_CHANGE:
2115 err = (*a->progress_cb)(a->progress_arg,
2116 GOT_STATUS_DELETE, path1);
2119 err = remove_ondisk_file(a->worktree->root_path, path1);
2123 got_fileindex_entry_mark_deleted_from_disk(ie);
2125 case GOT_STATUS_DELETE:
2126 case GOT_STATUS_MISSING:
2127 err = (*a->progress_cb)(a->progress_arg,
2128 GOT_STATUS_DELETE, path1);
2132 got_fileindex_entry_mark_deleted_from_disk(ie);
2134 case GOT_STATUS_ADD:
2135 case GOT_STATUS_MODIFY:
2136 case GOT_STATUS_CONFLICT:
2137 err = (*a->progress_cb)(a->progress_arg,
2138 GOT_STATUS_CANNOT_DELETE, path1);
2142 case GOT_STATUS_OBSTRUCTED:
2143 err = (*a->progress_cb)(a->progress_arg, status, path1);
2151 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2153 return got_error_from_errno("asprintf");
2154 ie = got_fileindex_entry_get(a->fileindex, path2,
2157 err = get_file_status(&status, &sb, ie, ondisk_path,
2161 if (status != GOT_STATUS_NO_CHANGE &&
2162 status != GOT_STATUS_MODIFY &&
2163 status != GOT_STATUS_CONFLICT &&
2164 status != GOT_STATUS_ADD) {
2165 err = (*a->progress_cb)(a->progress_arg,
2169 err = merge_blob(&local_changes_subsumed, a->worktree,
2170 NULL, ondisk_path, path2, sb.st_mode,
2171 a->label_orig, blob2, a->commit_id2, repo,
2174 if (status == GOT_STATUS_DELETE) {
2175 err = update_blob_fileindex_entry(a->worktree,
2176 a->fileindex, ie, ondisk_path, ie->path,
2182 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2183 err = install_blob(a->worktree, ondisk_path, path2,
2184 /* XXX get this from parent tree! */
2185 GOT_DEFAULT_FILE_MODE,
2186 sb.st_mode, blob2, 0, 0, repo,
2187 a->progress_cb, a->progress_arg);
2190 err = got_fileindex_entry_alloc(&ie,
2191 ondisk_path, path2, NULL, NULL);
2194 err = got_fileindex_entry_add(a->fileindex, ie);
2196 got_fileindex_entry_free(ie);
2206 struct check_merge_ok_arg {
2207 struct got_worktree *worktree;
2208 struct got_repository *repo;
2211 static const struct got_error *
2212 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2214 const struct got_error *err = NULL;
2215 struct check_merge_ok_arg *a = arg;
2216 unsigned char status;
2220 /* Reject merges into a work tree with mixed base commits. */
2221 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2222 SHA1_DIGEST_LENGTH))
2223 return got_error(GOT_ERR_MIXED_COMMITS);
2225 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2227 return got_error_from_errno("asprintf");
2229 /* Reject merges into a work tree with conflicted files. */
2230 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2233 if (status == GOT_STATUS_CONFLICT)
2234 return got_error(GOT_ERR_CONFLICTS);
2239 static const struct got_error *
2240 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2241 const char *fileindex_path, struct got_object_id *commit_id1,
2242 struct got_object_id *commit_id2, struct got_repository *repo,
2243 got_worktree_checkout_cb progress_cb, void *progress_arg,
2244 got_cancel_cb cancel_cb, void *cancel_arg)
2246 const struct got_error *err = NULL, *sync_err;
2247 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2248 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2249 struct merge_file_cb_arg arg;
2250 char *label_orig = NULL;
2255 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2256 worktree->path_prefix);
2260 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2264 err = got_object_id_str(&id_str, commit_id1);
2268 if (asprintf(&label_orig, "%s: commit %s",
2269 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2270 err = got_error_from_errno("asprintf");
2277 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2278 worktree->path_prefix);
2282 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2286 arg.worktree = worktree;
2287 arg.fileindex = fileindex;
2288 arg.progress_cb = progress_cb;
2289 arg.progress_arg = progress_arg;
2290 arg.cancel_cb = cancel_cb;
2291 arg.cancel_arg = cancel_arg;
2292 arg.label_orig = label_orig;
2293 arg.commit_id2 = commit_id2;
2294 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2295 sync_err = sync_fileindex(fileindex, fileindex_path);
2296 if (sync_err && err == NULL)
2300 got_object_tree_close(tree1);
2302 got_object_tree_close(tree2);
2307 const struct got_error *
2308 got_worktree_merge_files(struct got_worktree *worktree,
2309 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2310 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2311 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2313 const struct got_error *err, *unlockerr;
2314 char *fileindex_path = NULL;
2315 struct got_fileindex *fileindex = NULL;
2316 struct check_merge_ok_arg mok_arg;
2318 err = lock_worktree(worktree, LOCK_EX);
2322 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2326 mok_arg.worktree = worktree;
2327 mok_arg.repo = repo;
2328 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2333 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2334 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2337 got_fileindex_free(fileindex);
2338 free(fileindex_path);
2339 unlockerr = lock_worktree(worktree, LOCK_SH);
2340 if (unlockerr && err == NULL)
2345 struct diff_dir_cb_arg {
2346 struct got_fileindex *fileindex;
2347 struct got_worktree *worktree;
2348 const char *status_path;
2349 size_t status_path_len;
2350 struct got_repository *repo;
2351 got_worktree_status_cb status_cb;
2353 got_cancel_cb cancel_cb;
2355 /* A pathlist containing per-directory pathlists of ignore patterns. */
2356 struct got_pathlist_head ignores;
2357 int report_unchanged;
2360 static const struct got_error *
2361 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2362 int dirfd, const char *de_name,
2363 got_worktree_status_cb status_cb, void *status_arg,
2364 struct got_repository *repo, int report_unchanged)
2366 const struct got_error *err = NULL;
2367 unsigned char status = GOT_STATUS_NO_CHANGE;
2368 unsigned char staged_status = get_staged_status(ie);
2370 struct got_object_id blob_id, commit_id, staged_blob_id;
2371 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2372 struct got_object_id *staged_blob_idp = NULL;
2374 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2378 if (status == GOT_STATUS_NO_CHANGE &&
2379 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2382 if (got_fileindex_entry_has_blob(ie)) {
2383 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2384 blob_idp = &blob_id;
2386 if (got_fileindex_entry_has_commit(ie)) {
2387 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2388 commit_idp = &commit_id;
2390 if (staged_status == GOT_STATUS_ADD ||
2391 staged_status == GOT_STATUS_MODIFY) {
2392 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2393 SHA1_DIGEST_LENGTH);
2394 staged_blob_idp = &staged_blob_id;
2397 return (*status_cb)(status_arg, status, staged_status,
2398 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2401 static const struct got_error *
2402 status_old_new(void *arg, struct got_fileindex_entry *ie,
2403 struct dirent *de, const char *parent_path, int dirfd)
2405 const struct got_error *err = NULL;
2406 struct diff_dir_cb_arg *a = arg;
2409 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2410 return got_error(GOT_ERR_CANCELLED);
2412 if (got_path_cmp(parent_path, a->status_path,
2413 strlen(parent_path), a->status_path_len) != 0 &&
2414 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2417 if (parent_path[0]) {
2418 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2419 parent_path, de->d_name) == -1)
2420 return got_error_from_errno("asprintf");
2422 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2424 return got_error_from_errno("asprintf");
2427 err = report_file_status(ie, abspath, dirfd, de->d_name,
2428 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2433 static const struct got_error *
2434 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2436 struct diff_dir_cb_arg *a = arg;
2437 struct got_object_id blob_id, commit_id;
2438 unsigned char status;
2440 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2441 return got_error(GOT_ERR_CANCELLED);
2443 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2446 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2447 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2448 if (got_fileindex_entry_has_file_on_disk(ie))
2449 status = GOT_STATUS_MISSING;
2451 status = GOT_STATUS_DELETE;
2452 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2453 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2457 free_ignorelist(struct got_pathlist_head *ignorelist)
2459 struct got_pathlist_entry *pe;
2461 TAILQ_FOREACH(pe, ignorelist, entry)
2462 free((char *)pe->path);
2463 got_pathlist_free(ignorelist);
2467 free_ignores(struct got_pathlist_head *ignores)
2469 struct got_pathlist_entry *pe;
2471 TAILQ_FOREACH(pe, ignores, entry) {
2472 struct got_pathlist_head *ignorelist = pe->data;
2473 free_ignorelist(ignorelist);
2474 free((char *)pe->path);
2476 got_pathlist_free(ignores);
2479 static const struct got_error *
2480 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2482 const struct got_error *err = NULL;
2483 struct got_pathlist_entry *pe = NULL;
2484 struct got_pathlist_head *ignorelist;
2485 char *line = NULL, *pattern, *dirpath = NULL;
2486 size_t linesize = 0;
2489 ignorelist = calloc(1, sizeof(*ignorelist));
2490 if (ignorelist == NULL)
2491 return got_error_from_errno("calloc");
2492 TAILQ_INIT(ignorelist);
2494 while ((linelen = getline(&line, &linesize, f)) != -1) {
2495 if (linelen > 0 && line[linelen - 1] == '\n')
2496 line[linelen - 1] = '\0';
2498 /* Git's ignores may contain comments. */
2502 /* Git's negated patterns are not (yet?) supported. */
2506 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2508 err = got_error_from_errno("asprintf");
2511 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2516 err = got_error_from_errno("getline");
2520 dirpath = strdup(path);
2521 if (dirpath == NULL) {
2522 err = got_error_from_errno("strdup");
2525 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2528 if (err || pe == NULL) {
2530 free_ignorelist(ignorelist);
2536 match_ignores(struct got_pathlist_head *ignores, const char *path)
2538 struct got_pathlist_entry *pe;
2540 /* Handle patterns which match in all directories. */
2541 TAILQ_FOREACH(pe, ignores, entry) {
2542 struct got_pathlist_head *ignorelist = pe->data;
2543 struct got_pathlist_entry *pi;
2545 TAILQ_FOREACH(pi, ignorelist, entry) {
2546 const char *p, *pattern = pi->path;
2548 if (strncmp(pattern, "**/", 3) != 0)
2553 if (fnmatch(pattern, p,
2554 FNM_PATHNAME | FNM_LEADING_DIR)) {
2555 /* Retry in next directory. */
2556 while (*p && *p != '/')
2568 * The ignores pathlist contains ignore lists from children before
2569 * parents, so we can find the most specific ignorelist by walking
2570 * ignores backwards.
2572 pe = TAILQ_LAST(ignores, got_pathlist_head);
2574 if (got_path_is_child(path, pe->path, pe->path_len)) {
2575 struct got_pathlist_head *ignorelist = pe->data;
2576 struct got_pathlist_entry *pi;
2577 TAILQ_FOREACH(pi, ignorelist, entry) {
2578 const char *pattern = pi->path;
2579 int flags = FNM_LEADING_DIR;
2580 if (strstr(pattern, "/**/") == NULL)
2581 flags |= FNM_PATHNAME;
2582 if (fnmatch(pattern, path, flags))
2587 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2593 static const struct got_error *
2594 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2595 const char *path, int dirfd, const char *ignores_filename)
2597 const struct got_error *err = NULL;
2600 FILE *ignoresfile = NULL;
2602 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2603 path[0] ? "/" : "", ignores_filename) == -1)
2604 return got_error_from_errno("asprintf");
2607 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2609 if (errno != ENOENT && errno != EACCES)
2610 err = got_error_from_errno2("openat",
2613 ignoresfile = fdopen(fd, "r");
2614 if (ignoresfile == NULL)
2615 err = got_error_from_errno2("fdopen",
2619 err = read_ignores(ignores, path, ignoresfile);
2623 ignoresfile = fopen(ignorespath, "r");
2624 if (ignoresfile == NULL) {
2625 if (errno != ENOENT && errno != EACCES)
2626 err = got_error_from_errno2("fopen",
2629 err = read_ignores(ignores, path, ignoresfile);
2632 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2633 err = got_error_from_errno2("fclose", path);
2634 if (fd != -1 && close(fd) == -1 && err == NULL)
2635 err = got_error_from_errno2("close", path);
2640 static const struct got_error *
2641 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2643 const struct got_error *err = NULL;
2644 struct diff_dir_cb_arg *a = arg;
2647 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2648 return got_error(GOT_ERR_CANCELLED);
2650 /* XXX ignore symlinks for now */
2651 if (de->d_type == DT_LNK)
2654 if (parent_path[0]) {
2655 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2656 return got_error_from_errno("asprintf");
2661 if (de->d_type == DT_DIR) {
2662 int subdirfd = openat(dirfd, de->d_name,
2663 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2664 if (subdirfd == -1) {
2665 if (errno != ENOENT && errno != EACCES)
2666 err = got_error_from_errno2("openat", path);
2668 err = add_ignores(&a->ignores, a->worktree->root_path,
2669 path, subdirfd, ".cvsignore");
2671 err = add_ignores(&a->ignores,
2672 a->worktree->root_path, path,
2673 subdirfd, ".gitignore");
2674 if (close(subdirfd) == -1 && err == NULL)
2675 err = got_error_from_errno2("close", path);
2677 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2678 && !match_ignores(&a->ignores, path))
2679 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2680 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2686 static const struct got_error *
2687 report_single_file_status(const char *path, const char *ondisk_path,
2688 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2689 void *status_arg, struct got_repository *repo, int report_unchanged)
2691 struct got_fileindex_entry *ie;
2694 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2696 return report_file_status(ie, ondisk_path, -1, NULL,
2697 status_cb, status_arg, repo, report_unchanged);
2699 if (lstat(ondisk_path, &sb) == -1) {
2700 if (errno != ENOENT)
2701 return got_error_from_errno2("lstat", ondisk_path);
2702 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2703 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2707 if (S_ISREG(sb.st_mode))
2708 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2709 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2714 static const struct got_error *
2715 worktree_status(struct got_worktree *worktree, const char *path,
2716 struct got_fileindex *fileindex, struct got_repository *repo,
2717 got_worktree_status_cb status_cb, void *status_arg,
2718 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2719 int report_unchanged)
2721 const struct got_error *err = NULL;
2723 struct got_fileindex_diff_dir_cb fdiff_cb;
2724 struct diff_dir_cb_arg arg;
2725 char *ondisk_path = NULL;
2727 if (asprintf(&ondisk_path, "%s%s%s",
2728 worktree->root_path, path[0] ? "/" : "", path) == -1)
2729 return got_error_from_errno("asprintf");
2731 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2733 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2734 err = got_error_from_errno2("open", ondisk_path);
2736 err = report_single_file_status(path, ondisk_path,
2737 fileindex, status_cb, status_arg, repo,
2740 fdiff_cb.diff_old_new = status_old_new;
2741 fdiff_cb.diff_old = status_old;
2742 fdiff_cb.diff_new = status_new;
2743 arg.fileindex = fileindex;
2744 arg.worktree = worktree;
2745 arg.status_path = path;
2746 arg.status_path_len = strlen(path);
2748 arg.status_cb = status_cb;
2749 arg.status_arg = status_arg;
2750 arg.cancel_cb = cancel_cb;
2751 arg.cancel_arg = cancel_arg;
2752 arg.report_unchanged = report_unchanged;
2753 TAILQ_INIT(&arg.ignores);
2755 err = add_ignores(&arg.ignores, worktree->root_path,
2756 path, fd, ".cvsignore");
2758 err = add_ignores(&arg.ignores,
2759 worktree->root_path, path, fd,
2763 err = got_fileindex_diff_dir(fileindex, fd,
2764 worktree->root_path, path, repo, &fdiff_cb, &arg);
2765 free_ignores(&arg.ignores);
2768 if (fd != -1 && close(fd) != 0 && err == NULL)
2769 err = got_error_from_errno("close");
2774 const struct got_error *
2775 got_worktree_status(struct got_worktree *worktree,
2776 struct got_pathlist_head *paths, struct got_repository *repo,
2777 got_worktree_status_cb status_cb, void *status_arg,
2778 got_cancel_cb cancel_cb, void *cancel_arg)
2780 const struct got_error *err = NULL;
2781 char *fileindex_path = NULL;
2782 struct got_fileindex *fileindex = NULL;
2783 struct got_pathlist_entry *pe;
2785 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2789 TAILQ_FOREACH(pe, paths, entry) {
2790 err = worktree_status(worktree, pe->path, fileindex, repo,
2791 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2795 free(fileindex_path);
2796 got_fileindex_free(fileindex);
2800 const struct got_error *
2801 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2804 const struct got_error *err = NULL;
2805 char *resolved, *cwd = NULL, *path = NULL;
2810 resolved = realpath(arg, NULL);
2811 if (resolved == NULL) {
2812 if (errno != ENOENT)
2813 return got_error_from_errno2("realpath", arg);
2814 cwd = getcwd(NULL, 0);
2816 return got_error_from_errno("getcwd");
2817 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2818 err = got_error_from_errno("asprintf");
2823 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2824 strlen(got_worktree_get_root_path(worktree)))) {
2825 err = got_error(GOT_ERR_BAD_PATH);
2829 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2830 err = got_path_skip_common_ancestor(&path,
2831 got_worktree_get_root_path(worktree), resolved);
2837 err = got_error_from_errno("strdup");
2842 /* XXX status walk can't deal with trailing slash! */
2844 while (len > 0 && path[len - 1] == '/') {
2845 path[len - 1] = '\0';
2858 struct schedule_addition_args {
2859 struct got_worktree *worktree;
2860 struct got_fileindex *fileindex;
2861 got_worktree_checkout_cb progress_cb;
2863 struct got_repository *repo;
2866 static const struct got_error *
2867 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2868 const char *relpath, struct got_object_id *blob_id,
2869 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2870 int dirfd, const char *de_name)
2872 struct schedule_addition_args *a = arg;
2873 const struct got_error *err = NULL;
2874 struct got_fileindex_entry *ie;
2878 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2880 return got_error_from_errno("asprintf");
2882 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2884 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2888 /* Re-adding an existing entry is a no-op. */
2889 if (status == GOT_STATUS_ADD)
2891 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2896 if (status != GOT_STATUS_UNVERSIONED) {
2897 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2901 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2905 err = got_fileindex_entry_add(a->fileindex, ie);
2907 got_fileindex_entry_free(ie);
2914 if (status == GOT_STATUS_ADD)
2916 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2919 const struct got_error *
2920 got_worktree_schedule_add(struct got_worktree *worktree,
2921 struct got_pathlist_head *paths,
2922 got_worktree_checkout_cb progress_cb, void *progress_arg,
2923 struct got_repository *repo, int no_ignores)
2925 struct got_fileindex *fileindex = NULL;
2926 char *fileindex_path = NULL;
2927 const struct got_error *err = NULL, *sync_err, *unlockerr;
2928 struct got_pathlist_entry *pe;
2929 struct schedule_addition_args saa;
2931 err = lock_worktree(worktree, LOCK_EX);
2935 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2939 saa.worktree = worktree;
2940 saa.fileindex = fileindex;
2941 saa.progress_cb = progress_cb;
2942 saa.progress_arg = progress_arg;
2945 TAILQ_FOREACH(pe, paths, entry) {
2946 err = worktree_status(worktree, pe->path, fileindex, repo,
2947 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2951 sync_err = sync_fileindex(fileindex, fileindex_path);
2952 if (sync_err && err == NULL)
2955 free(fileindex_path);
2957 got_fileindex_free(fileindex);
2958 unlockerr = lock_worktree(worktree, LOCK_SH);
2959 if (unlockerr && err == NULL)
2964 struct schedule_deletion_args {
2965 struct got_worktree *worktree;
2966 struct got_fileindex *fileindex;
2967 got_worktree_delete_cb progress_cb;
2969 struct got_repository *repo;
2970 int delete_local_mods;
2974 static const struct got_error *
2975 schedule_for_deletion(void *arg, unsigned char status,
2976 unsigned char staged_status, const char *relpath,
2977 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2978 struct got_object_id *commit_id, int dirfd, const char *de_name)
2980 struct schedule_deletion_args *a = arg;
2981 const struct got_error *err = NULL;
2982 struct got_fileindex_entry *ie = NULL;
2986 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2988 return got_error(GOT_ERR_BAD_PATH);
2990 staged_status = get_staged_status(ie);
2991 if (staged_status != GOT_STATUS_NO_CHANGE) {
2992 if (staged_status == GOT_STATUS_DELETE)
2994 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2997 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2999 return got_error_from_errno("asprintf");
3001 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3006 if (status != GOT_STATUS_NO_CHANGE) {
3007 if (status == GOT_STATUS_DELETE)
3009 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3010 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3013 if (status != GOT_STATUS_MODIFY &&
3014 status != GOT_STATUS_MISSING) {
3015 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3020 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3022 if (unlinkat(dirfd, de_name, 0) != 0) {
3023 err = got_error_from_errno2("unlinkat",
3027 } else if (unlink(ondisk_path) != 0) {
3028 err = got_error_from_errno2("unlink", ondisk_path);
3033 got_fileindex_entry_mark_deleted_from_disk(ie);
3038 if (status == GOT_STATUS_DELETE)
3040 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3041 staged_status, relpath);
3044 const struct got_error *
3045 got_worktree_schedule_delete(struct got_worktree *worktree,
3046 struct got_pathlist_head *paths, int delete_local_mods,
3047 got_worktree_delete_cb progress_cb, void *progress_arg,
3048 struct got_repository *repo, int keep_on_disk)
3050 struct got_fileindex *fileindex = NULL;
3051 char *fileindex_path = NULL;
3052 const struct got_error *err = NULL, *sync_err, *unlockerr;
3053 struct got_pathlist_entry *pe;
3054 struct schedule_deletion_args sda;
3056 err = lock_worktree(worktree, LOCK_EX);
3060 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3064 sda.worktree = worktree;
3065 sda.fileindex = fileindex;
3066 sda.progress_cb = progress_cb;
3067 sda.progress_arg = progress_arg;
3069 sda.delete_local_mods = delete_local_mods;
3070 sda.keep_on_disk = keep_on_disk;
3072 TAILQ_FOREACH(pe, paths, entry) {
3073 err = worktree_status(worktree, pe->path, fileindex, repo,
3074 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3078 sync_err = sync_fileindex(fileindex, fileindex_path);
3079 if (sync_err && err == NULL)
3082 free(fileindex_path);
3084 got_fileindex_free(fileindex);
3085 unlockerr = lock_worktree(worktree, LOCK_SH);
3086 if (unlockerr && err == NULL)
3091 static const struct got_error *
3092 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3094 const struct got_error *err = NULL;
3096 size_t linesize = 0, n;
3099 linelen = getline(&line, &linesize, infile);
3100 if (linelen == -1) {
3101 if (ferror(infile)) {
3102 err = got_error_from_errno("getline");
3108 n = fwrite(line, 1, linelen, outfile);
3110 err = got_ferror(outfile, GOT_ERR_IO);
3115 n = fwrite(line, 1, linelen, rejectfile);
3117 err = got_ferror(outfile, GOT_ERR_IO);
3124 static const struct got_error *
3125 skip_one_line(FILE *f)
3128 size_t linesize = 0;
3131 linelen = getline(&line, &linesize, f);
3132 if (linelen == -1) {
3134 return got_error_from_errno("getline");
3141 static const struct got_error *
3142 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3143 int start_old, int end_old, int start_new, int end_new,
3144 FILE *outfile, FILE *rejectfile)
3146 const struct got_error *err;
3148 /* Copy old file's lines leading up to patch. */
3149 while (!feof(f1) && *line_cur1 < start_old) {
3150 err = copy_one_line(f1, outfile, NULL);
3155 /* Skip new file's lines leading up to patch. */
3156 while (!feof(f2) && *line_cur2 < start_new) {
3158 err = copy_one_line(f2, NULL, rejectfile);
3160 err = skip_one_line(f2);
3165 /* Copy patched lines. */
3166 while (!feof(f2) && *line_cur2 <= end_new) {
3167 err = copy_one_line(f2, outfile, NULL);
3172 /* Skip over old file's replaced lines. */
3173 while (!feof(f1) && *line_cur1 <= end_old) {
3175 err = copy_one_line(f1, NULL, rejectfile);
3177 err = skip_one_line(f1);
3186 static const struct got_error *
3187 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3188 FILE *outfile, FILE *rejectfile)
3190 const struct got_error *err;
3193 /* Copy old file's lines until EOF. */
3195 err = copy_one_line(f1, outfile, NULL);
3202 /* Copy new file's lines until EOF. */
3204 err = copy_one_line(f2, NULL, rejectfile);
3214 static const struct got_error *
3215 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3216 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3217 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3218 int *line_cur2, FILE *outfile, FILE *rejectfile,
3219 got_worktree_patch_cb patch_cb, void *patch_arg)
3221 const struct got_error *err = NULL;
3222 int start_old = change->cv.a;
3223 int end_old = change->cv.b;
3224 int start_new = change->cv.c;
3225 int end_new = change->cv.d;
3229 *choice = GOT_PATCH_CHOICE_NONE;
3231 hunkfile = got_opentemp();
3232 if (hunkfile == NULL)
3233 return got_error_from_errno("got_opentemp");
3238 /* XXX TODO needs error checking */
3239 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3241 if (fseek(f1, pos1, SEEK_SET) == -1) {
3242 err = got_ferror(f1, GOT_ERR_IO);
3245 if (fseek(f2, pos2, SEEK_SET) == -1) {
3246 err = got_ferror(f1, GOT_ERR_IO);
3249 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3250 err = got_ferror(hunkfile, GOT_ERR_IO);
3254 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3255 hunkfile, n, nchanges);
3260 case GOT_PATCH_CHOICE_YES:
3261 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3262 end_old, start_new, end_new, outfile, rejectfile);
3264 case GOT_PATCH_CHOICE_NO:
3265 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3266 end_old, start_new, end_new, rejectfile, outfile);
3268 case GOT_PATCH_CHOICE_QUIT:
3271 err = got_error(GOT_ERR_PATCH_CHOICE);
3275 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3276 err = got_error_from_errno("fclose");
3280 struct revert_file_args {
3281 struct got_worktree *worktree;
3282 struct got_fileindex *fileindex;
3283 got_worktree_checkout_cb progress_cb;
3285 got_worktree_patch_cb patch_cb;
3287 struct got_repository *repo;
3290 static const struct got_error *
3291 create_patched_content(char **path_outfile, int reverse_patch,
3292 struct got_object_id *blob_id, const char *path2,
3293 int dirfd2, const char *de_name2,
3294 const char *relpath, struct got_repository *repo,
3295 got_worktree_patch_cb patch_cb, void *patch_arg)
3297 const struct got_error *err;
3298 struct got_blob_object *blob = NULL;
3299 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3301 char *path1 = NULL, *id_str = NULL;
3302 struct stat sb1, sb2;
3303 struct got_diff_changes *changes = NULL;
3304 struct got_diff_state *ds = NULL;
3305 struct got_diff_args *args = NULL;
3306 struct got_diff_change *change;
3307 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3310 *path_outfile = NULL;
3312 err = got_object_id_str(&id_str, blob_id);
3317 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3319 err = got_error_from_errno2("openat", path2);
3323 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3325 err = got_error_from_errno2("open", path2);
3329 if (fstat(fd2, &sb2) == -1) {
3330 err = got_error_from_errno2("fstat", path2);
3334 f2 = fdopen(fd2, "r");
3336 err = got_error_from_errno2("fdopen", path2);
3341 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3345 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3349 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3353 if (stat(path1, &sb1) == -1) {
3354 err = got_error_from_errno2("stat", path1);
3358 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3359 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3363 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3367 if (fseek(f1, 0L, SEEK_SET) == -1)
3368 return got_ferror(f1, GOT_ERR_IO);
3369 if (fseek(f2, 0L, SEEK_SET) == -1)
3370 return got_ferror(f2, GOT_ERR_IO);
3371 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3373 err = apply_or_reject_change(&choice, change, ++n,
3374 changes->nchanges, ds, args, diff_flags, relpath,
3375 f1, f2, &line_cur1, &line_cur2,
3376 reverse_patch ? NULL : outfile,
3377 reverse_patch ? outfile : NULL,
3378 patch_cb, patch_arg);
3381 if (choice == GOT_PATCH_CHOICE_YES)
3383 else if (choice == GOT_PATCH_CHOICE_QUIT)
3387 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3388 reverse_patch ? NULL : outfile,
3389 reverse_patch ? outfile : NULL);
3393 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3394 err = got_error_from_errno2("chmod", path2);
3401 got_object_blob_close(blob);
3402 if (f1 && fclose(f1) == EOF && err == NULL)
3403 err = got_error_from_errno2("fclose", path1);
3404 if (f2 && fclose(f2) == EOF && err == NULL)
3405 err = got_error_from_errno2("fclose", path2);
3406 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3407 err = got_error_from_errno2("close", path2);
3408 if (outfile && fclose(outfile) == EOF && err == NULL)
3409 err = got_error_from_errno2("fclose", *path_outfile);
3410 if (path1 && unlink(path1) == -1 && err == NULL)
3411 err = got_error_from_errno2("unlink", path1);
3412 if (err || !have_content) {
3413 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3414 err = got_error_from_errno2("unlink", *path_outfile);
3415 free(*path_outfile);
3416 *path_outfile = NULL;
3420 got_diff_state_free(ds);
3424 got_diff_free_changes(changes);
3429 static const struct got_error *
3430 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3431 const char *relpath, struct got_object_id *blob_id,
3432 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3433 int dirfd, const char *de_name)
3435 struct revert_file_args *a = arg;
3436 const struct got_error *err = NULL;
3437 char *parent_path = NULL;
3438 struct got_fileindex_entry *ie;
3439 struct got_tree_object *tree = NULL;
3440 struct got_object_id *tree_id = NULL;
3441 const struct got_tree_entry *te = NULL;
3442 char *tree_path = NULL, *te_name;
3443 char *ondisk_path = NULL, *path_content = NULL;
3444 struct got_blob_object *blob = NULL;
3446 /* Reverting a staged deletion is a no-op. */
3447 if (status == GOT_STATUS_DELETE &&
3448 staged_status != GOT_STATUS_NO_CHANGE)
3451 if (status == GOT_STATUS_UNVERSIONED)
3452 return (*a->progress_cb)(a->progress_arg,
3453 GOT_STATUS_UNVERSIONED, relpath);
3455 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3457 return got_error(GOT_ERR_BAD_PATH);
3459 /* Construct in-repository path of tree which contains this blob. */
3460 err = got_path_dirname(&parent_path, ie->path);
3462 if (err->code != GOT_ERR_BAD_PATH)
3464 parent_path = strdup("/");
3465 if (parent_path == NULL) {
3466 err = got_error_from_errno("strdup");
3470 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3471 tree_path = strdup(parent_path);
3472 if (tree_path == NULL) {
3473 err = got_error_from_errno("strdup");
3477 if (got_path_is_root_dir(parent_path)) {
3478 tree_path = strdup(a->worktree->path_prefix);
3479 if (tree_path == NULL) {
3480 err = got_error_from_errno("strdup");
3484 if (asprintf(&tree_path, "%s/%s",
3485 a->worktree->path_prefix, parent_path) == -1) {
3486 err = got_error_from_errno("asprintf");
3492 err = got_object_id_by_path(&tree_id, a->repo,
3493 a->worktree->base_commit_id, tree_path);
3495 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3496 (status == GOT_STATUS_ADD ||
3497 staged_status == GOT_STATUS_ADD)))
3500 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3504 te_name = basename(ie->path);
3505 if (te_name == NULL) {
3506 err = got_error_from_errno2("basename", ie->path);
3510 te = got_object_tree_find_entry(tree, te_name);
3511 if (te == NULL && status != GOT_STATUS_ADD &&
3512 staged_status != GOT_STATUS_ADD) {
3513 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3519 case GOT_STATUS_ADD:
3521 int choice = GOT_PATCH_CHOICE_NONE;
3522 err = (*a->patch_cb)(&choice, a->patch_arg,
3523 status, ie->path, NULL, 1, 1);
3526 if (choice != GOT_PATCH_CHOICE_YES)
3529 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3533 got_fileindex_entry_remove(a->fileindex, ie);
3535 case GOT_STATUS_DELETE:
3537 int choice = GOT_PATCH_CHOICE_NONE;
3538 err = (*a->patch_cb)(&choice, a->patch_arg,
3539 status, ie->path, NULL, 1, 1);
3542 if (choice != GOT_PATCH_CHOICE_YES)
3546 case GOT_STATUS_MODIFY:
3547 case GOT_STATUS_MODE_CHANGE:
3548 case GOT_STATUS_CONFLICT:
3549 case GOT_STATUS_MISSING: {
3550 struct got_object_id id;
3551 if (staged_status == GOT_STATUS_ADD ||
3552 staged_status == GOT_STATUS_MODIFY) {
3553 memcpy(id.sha1, ie->staged_blob_sha1,
3554 SHA1_DIGEST_LENGTH);
3556 memcpy(id.sha1, ie->blob_sha1,
3557 SHA1_DIGEST_LENGTH);
3558 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3562 if (asprintf(&ondisk_path, "%s/%s",
3563 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3564 err = got_error_from_errno("asprintf");
3568 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3569 status == GOT_STATUS_CONFLICT)) {
3570 err = create_patched_content(&path_content, 1, &id,
3571 ondisk_path, dirfd, de_name, ie->path, a->repo,
3572 a->patch_cb, a->patch_arg);
3573 if (err || path_content == NULL)
3575 if (rename(path_content, ondisk_path) == -1) {
3576 err = got_error_from_errno3("rename",
3577 path_content, ondisk_path);
3581 err = install_blob(a->worktree, ondisk_path, ie->path,
3582 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3583 got_fileindex_perms_to_st(ie), blob, 0, 1,
3584 a->repo, a->progress_cb, a->progress_arg);
3587 if (status == GOT_STATUS_DELETE ||
3588 status == GOT_STATUS_MODE_CHANGE) {
3589 err = update_blob_fileindex_entry(a->worktree,
3590 a->fileindex, ie, ondisk_path, ie->path,
3607 got_object_blob_close(blob);
3609 got_object_tree_close(tree);
3614 const struct got_error *
3615 got_worktree_revert(struct got_worktree *worktree,
3616 struct got_pathlist_head *paths,
3617 got_worktree_checkout_cb progress_cb, void *progress_arg,
3618 got_worktree_patch_cb patch_cb, void *patch_arg,
3619 struct got_repository *repo)
3621 struct got_fileindex *fileindex = NULL;
3622 char *fileindex_path = NULL;
3623 const struct got_error *err = NULL, *unlockerr = NULL;
3624 const struct got_error *sync_err = NULL;
3625 struct got_pathlist_entry *pe;
3626 struct revert_file_args rfa;
3628 err = lock_worktree(worktree, LOCK_EX);
3632 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3636 rfa.worktree = worktree;
3637 rfa.fileindex = fileindex;
3638 rfa.progress_cb = progress_cb;
3639 rfa.progress_arg = progress_arg;
3640 rfa.patch_cb = patch_cb;
3641 rfa.patch_arg = patch_arg;
3643 TAILQ_FOREACH(pe, paths, entry) {
3644 err = worktree_status(worktree, pe->path, fileindex, repo,
3645 revert_file, &rfa, NULL, NULL, 0, 0);
3649 sync_err = sync_fileindex(fileindex, fileindex_path);
3650 if (sync_err && err == NULL)
3653 free(fileindex_path);
3655 got_fileindex_free(fileindex);
3656 unlockerr = lock_worktree(worktree, LOCK_SH);
3657 if (unlockerr && err == NULL)
3663 free_commitable(struct got_commitable *ct)
3666 free(ct->in_repo_path);
3667 free(ct->ondisk_path);
3669 free(ct->base_blob_id);
3670 free(ct->staged_blob_id);
3671 free(ct->base_commit_id);
3675 struct collect_commitables_arg {
3676 struct got_pathlist_head *commitable_paths;
3677 struct got_repository *repo;
3678 struct got_worktree *worktree;
3679 int have_staged_files;
3682 static const struct got_error *
3683 collect_commitables(void *arg, unsigned char status,
3684 unsigned char staged_status, const char *relpath,
3685 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3686 struct got_object_id *commit_id, int dirfd, const char *de_name)
3688 struct collect_commitables_arg *a = arg;
3689 const struct got_error *err = NULL;
3690 struct got_commitable *ct = NULL;
3691 struct got_pathlist_entry *new = NULL;
3692 char *parent_path = NULL, *path = NULL;
3695 if (a->have_staged_files) {
3696 if (staged_status != GOT_STATUS_MODIFY &&
3697 staged_status != GOT_STATUS_ADD &&
3698 staged_status != GOT_STATUS_DELETE)
3701 if (status == GOT_STATUS_CONFLICT)
3702 return got_error(GOT_ERR_COMMIT_CONFLICT);
3704 if (status != GOT_STATUS_MODIFY &&
3705 status != GOT_STATUS_MODE_CHANGE &&
3706 status != GOT_STATUS_ADD &&
3707 status != GOT_STATUS_DELETE)
3711 if (asprintf(&path, "/%s", relpath) == -1) {
3712 err = got_error_from_errno("asprintf");
3715 if (strcmp(path, "/") == 0) {
3716 parent_path = strdup("");
3717 if (parent_path == NULL)
3718 return got_error_from_errno("strdup");
3720 err = got_path_dirname(&parent_path, path);
3725 ct = calloc(1, sizeof(*ct));
3727 err = got_error_from_errno("calloc");
3731 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3733 err = got_error_from_errno("asprintf");
3736 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3737 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3740 if (fstatat(dirfd, de_name, &sb,
3741 AT_SYMLINK_NOFOLLOW) == -1) {
3742 err = got_error_from_errno2("fstatat",
3746 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3747 err = got_error_from_errno2("lstat", ct->ondisk_path);
3750 ct->mode = sb.st_mode;
3753 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3754 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3756 err = got_error_from_errno("asprintf");
3760 ct->status = status;
3761 ct->staged_status = staged_status;
3762 ct->blob_id = NULL; /* will be filled in when blob gets created */
3763 if (ct->status != GOT_STATUS_ADD &&
3764 ct->staged_status != GOT_STATUS_ADD) {
3765 ct->base_blob_id = got_object_id_dup(blob_id);
3766 if (ct->base_blob_id == NULL) {
3767 err = got_error_from_errno("got_object_id_dup");
3770 ct->base_commit_id = got_object_id_dup(commit_id);
3771 if (ct->base_commit_id == NULL) {
3772 err = got_error_from_errno("got_object_id_dup");
3776 if (ct->staged_status == GOT_STATUS_ADD ||
3777 ct->staged_status == GOT_STATUS_MODIFY) {
3778 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3779 if (ct->staged_blob_id == NULL) {
3780 err = got_error_from_errno("got_object_id_dup");
3784 ct->path = strdup(path);
3785 if (ct->path == NULL) {
3786 err = got_error_from_errno("strdup");
3789 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3791 if (ct && (err || new == NULL))
3792 free_commitable(ct);
3798 static const struct got_error *write_tree(struct got_object_id **,
3799 struct got_tree_object *, const char *, struct got_pathlist_head *,
3800 got_worktree_status_cb status_cb, void *status_arg,
3801 struct got_repository *);
3803 static const struct got_error *
3804 write_subtree(struct got_object_id **new_subtree_id,
3805 struct got_tree_entry *te, const char *parent_path,
3806 struct got_pathlist_head *commitable_paths,
3807 got_worktree_status_cb status_cb, void *status_arg,
3808 struct got_repository *repo)
3810 const struct got_error *err = NULL;
3811 struct got_tree_object *subtree;
3814 if (asprintf(&subpath, "%s%s%s", parent_path,
3815 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3816 return got_error_from_errno("asprintf");
3818 err = got_object_open_as_tree(&subtree, repo, &te->id);
3822 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3823 status_cb, status_arg, repo);
3824 got_object_tree_close(subtree);
3829 static const struct got_error *
3830 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3832 const struct got_error *err = NULL;
3833 char *ct_parent_path = NULL;
3837 if (strchr(ct->in_repo_path, '/') == NULL) {
3838 *match = got_path_is_root_dir(path);
3842 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3845 *match = (strcmp(path, ct_parent_path) == 0);
3846 free(ct_parent_path);
3851 get_ct_file_mode(struct got_commitable *ct)
3853 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3856 static const struct got_error *
3857 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3858 struct got_tree_entry *te, struct got_commitable *ct)
3860 const struct got_error *err = NULL;
3864 err = got_object_tree_entry_dup(new_te, te);
3868 (*new_te)->mode = get_ct_file_mode(ct);
3870 if (ct->staged_status == GOT_STATUS_MODIFY)
3871 memcpy(&(*new_te)->id, ct->staged_blob_id,
3872 sizeof((*new_te)->id));
3874 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3876 if (err && *new_te) {
3883 static const struct got_error *
3884 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3885 struct got_commitable *ct)
3887 const struct got_error *err = NULL;
3892 *new_te = calloc(1, sizeof(**new_te));
3893 if (*new_te == NULL)
3894 return got_error_from_errno("calloc");
3896 ct_name = basename(ct->path);
3897 if (ct_name == NULL) {
3898 err = got_error_from_errno2("basename", ct->path);
3901 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3902 sizeof((*new_te)->name)) {
3903 err = got_error(GOT_ERR_NO_SPACE);
3907 (*new_te)->mode = get_ct_file_mode(ct);
3909 if (ct->staged_status == GOT_STATUS_ADD)
3910 memcpy(&(*new_te)->id, ct->staged_blob_id,
3911 sizeof((*new_te)->id));
3913 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3915 if (err && *new_te) {
3922 static const struct got_error *
3923 insert_tree_entry(struct got_tree_entry *new_te,
3924 struct got_pathlist_head *paths)
3926 const struct got_error *err = NULL;
3927 struct got_pathlist_entry *new_pe;
3929 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3933 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3937 static const struct got_error *
3938 report_ct_status(struct got_commitable *ct,
3939 got_worktree_status_cb status_cb, void *status_arg)
3941 const char *ct_path = ct->path;
3942 unsigned char status;
3944 while (ct_path[0] == '/')
3947 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3948 status = ct->staged_status;
3950 status = ct->status;
3952 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3953 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3956 static const struct got_error *
3957 match_modified_subtree(int *modified, struct got_tree_entry *te,
3958 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3960 const struct got_error *err = NULL;
3961 struct got_pathlist_entry *pe;
3966 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3967 got_path_is_root_dir(base_tree_path) ? "" : "/",
3969 return got_error_from_errno("asprintf");
3971 TAILQ_FOREACH(pe, commitable_paths, entry) {
3972 struct got_commitable *ct = pe->data;
3973 *modified = got_path_is_child(ct->in_repo_path, te_path,
3983 static const struct got_error *
3984 match_deleted_or_modified_ct(struct got_commitable **ctp,
3985 struct got_tree_entry *te, const char *base_tree_path,
3986 struct got_pathlist_head *commitable_paths)
3988 const struct got_error *err = NULL;
3989 struct got_pathlist_entry *pe;
3993 TAILQ_FOREACH(pe, commitable_paths, entry) {
3994 struct got_commitable *ct = pe->data;
3995 char *ct_name = NULL;
3998 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3999 if (ct->status != GOT_STATUS_MODIFY &&
4000 ct->status != GOT_STATUS_MODE_CHANGE &&
4001 ct->status != GOT_STATUS_DELETE)
4004 if (ct->staged_status != GOT_STATUS_MODIFY &&
4005 ct->staged_status != GOT_STATUS_DELETE)
4009 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4012 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4018 ct_name = basename(pe->path);
4019 if (ct_name == NULL)
4020 return got_error_from_errno2("basename", pe->path);
4022 if (strcmp(te->name, ct_name) != 0)
4032 static const struct got_error *
4033 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4034 const char *child_path, const char *path_base_tree,
4035 struct got_pathlist_head *commitable_paths,
4036 got_worktree_status_cb status_cb, void *status_arg,
4037 struct got_repository *repo)
4039 const struct got_error *err = NULL;
4040 struct got_tree_entry *new_te;
4042 struct got_object_id *id = NULL;
4046 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4047 got_path_is_root_dir(path_base_tree) ? "" : "/",
4049 return got_error_from_errno("asprintf");
4051 new_te = calloc(1, sizeof(*new_te));
4053 return got_error_from_errno("calloc");
4054 new_te->mode = S_IFDIR;
4056 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4057 sizeof(new_te->name)) {
4058 err = got_error(GOT_ERR_NO_SPACE);
4061 err = write_tree(&id, NULL, subtree_path,
4062 commitable_paths, status_cb, status_arg, repo);
4067 memcpy(&new_te->id, id, sizeof(new_te->id));
4076 static const struct got_error *
4077 write_tree(struct got_object_id **new_tree_id,
4078 struct got_tree_object *base_tree, const char *path_base_tree,
4079 struct got_pathlist_head *commitable_paths,
4080 got_worktree_status_cb status_cb, void *status_arg,
4081 struct got_repository *repo)
4083 const struct got_error *err = NULL;
4084 struct got_pathlist_head paths;
4085 struct got_tree_entry *te, *new_te = NULL;
4086 struct got_pathlist_entry *pe;
4091 /* Insert, and recurse into, newly added entries first. */
4092 TAILQ_FOREACH(pe, commitable_paths, entry) {
4093 struct got_commitable *ct = pe->data;
4094 char *child_path = NULL, *slash;
4096 if ((ct->status != GOT_STATUS_ADD &&
4097 ct->staged_status != GOT_STATUS_ADD) ||
4098 (ct->flags & GOT_COMMITABLE_ADDED))
4101 if (!got_path_is_child(pe->path, path_base_tree,
4102 strlen(path_base_tree)))
4105 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4110 slash = strchr(child_path, '/');
4111 if (slash == NULL) {
4112 err = alloc_added_blob_tree_entry(&new_te, ct);
4115 err = report_ct_status(ct, status_cb, status_arg);
4118 ct->flags |= GOT_COMMITABLE_ADDED;
4119 err = insert_tree_entry(new_te, &paths);
4124 *slash = '\0'; /* trim trailing path components */
4125 if (base_tree == NULL ||
4126 got_object_tree_find_entry(base_tree, child_path)
4128 err = make_subtree_for_added_blob(&new_te,
4129 child_path, path_base_tree,
4130 commitable_paths, status_cb, status_arg,
4134 err = insert_tree_entry(new_te, &paths);
4143 int i, nbase_entries;
4144 /* Handle modified and deleted entries. */
4145 nbase_entries = got_object_tree_get_nentries(base_tree);
4146 for (i = 0; i < nbase_entries; i++) {
4147 struct got_commitable *ct = NULL;
4149 te = got_object_tree_get_entry(base_tree, i);
4150 if (got_object_tree_entry_is_submodule(te)) {
4151 /* Entry is a submodule; just copy it. */
4152 err = got_object_tree_entry_dup(&new_te, te);
4155 err = insert_tree_entry(new_te, &paths);
4162 if (S_ISDIR(te->mode)) {
4164 err = got_object_tree_entry_dup(&new_te, te);
4167 err = match_modified_subtree(&modified, te,
4168 path_base_tree, commitable_paths);
4171 /* Avoid recursion into unmodified subtrees. */
4173 struct got_object_id *new_id;
4174 err = write_subtree(&new_id, te,
4175 path_base_tree, commitable_paths,
4176 status_cb, status_arg, repo);
4179 memcpy(&new_te->id, new_id,
4180 sizeof(new_te->id));
4183 err = insert_tree_entry(new_te, &paths);
4190 err = match_deleted_or_modified_ct(&ct, te,
4191 path_base_tree, commitable_paths);
4195 /* NB: Deleted entries get dropped here. */
4196 if (ct->status == GOT_STATUS_MODIFY ||
4197 ct->status == GOT_STATUS_MODE_CHANGE ||
4198 ct->staged_status == GOT_STATUS_MODIFY) {
4199 err = alloc_modified_blob_tree_entry(
4203 err = insert_tree_entry(new_te, &paths);
4208 err = report_ct_status(ct, status_cb,
4213 /* Entry is unchanged; just copy it. */
4214 err = got_object_tree_entry_dup(&new_te, te);
4217 err = insert_tree_entry(new_te, &paths);
4225 /* Write new list of entries; deleted entries have been dropped. */
4226 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4228 got_pathlist_free(&paths);
4232 static const struct got_error *
4233 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4234 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4235 int have_staged_files)
4237 const struct got_error *err = NULL;
4238 struct got_pathlist_entry *pe;
4240 TAILQ_FOREACH(pe, commitable_paths, entry) {
4241 struct got_fileindex_entry *ie;
4242 struct got_commitable *ct = pe->data;
4244 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4246 if (ct->status == GOT_STATUS_DELETE ||
4247 ct->staged_status == GOT_STATUS_DELETE) {
4248 got_fileindex_entry_remove(fileindex, ie);
4249 got_fileindex_entry_free(ie);
4250 } else if (ct->staged_status == GOT_STATUS_ADD ||
4251 ct->staged_status == GOT_STATUS_MODIFY) {
4252 got_fileindex_entry_stage_set(ie,
4253 GOT_FILEIDX_STAGE_NONE);
4254 err = got_fileindex_entry_update(ie,
4255 ct->ondisk_path, ct->staged_blob_id->sha1,
4256 new_base_commit_id->sha1,
4257 !have_staged_files);
4259 err = got_fileindex_entry_update(ie,
4260 ct->ondisk_path, ct->blob_id->sha1,
4261 new_base_commit_id->sha1,
4262 !have_staged_files);
4264 err = got_fileindex_entry_alloc(&ie,
4265 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4266 new_base_commit_id->sha1);
4269 err = got_fileindex_entry_add(fileindex, ie);
4278 static const struct got_error *
4279 check_out_of_date(const char *in_repo_path, unsigned char status,
4280 unsigned char staged_status, struct got_object_id *base_blob_id,
4281 struct got_object_id *base_commit_id,
4282 struct got_object_id *head_commit_id, struct got_repository *repo,
4285 const struct got_error *err = NULL;
4286 struct got_object_id *id = NULL;
4288 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4289 /* Trivial case: base commit == head commit */
4290 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4293 * Ensure file content which local changes were based
4294 * on matches file content in the branch head.
4296 err = got_object_id_by_path(&id, repo, head_commit_id,
4299 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4300 err = got_error(ood_errcode);
4302 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4303 err = got_error(ood_errcode);
4305 /* Require that added files don't exist in the branch head. */
4306 err = got_object_id_by_path(&id, repo, head_commit_id,
4308 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4310 err = id ? got_error(ood_errcode) : NULL;
4317 const struct got_error *
4318 commit_worktree(struct got_object_id **new_commit_id,
4319 struct got_pathlist_head *commitable_paths,
4320 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4321 const char *author, const char *committer,
4322 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4323 got_worktree_status_cb status_cb, void *status_arg,
4324 struct got_repository *repo)
4326 const struct got_error *err = NULL, *unlockerr = NULL;
4327 struct got_pathlist_entry *pe;
4328 const char *head_ref_name = NULL;
4329 struct got_commit_object *head_commit = NULL;
4330 struct got_reference *head_ref2 = NULL;
4331 struct got_object_id *head_commit_id2 = NULL;
4332 struct got_tree_object *head_tree = NULL;
4333 struct got_object_id *new_tree_id = NULL;
4334 struct got_object_id_queue parent_ids;
4335 struct got_object_qid *pid = NULL;
4336 char *logmsg = NULL;
4338 *new_commit_id = NULL;
4340 SIMPLEQ_INIT(&parent_ids);
4342 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4346 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4350 if (commit_msg_cb != NULL) {
4351 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4356 if (logmsg == NULL || strlen(logmsg) == 0) {
4357 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4361 /* Create blobs from added and modified files and record their IDs. */
4362 TAILQ_FOREACH(pe, commitable_paths, entry) {
4363 struct got_commitable *ct = pe->data;
4366 /* Blobs for staged files already exist. */
4367 if (ct->staged_status == GOT_STATUS_ADD ||
4368 ct->staged_status == GOT_STATUS_MODIFY)
4371 if (ct->status != GOT_STATUS_ADD &&
4372 ct->status != GOT_STATUS_MODIFY &&
4373 ct->status != GOT_STATUS_MODE_CHANGE)
4376 if (asprintf(&ondisk_path, "%s/%s",
4377 worktree->root_path, pe->path) == -1) {
4378 err = got_error_from_errno("asprintf");
4381 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4387 /* Recursively write new tree objects. */
4388 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4389 status_cb, status_arg, repo);
4393 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4396 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4397 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4398 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4399 got_object_qid_free(pid);
4405 /* Check if a concurrent commit to our branch has occurred. */
4406 head_ref_name = got_worktree_get_head_ref_name(worktree);
4407 if (head_ref_name == NULL) {
4408 err = got_error_from_errno("got_worktree_get_head_ref_name");
4411 /* Lock the reference here to prevent concurrent modification. */
4412 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4415 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4418 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4419 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4422 /* Update branch head in repository. */
4423 err = got_ref_change_ref(head_ref2, *new_commit_id);
4426 err = got_ref_write(head_ref2, repo);
4430 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4434 err = ref_base_commit(worktree, repo);
4439 got_object_tree_close(head_tree);
4441 got_object_commit_close(head_commit);
4442 free(head_commit_id2);
4444 unlockerr = got_ref_unlock(head_ref2);
4445 if (unlockerr && err == NULL)
4447 got_ref_close(head_ref2);
4452 static const struct got_error *
4453 check_path_is_commitable(const char *path,
4454 struct got_pathlist_head *commitable_paths)
4456 struct got_pathlist_entry *cpe = NULL;
4457 size_t path_len = strlen(path);
4459 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4460 struct got_commitable *ct = cpe->data;
4461 const char *ct_path = ct->path;
4463 while (ct_path[0] == '/')
4466 if (strcmp(path, ct_path) == 0 ||
4467 got_path_is_child(ct_path, path, path_len))
4472 return got_error_path(path, GOT_ERR_BAD_PATH);
4477 static const struct got_error *
4478 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4480 int *have_staged_files = arg;
4482 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4483 *have_staged_files = 1;
4484 return got_error(GOT_ERR_CANCELLED);
4490 static const struct got_error *
4491 check_non_staged_files(struct got_fileindex *fileindex,
4492 struct got_pathlist_head *paths)
4494 struct got_pathlist_entry *pe;
4495 struct got_fileindex_entry *ie;
4497 TAILQ_FOREACH(pe, paths, entry) {
4498 if (pe->path[0] == '\0')
4500 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4502 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4503 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4504 return got_error_path(pe->path,
4505 GOT_ERR_FILE_NOT_STAGED);
4511 const struct got_error *
4512 got_worktree_commit(struct got_object_id **new_commit_id,
4513 struct got_worktree *worktree, struct got_pathlist_head *paths,
4514 const char *author, const char *committer,
4515 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4516 got_worktree_status_cb status_cb, void *status_arg,
4517 struct got_repository *repo)
4519 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4520 struct got_fileindex *fileindex = NULL;
4521 char *fileindex_path = NULL;
4522 struct got_pathlist_head commitable_paths;
4523 struct collect_commitables_arg cc_arg;
4524 struct got_pathlist_entry *pe;
4525 struct got_reference *head_ref = NULL;
4526 struct got_object_id *head_commit_id = NULL;
4527 int have_staged_files = 0;
4529 *new_commit_id = NULL;
4531 TAILQ_INIT(&commitable_paths);
4533 err = lock_worktree(worktree, LOCK_EX);
4537 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4541 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4545 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4549 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4550 &have_staged_files);
4551 if (err && err->code != GOT_ERR_CANCELLED)
4553 if (have_staged_files) {
4554 err = check_non_staged_files(fileindex, paths);
4559 cc_arg.commitable_paths = &commitable_paths;
4560 cc_arg.worktree = worktree;
4562 cc_arg.have_staged_files = have_staged_files;
4563 TAILQ_FOREACH(pe, paths, entry) {
4564 err = worktree_status(worktree, pe->path, fileindex, repo,
4565 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4570 if (TAILQ_EMPTY(&commitable_paths)) {
4571 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4575 TAILQ_FOREACH(pe, paths, entry) {
4576 err = check_path_is_commitable(pe->path, &commitable_paths);
4581 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4582 struct got_commitable *ct = pe->data;
4583 const char *ct_path = ct->in_repo_path;
4585 while (ct_path[0] == '/')
4587 err = check_out_of_date(ct_path, ct->status,
4588 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4589 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4595 err = commit_worktree(new_commit_id, &commitable_paths,
4596 head_commit_id, worktree, author, committer,
4597 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4601 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4602 fileindex, have_staged_files);
4603 sync_err = sync_fileindex(fileindex, fileindex_path);
4604 if (sync_err && err == NULL)
4608 got_fileindex_free(fileindex);
4609 free(fileindex_path);
4610 unlockerr = lock_worktree(worktree, LOCK_SH);
4611 if (unlockerr && err == NULL)
4613 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4614 struct got_commitable *ct = pe->data;
4615 free_commitable(ct);
4617 got_pathlist_free(&commitable_paths);
4622 got_commitable_get_path(struct got_commitable *ct)
4628 got_commitable_get_status(struct got_commitable *ct)
4633 struct check_rebase_ok_arg {
4634 struct got_worktree *worktree;
4635 struct got_repository *repo;
4638 static const struct got_error *
4639 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4641 const struct got_error *err = NULL;
4642 struct check_rebase_ok_arg *a = arg;
4643 unsigned char status;
4647 /* Reject rebase of a work tree with mixed base commits. */
4648 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4649 SHA1_DIGEST_LENGTH))
4650 return got_error(GOT_ERR_MIXED_COMMITS);
4652 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4654 return got_error_from_errno("asprintf");
4656 /* Reject rebase of a work tree with modified or staged files. */
4657 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4662 if (status != GOT_STATUS_NO_CHANGE)
4663 return got_error(GOT_ERR_MODIFIED);
4664 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4665 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4670 const struct got_error *
4671 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4672 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4673 struct got_worktree *worktree, struct got_reference *branch,
4674 struct got_repository *repo)
4676 const struct got_error *err = NULL;
4677 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4678 char *branch_ref_name = NULL;
4679 char *fileindex_path = NULL;
4680 struct check_rebase_ok_arg ok_arg;
4681 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4682 struct got_object_id *wt_branch_tip = NULL;
4684 *new_base_branch_ref = NULL;
4688 err = lock_worktree(worktree, LOCK_EX);
4692 err = open_fileindex(fileindex, &fileindex_path, worktree);
4696 ok_arg.worktree = worktree;
4698 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4703 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4707 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4711 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4715 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4720 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4723 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4724 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4728 err = got_ref_alloc_symref(new_base_branch_ref,
4729 new_base_branch_ref_name, wt_branch);
4732 err = got_ref_write(*new_base_branch_ref, repo);
4736 /* TODO Lock original branch's ref while rebasing? */
4738 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4742 err = got_ref_write(branch_ref, repo);
4746 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4747 worktree->base_commit_id);
4750 err = got_ref_write(*tmp_branch, repo);
4754 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4758 free(fileindex_path);
4759 free(tmp_branch_name);
4760 free(new_base_branch_ref_name);
4761 free(branch_ref_name);
4763 got_ref_close(branch_ref);
4765 got_ref_close(wt_branch);
4766 free(wt_branch_tip);
4768 if (*new_base_branch_ref) {
4769 got_ref_close(*new_base_branch_ref);
4770 *new_base_branch_ref = NULL;
4773 got_ref_close(*tmp_branch);
4777 got_fileindex_free(*fileindex);
4780 lock_worktree(worktree, LOCK_SH);
4785 const struct got_error *
4786 got_worktree_rebase_continue(struct got_object_id **commit_id,
4787 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4788 struct got_reference **branch, struct got_fileindex **fileindex,
4789 struct got_worktree *worktree, struct got_repository *repo)
4791 const struct got_error *err;
4792 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4793 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4794 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4795 char *fileindex_path = NULL;
4796 int have_staged_files = 0;
4799 *new_base_branch = NULL;
4804 err = lock_worktree(worktree, LOCK_EX);
4808 err = open_fileindex(fileindex, &fileindex_path, worktree);
4812 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4813 &have_staged_files);
4814 if (err && err->code != GOT_ERR_CANCELLED)
4816 if (have_staged_files) {
4817 err = got_error(GOT_ERR_STAGED_PATHS);
4821 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4825 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4829 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4833 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4837 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4841 err = got_ref_open(branch, repo,
4842 got_ref_get_symref_target(branch_ref), 0);
4846 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4850 err = got_ref_resolve(commit_id, repo, commit_ref);
4854 err = got_ref_open(new_base_branch, repo,
4855 new_base_branch_ref_name, 0);
4859 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4863 free(commit_ref_name);
4864 free(branch_ref_name);
4865 free(fileindex_path);
4867 got_ref_close(commit_ref);
4869 got_ref_close(branch_ref);
4874 got_ref_close(*tmp_branch);
4877 if (*new_base_branch) {
4878 got_ref_close(*new_base_branch);
4879 *new_base_branch = NULL;
4882 got_ref_close(*branch);
4886 got_fileindex_free(*fileindex);
4889 lock_worktree(worktree, LOCK_SH);
4894 const struct got_error *
4895 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4897 const struct got_error *err;
4898 char *tmp_branch_name = NULL;
4900 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4904 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4905 free(tmp_branch_name);
4909 static const struct got_error *
4910 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4911 char **logmsg, void *arg)
4917 static const struct got_error *
4918 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4919 const char *path, struct got_object_id *blob_id,
4920 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4921 int dirfd, const char *de_name)
4926 struct collect_merged_paths_arg {
4927 got_worktree_checkout_cb progress_cb;
4929 struct got_pathlist_head *merged_paths;
4932 static const struct got_error *
4933 collect_merged_paths(void *arg, unsigned char status, const char *path)
4935 const struct got_error *err;
4936 struct collect_merged_paths_arg *a = arg;
4938 struct got_pathlist_entry *new;
4940 err = (*a->progress_cb)(a->progress_arg, status, path);
4944 if (status != GOT_STATUS_MERGE &&
4945 status != GOT_STATUS_ADD &&
4946 status != GOT_STATUS_DELETE &&
4947 status != GOT_STATUS_CONFLICT)
4952 return got_error_from_errno("strdup");
4954 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4955 if (err || new == NULL)
4961 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4963 struct got_pathlist_entry *pe;
4965 TAILQ_FOREACH(pe, merged_paths, entry)
4966 free((char *)pe->path);
4968 got_pathlist_free(merged_paths);
4971 static const struct got_error *
4972 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4973 struct got_repository *repo)
4975 const struct got_error *err;
4976 struct got_reference *commit_ref = NULL;
4978 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4980 if (err->code != GOT_ERR_NOT_REF)
4982 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4985 err = got_ref_write(commit_ref, repo);
4989 struct got_object_id *stored_id;
4992 err = got_ref_resolve(&stored_id, repo, commit_ref);
4995 cmp = got_object_id_cmp(commit_id, stored_id);
4998 err = got_error(GOT_ERR_REBASE_COMMITID);
5004 got_ref_close(commit_ref);
5008 static const struct got_error *
5009 rebase_merge_files(struct got_pathlist_head *merged_paths,
5010 const char *commit_ref_name, struct got_worktree *worktree,
5011 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5012 struct got_object_id *commit_id, struct got_repository *repo,
5013 got_worktree_checkout_cb progress_cb, void *progress_arg,
5014 got_cancel_cb cancel_cb, void *cancel_arg)
5016 const struct got_error *err;
5017 struct got_reference *commit_ref = NULL;
5018 struct collect_merged_paths_arg cmp_arg;
5019 char *fileindex_path;
5021 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5023 err = get_fileindex_path(&fileindex_path, worktree);
5027 cmp_arg.progress_cb = progress_cb;
5028 cmp_arg.progress_arg = progress_arg;
5029 cmp_arg.merged_paths = merged_paths;
5030 err = merge_files(worktree, fileindex, fileindex_path,
5031 parent_commit_id, commit_id, repo, collect_merged_paths,
5032 &cmp_arg, cancel_cb, cancel_arg);
5034 got_ref_close(commit_ref);
5038 const struct got_error *
5039 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5040 struct got_worktree *worktree, struct got_fileindex *fileindex,
5041 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5042 struct got_repository *repo,
5043 got_worktree_checkout_cb progress_cb, void *progress_arg,
5044 got_cancel_cb cancel_cb, void *cancel_arg)
5046 const struct got_error *err;
5047 char *commit_ref_name;
5049 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5053 err = store_commit_id(commit_ref_name, commit_id, repo);
5057 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5058 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5059 progress_arg, cancel_cb, cancel_arg);
5061 free(commit_ref_name);
5065 const struct got_error *
5066 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5067 struct got_worktree *worktree, struct got_fileindex *fileindex,
5068 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5069 struct got_repository *repo,
5070 got_worktree_checkout_cb progress_cb, void *progress_arg,
5071 got_cancel_cb cancel_cb, void *cancel_arg)
5073 const struct got_error *err;
5074 char *commit_ref_name;
5076 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5080 err = store_commit_id(commit_ref_name, commit_id, repo);
5084 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5085 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5086 progress_arg, cancel_cb, cancel_arg);
5088 free(commit_ref_name);
5092 static const struct got_error *
5093 rebase_commit(struct got_object_id **new_commit_id,
5094 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5095 struct got_worktree *worktree, struct got_fileindex *fileindex,
5096 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5097 const char *new_logmsg, struct got_repository *repo)
5099 const struct got_error *err, *sync_err;
5100 struct got_pathlist_head commitable_paths;
5101 struct collect_commitables_arg cc_arg;
5102 char *fileindex_path = NULL;
5103 struct got_reference *head_ref = NULL;
5104 struct got_object_id *head_commit_id = NULL;
5105 char *logmsg = NULL;
5107 TAILQ_INIT(&commitable_paths);
5108 *new_commit_id = NULL;
5110 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5112 err = get_fileindex_path(&fileindex_path, worktree);
5116 cc_arg.commitable_paths = &commitable_paths;
5117 cc_arg.worktree = worktree;
5119 cc_arg.have_staged_files = 0;
5121 * If possible get the status of individual files directly to
5122 * avoid crawling the entire work tree once per rebased commit.
5123 * TODO: Ideally, merged_paths would contain a list of commitables
5124 * we could use so we could skip worktree_status() entirely.
5127 struct got_pathlist_entry *pe;
5128 if (TAILQ_EMPTY(merged_paths)) {
5129 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5132 TAILQ_FOREACH(pe, merged_paths, entry) {
5133 err = worktree_status(worktree, pe->path, fileindex,
5134 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5140 err = worktree_status(worktree, "", fileindex, repo,
5141 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5146 if (TAILQ_EMPTY(&commitable_paths)) {
5147 /* No-op change; commit will be elided. */
5148 err = got_ref_delete(commit_ref, repo);
5151 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5155 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5159 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5164 logmsg = strdup(new_logmsg);
5165 if (logmsg == NULL) {
5166 err = got_error_from_errno("strdup");
5170 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5175 /* NB: commit_worktree will call free(logmsg) */
5176 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5177 worktree, got_object_commit_get_author(orig_commit),
5178 got_object_commit_get_committer(orig_commit),
5179 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5183 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5187 err = got_ref_delete(commit_ref, repo);
5191 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5193 sync_err = sync_fileindex(fileindex, fileindex_path);
5194 if (sync_err && err == NULL)
5197 free(fileindex_path);
5198 free(head_commit_id);
5200 got_ref_close(head_ref);
5202 free(*new_commit_id);
5203 *new_commit_id = NULL;
5208 const struct got_error *
5209 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5210 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5211 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5212 struct got_commit_object *orig_commit,
5213 struct got_object_id *orig_commit_id, struct got_repository *repo)
5215 const struct got_error *err;
5216 char *commit_ref_name;
5217 struct got_reference *commit_ref = NULL;
5218 struct got_object_id *commit_id = NULL;
5220 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5224 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5227 err = got_ref_resolve(&commit_id, repo, commit_ref);
5230 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5231 err = got_error(GOT_ERR_REBASE_COMMITID);
5235 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5236 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5239 got_ref_close(commit_ref);
5240 free(commit_ref_name);
5245 const struct got_error *
5246 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5247 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5248 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5249 struct got_commit_object *orig_commit,
5250 struct got_object_id *orig_commit_id, const char *new_logmsg,
5251 struct got_repository *repo)
5253 const struct got_error *err;
5254 char *commit_ref_name;
5255 struct got_reference *commit_ref = NULL;
5256 struct got_object_id *commit_id = NULL;
5258 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5262 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5265 err = got_ref_resolve(&commit_id, repo, commit_ref);
5268 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5269 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5273 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5274 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5277 got_ref_close(commit_ref);
5278 free(commit_ref_name);
5283 const struct got_error *
5284 got_worktree_rebase_postpone(struct got_worktree *worktree,
5285 struct got_fileindex *fileindex)
5288 got_fileindex_free(fileindex);
5289 return lock_worktree(worktree, LOCK_SH);
5292 static const struct got_error *
5293 delete_ref(const char *name, struct got_repository *repo)
5295 const struct got_error *err;
5296 struct got_reference *ref;
5298 err = got_ref_open(&ref, repo, name, 0);
5300 if (err->code == GOT_ERR_NOT_REF)
5305 err = got_ref_delete(ref, repo);
5310 static const struct got_error *
5311 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5313 const struct got_error *err;
5314 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5315 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5317 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5320 err = delete_ref(tmp_branch_name, repo);
5324 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5327 err = delete_ref(new_base_branch_ref_name, repo);
5331 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5334 err = delete_ref(branch_ref_name, repo);
5338 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5341 err = delete_ref(commit_ref_name, repo);
5346 free(tmp_branch_name);
5347 free(new_base_branch_ref_name);
5348 free(branch_ref_name);
5349 free(commit_ref_name);
5353 const struct got_error *
5354 got_worktree_rebase_complete(struct got_worktree *worktree,
5355 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5356 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5357 struct got_repository *repo)
5359 const struct got_error *err, *unlockerr;
5360 struct got_object_id *new_head_commit_id = NULL;
5362 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5366 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5370 err = got_ref_write(rebased_branch, repo);
5374 err = got_worktree_set_head_ref(worktree, rebased_branch);
5378 err = delete_rebase_refs(worktree, repo);
5381 got_fileindex_free(fileindex);
5382 free(new_head_commit_id);
5383 unlockerr = lock_worktree(worktree, LOCK_SH);
5384 if (unlockerr && err == NULL)
5389 const struct got_error *
5390 got_worktree_rebase_abort(struct got_worktree *worktree,
5391 struct got_fileindex *fileindex, struct got_repository *repo,
5392 struct got_reference *new_base_branch,
5393 got_worktree_checkout_cb progress_cb, void *progress_arg)
5395 const struct got_error *err, *unlockerr, *sync_err;
5396 struct got_reference *resolved = NULL;
5397 struct got_object_id *commit_id = NULL;
5398 char *fileindex_path = NULL;
5399 struct revert_file_args rfa;
5400 struct got_object_id *tree_id = NULL;
5402 err = lock_worktree(worktree, LOCK_EX);
5406 err = got_ref_open(&resolved, repo,
5407 got_ref_get_symref_target(new_base_branch), 0);
5411 err = got_worktree_set_head_ref(worktree, resolved);
5416 * XXX commits to the base branch could have happened while
5417 * we were busy rebasing; should we store the original commit ID
5418 * when rebase begins and read it back here?
5420 err = got_ref_resolve(&commit_id, repo, resolved);
5424 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5428 err = got_object_id_by_path(&tree_id, repo,
5429 worktree->base_commit_id, worktree->path_prefix);
5433 err = delete_rebase_refs(worktree, repo);
5437 err = get_fileindex_path(&fileindex_path, worktree);
5441 rfa.worktree = worktree;
5442 rfa.fileindex = fileindex;
5443 rfa.progress_cb = progress_cb;
5444 rfa.progress_arg = progress_arg;
5445 rfa.patch_cb = NULL;
5446 rfa.patch_arg = NULL;
5448 err = worktree_status(worktree, "", fileindex, repo,
5449 revert_file, &rfa, NULL, NULL, 0, 0);
5453 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5454 repo, progress_cb, progress_arg, NULL, NULL);
5456 sync_err = sync_fileindex(fileindex, fileindex_path);
5457 if (sync_err && err == NULL)
5460 got_ref_close(resolved);
5464 got_fileindex_free(fileindex);
5465 free(fileindex_path);
5467 unlockerr = lock_worktree(worktree, LOCK_SH);
5468 if (unlockerr && err == NULL)
5473 const struct got_error *
5474 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5475 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5476 struct got_fileindex **fileindex, struct got_worktree *worktree,
5477 struct got_repository *repo)
5479 const struct got_error *err = NULL;
5480 char *tmp_branch_name = NULL;
5481 char *branch_ref_name = NULL;
5482 char *base_commit_ref_name = NULL;
5483 char *fileindex_path = NULL;
5484 struct check_rebase_ok_arg ok_arg;
5485 struct got_reference *wt_branch = NULL;
5486 struct got_reference *base_commit_ref = NULL;
5490 *base_commit_id = NULL;
5493 err = lock_worktree(worktree, LOCK_EX);
5497 err = open_fileindex(fileindex, &fileindex_path, worktree);
5501 ok_arg.worktree = worktree;
5503 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5508 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5512 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5516 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5521 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5526 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5530 err = got_ref_write(*branch_ref, repo);
5534 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5535 worktree->base_commit_id);
5538 err = got_ref_write(base_commit_ref, repo);
5541 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5542 if (*base_commit_id == NULL) {
5543 err = got_error_from_errno("got_object_id_dup");
5547 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5548 worktree->base_commit_id);
5551 err = got_ref_write(*tmp_branch, repo);
5555 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5559 free(fileindex_path);
5560 free(tmp_branch_name);
5561 free(branch_ref_name);
5562 free(base_commit_ref_name);
5564 got_ref_close(wt_branch);
5567 got_ref_close(*branch_ref);
5571 got_ref_close(*tmp_branch);
5574 free(*base_commit_id);
5576 got_fileindex_free(*fileindex);
5579 lock_worktree(worktree, LOCK_SH);
5584 const struct got_error *
5585 got_worktree_histedit_postpone(struct got_worktree *worktree,
5586 struct got_fileindex *fileindex)
5589 got_fileindex_free(fileindex);
5590 return lock_worktree(worktree, LOCK_SH);
5593 const struct got_error *
5594 got_worktree_histedit_in_progress(int *in_progress,
5595 struct got_worktree *worktree)
5597 const struct got_error *err;
5598 char *tmp_branch_name = NULL;
5600 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5604 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5605 free(tmp_branch_name);
5609 const struct got_error *
5610 got_worktree_histedit_continue(struct got_object_id **commit_id,
5611 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5612 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5613 struct got_worktree *worktree, struct got_repository *repo)
5615 const struct got_error *err;
5616 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5617 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5618 struct got_reference *commit_ref = NULL;
5619 struct got_reference *base_commit_ref = NULL;
5620 char *fileindex_path = NULL;
5621 int have_staged_files = 0;
5625 *base_commit_id = NULL;
5628 err = lock_worktree(worktree, LOCK_EX);
5632 err = open_fileindex(fileindex, &fileindex_path, worktree);
5636 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5637 &have_staged_files);
5638 if (err && err->code != GOT_ERR_CANCELLED)
5640 if (have_staged_files) {
5641 err = got_error(GOT_ERR_STAGED_PATHS);
5645 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5649 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5653 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5657 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5662 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5666 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5669 err = got_ref_resolve(commit_id, repo, commit_ref);
5673 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5676 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5680 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5684 free(commit_ref_name);
5685 free(branch_ref_name);
5686 free(fileindex_path);
5688 got_ref_close(commit_ref);
5689 if (base_commit_ref)
5690 got_ref_close(base_commit_ref);
5694 free(*base_commit_id);
5695 *base_commit_id = NULL;
5697 got_ref_close(*tmp_branch);
5701 got_fileindex_free(*fileindex);
5704 lock_worktree(worktree, LOCK_EX);
5709 static const struct got_error *
5710 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5712 const struct got_error *err;
5713 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5714 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5716 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5719 err = delete_ref(tmp_branch_name, repo);
5723 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5727 err = delete_ref(base_commit_ref_name, repo);
5731 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5734 err = delete_ref(branch_ref_name, repo);
5738 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5741 err = delete_ref(commit_ref_name, repo);
5745 free(tmp_branch_name);
5746 free(base_commit_ref_name);
5747 free(branch_ref_name);
5748 free(commit_ref_name);
5752 const struct got_error *
5753 got_worktree_histedit_abort(struct got_worktree *worktree,
5754 struct got_fileindex *fileindex, struct got_repository *repo,
5755 struct got_reference *branch, struct got_object_id *base_commit_id,
5756 got_worktree_checkout_cb progress_cb, void *progress_arg)
5758 const struct got_error *err, *unlockerr, *sync_err;
5759 struct got_reference *resolved = NULL;
5760 char *fileindex_path = NULL;
5761 struct got_object_id *tree_id = NULL;
5762 struct revert_file_args rfa;
5764 err = lock_worktree(worktree, LOCK_EX);
5768 err = got_ref_open(&resolved, repo,
5769 got_ref_get_symref_target(branch), 0);
5773 err = got_worktree_set_head_ref(worktree, resolved);
5777 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5781 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5782 worktree->path_prefix);
5786 err = delete_histedit_refs(worktree, repo);
5790 err = get_fileindex_path(&fileindex_path, worktree);
5794 rfa.worktree = worktree;
5795 rfa.fileindex = fileindex;
5796 rfa.progress_cb = progress_cb;
5797 rfa.progress_arg = progress_arg;
5798 rfa.patch_cb = NULL;
5799 rfa.patch_arg = NULL;
5801 err = worktree_status(worktree, "", fileindex, repo,
5802 revert_file, &rfa, NULL, NULL, 0, 0);
5806 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5807 repo, progress_cb, progress_arg, NULL, NULL);
5809 sync_err = sync_fileindex(fileindex, fileindex_path);
5810 if (sync_err && err == NULL)
5813 got_ref_close(resolved);
5815 free(fileindex_path);
5817 unlockerr = lock_worktree(worktree, LOCK_SH);
5818 if (unlockerr && err == NULL)
5823 const struct got_error *
5824 got_worktree_histedit_complete(struct got_worktree *worktree,
5825 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5826 struct got_reference *edited_branch, struct got_repository *repo)
5828 const struct got_error *err, *unlockerr;
5829 struct got_object_id *new_head_commit_id = NULL;
5830 struct got_reference *resolved = NULL;
5832 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5836 err = got_ref_open(&resolved, repo,
5837 got_ref_get_symref_target(edited_branch), 0);
5841 err = got_ref_change_ref(resolved, new_head_commit_id);
5845 err = got_ref_write(resolved, repo);
5849 err = got_worktree_set_head_ref(worktree, resolved);
5853 err = delete_histedit_refs(worktree, repo);
5856 got_fileindex_free(fileindex);
5857 free(new_head_commit_id);
5858 unlockerr = lock_worktree(worktree, LOCK_SH);
5859 if (unlockerr && err == NULL)
5864 const struct got_error *
5865 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5866 struct got_object_id *commit_id, struct got_repository *repo)
5868 const struct got_error *err;
5869 char *commit_ref_name;
5871 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5875 err = store_commit_id(commit_ref_name, commit_id, repo);
5879 err = delete_ref(commit_ref_name, repo);
5881 free(commit_ref_name);
5885 const struct got_error *
5886 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5887 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5888 struct got_worktree *worktree, const char *refname,
5889 struct got_repository *repo)
5891 const struct got_error *err = NULL;
5892 char *fileindex_path = NULL;
5893 struct check_rebase_ok_arg ok_arg;
5897 *base_branch_ref = NULL;
5899 err = lock_worktree(worktree, LOCK_EX);
5903 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5904 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5905 "cannot integrate a branch into itself; "
5906 "update -b or different branch name required");
5910 err = open_fileindex(fileindex, &fileindex_path, worktree);
5914 /* Preconditions are the same as for rebase. */
5915 ok_arg.worktree = worktree;
5917 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5922 err = got_ref_open(branch_ref, repo, refname, 1);
5926 err = got_ref_open(base_branch_ref, repo,
5927 got_worktree_get_head_ref_name(worktree), 1);
5931 got_ref_close(*branch_ref);
5934 if (*base_branch_ref) {
5935 got_ref_close(*base_branch_ref);
5936 *base_branch_ref = NULL;
5939 got_fileindex_free(*fileindex);
5942 lock_worktree(worktree, LOCK_SH);
5947 const struct got_error *
5948 got_worktree_integrate_continue(struct got_worktree *worktree,
5949 struct got_fileindex *fileindex, struct got_repository *repo,
5950 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5951 got_worktree_checkout_cb progress_cb, void *progress_arg,
5952 got_cancel_cb cancel_cb, void *cancel_arg)
5954 const struct got_error *err = NULL, *sync_err, *unlockerr;
5955 char *fileindex_path = NULL;
5956 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5958 err = get_fileindex_path(&fileindex_path, worktree);
5962 err = got_ref_resolve(&commit_id, repo, branch_ref);
5966 err = got_object_id_by_path(&tree_id, repo, commit_id,
5967 worktree->path_prefix);
5971 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5975 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5976 progress_cb, progress_arg, cancel_cb, cancel_arg);
5980 err = got_ref_change_ref(base_branch_ref, commit_id);
5984 err = got_ref_write(base_branch_ref, repo);
5986 sync_err = sync_fileindex(fileindex, fileindex_path);
5987 if (sync_err && err == NULL)
5991 unlockerr = got_ref_unlock(branch_ref);
5992 if (unlockerr && err == NULL)
5994 got_ref_close(branch_ref);
5996 unlockerr = got_ref_unlock(base_branch_ref);
5997 if (unlockerr && err == NULL)
5999 got_ref_close(base_branch_ref);
6001 got_fileindex_free(fileindex);
6002 free(fileindex_path);
6005 unlockerr = lock_worktree(worktree, LOCK_SH);
6006 if (unlockerr && err == NULL)
6011 const struct got_error *
6012 got_worktree_integrate_abort(struct got_worktree *worktree,
6013 struct got_fileindex *fileindex, struct got_repository *repo,
6014 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6016 const struct got_error *err = NULL, *unlockerr = NULL;
6018 got_fileindex_free(fileindex);
6020 err = lock_worktree(worktree, LOCK_SH);
6022 unlockerr = got_ref_unlock(branch_ref);
6023 if (unlockerr && err == NULL)
6025 got_ref_close(branch_ref);
6027 unlockerr = got_ref_unlock(base_branch_ref);
6028 if (unlockerr && err == NULL)
6030 got_ref_close(base_branch_ref);
6035 struct check_stage_ok_arg {
6036 struct got_object_id *head_commit_id;
6037 struct got_worktree *worktree;
6038 struct got_fileindex *fileindex;
6039 struct got_repository *repo;
6043 const struct got_error *
6044 check_stage_ok(void *arg, unsigned char status,
6045 unsigned char staged_status, const char *relpath,
6046 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6047 struct got_object_id *commit_id, int dirfd, const char *de_name)
6049 struct check_stage_ok_arg *a = arg;
6050 const struct got_error *err = NULL;
6051 struct got_fileindex_entry *ie;
6052 struct got_object_id base_commit_id;
6053 struct got_object_id *base_commit_idp = NULL;
6054 char *in_repo_path = NULL, *p;
6056 if (status == GOT_STATUS_UNVERSIONED ||
6057 status == GOT_STATUS_NO_CHANGE)
6059 if (status == GOT_STATUS_NONEXISTENT)
6060 return got_error_set_errno(ENOENT, relpath);
6062 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6064 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6066 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6067 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6069 return got_error_from_errno("asprintf");
6071 if (got_fileindex_entry_has_commit(ie)) {
6072 memcpy(base_commit_id.sha1, ie->commit_sha1,
6073 SHA1_DIGEST_LENGTH);
6074 base_commit_idp = &base_commit_id;
6077 if (status == GOT_STATUS_CONFLICT) {
6078 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6080 } else if (status != GOT_STATUS_ADD &&
6081 status != GOT_STATUS_MODIFY &&
6082 status != GOT_STATUS_DELETE) {
6083 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6087 a->have_changes = 1;
6092 err = check_out_of_date(p, status, staged_status,
6093 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6094 GOT_ERR_STAGE_OUT_OF_DATE);
6100 struct stage_path_arg {
6101 struct got_worktree *worktree;
6102 struct got_fileindex *fileindex;
6103 struct got_repository *repo;
6104 got_worktree_status_cb status_cb;
6106 got_worktree_patch_cb patch_cb;
6108 int staged_something;
6111 static const struct got_error *
6112 stage_path(void *arg, unsigned char status,
6113 unsigned char staged_status, const char *relpath,
6114 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6115 struct got_object_id *commit_id, int dirfd, const char *de_name)
6117 struct stage_path_arg *a = arg;
6118 const struct got_error *err = NULL;
6119 struct got_fileindex_entry *ie;
6120 char *ondisk_path = NULL, *path_content = NULL;
6122 struct got_object_id *new_staged_blob_id = NULL;
6124 if (status == GOT_STATUS_UNVERSIONED)
6127 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6129 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6131 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6133 return got_error_from_errno("asprintf");
6136 case GOT_STATUS_ADD:
6137 case GOT_STATUS_MODIFY:
6139 if (status == GOT_STATUS_ADD) {
6140 int choice = GOT_PATCH_CHOICE_NONE;
6141 err = (*a->patch_cb)(&choice, a->patch_arg,
6142 status, ie->path, NULL, 1, 1);
6145 if (choice != GOT_PATCH_CHOICE_YES)
6148 err = create_patched_content(&path_content, 0,
6149 staged_blob_id ? staged_blob_id : blob_id,
6150 ondisk_path, dirfd, de_name, ie->path,
6151 a->repo, a->patch_cb, a->patch_arg);
6152 if (err || path_content == NULL)
6156 err = got_object_blob_create(&new_staged_blob_id,
6157 path_content ? path_content : ondisk_path, a->repo);
6160 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6161 SHA1_DIGEST_LENGTH);
6162 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6163 stage = GOT_FILEIDX_STAGE_ADD;
6165 stage = GOT_FILEIDX_STAGE_MODIFY;
6166 got_fileindex_entry_stage_set(ie, stage);
6167 a->staged_something = 1;
6168 if (a->status_cb == NULL)
6170 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6171 get_staged_status(ie), relpath, blob_id,
6172 new_staged_blob_id, NULL, dirfd, de_name);
6174 case GOT_STATUS_DELETE:
6175 if (staged_status == GOT_STATUS_DELETE)
6178 int choice = GOT_PATCH_CHOICE_NONE;
6179 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6180 ie->path, NULL, 1, 1);
6183 if (choice == GOT_PATCH_CHOICE_NO)
6185 if (choice != GOT_PATCH_CHOICE_YES) {
6186 err = got_error(GOT_ERR_PATCH_CHOICE);
6190 stage = GOT_FILEIDX_STAGE_DELETE;
6191 got_fileindex_entry_stage_set(ie, stage);
6192 a->staged_something = 1;
6193 if (a->status_cb == NULL)
6195 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6196 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6199 case GOT_STATUS_NO_CHANGE:
6201 case GOT_STATUS_CONFLICT:
6202 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6204 case GOT_STATUS_NONEXISTENT:
6205 err = got_error_set_errno(ENOENT, relpath);
6208 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6212 if (path_content && unlink(path_content) == -1 && err == NULL)
6213 err = got_error_from_errno2("unlink", path_content);
6216 free(new_staged_blob_id);
6220 const struct got_error *
6221 got_worktree_stage(struct got_worktree *worktree,
6222 struct got_pathlist_head *paths,
6223 got_worktree_status_cb status_cb, void *status_arg,
6224 got_worktree_patch_cb patch_cb, void *patch_arg,
6225 struct got_repository *repo)
6227 const struct got_error *err = NULL, *sync_err, *unlockerr;
6228 struct got_pathlist_entry *pe;
6229 struct got_fileindex *fileindex = NULL;
6230 char *fileindex_path = NULL;
6231 struct got_reference *head_ref = NULL;
6232 struct got_object_id *head_commit_id = NULL;
6233 struct check_stage_ok_arg oka;
6234 struct stage_path_arg spa;
6236 err = lock_worktree(worktree, LOCK_EX);
6240 err = got_ref_open(&head_ref, repo,
6241 got_worktree_get_head_ref_name(worktree), 0);
6244 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6247 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6251 /* Check pre-conditions before staging anything. */
6252 oka.head_commit_id = head_commit_id;
6253 oka.worktree = worktree;
6254 oka.fileindex = fileindex;
6256 oka.have_changes = 0;
6257 TAILQ_FOREACH(pe, paths, entry) {
6258 err = worktree_status(worktree, pe->path, fileindex, repo,
6259 check_stage_ok, &oka, NULL, NULL, 0, 0);
6263 if (!oka.have_changes) {
6264 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6268 spa.worktree = worktree;
6269 spa.fileindex = fileindex;
6271 spa.patch_cb = patch_cb;
6272 spa.patch_arg = patch_arg;
6273 spa.status_cb = status_cb;
6274 spa.status_arg = status_arg;
6275 spa.staged_something = 0;
6276 TAILQ_FOREACH(pe, paths, entry) {
6277 err = worktree_status(worktree, pe->path, fileindex, repo,
6278 stage_path, &spa, NULL, NULL, 0, 0);
6282 if (!spa.staged_something) {
6283 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6287 sync_err = sync_fileindex(fileindex, fileindex_path);
6288 if (sync_err && err == NULL)
6292 got_ref_close(head_ref);
6293 free(head_commit_id);
6294 free(fileindex_path);
6296 got_fileindex_free(fileindex);
6297 unlockerr = lock_worktree(worktree, LOCK_SH);
6298 if (unlockerr && err == NULL)
6303 struct unstage_path_arg {
6304 struct got_worktree *worktree;
6305 struct got_fileindex *fileindex;
6306 struct got_repository *repo;
6307 got_worktree_checkout_cb progress_cb;
6309 got_worktree_patch_cb patch_cb;
6313 static const struct got_error *
6314 create_unstaged_content(char **path_unstaged_content,
6315 char **path_new_staged_content, struct got_object_id *blob_id,
6316 struct got_object_id *staged_blob_id, const char *relpath,
6317 struct got_repository *repo,
6318 got_worktree_patch_cb patch_cb, void *patch_arg)
6320 const struct got_error *err;
6321 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6322 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6323 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6324 struct stat sb1, sb2;
6325 struct got_diff_changes *changes = NULL;
6326 struct got_diff_state *ds = NULL;
6327 struct got_diff_args *args = NULL;
6328 struct got_diff_change *change;
6329 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6330 int have_content = 0, have_rejected_content = 0;
6332 *path_unstaged_content = NULL;
6333 *path_new_staged_content = NULL;
6335 err = got_object_id_str(&label1, blob_id);
6338 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6342 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6346 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6350 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6354 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6358 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6362 if (stat(path1, &sb1) == -1) {
6363 err = got_error_from_errno2("stat", path1);
6367 if (stat(path2, &sb2) == -1) {
6368 err = got_error_from_errno2("stat", path2);
6372 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6373 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6377 err = got_opentemp_named(path_unstaged_content, &outfile,
6378 "got-unstaged-content");
6381 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6382 "got-new-staged-content");
6386 if (fseek(f1, 0L, SEEK_SET) == -1) {
6387 err = got_ferror(f1, GOT_ERR_IO);
6390 if (fseek(f2, 0L, SEEK_SET) == -1) {
6391 err = got_ferror(f2, GOT_ERR_IO);
6394 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6396 err = apply_or_reject_change(&choice, change, ++n,
6397 changes->nchanges, ds, args, diff_flags, relpath,
6398 f1, f2, &line_cur1, &line_cur2,
6399 outfile, rejectfile, patch_cb, patch_arg);
6402 if (choice == GOT_PATCH_CHOICE_YES)
6405 have_rejected_content = 1;
6406 if (choice == GOT_PATCH_CHOICE_QUIT)
6409 if (have_content || have_rejected_content)
6410 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6411 outfile, rejectfile);
6415 got_object_blob_close(blob);
6417 got_object_blob_close(staged_blob);
6418 if (f1 && fclose(f1) == EOF && err == NULL)
6419 err = got_error_from_errno2("fclose", path1);
6420 if (f2 && fclose(f2) == EOF && err == NULL)
6421 err = got_error_from_errno2("fclose", path2);
6422 if (outfile && fclose(outfile) == EOF && err == NULL)
6423 err = got_error_from_errno2("fclose", *path_unstaged_content);
6424 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6425 err = got_error_from_errno2("fclose", *path_new_staged_content);
6426 if (path1 && unlink(path1) == -1 && err == NULL)
6427 err = got_error_from_errno2("unlink", path1);
6428 if (path2 && unlink(path2) == -1 && err == NULL)
6429 err = got_error_from_errno2("unlink", path2);
6430 if (err || !have_content) {
6431 if (*path_unstaged_content &&
6432 unlink(*path_unstaged_content) == -1 && err == NULL)
6433 err = got_error_from_errno2("unlink",
6434 *path_unstaged_content);
6435 free(*path_unstaged_content);
6436 *path_unstaged_content = NULL;
6438 if (err || !have_rejected_content) {
6439 if (*path_new_staged_content &&
6440 unlink(*path_new_staged_content) == -1 && err == NULL)
6441 err = got_error_from_errno2("unlink",
6442 *path_new_staged_content);
6443 free(*path_new_staged_content);
6444 *path_new_staged_content = NULL;
6448 got_diff_state_free(ds);
6452 got_diff_free_changes(changes);
6458 static const struct got_error *
6459 unstage_path(void *arg, unsigned char status,
6460 unsigned char staged_status, const char *relpath,
6461 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6462 struct got_object_id *commit_id, int dirfd, const char *de_name)
6464 const struct got_error *err = NULL;
6465 struct unstage_path_arg *a = arg;
6466 struct got_fileindex_entry *ie;
6467 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6468 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6469 char *path_new_staged_content = NULL;
6470 char *id_str = NULL, *label_orig = NULL;
6471 int local_changes_subsumed;
6474 if (staged_status != GOT_STATUS_ADD &&
6475 staged_status != GOT_STATUS_MODIFY &&
6476 staged_status != GOT_STATUS_DELETE)
6479 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6481 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6483 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6485 return got_error_from_errno("asprintf");
6487 err = got_object_id_str(&id_str,
6488 commit_id ? commit_id : a->worktree->base_commit_id);
6491 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6493 err = got_error_from_errno("asprintf");
6497 switch (staged_status) {
6498 case GOT_STATUS_MODIFY:
6499 err = got_object_open_as_blob(&blob_base, a->repo,
6504 case GOT_STATUS_ADD:
6506 if (staged_status == GOT_STATUS_ADD) {
6507 int choice = GOT_PATCH_CHOICE_NONE;
6508 err = (*a->patch_cb)(&choice, a->patch_arg,
6509 staged_status, ie->path, NULL, 1, 1);
6512 if (choice != GOT_PATCH_CHOICE_YES)
6515 err = create_unstaged_content(
6516 &path_unstaged_content,
6517 &path_new_staged_content, blob_id,
6518 staged_blob_id, ie->path, a->repo,
6519 a->patch_cb, a->patch_arg);
6520 if (err || path_unstaged_content == NULL)
6522 if (path_new_staged_content) {
6523 err = got_object_blob_create(
6525 path_new_staged_content,
6529 memcpy(ie->staged_blob_sha1,
6530 staged_blob_id->sha1,
6531 SHA1_DIGEST_LENGTH);
6533 err = merge_file(&local_changes_subsumed,
6534 a->worktree, blob_base, ondisk_path,
6535 relpath, got_fileindex_perms_to_st(ie),
6536 path_unstaged_content, label_orig,
6537 "unstaged", a->repo, a->progress_cb,
6540 path_new_staged_content == NULL)
6541 got_fileindex_entry_stage_set(ie,
6542 GOT_FILEIDX_STAGE_NONE);
6543 break; /* Done with this file. */
6546 err = got_object_open_as_blob(&blob_staged, a->repo,
6547 staged_blob_id, 8192);
6550 err = merge_blob(&local_changes_subsumed, a->worktree,
6551 blob_base, ondisk_path, relpath,
6552 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6553 commit_id ? commit_id : a->worktree->base_commit_id,
6554 a->repo, a->progress_cb, a->progress_arg);
6556 got_fileindex_entry_stage_set(ie,
6557 GOT_FILEIDX_STAGE_NONE);
6559 case GOT_STATUS_DELETE:
6561 int choice = GOT_PATCH_CHOICE_NONE;
6562 err = (*a->patch_cb)(&choice, a->patch_arg,
6563 staged_status, ie->path, NULL, 1, 1);
6566 if (choice == GOT_PATCH_CHOICE_NO)
6568 if (choice != GOT_PATCH_CHOICE_YES) {
6569 err = got_error(GOT_ERR_PATCH_CHOICE);
6573 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6574 err = get_file_status(&status, &sb, ie, ondisk_path,
6575 dirfd, de_name, a->repo);
6578 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6583 if (path_unstaged_content &&
6584 unlink(path_unstaged_content) == -1 && err == NULL)
6585 err = got_error_from_errno2("unlink", path_unstaged_content);
6586 if (path_new_staged_content &&
6587 unlink(path_new_staged_content) == -1 && err == NULL)
6588 err = got_error_from_errno2("unlink", path_new_staged_content);
6589 free(path_unstaged_content);
6590 free(path_new_staged_content);
6592 got_object_blob_close(blob_base);
6594 got_object_blob_close(blob_staged);
6600 const struct got_error *
6601 got_worktree_unstage(struct got_worktree *worktree,
6602 struct got_pathlist_head *paths,
6603 got_worktree_checkout_cb progress_cb, void *progress_arg,
6604 got_worktree_patch_cb patch_cb, void *patch_arg,
6605 struct got_repository *repo)
6607 const struct got_error *err = NULL, *sync_err, *unlockerr;
6608 struct got_pathlist_entry *pe;
6609 struct got_fileindex *fileindex = NULL;
6610 char *fileindex_path = NULL;
6611 struct unstage_path_arg upa;
6613 err = lock_worktree(worktree, LOCK_EX);
6617 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6621 upa.worktree = worktree;
6622 upa.fileindex = fileindex;
6624 upa.progress_cb = progress_cb;
6625 upa.progress_arg = progress_arg;
6626 upa.patch_cb = patch_cb;
6627 upa.patch_arg = patch_arg;
6628 TAILQ_FOREACH(pe, paths, entry) {
6629 err = worktree_status(worktree, pe->path, fileindex, repo,
6630 unstage_path, &upa, NULL, NULL, 0, 0);
6635 sync_err = sync_fileindex(fileindex, fileindex_path);
6636 if (sync_err && err == NULL)
6639 free(fileindex_path);
6641 got_fileindex_free(fileindex);
6642 unlockerr = lock_worktree(worktree, LOCK_SH);
6643 if (unlockerr && err == NULL)