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, path);
916 err = got_fileindex_entry_update(new_ie, ondisk_path,
917 blob->id.sha1, worktree->base_commit_id->sha1, 1);
919 got_fileindex_entry_free(new_ie);
922 err = got_fileindex_entry_add(fileindex, new_ie);
924 got_fileindex_entry_free(new_ie);
930 get_ondisk_perms(int executable, mode_t st_mode)
932 mode_t xbits = S_IXUSR;
935 /* Map read bits to execute bits. */
936 if (st_mode & S_IRGRP)
938 if (st_mode & S_IROTH)
940 return st_mode | xbits;
943 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
946 static const struct got_error *
947 install_blob(struct got_worktree *worktree, const char *ondisk_path,
948 const char *path, mode_t te_mode, mode_t st_mode,
949 struct got_blob_object *blob, int restoring_missing_file,
950 int reverting_versioned_file, struct got_repository *repo,
951 got_worktree_checkout_cb progress_cb, void *progress_arg)
953 const struct got_error *err = NULL;
957 char *tmppath = NULL;
959 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
960 GOT_DEFAULT_FILE_MODE);
962 if (errno == ENOENT) {
963 char *parent = dirname(path);
965 return got_error_from_errno2("dirname", path);
966 err = add_dir_on_disk(worktree, parent);
969 fd = open(ondisk_path,
970 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
971 GOT_DEFAULT_FILE_MODE);
973 return got_error_from_errno2("open",
975 } else if (errno == EEXIST) {
976 if (!S_ISREG(st_mode)) {
977 /* TODO file is obstructed; do something */
978 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
981 err = got_opentemp_named_fd(&tmppath, &fd,
988 return got_error_from_errno2("open", ondisk_path);
991 if (restoring_missing_file)
992 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
993 else if (reverting_versioned_file)
994 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
996 err = (*progress_cb)(progress_arg,
997 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1001 hdrlen = got_object_blob_get_hdrlen(blob);
1003 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1004 err = got_object_blob_read_block(&len, blob);
1008 /* Skip blob object header first time around. */
1009 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1011 err = got_error_from_errno("write");
1013 } else if (outlen != len - hdrlen) {
1014 err = got_error(GOT_ERR_IO);
1021 if (fsync(fd) != 0) {
1022 err = got_error_from_errno("fsync");
1027 if (rename(tmppath, ondisk_path) != 0) {
1028 err = got_error_from_errno3("rename", tmppath,
1035 if (chmod(ondisk_path,
1036 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1037 err = got_error_from_errno2("chmod", ondisk_path);
1042 if (fd != -1 && close(fd) != 0 && err == NULL)
1043 err = got_error_from_errno("close");
1048 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1049 static const struct got_error *
1050 get_modified_file_content_status(unsigned char *status, FILE *f)
1052 const struct got_error *err = NULL;
1053 const char *markers[3] = {
1054 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1055 GOT_DIFF_CONFLICT_MARKER_SEP,
1056 GOT_DIFF_CONFLICT_MARKER_END
1061 const char delim[3] = {'\0', '\0', '\0'};
1063 while (*status == GOT_STATUS_MODIFY) {
1064 line = fparseln(f, &len, NULL, delim, 0);
1068 err = got_ferror(f, GOT_ERR_IO);
1072 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1073 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1075 *status = GOT_STATUS_CONFLICT;
1085 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1087 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1088 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1092 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1094 return !(ie->ctime_sec == sb->st_ctime &&
1095 ie->ctime_nsec == sb->st_ctimensec &&
1096 ie->mtime_sec == sb->st_mtime &&
1097 ie->mtime_nsec == sb->st_mtimensec &&
1098 ie->size == (sb->st_size & 0xffffffff) &&
1099 !xbit_differs(ie, sb->st_mode));
1102 static unsigned char
1103 get_staged_status(struct got_fileindex_entry *ie)
1105 switch (got_fileindex_entry_stage_get(ie)) {
1106 case GOT_FILEIDX_STAGE_ADD:
1107 return GOT_STATUS_ADD;
1108 case GOT_FILEIDX_STAGE_DELETE:
1109 return GOT_STATUS_DELETE;
1110 case GOT_FILEIDX_STAGE_MODIFY:
1111 return GOT_STATUS_MODIFY;
1113 return GOT_STATUS_NO_CHANGE;
1117 static const struct got_error *
1118 get_file_status(unsigned char *status, struct stat *sb,
1119 struct got_fileindex_entry *ie, const char *abspath,
1120 int dirfd, const char *de_name, struct got_repository *repo)
1122 const struct got_error *err = NULL;
1123 struct got_object_id id;
1128 struct got_blob_object *blob = NULL;
1130 unsigned char staged_status = get_staged_status(ie);
1132 *status = GOT_STATUS_NO_CHANGE;
1135 * Whenever the caller provides a directory descriptor and a
1136 * directory entry name for the file, use them! This prevents
1137 * race conditions if filesystem paths change beneath our feet.
1140 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1141 if (errno == ENOENT) {
1142 if (got_fileindex_entry_has_file_on_disk(ie))
1143 *status = GOT_STATUS_MISSING;
1145 *status = GOT_STATUS_DELETE;
1148 err = got_error_from_errno2("fstatat", abspath);
1152 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1153 if (fd == -1 && errno != ENOENT)
1154 return got_error_from_errno2("open", abspath);
1155 if (fd == -1 || fstat(fd, sb) == -1) {
1156 if (errno == ENOENT) {
1157 if (got_fileindex_entry_has_file_on_disk(ie))
1158 *status = GOT_STATUS_MISSING;
1160 *status = GOT_STATUS_DELETE;
1163 err = got_error_from_errno2("fstat", abspath);
1168 if (!S_ISREG(sb->st_mode)) {
1169 *status = GOT_STATUS_OBSTRUCTED;
1173 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1174 *status = GOT_STATUS_DELETE;
1176 } else if (!got_fileindex_entry_has_blob(ie) &&
1177 staged_status != GOT_STATUS_ADD) {
1178 *status = GOT_STATUS_ADD;
1182 if (!stat_info_differs(ie, sb))
1185 if (staged_status == GOT_STATUS_MODIFY ||
1186 staged_status == GOT_STATUS_ADD)
1187 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1189 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1191 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1196 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1198 err = got_error_from_errno2("openat", abspath);
1203 f = fdopen(fd, "r");
1205 err = got_error_from_errno2("fdopen", abspath);
1209 hdrlen = got_object_blob_get_hdrlen(blob);
1211 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1212 err = got_object_blob_read_block(&blen, blob);
1215 /* Skip length of blob object header first time around. */
1216 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1217 if (flen == 0 && ferror(f)) {
1218 err = got_error_from_errno("fread");
1223 *status = GOT_STATUS_MODIFY;
1225 } else if (flen == 0) {
1227 *status = GOT_STATUS_MODIFY;
1229 } else if (blen - hdrlen == flen) {
1230 /* Skip blob object header first time around. */
1231 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1232 *status = GOT_STATUS_MODIFY;
1236 *status = GOT_STATUS_MODIFY;
1242 if (*status == GOT_STATUS_MODIFY) {
1244 err = get_modified_file_content_status(status, f);
1245 } else if (xbit_differs(ie, sb->st_mode))
1246 *status = GOT_STATUS_MODE_CHANGE;
1249 got_object_blob_close(blob);
1250 if (f != NULL && fclose(f) == EOF && err == NULL)
1251 err = got_error_from_errno2("fclose", abspath);
1252 if (fd != -1 && close(fd) == -1 && err == NULL)
1253 err = got_error_from_errno2("close", abspath);
1258 * Update timestamps in the file index if a file is unmodified and
1259 * we had to run a full content comparison to find out.
1261 static const struct got_error *
1262 sync_timestamps(char *ondisk_path, unsigned char status,
1263 struct got_fileindex_entry *ie, struct stat *sb)
1265 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1266 return got_fileindex_entry_update(ie, ondisk_path,
1267 ie->blob_sha1, ie->commit_sha1, 1);
1272 static const struct got_error *
1273 update_blob(struct got_worktree *worktree,
1274 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1275 struct got_tree_entry *te, const char *path,
1276 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1279 const struct got_error *err = NULL;
1280 struct got_blob_object *blob = NULL;
1282 unsigned char status = GOT_STATUS_NO_CHANGE;
1285 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1286 return got_error_from_errno("asprintf");
1289 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1290 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1293 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1297 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1298 sb.st_mode = got_fileindex_perms_to_st(ie);
1300 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1302 if (status == GOT_STATUS_OBSTRUCTED) {
1303 err = (*progress_cb)(progress_arg, status, path);
1307 if (ie && status != GOT_STATUS_MISSING &&
1308 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1309 if (got_fileindex_entry_has_commit(ie) &&
1310 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1311 SHA1_DIGEST_LENGTH) == 0) {
1312 err = sync_timestamps(ondisk_path, status, ie, &sb);
1315 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1319 if (got_fileindex_entry_has_blob(ie) &&
1320 memcmp(ie->blob_sha1, te->id.sha1,
1321 SHA1_DIGEST_LENGTH) == 0) {
1322 err = sync_timestamps(ondisk_path, status, ie, &sb);
1327 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1331 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1332 int update_timestamps;
1333 struct got_blob_object *blob2 = NULL;
1334 char *label_orig = NULL;
1335 if (got_fileindex_entry_has_blob(ie)) {
1336 struct got_object_id id2;
1337 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1338 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1342 if (got_fileindex_entry_has_commit(ie)) {
1343 char id_str[SHA1_DIGEST_STRING_LENGTH];
1344 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1345 sizeof(id_str)) == NULL) {
1346 err = got_error_path(id_str,
1347 GOT_ERR_BAD_OBJ_ID_STR);
1350 if (asprintf(&label_orig, "%s: commit %s",
1351 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1352 err = got_error_from_errno("asprintf");
1356 err = merge_blob(&update_timestamps, worktree, blob2,
1357 ondisk_path, path, sb.st_mode, label_orig, blob,
1358 worktree->base_commit_id, repo,
1359 progress_cb, progress_arg);
1362 got_object_blob_close(blob2);
1366 * Do not update timestamps of files with local changes.
1367 * Otherwise, a future status walk would treat them as
1368 * unmodified files again.
1370 err = got_fileindex_entry_update(ie, ondisk_path,
1371 blob->id.sha1, worktree->base_commit_id->sha1,
1373 } else if (status == GOT_STATUS_MODE_CHANGE) {
1374 err = got_fileindex_entry_update(ie, ondisk_path,
1375 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1376 } else if (status == GOT_STATUS_DELETE) {
1377 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1380 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1381 ondisk_path, path, blob, 0);
1385 err = install_blob(worktree, ondisk_path, path, te->mode,
1386 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1387 repo, progress_cb, progress_arg);
1390 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1391 ondisk_path, path, blob, 1);
1395 got_object_blob_close(blob);
1401 static const struct got_error *
1402 remove_ondisk_file(const char *root_path, const char *path)
1404 const struct got_error *err = NULL;
1405 char *ondisk_path = NULL;
1407 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1408 return got_error_from_errno("asprintf");
1410 if (unlink(ondisk_path) == -1) {
1411 if (errno != ENOENT)
1412 err = got_error_from_errno2("unlink", ondisk_path);
1414 char *parent = dirname(ondisk_path);
1415 while (parent && strcmp(parent, root_path) != 0) {
1416 if (rmdir(parent) == -1) {
1417 if (errno != ENOTEMPTY)
1418 err = got_error_from_errno2("rmdir",
1422 parent = dirname(parent);
1429 static const struct got_error *
1430 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1431 struct got_fileindex_entry *ie, struct got_repository *repo,
1432 got_worktree_checkout_cb progress_cb, void *progress_arg)
1434 const struct got_error *err = NULL;
1435 unsigned char status;
1439 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1440 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1442 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1444 return got_error_from_errno("asprintf");
1446 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1450 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1451 status == GOT_STATUS_ADD) {
1452 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1456 * Preserve the working file and change the deleted blob's
1457 * entry into a schedule-add entry.
1459 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1464 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1467 if (status == GOT_STATUS_NO_CHANGE) {
1468 err = remove_ondisk_file(worktree->root_path, ie->path);
1472 got_fileindex_entry_remove(fileindex, ie);
1478 struct diff_cb_arg {
1479 struct got_fileindex *fileindex;
1480 struct got_worktree *worktree;
1481 struct got_repository *repo;
1482 got_worktree_checkout_cb progress_cb;
1484 got_cancel_cb cancel_cb;
1488 static const struct got_error *
1489 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1490 struct got_tree_entry *te, const char *parent_path)
1492 struct diff_cb_arg *a = arg;
1494 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1495 return got_error(GOT_ERR_CANCELLED);
1497 return update_blob(a->worktree, a->fileindex, ie, te,
1498 ie->path, a->repo, a->progress_cb, a->progress_arg);
1501 static const struct got_error *
1502 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1504 struct diff_cb_arg *a = arg;
1506 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1507 return got_error(GOT_ERR_CANCELLED);
1509 return delete_blob(a->worktree, a->fileindex, ie,
1510 a->repo, a->progress_cb, a->progress_arg);
1513 static const struct got_error *
1514 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1516 struct diff_cb_arg *a = arg;
1517 const struct got_error *err;
1520 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1521 return got_error(GOT_ERR_CANCELLED);
1523 if (got_object_tree_entry_is_submodule(te))
1526 if (asprintf(&path, "%s%s%s", parent_path,
1527 parent_path[0] ? "/" : "", te->name)
1529 return got_error_from_errno("asprintf");
1531 if (S_ISDIR(te->mode))
1532 err = add_dir_on_disk(a->worktree, path);
1534 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1535 a->repo, a->progress_cb, a->progress_arg);
1541 static const struct got_error *
1542 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1544 const struct got_error *err = NULL;
1545 char *uuidstr = NULL;
1546 uint32_t uuid_status;
1550 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1551 if (uuid_status != uuid_s_ok)
1552 return got_error_uuid(uuid_status, "uuid_to_string");
1554 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1556 err = got_error_from_errno("asprintf");
1563 const struct got_error *
1564 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1566 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1569 static const struct got_error *
1570 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1572 return get_ref_name(refname, worktree,
1573 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1576 static const struct got_error *
1577 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1579 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1582 static const struct got_error *
1583 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1585 return get_ref_name(refname, worktree,
1586 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1589 static const struct got_error *
1590 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1592 return get_ref_name(refname, worktree,
1593 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1596 static const struct got_error *
1597 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1599 return get_ref_name(refname, worktree,
1600 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1603 static const struct got_error *
1604 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1606 return get_ref_name(refname, worktree,
1607 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1610 static const struct got_error *
1611 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1613 return get_ref_name(refname, worktree,
1614 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1617 static const struct got_error *
1618 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1620 return get_ref_name(refname, worktree,
1621 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1624 const struct got_error *
1625 got_worktree_get_histedit_script_path(char **path,
1626 struct got_worktree *worktree)
1628 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1629 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1631 return got_error_from_errno("asprintf");
1637 * Prevent Git's garbage collector from deleting our base commit by
1638 * setting a reference to our base commit's ID.
1640 static const struct got_error *
1641 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1643 const struct got_error *err = NULL;
1644 struct got_reference *ref = NULL;
1647 err = got_worktree_get_base_ref_name(&refname, worktree);
1651 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1655 err = got_ref_write(ref, repo);
1663 static const struct got_error *
1664 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1666 const struct got_error *err = NULL;
1668 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1669 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1670 err = got_error_from_errno("asprintf");
1671 *fileindex_path = NULL;
1677 static const struct got_error *
1678 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1679 struct got_worktree *worktree)
1681 const struct got_error *err = NULL;
1684 *fileindex_path = NULL;
1685 *fileindex = got_fileindex_alloc();
1686 if (*fileindex == NULL)
1687 return got_error_from_errno("got_fileindex_alloc");
1689 err = get_fileindex_path(fileindex_path, worktree);
1693 index = fopen(*fileindex_path, "rb");
1694 if (index == NULL) {
1695 if (errno != ENOENT)
1696 err = got_error_from_errno2("fopen", *fileindex_path);
1698 err = got_fileindex_read(*fileindex, index);
1699 if (fclose(index) != 0 && err == NULL)
1700 err = got_error_from_errno("fclose");
1704 free(*fileindex_path);
1705 *fileindex_path = NULL;
1706 got_fileindex_free(*fileindex);
1712 struct bump_base_commit_id_arg {
1713 struct got_object_id *base_commit_id;
1716 const char *entry_name;
1717 got_worktree_checkout_cb progress_cb;
1721 /* Bump base commit ID of all files within an updated part of the work tree. */
1722 static const struct got_error *
1723 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1725 const struct got_error *err;
1726 struct bump_base_commit_id_arg *a = arg;
1728 if (a->entry_name) {
1729 if (strcmp(ie->path, a->path) != 0)
1731 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1734 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1735 SHA1_DIGEST_LENGTH) == 0)
1738 if (a->progress_cb) {
1739 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1744 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1748 static const struct got_error *
1749 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1751 const struct got_error *err = NULL;
1752 char *new_fileindex_path = NULL;
1753 FILE *new_index = NULL;
1754 struct timespec timeout;
1756 err = got_opentemp_named(&new_fileindex_path, &new_index,
1761 err = got_fileindex_write(fileindex, new_index);
1765 if (rename(new_fileindex_path, fileindex_path) != 0) {
1766 err = got_error_from_errno3("rename", new_fileindex_path,
1768 unlink(new_fileindex_path);
1772 * Sleep for a short amount of time to ensure that files modified after
1773 * this program exits have a different time stamp from the one which
1774 * was recorded in the file index.
1777 timeout.tv_nsec = 1;
1778 nanosleep(&timeout, NULL);
1782 free(new_fileindex_path);
1786 static const struct got_error *
1787 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1788 struct got_object_id **tree_id, const char *wt_relpath,
1789 struct got_worktree *worktree, struct got_repository *repo)
1791 const struct got_error *err = NULL;
1792 struct got_object_id *id = NULL;
1793 char *in_repo_path = NULL;
1794 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1796 *entry_type = GOT_OBJ_TYPE_ANY;
1797 *tree_relpath = NULL;
1800 if (wt_relpath[0] == '\0') {
1801 /* Check out all files within the work tree. */
1802 *entry_type = GOT_OBJ_TYPE_TREE;
1803 *tree_relpath = strdup("");
1804 if (*tree_relpath == NULL) {
1805 err = got_error_from_errno("strdup");
1808 err = got_object_id_by_path(tree_id, repo,
1809 worktree->base_commit_id, worktree->path_prefix);
1815 /* Check out a subset of files in the work tree. */
1817 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1818 is_root_wt ? "" : "/", wt_relpath) == -1) {
1819 err = got_error_from_errno("asprintf");
1823 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1829 in_repo_path = NULL;
1831 err = got_object_get_type(entry_type, repo, id);
1835 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1836 /* Check out a single file. */
1837 if (strchr(wt_relpath, '/') == NULL) {
1838 /* Check out a single file in work tree's root dir. */
1839 in_repo_path = strdup(worktree->path_prefix);
1840 if (in_repo_path == NULL) {
1841 err = got_error_from_errno("strdup");
1844 *tree_relpath = strdup("");
1845 if (*tree_relpath == NULL) {
1846 err = got_error_from_errno("strdup");
1850 /* Check out a single file in a subdirectory. */
1851 err = got_path_dirname(tree_relpath, wt_relpath);
1854 if (asprintf(&in_repo_path, "%s%s%s",
1855 worktree->path_prefix, is_root_wt ? "" : "/",
1856 *tree_relpath) == -1) {
1857 err = got_error_from_errno("asprintf");
1861 err = got_object_id_by_path(tree_id, repo,
1862 worktree->base_commit_id, in_repo_path);
1864 /* Check out all files within a subdirectory. */
1865 *tree_id = got_object_id_dup(id);
1866 if (*tree_id == NULL) {
1867 err = got_error_from_errno("got_object_id_dup");
1870 *tree_relpath = strdup(wt_relpath);
1871 if (*tree_relpath == NULL) {
1872 err = got_error_from_errno("strdup");
1880 *entry_type = GOT_OBJ_TYPE_ANY;
1881 free(*tree_relpath);
1882 *tree_relpath = NULL;
1889 static const struct got_error *
1890 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1891 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1892 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1893 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1895 const struct got_error *err = NULL;
1896 struct got_commit_object *commit = NULL;
1897 struct got_tree_object *tree = NULL;
1898 struct got_fileindex_diff_tree_cb diff_cb;
1899 struct diff_cb_arg arg;
1901 err = ref_base_commit(worktree, repo);
1903 if (!(err->code == GOT_ERR_ERRNO &&
1904 (errno == EACCES || errno == EROFS)))
1906 err = (*progress_cb)(progress_arg,
1907 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1912 err = got_object_open_as_commit(&commit, repo,
1913 worktree->base_commit_id);
1917 err = got_object_open_as_tree(&tree, repo, tree_id);
1922 got_object_tree_find_entry(tree, entry_name) == NULL) {
1923 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1927 diff_cb.diff_old_new = diff_old_new;
1928 diff_cb.diff_old = diff_old;
1929 diff_cb.diff_new = diff_new;
1930 arg.fileindex = fileindex;
1931 arg.worktree = worktree;
1933 arg.progress_cb = progress_cb;
1934 arg.progress_arg = progress_arg;
1935 arg.cancel_cb = cancel_cb;
1936 arg.cancel_arg = cancel_arg;
1937 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1938 entry_name, repo, &diff_cb, &arg);
1941 got_object_tree_close(tree);
1943 got_object_commit_close(commit);
1947 const struct got_error *
1948 got_worktree_checkout_files(struct got_worktree *worktree,
1949 struct got_pathlist_head *paths, struct got_repository *repo,
1950 got_worktree_checkout_cb progress_cb, void *progress_arg,
1951 got_cancel_cb cancel_cb, void *cancel_arg)
1953 const struct got_error *err = NULL, *sync_err, *unlockerr;
1954 struct got_commit_object *commit = NULL;
1955 struct got_tree_object *tree = NULL;
1956 struct got_fileindex *fileindex = NULL;
1957 char *fileindex_path = NULL;
1958 struct got_pathlist_entry *pe;
1959 struct tree_path_data {
1960 SIMPLEQ_ENTRY(tree_path_data) entry;
1961 struct got_object_id *tree_id;
1966 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1968 SIMPLEQ_INIT(&tree_paths);
1970 err = lock_worktree(worktree, LOCK_EX);
1974 /* Map all specified paths to in-repository trees. */
1975 TAILQ_FOREACH(pe, paths, entry) {
1976 tpd = malloc(sizeof(*tpd));
1978 err = got_error_from_errno("malloc");
1982 err = find_tree_entry_for_checkout(&tpd->entry_type,
1983 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1989 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1990 err = got_path_basename(&tpd->entry_name, pe->path);
1998 tpd->entry_name = NULL;
2000 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2004 * Read the file index.
2005 * Checking out files is supposed to be an idempotent operation.
2006 * If the on-disk file index is incomplete we will try to complete it.
2008 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2012 tpd = SIMPLEQ_FIRST(&tree_paths);
2013 TAILQ_FOREACH(pe, paths, entry) {
2014 struct bump_base_commit_id_arg bbc_arg;
2016 err = checkout_files(worktree, fileindex, tpd->relpath,
2017 tpd->tree_id, tpd->entry_name, repo,
2018 progress_cb, progress_arg, cancel_cb, cancel_arg);
2022 bbc_arg.base_commit_id = worktree->base_commit_id;
2023 bbc_arg.entry_name = tpd->entry_name;
2024 bbc_arg.path = pe->path;
2025 bbc_arg.path_len = pe->path_len;
2026 bbc_arg.progress_cb = progress_cb;
2027 bbc_arg.progress_arg = progress_arg;
2028 err = got_fileindex_for_each_entry_safe(fileindex,
2029 bump_base_commit_id, &bbc_arg);
2033 tpd = SIMPLEQ_NEXT(tpd, entry);
2035 sync_err = sync_fileindex(fileindex, fileindex_path);
2036 if (sync_err && err == NULL)
2039 free(fileindex_path);
2041 got_object_tree_close(tree);
2043 got_object_commit_close(commit);
2045 got_fileindex_free(fileindex);
2046 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2047 tpd = SIMPLEQ_FIRST(&tree_paths);
2048 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2053 unlockerr = lock_worktree(worktree, LOCK_SH);
2054 if (unlockerr && err == NULL)
2059 struct merge_file_cb_arg {
2060 struct got_worktree *worktree;
2061 struct got_fileindex *fileindex;
2062 got_worktree_checkout_cb progress_cb;
2064 got_cancel_cb cancel_cb;
2066 const char *label_orig;
2067 struct got_object_id *commit_id2;
2070 static const struct got_error *
2071 merge_file_cb(void *arg, struct got_blob_object *blob1,
2072 struct got_blob_object *blob2, struct got_object_id *id1,
2073 struct got_object_id *id2, const char *path1, const char *path2,
2074 mode_t mode1, mode_t mode2, struct got_repository *repo)
2076 static const struct got_error *err = NULL;
2077 struct merge_file_cb_arg *a = arg;
2078 struct got_fileindex_entry *ie;
2079 char *ondisk_path = NULL;
2081 unsigned char status;
2082 int local_changes_subsumed;
2084 if (blob1 && blob2) {
2085 ie = got_fileindex_entry_get(a->fileindex, path2,
2088 return (*a->progress_cb)(a->progress_arg,
2089 GOT_STATUS_MISSING, path2);
2091 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2093 return got_error_from_errno("asprintf");
2095 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2100 if (status == GOT_STATUS_DELETE) {
2101 err = (*a->progress_cb)(a->progress_arg,
2102 GOT_STATUS_MERGE, path2);
2105 if (status != GOT_STATUS_NO_CHANGE &&
2106 status != GOT_STATUS_MODIFY &&
2107 status != GOT_STATUS_CONFLICT &&
2108 status != GOT_STATUS_ADD) {
2109 err = (*a->progress_cb)(a->progress_arg, status, path2);
2113 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2114 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2115 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2117 ie = got_fileindex_entry_get(a->fileindex, path1,
2120 return (*a->progress_cb)(a->progress_arg,
2121 GOT_STATUS_MISSING, path1);
2123 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2125 return got_error_from_errno("asprintf");
2127 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2133 case GOT_STATUS_NO_CHANGE:
2134 err = (*a->progress_cb)(a->progress_arg,
2135 GOT_STATUS_DELETE, path1);
2138 err = remove_ondisk_file(a->worktree->root_path, path1);
2142 got_fileindex_entry_mark_deleted_from_disk(ie);
2144 case GOT_STATUS_DELETE:
2145 case GOT_STATUS_MISSING:
2146 err = (*a->progress_cb)(a->progress_arg,
2147 GOT_STATUS_DELETE, path1);
2151 got_fileindex_entry_mark_deleted_from_disk(ie);
2153 case GOT_STATUS_ADD:
2154 case GOT_STATUS_MODIFY:
2155 case GOT_STATUS_CONFLICT:
2156 err = (*a->progress_cb)(a->progress_arg,
2157 GOT_STATUS_CANNOT_DELETE, path1);
2161 case GOT_STATUS_OBSTRUCTED:
2162 err = (*a->progress_cb)(a->progress_arg, status, path1);
2170 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2172 return got_error_from_errno("asprintf");
2173 ie = got_fileindex_entry_get(a->fileindex, path2,
2176 err = get_file_status(&status, &sb, ie, ondisk_path,
2180 if (status != GOT_STATUS_NO_CHANGE &&
2181 status != GOT_STATUS_MODIFY &&
2182 status != GOT_STATUS_CONFLICT &&
2183 status != GOT_STATUS_ADD) {
2184 err = (*a->progress_cb)(a->progress_arg,
2188 err = merge_blob(&local_changes_subsumed, a->worktree,
2189 NULL, ondisk_path, path2, sb.st_mode,
2190 a->label_orig, blob2, a->commit_id2, repo,
2193 if (status == GOT_STATUS_DELETE) {
2194 err = update_blob_fileindex_entry(a->worktree,
2195 a->fileindex, ie, ondisk_path, ie->path,
2201 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2202 err = install_blob(a->worktree, ondisk_path, path2,
2203 /* XXX get this from parent tree! */
2204 GOT_DEFAULT_FILE_MODE,
2205 sb.st_mode, blob2, 0, 0, repo,
2206 a->progress_cb, a->progress_arg);
2209 err = got_fileindex_entry_alloc(&ie, path2);
2212 err = got_fileindex_entry_update(ie, ondisk_path,
2215 got_fileindex_entry_free(ie);
2218 err = got_fileindex_entry_add(a->fileindex, ie);
2220 got_fileindex_entry_free(ie);
2230 struct check_merge_ok_arg {
2231 struct got_worktree *worktree;
2232 struct got_repository *repo;
2235 static const struct got_error *
2236 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2238 const struct got_error *err = NULL;
2239 struct check_merge_ok_arg *a = arg;
2240 unsigned char status;
2244 /* Reject merges into a work tree with mixed base commits. */
2245 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2246 SHA1_DIGEST_LENGTH))
2247 return got_error(GOT_ERR_MIXED_COMMITS);
2249 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2251 return got_error_from_errno("asprintf");
2253 /* Reject merges into a work tree with conflicted files. */
2254 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2257 if (status == GOT_STATUS_CONFLICT)
2258 return got_error(GOT_ERR_CONFLICTS);
2263 static const struct got_error *
2264 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2265 const char *fileindex_path, struct got_object_id *commit_id1,
2266 struct got_object_id *commit_id2, struct got_repository *repo,
2267 got_worktree_checkout_cb progress_cb, void *progress_arg,
2268 got_cancel_cb cancel_cb, void *cancel_arg)
2270 const struct got_error *err = NULL, *sync_err;
2271 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2272 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2273 struct merge_file_cb_arg arg;
2274 char *label_orig = NULL;
2279 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2280 worktree->path_prefix);
2284 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2288 err = got_object_id_str(&id_str, commit_id1);
2292 if (asprintf(&label_orig, "%s: commit %s",
2293 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2294 err = got_error_from_errno("asprintf");
2301 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2302 worktree->path_prefix);
2306 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2310 arg.worktree = worktree;
2311 arg.fileindex = fileindex;
2312 arg.progress_cb = progress_cb;
2313 arg.progress_arg = progress_arg;
2314 arg.cancel_cb = cancel_cb;
2315 arg.cancel_arg = cancel_arg;
2316 arg.label_orig = label_orig;
2317 arg.commit_id2 = commit_id2;
2318 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2319 sync_err = sync_fileindex(fileindex, fileindex_path);
2320 if (sync_err && err == NULL)
2324 got_object_tree_close(tree1);
2326 got_object_tree_close(tree2);
2331 const struct got_error *
2332 got_worktree_merge_files(struct got_worktree *worktree,
2333 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2334 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2335 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2337 const struct got_error *err, *unlockerr;
2338 char *fileindex_path = NULL;
2339 struct got_fileindex *fileindex = NULL;
2340 struct check_merge_ok_arg mok_arg;
2342 err = lock_worktree(worktree, LOCK_EX);
2346 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2350 mok_arg.worktree = worktree;
2351 mok_arg.repo = repo;
2352 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2357 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2358 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2361 got_fileindex_free(fileindex);
2362 free(fileindex_path);
2363 unlockerr = lock_worktree(worktree, LOCK_SH);
2364 if (unlockerr && err == NULL)
2369 struct diff_dir_cb_arg {
2370 struct got_fileindex *fileindex;
2371 struct got_worktree *worktree;
2372 const char *status_path;
2373 size_t status_path_len;
2374 struct got_repository *repo;
2375 got_worktree_status_cb status_cb;
2377 got_cancel_cb cancel_cb;
2379 /* A pathlist containing per-directory pathlists of ignore patterns. */
2380 struct got_pathlist_head ignores;
2381 int report_unchanged;
2384 static const struct got_error *
2385 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2386 int dirfd, const char *de_name,
2387 got_worktree_status_cb status_cb, void *status_arg,
2388 struct got_repository *repo, int report_unchanged)
2390 const struct got_error *err = NULL;
2391 unsigned char status = GOT_STATUS_NO_CHANGE;
2392 unsigned char staged_status = get_staged_status(ie);
2394 struct got_object_id blob_id, commit_id, staged_blob_id;
2395 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2396 struct got_object_id *staged_blob_idp = NULL;
2398 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2402 if (status == GOT_STATUS_NO_CHANGE &&
2403 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2406 if (got_fileindex_entry_has_blob(ie)) {
2407 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2408 blob_idp = &blob_id;
2410 if (got_fileindex_entry_has_commit(ie)) {
2411 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2412 commit_idp = &commit_id;
2414 if (staged_status == GOT_STATUS_ADD ||
2415 staged_status == GOT_STATUS_MODIFY) {
2416 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2417 SHA1_DIGEST_LENGTH);
2418 staged_blob_idp = &staged_blob_id;
2421 return (*status_cb)(status_arg, status, staged_status,
2422 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2425 static const struct got_error *
2426 status_old_new(void *arg, struct got_fileindex_entry *ie,
2427 struct dirent *de, const char *parent_path, int dirfd)
2429 const struct got_error *err = NULL;
2430 struct diff_dir_cb_arg *a = arg;
2433 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2434 return got_error(GOT_ERR_CANCELLED);
2436 if (got_path_cmp(parent_path, a->status_path,
2437 strlen(parent_path), a->status_path_len) != 0 &&
2438 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2441 if (parent_path[0]) {
2442 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2443 parent_path, de->d_name) == -1)
2444 return got_error_from_errno("asprintf");
2446 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2448 return got_error_from_errno("asprintf");
2451 err = report_file_status(ie, abspath, dirfd, de->d_name,
2452 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2457 static const struct got_error *
2458 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2460 struct diff_dir_cb_arg *a = arg;
2461 struct got_object_id blob_id, commit_id;
2462 unsigned char status;
2464 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2465 return got_error(GOT_ERR_CANCELLED);
2467 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2470 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2471 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2472 if (got_fileindex_entry_has_file_on_disk(ie))
2473 status = GOT_STATUS_MISSING;
2475 status = GOT_STATUS_DELETE;
2476 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2477 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2481 free_ignorelist(struct got_pathlist_head *ignorelist)
2483 struct got_pathlist_entry *pe;
2485 TAILQ_FOREACH(pe, ignorelist, entry)
2486 free((char *)pe->path);
2487 got_pathlist_free(ignorelist);
2491 free_ignores(struct got_pathlist_head *ignores)
2493 struct got_pathlist_entry *pe;
2495 TAILQ_FOREACH(pe, ignores, entry) {
2496 struct got_pathlist_head *ignorelist = pe->data;
2497 free_ignorelist(ignorelist);
2498 free((char *)pe->path);
2500 got_pathlist_free(ignores);
2503 static const struct got_error *
2504 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2506 const struct got_error *err = NULL;
2507 struct got_pathlist_entry *pe = NULL;
2508 struct got_pathlist_head *ignorelist;
2509 char *line = NULL, *pattern, *dirpath = NULL;
2510 size_t linesize = 0;
2513 ignorelist = calloc(1, sizeof(*ignorelist));
2514 if (ignorelist == NULL)
2515 return got_error_from_errno("calloc");
2516 TAILQ_INIT(ignorelist);
2518 while ((linelen = getline(&line, &linesize, f)) != -1) {
2519 if (linelen > 0 && line[linelen - 1] == '\n')
2520 line[linelen - 1] = '\0';
2522 /* Git's ignores may contain comments. */
2526 /* Git's negated patterns are not (yet?) supported. */
2530 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2532 err = got_error_from_errno("asprintf");
2535 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2540 err = got_error_from_errno("getline");
2544 dirpath = strdup(path);
2545 if (dirpath == NULL) {
2546 err = got_error_from_errno("strdup");
2549 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2552 if (err || pe == NULL) {
2554 free_ignorelist(ignorelist);
2560 match_ignores(struct got_pathlist_head *ignores, const char *path)
2562 struct got_pathlist_entry *pe;
2564 /* Handle patterns which match in all directories. */
2565 TAILQ_FOREACH(pe, ignores, entry) {
2566 struct got_pathlist_head *ignorelist = pe->data;
2567 struct got_pathlist_entry *pi;
2569 TAILQ_FOREACH(pi, ignorelist, entry) {
2570 const char *p, *pattern = pi->path;
2572 if (strncmp(pattern, "**/", 3) != 0)
2577 if (fnmatch(pattern, p,
2578 FNM_PATHNAME | FNM_LEADING_DIR)) {
2579 /* Retry in next directory. */
2580 while (*p && *p != '/')
2592 * The ignores pathlist contains ignore lists from children before
2593 * parents, so we can find the most specific ignorelist by walking
2594 * ignores backwards.
2596 pe = TAILQ_LAST(ignores, got_pathlist_head);
2598 if (got_path_is_child(path, pe->path, pe->path_len)) {
2599 struct got_pathlist_head *ignorelist = pe->data;
2600 struct got_pathlist_entry *pi;
2601 TAILQ_FOREACH(pi, ignorelist, entry) {
2602 const char *pattern = pi->path;
2603 int flags = FNM_LEADING_DIR;
2604 if (strstr(pattern, "/**/") == NULL)
2605 flags |= FNM_PATHNAME;
2606 if (fnmatch(pattern, path, flags))
2611 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2617 static const struct got_error *
2618 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2619 const char *path, int dirfd, const char *ignores_filename)
2621 const struct got_error *err = NULL;
2624 FILE *ignoresfile = NULL;
2626 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2627 path[0] ? "/" : "", ignores_filename) == -1)
2628 return got_error_from_errno("asprintf");
2631 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2633 if (errno != ENOENT && errno != EACCES)
2634 err = got_error_from_errno2("openat",
2637 ignoresfile = fdopen(fd, "r");
2638 if (ignoresfile == NULL)
2639 err = got_error_from_errno2("fdopen",
2643 err = read_ignores(ignores, path, ignoresfile);
2647 ignoresfile = fopen(ignorespath, "r");
2648 if (ignoresfile == NULL) {
2649 if (errno != ENOENT && errno != EACCES)
2650 err = got_error_from_errno2("fopen",
2653 err = read_ignores(ignores, path, ignoresfile);
2656 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2657 err = got_error_from_errno2("fclose", path);
2658 if (fd != -1 && close(fd) == -1 && err == NULL)
2659 err = got_error_from_errno2("close", path);
2664 static const struct got_error *
2665 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2667 const struct got_error *err = NULL;
2668 struct diff_dir_cb_arg *a = arg;
2671 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2672 return got_error(GOT_ERR_CANCELLED);
2674 /* XXX ignore symlinks for now */
2675 if (de->d_type == DT_LNK)
2678 if (parent_path[0]) {
2679 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2680 return got_error_from_errno("asprintf");
2685 if (de->d_type == DT_DIR) {
2686 int subdirfd = openat(dirfd, de->d_name,
2687 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2688 if (subdirfd == -1) {
2689 if (errno != ENOENT && errno != EACCES)
2690 err = got_error_from_errno2("openat", path);
2692 err = add_ignores(&a->ignores, a->worktree->root_path,
2693 path, subdirfd, ".cvsignore");
2695 err = add_ignores(&a->ignores,
2696 a->worktree->root_path, path,
2697 subdirfd, ".gitignore");
2698 if (close(subdirfd) == -1 && err == NULL)
2699 err = got_error_from_errno2("close", path);
2701 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2702 && !match_ignores(&a->ignores, path))
2703 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2704 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2710 static const struct got_error *
2711 report_single_file_status(const char *path, const char *ondisk_path,
2712 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2713 void *status_arg, struct got_repository *repo, int report_unchanged)
2715 struct got_fileindex_entry *ie;
2718 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2720 return report_file_status(ie, ondisk_path, -1, NULL,
2721 status_cb, status_arg, repo, report_unchanged);
2723 if (lstat(ondisk_path, &sb) == -1) {
2724 if (errno != ENOENT)
2725 return got_error_from_errno2("lstat", ondisk_path);
2726 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2727 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2731 if (S_ISREG(sb.st_mode))
2732 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2733 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2738 static const struct got_error *
2739 worktree_status(struct got_worktree *worktree, const char *path,
2740 struct got_fileindex *fileindex, struct got_repository *repo,
2741 got_worktree_status_cb status_cb, void *status_arg,
2742 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2743 int report_unchanged)
2745 const struct got_error *err = NULL;
2747 struct got_fileindex_diff_dir_cb fdiff_cb;
2748 struct diff_dir_cb_arg arg;
2749 char *ondisk_path = NULL;
2751 if (asprintf(&ondisk_path, "%s%s%s",
2752 worktree->root_path, path[0] ? "/" : "", path) == -1)
2753 return got_error_from_errno("asprintf");
2755 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2757 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2758 err = got_error_from_errno2("open", ondisk_path);
2760 err = report_single_file_status(path, ondisk_path,
2761 fileindex, status_cb, status_arg, repo,
2764 fdiff_cb.diff_old_new = status_old_new;
2765 fdiff_cb.diff_old = status_old;
2766 fdiff_cb.diff_new = status_new;
2767 arg.fileindex = fileindex;
2768 arg.worktree = worktree;
2769 arg.status_path = path;
2770 arg.status_path_len = strlen(path);
2772 arg.status_cb = status_cb;
2773 arg.status_arg = status_arg;
2774 arg.cancel_cb = cancel_cb;
2775 arg.cancel_arg = cancel_arg;
2776 arg.report_unchanged = report_unchanged;
2777 TAILQ_INIT(&arg.ignores);
2779 err = add_ignores(&arg.ignores, worktree->root_path,
2780 path, fd, ".cvsignore");
2782 err = add_ignores(&arg.ignores,
2783 worktree->root_path, path, fd,
2787 err = got_fileindex_diff_dir(fileindex, fd,
2788 worktree->root_path, path, repo, &fdiff_cb, &arg);
2789 free_ignores(&arg.ignores);
2792 if (fd != -1 && close(fd) != 0 && err == NULL)
2793 err = got_error_from_errno("close");
2798 const struct got_error *
2799 got_worktree_status(struct got_worktree *worktree,
2800 struct got_pathlist_head *paths, struct got_repository *repo,
2801 got_worktree_status_cb status_cb, void *status_arg,
2802 got_cancel_cb cancel_cb, void *cancel_arg)
2804 const struct got_error *err = NULL;
2805 char *fileindex_path = NULL;
2806 struct got_fileindex *fileindex = NULL;
2807 struct got_pathlist_entry *pe;
2809 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2813 TAILQ_FOREACH(pe, paths, entry) {
2814 err = worktree_status(worktree, pe->path, fileindex, repo,
2815 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2819 free(fileindex_path);
2820 got_fileindex_free(fileindex);
2824 const struct got_error *
2825 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2828 const struct got_error *err = NULL;
2829 char *resolved, *cwd = NULL, *path = NULL;
2834 resolved = realpath(arg, NULL);
2835 if (resolved == NULL) {
2836 if (errno != ENOENT)
2837 return got_error_from_errno2("realpath", arg);
2838 cwd = getcwd(NULL, 0);
2840 return got_error_from_errno("getcwd");
2841 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2842 err = got_error_from_errno("asprintf");
2847 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2848 strlen(got_worktree_get_root_path(worktree)))) {
2849 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
2853 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2854 err = got_path_skip_common_ancestor(&path,
2855 got_worktree_get_root_path(worktree), resolved);
2861 err = got_error_from_errno("strdup");
2866 /* XXX status walk can't deal with trailing slash! */
2868 while (len > 0 && path[len - 1] == '/') {
2869 path[len - 1] = '\0';
2882 struct schedule_addition_args {
2883 struct got_worktree *worktree;
2884 struct got_fileindex *fileindex;
2885 got_worktree_checkout_cb progress_cb;
2887 struct got_repository *repo;
2890 static const struct got_error *
2891 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2892 const char *relpath, struct got_object_id *blob_id,
2893 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2894 int dirfd, const char *de_name)
2896 struct schedule_addition_args *a = arg;
2897 const struct got_error *err = NULL;
2898 struct got_fileindex_entry *ie;
2902 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2904 return got_error_from_errno("asprintf");
2906 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2908 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2912 /* Re-adding an existing entry is a no-op. */
2913 if (status == GOT_STATUS_ADD)
2915 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2920 if (status != GOT_STATUS_UNVERSIONED) {
2921 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2925 err = got_fileindex_entry_alloc(&ie, relpath);
2928 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
2930 got_fileindex_entry_free(ie);
2933 err = got_fileindex_entry_add(a->fileindex, ie);
2935 got_fileindex_entry_free(ie);
2942 if (status == GOT_STATUS_ADD)
2944 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2947 const struct got_error *
2948 got_worktree_schedule_add(struct got_worktree *worktree,
2949 struct got_pathlist_head *paths,
2950 got_worktree_checkout_cb progress_cb, void *progress_arg,
2951 struct got_repository *repo, int no_ignores)
2953 struct got_fileindex *fileindex = NULL;
2954 char *fileindex_path = NULL;
2955 const struct got_error *err = NULL, *sync_err, *unlockerr;
2956 struct got_pathlist_entry *pe;
2957 struct schedule_addition_args saa;
2959 err = lock_worktree(worktree, LOCK_EX);
2963 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2967 saa.worktree = worktree;
2968 saa.fileindex = fileindex;
2969 saa.progress_cb = progress_cb;
2970 saa.progress_arg = progress_arg;
2973 TAILQ_FOREACH(pe, paths, entry) {
2974 err = worktree_status(worktree, pe->path, fileindex, repo,
2975 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2979 sync_err = sync_fileindex(fileindex, fileindex_path);
2980 if (sync_err && err == NULL)
2983 free(fileindex_path);
2985 got_fileindex_free(fileindex);
2986 unlockerr = lock_worktree(worktree, LOCK_SH);
2987 if (unlockerr && err == NULL)
2992 struct schedule_deletion_args {
2993 struct got_worktree *worktree;
2994 struct got_fileindex *fileindex;
2995 got_worktree_delete_cb progress_cb;
2997 struct got_repository *repo;
2998 int delete_local_mods;
3002 static const struct got_error *
3003 schedule_for_deletion(void *arg, unsigned char status,
3004 unsigned char staged_status, const char *relpath,
3005 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3006 struct got_object_id *commit_id, int dirfd, const char *de_name)
3008 struct schedule_deletion_args *a = arg;
3009 const struct got_error *err = NULL;
3010 struct got_fileindex_entry *ie = NULL;
3012 char *ondisk_path, *parent = NULL;
3014 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3016 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3018 staged_status = get_staged_status(ie);
3019 if (staged_status != GOT_STATUS_NO_CHANGE) {
3020 if (staged_status == GOT_STATUS_DELETE)
3022 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3025 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3027 return got_error_from_errno("asprintf");
3029 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3034 if (status != GOT_STATUS_NO_CHANGE) {
3035 if (status == GOT_STATUS_DELETE)
3037 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3038 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3041 if (status != GOT_STATUS_MODIFY &&
3042 status != GOT_STATUS_MISSING) {
3043 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3048 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3050 if (unlinkat(dirfd, de_name, 0) != 0) {
3051 err = got_error_from_errno2("unlinkat",
3055 } else if (unlink(ondisk_path) != 0) {
3056 err = got_error_from_errno2("unlink", ondisk_path);
3060 parent = dirname(ondisk_path);
3062 if (parent == NULL) {
3063 err = got_error_from_errno2("dirname", ondisk_path);
3066 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3067 if (rmdir(parent) == -1) {
3068 if (errno != ENOTEMPTY)
3069 err = got_error_from_errno2("rmdir",
3073 parent = dirname(parent);
3074 if (parent == NULL) {
3075 err = got_error_from_errno2("dirname", parent);
3081 got_fileindex_entry_mark_deleted_from_disk(ie);
3086 if (status == GOT_STATUS_DELETE)
3088 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3089 staged_status, relpath);
3092 const struct got_error *
3093 got_worktree_schedule_delete(struct got_worktree *worktree,
3094 struct got_pathlist_head *paths, int delete_local_mods,
3095 got_worktree_delete_cb progress_cb, void *progress_arg,
3096 struct got_repository *repo, int keep_on_disk)
3098 struct got_fileindex *fileindex = NULL;
3099 char *fileindex_path = NULL;
3100 const struct got_error *err = NULL, *sync_err, *unlockerr;
3101 struct got_pathlist_entry *pe;
3102 struct schedule_deletion_args sda;
3104 err = lock_worktree(worktree, LOCK_EX);
3108 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3112 sda.worktree = worktree;
3113 sda.fileindex = fileindex;
3114 sda.progress_cb = progress_cb;
3115 sda.progress_arg = progress_arg;
3117 sda.delete_local_mods = delete_local_mods;
3118 sda.keep_on_disk = keep_on_disk;
3120 TAILQ_FOREACH(pe, paths, entry) {
3121 err = worktree_status(worktree, pe->path, fileindex, repo,
3122 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3126 sync_err = sync_fileindex(fileindex, fileindex_path);
3127 if (sync_err && err == NULL)
3130 free(fileindex_path);
3132 got_fileindex_free(fileindex);
3133 unlockerr = lock_worktree(worktree, LOCK_SH);
3134 if (unlockerr && err == NULL)
3139 static const struct got_error *
3140 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3142 const struct got_error *err = NULL;
3144 size_t linesize = 0, n;
3147 linelen = getline(&line, &linesize, infile);
3148 if (linelen == -1) {
3149 if (ferror(infile)) {
3150 err = got_error_from_errno("getline");
3156 n = fwrite(line, 1, linelen, outfile);
3158 err = got_ferror(outfile, GOT_ERR_IO);
3163 n = fwrite(line, 1, linelen, rejectfile);
3165 err = got_ferror(outfile, GOT_ERR_IO);
3172 static const struct got_error *
3173 skip_one_line(FILE *f)
3176 size_t linesize = 0;
3179 linelen = getline(&line, &linesize, f);
3180 if (linelen == -1) {
3182 return got_error_from_errno("getline");
3189 static const struct got_error *
3190 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3191 int start_old, int end_old, int start_new, int end_new,
3192 FILE *outfile, FILE *rejectfile)
3194 const struct got_error *err;
3196 /* Copy old file's lines leading up to patch. */
3197 while (!feof(f1) && *line_cur1 < start_old) {
3198 err = copy_one_line(f1, outfile, NULL);
3203 /* Skip new file's lines leading up to patch. */
3204 while (!feof(f2) && *line_cur2 < start_new) {
3206 err = copy_one_line(f2, NULL, rejectfile);
3208 err = skip_one_line(f2);
3213 /* Copy patched lines. */
3214 while (!feof(f2) && *line_cur2 <= end_new) {
3215 err = copy_one_line(f2, outfile, NULL);
3220 /* Skip over old file's replaced lines. */
3221 while (!feof(f1) && *line_cur1 <= end_old) {
3223 err = copy_one_line(f1, NULL, rejectfile);
3225 err = skip_one_line(f1);
3234 static const struct got_error *
3235 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3236 FILE *outfile, FILE *rejectfile)
3238 const struct got_error *err;
3241 /* Copy old file's lines until EOF. */
3243 err = copy_one_line(f1, outfile, NULL);
3250 /* Copy new file's lines until EOF. */
3252 err = copy_one_line(f2, NULL, rejectfile);
3262 static const struct got_error *
3263 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3264 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3265 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3266 int *line_cur2, FILE *outfile, FILE *rejectfile,
3267 got_worktree_patch_cb patch_cb, void *patch_arg)
3269 const struct got_error *err = NULL;
3270 int start_old = change->cv.a;
3271 int end_old = change->cv.b;
3272 int start_new = change->cv.c;
3273 int end_new = change->cv.d;
3277 *choice = GOT_PATCH_CHOICE_NONE;
3279 hunkfile = got_opentemp();
3280 if (hunkfile == NULL)
3281 return got_error_from_errno("got_opentemp");
3286 /* XXX TODO needs error checking */
3287 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3289 if (fseek(f1, pos1, SEEK_SET) == -1) {
3290 err = got_ferror(f1, GOT_ERR_IO);
3293 if (fseek(f2, pos2, SEEK_SET) == -1) {
3294 err = got_ferror(f1, GOT_ERR_IO);
3297 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3298 err = got_ferror(hunkfile, GOT_ERR_IO);
3302 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3303 hunkfile, n, nchanges);
3308 case GOT_PATCH_CHOICE_YES:
3309 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3310 end_old, start_new, end_new, outfile, rejectfile);
3312 case GOT_PATCH_CHOICE_NO:
3313 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3314 end_old, start_new, end_new, rejectfile, outfile);
3316 case GOT_PATCH_CHOICE_QUIT:
3319 err = got_error(GOT_ERR_PATCH_CHOICE);
3323 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3324 err = got_error_from_errno("fclose");
3328 struct revert_file_args {
3329 struct got_worktree *worktree;
3330 struct got_fileindex *fileindex;
3331 got_worktree_checkout_cb progress_cb;
3333 got_worktree_patch_cb patch_cb;
3335 struct got_repository *repo;
3338 static const struct got_error *
3339 create_patched_content(char **path_outfile, int reverse_patch,
3340 struct got_object_id *blob_id, const char *path2,
3341 int dirfd2, const char *de_name2,
3342 const char *relpath, struct got_repository *repo,
3343 got_worktree_patch_cb patch_cb, void *patch_arg)
3345 const struct got_error *err;
3346 struct got_blob_object *blob = NULL;
3347 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3349 char *path1 = NULL, *id_str = NULL;
3350 struct stat sb1, sb2;
3351 struct got_diff_changes *changes = NULL;
3352 struct got_diff_state *ds = NULL;
3353 struct got_diff_args *args = NULL;
3354 struct got_diff_change *change;
3355 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3358 *path_outfile = NULL;
3360 err = got_object_id_str(&id_str, blob_id);
3365 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3367 err = got_error_from_errno2("openat", path2);
3371 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3373 err = got_error_from_errno2("open", path2);
3377 if (fstat(fd2, &sb2) == -1) {
3378 err = got_error_from_errno2("fstat", path2);
3382 f2 = fdopen(fd2, "r");
3384 err = got_error_from_errno2("fdopen", path2);
3389 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3393 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3397 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3401 if (stat(path1, &sb1) == -1) {
3402 err = got_error_from_errno2("stat", path1);
3406 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3407 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3411 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3415 if (fseek(f1, 0L, SEEK_SET) == -1)
3416 return got_ferror(f1, GOT_ERR_IO);
3417 if (fseek(f2, 0L, SEEK_SET) == -1)
3418 return got_ferror(f2, GOT_ERR_IO);
3419 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3421 err = apply_or_reject_change(&choice, change, ++n,
3422 changes->nchanges, ds, args, diff_flags, relpath,
3423 f1, f2, &line_cur1, &line_cur2,
3424 reverse_patch ? NULL : outfile,
3425 reverse_patch ? outfile : NULL,
3426 patch_cb, patch_arg);
3429 if (choice == GOT_PATCH_CHOICE_YES)
3431 else if (choice == GOT_PATCH_CHOICE_QUIT)
3435 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3436 reverse_patch ? NULL : outfile,
3437 reverse_patch ? outfile : NULL);
3441 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3442 err = got_error_from_errno2("chmod", path2);
3449 got_object_blob_close(blob);
3450 if (f1 && fclose(f1) == EOF && err == NULL)
3451 err = got_error_from_errno2("fclose", path1);
3452 if (f2 && fclose(f2) == EOF && err == NULL)
3453 err = got_error_from_errno2("fclose", path2);
3454 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3455 err = got_error_from_errno2("close", path2);
3456 if (outfile && fclose(outfile) == EOF && err == NULL)
3457 err = got_error_from_errno2("fclose", *path_outfile);
3458 if (path1 && unlink(path1) == -1 && err == NULL)
3459 err = got_error_from_errno2("unlink", path1);
3460 if (err || !have_content) {
3461 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3462 err = got_error_from_errno2("unlink", *path_outfile);
3463 free(*path_outfile);
3464 *path_outfile = NULL;
3468 got_diff_state_free(ds);
3472 got_diff_free_changes(changes);
3477 static const struct got_error *
3478 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3479 const char *relpath, struct got_object_id *blob_id,
3480 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3481 int dirfd, const char *de_name)
3483 struct revert_file_args *a = arg;
3484 const struct got_error *err = NULL;
3485 char *parent_path = NULL;
3486 struct got_fileindex_entry *ie;
3487 struct got_tree_object *tree = NULL;
3488 struct got_object_id *tree_id = NULL;
3489 const struct got_tree_entry *te = NULL;
3490 char *tree_path = NULL, *te_name;
3491 char *ondisk_path = NULL, *path_content = NULL;
3492 struct got_blob_object *blob = NULL;
3494 /* Reverting a staged deletion is a no-op. */
3495 if (status == GOT_STATUS_DELETE &&
3496 staged_status != GOT_STATUS_NO_CHANGE)
3499 if (status == GOT_STATUS_UNVERSIONED)
3500 return (*a->progress_cb)(a->progress_arg,
3501 GOT_STATUS_UNVERSIONED, relpath);
3503 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3505 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3507 /* Construct in-repository path of tree which contains this blob. */
3508 err = got_path_dirname(&parent_path, ie->path);
3510 if (err->code != GOT_ERR_BAD_PATH)
3512 parent_path = strdup("/");
3513 if (parent_path == NULL) {
3514 err = got_error_from_errno("strdup");
3518 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3519 tree_path = strdup(parent_path);
3520 if (tree_path == NULL) {
3521 err = got_error_from_errno("strdup");
3525 if (got_path_is_root_dir(parent_path)) {
3526 tree_path = strdup(a->worktree->path_prefix);
3527 if (tree_path == NULL) {
3528 err = got_error_from_errno("strdup");
3532 if (asprintf(&tree_path, "%s/%s",
3533 a->worktree->path_prefix, parent_path) == -1) {
3534 err = got_error_from_errno("asprintf");
3540 err = got_object_id_by_path(&tree_id, a->repo,
3541 a->worktree->base_commit_id, tree_path);
3543 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3544 (status == GOT_STATUS_ADD ||
3545 staged_status == GOT_STATUS_ADD)))
3548 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3552 te_name = basename(ie->path);
3553 if (te_name == NULL) {
3554 err = got_error_from_errno2("basename", ie->path);
3558 te = got_object_tree_find_entry(tree, te_name);
3559 if (te == NULL && status != GOT_STATUS_ADD &&
3560 staged_status != GOT_STATUS_ADD) {
3561 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3567 case GOT_STATUS_ADD:
3569 int choice = GOT_PATCH_CHOICE_NONE;
3570 err = (*a->patch_cb)(&choice, a->patch_arg,
3571 status, ie->path, NULL, 1, 1);
3574 if (choice != GOT_PATCH_CHOICE_YES)
3577 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3581 got_fileindex_entry_remove(a->fileindex, ie);
3583 case GOT_STATUS_DELETE:
3585 int choice = GOT_PATCH_CHOICE_NONE;
3586 err = (*a->patch_cb)(&choice, a->patch_arg,
3587 status, ie->path, NULL, 1, 1);
3590 if (choice != GOT_PATCH_CHOICE_YES)
3594 case GOT_STATUS_MODIFY:
3595 case GOT_STATUS_MODE_CHANGE:
3596 case GOT_STATUS_CONFLICT:
3597 case GOT_STATUS_MISSING: {
3598 struct got_object_id id;
3599 if (staged_status == GOT_STATUS_ADD ||
3600 staged_status == GOT_STATUS_MODIFY) {
3601 memcpy(id.sha1, ie->staged_blob_sha1,
3602 SHA1_DIGEST_LENGTH);
3604 memcpy(id.sha1, ie->blob_sha1,
3605 SHA1_DIGEST_LENGTH);
3606 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3610 if (asprintf(&ondisk_path, "%s/%s",
3611 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3612 err = got_error_from_errno("asprintf");
3616 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3617 status == GOT_STATUS_CONFLICT)) {
3618 err = create_patched_content(&path_content, 1, &id,
3619 ondisk_path, dirfd, de_name, ie->path, a->repo,
3620 a->patch_cb, a->patch_arg);
3621 if (err || path_content == NULL)
3623 if (rename(path_content, ondisk_path) == -1) {
3624 err = got_error_from_errno3("rename",
3625 path_content, ondisk_path);
3629 err = install_blob(a->worktree, ondisk_path, ie->path,
3630 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3631 got_fileindex_perms_to_st(ie), blob, 0, 1,
3632 a->repo, a->progress_cb, a->progress_arg);
3635 if (status == GOT_STATUS_DELETE ||
3636 status == GOT_STATUS_MODE_CHANGE) {
3637 err = update_blob_fileindex_entry(a->worktree,
3638 a->fileindex, ie, ondisk_path, ie->path,
3655 got_object_blob_close(blob);
3657 got_object_tree_close(tree);
3662 const struct got_error *
3663 got_worktree_revert(struct got_worktree *worktree,
3664 struct got_pathlist_head *paths,
3665 got_worktree_checkout_cb progress_cb, void *progress_arg,
3666 got_worktree_patch_cb patch_cb, void *patch_arg,
3667 struct got_repository *repo)
3669 struct got_fileindex *fileindex = NULL;
3670 char *fileindex_path = NULL;
3671 const struct got_error *err = NULL, *unlockerr = NULL;
3672 const struct got_error *sync_err = NULL;
3673 struct got_pathlist_entry *pe;
3674 struct revert_file_args rfa;
3676 err = lock_worktree(worktree, LOCK_EX);
3680 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3684 rfa.worktree = worktree;
3685 rfa.fileindex = fileindex;
3686 rfa.progress_cb = progress_cb;
3687 rfa.progress_arg = progress_arg;
3688 rfa.patch_cb = patch_cb;
3689 rfa.patch_arg = patch_arg;
3691 TAILQ_FOREACH(pe, paths, entry) {
3692 err = worktree_status(worktree, pe->path, fileindex, repo,
3693 revert_file, &rfa, NULL, NULL, 0, 0);
3697 sync_err = sync_fileindex(fileindex, fileindex_path);
3698 if (sync_err && err == NULL)
3701 free(fileindex_path);
3703 got_fileindex_free(fileindex);
3704 unlockerr = lock_worktree(worktree, LOCK_SH);
3705 if (unlockerr && err == NULL)
3711 free_commitable(struct got_commitable *ct)
3714 free(ct->in_repo_path);
3715 free(ct->ondisk_path);
3717 free(ct->base_blob_id);
3718 free(ct->staged_blob_id);
3719 free(ct->base_commit_id);
3723 struct collect_commitables_arg {
3724 struct got_pathlist_head *commitable_paths;
3725 struct got_repository *repo;
3726 struct got_worktree *worktree;
3727 int have_staged_files;
3730 static const struct got_error *
3731 collect_commitables(void *arg, unsigned char status,
3732 unsigned char staged_status, const char *relpath,
3733 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3734 struct got_object_id *commit_id, int dirfd, const char *de_name)
3736 struct collect_commitables_arg *a = arg;
3737 const struct got_error *err = NULL;
3738 struct got_commitable *ct = NULL;
3739 struct got_pathlist_entry *new = NULL;
3740 char *parent_path = NULL, *path = NULL;
3743 if (a->have_staged_files) {
3744 if (staged_status != GOT_STATUS_MODIFY &&
3745 staged_status != GOT_STATUS_ADD &&
3746 staged_status != GOT_STATUS_DELETE)
3749 if (status == GOT_STATUS_CONFLICT)
3750 return got_error(GOT_ERR_COMMIT_CONFLICT);
3752 if (status != GOT_STATUS_MODIFY &&
3753 status != GOT_STATUS_MODE_CHANGE &&
3754 status != GOT_STATUS_ADD &&
3755 status != GOT_STATUS_DELETE)
3759 if (asprintf(&path, "/%s", relpath) == -1) {
3760 err = got_error_from_errno("asprintf");
3763 if (strcmp(path, "/") == 0) {
3764 parent_path = strdup("");
3765 if (parent_path == NULL)
3766 return got_error_from_errno("strdup");
3768 err = got_path_dirname(&parent_path, path);
3773 ct = calloc(1, sizeof(*ct));
3775 err = got_error_from_errno("calloc");
3779 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3781 err = got_error_from_errno("asprintf");
3784 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3785 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3788 if (fstatat(dirfd, de_name, &sb,
3789 AT_SYMLINK_NOFOLLOW) == -1) {
3790 err = got_error_from_errno2("fstatat",
3794 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3795 err = got_error_from_errno2("lstat", ct->ondisk_path);
3798 ct->mode = sb.st_mode;
3801 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3802 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3804 err = got_error_from_errno("asprintf");
3808 ct->status = status;
3809 ct->staged_status = staged_status;
3810 ct->blob_id = NULL; /* will be filled in when blob gets created */
3811 if (ct->status != GOT_STATUS_ADD &&
3812 ct->staged_status != GOT_STATUS_ADD) {
3813 ct->base_blob_id = got_object_id_dup(blob_id);
3814 if (ct->base_blob_id == NULL) {
3815 err = got_error_from_errno("got_object_id_dup");
3818 ct->base_commit_id = got_object_id_dup(commit_id);
3819 if (ct->base_commit_id == NULL) {
3820 err = got_error_from_errno("got_object_id_dup");
3824 if (ct->staged_status == GOT_STATUS_ADD ||
3825 ct->staged_status == GOT_STATUS_MODIFY) {
3826 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3827 if (ct->staged_blob_id == NULL) {
3828 err = got_error_from_errno("got_object_id_dup");
3832 ct->path = strdup(path);
3833 if (ct->path == NULL) {
3834 err = got_error_from_errno("strdup");
3837 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3839 if (ct && (err || new == NULL))
3840 free_commitable(ct);
3846 static const struct got_error *write_tree(struct got_object_id **,
3847 struct got_tree_object *, const char *, struct got_pathlist_head *,
3848 got_worktree_status_cb status_cb, void *status_arg,
3849 struct got_repository *);
3851 static const struct got_error *
3852 write_subtree(struct got_object_id **new_subtree_id,
3853 struct got_tree_entry *te, const char *parent_path,
3854 struct got_pathlist_head *commitable_paths,
3855 got_worktree_status_cb status_cb, void *status_arg,
3856 struct got_repository *repo)
3858 const struct got_error *err = NULL;
3859 struct got_tree_object *subtree;
3862 if (asprintf(&subpath, "%s%s%s", parent_path,
3863 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3864 return got_error_from_errno("asprintf");
3866 err = got_object_open_as_tree(&subtree, repo, &te->id);
3870 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3871 status_cb, status_arg, repo);
3872 got_object_tree_close(subtree);
3877 static const struct got_error *
3878 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3880 const struct got_error *err = NULL;
3881 char *ct_parent_path = NULL;
3885 if (strchr(ct->in_repo_path, '/') == NULL) {
3886 *match = got_path_is_root_dir(path);
3890 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3893 *match = (strcmp(path, ct_parent_path) == 0);
3894 free(ct_parent_path);
3899 get_ct_file_mode(struct got_commitable *ct)
3901 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3904 static const struct got_error *
3905 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3906 struct got_tree_entry *te, struct got_commitable *ct)
3908 const struct got_error *err = NULL;
3912 err = got_object_tree_entry_dup(new_te, te);
3916 (*new_te)->mode = get_ct_file_mode(ct);
3918 if (ct->staged_status == GOT_STATUS_MODIFY)
3919 memcpy(&(*new_te)->id, ct->staged_blob_id,
3920 sizeof((*new_te)->id));
3922 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3924 if (err && *new_te) {
3931 static const struct got_error *
3932 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3933 struct got_commitable *ct)
3935 const struct got_error *err = NULL;
3940 *new_te = calloc(1, sizeof(**new_te));
3941 if (*new_te == NULL)
3942 return got_error_from_errno("calloc");
3944 ct_name = basename(ct->path);
3945 if (ct_name == NULL) {
3946 err = got_error_from_errno2("basename", ct->path);
3949 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3950 sizeof((*new_te)->name)) {
3951 err = got_error(GOT_ERR_NO_SPACE);
3955 (*new_te)->mode = get_ct_file_mode(ct);
3957 if (ct->staged_status == GOT_STATUS_ADD)
3958 memcpy(&(*new_te)->id, ct->staged_blob_id,
3959 sizeof((*new_te)->id));
3961 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3963 if (err && *new_te) {
3970 static const struct got_error *
3971 insert_tree_entry(struct got_tree_entry *new_te,
3972 struct got_pathlist_head *paths)
3974 const struct got_error *err = NULL;
3975 struct got_pathlist_entry *new_pe;
3977 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3981 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3985 static const struct got_error *
3986 report_ct_status(struct got_commitable *ct,
3987 got_worktree_status_cb status_cb, void *status_arg)
3989 const char *ct_path = ct->path;
3990 unsigned char status;
3992 while (ct_path[0] == '/')
3995 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3996 status = ct->staged_status;
3998 status = ct->status;
4000 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4001 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4004 static const struct got_error *
4005 match_modified_subtree(int *modified, struct got_tree_entry *te,
4006 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4008 const struct got_error *err = NULL;
4009 struct got_pathlist_entry *pe;
4014 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4015 got_path_is_root_dir(base_tree_path) ? "" : "/",
4017 return got_error_from_errno("asprintf");
4019 TAILQ_FOREACH(pe, commitable_paths, entry) {
4020 struct got_commitable *ct = pe->data;
4021 *modified = got_path_is_child(ct->in_repo_path, te_path,
4031 static const struct got_error *
4032 match_deleted_or_modified_ct(struct got_commitable **ctp,
4033 struct got_tree_entry *te, const char *base_tree_path,
4034 struct got_pathlist_head *commitable_paths)
4036 const struct got_error *err = NULL;
4037 struct got_pathlist_entry *pe;
4041 TAILQ_FOREACH(pe, commitable_paths, entry) {
4042 struct got_commitable *ct = pe->data;
4043 char *ct_name = NULL;
4046 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4047 if (ct->status != GOT_STATUS_MODIFY &&
4048 ct->status != GOT_STATUS_MODE_CHANGE &&
4049 ct->status != GOT_STATUS_DELETE)
4052 if (ct->staged_status != GOT_STATUS_MODIFY &&
4053 ct->staged_status != GOT_STATUS_DELETE)
4057 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4060 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4066 ct_name = basename(pe->path);
4067 if (ct_name == NULL)
4068 return got_error_from_errno2("basename", pe->path);
4070 if (strcmp(te->name, ct_name) != 0)
4080 static const struct got_error *
4081 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4082 const char *child_path, const char *path_base_tree,
4083 struct got_pathlist_head *commitable_paths,
4084 got_worktree_status_cb status_cb, void *status_arg,
4085 struct got_repository *repo)
4087 const struct got_error *err = NULL;
4088 struct got_tree_entry *new_te;
4090 struct got_object_id *id = NULL;
4094 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4095 got_path_is_root_dir(path_base_tree) ? "" : "/",
4097 return got_error_from_errno("asprintf");
4099 new_te = calloc(1, sizeof(*new_te));
4101 return got_error_from_errno("calloc");
4102 new_te->mode = S_IFDIR;
4104 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4105 sizeof(new_te->name)) {
4106 err = got_error(GOT_ERR_NO_SPACE);
4109 err = write_tree(&id, NULL, subtree_path,
4110 commitable_paths, status_cb, status_arg, repo);
4115 memcpy(&new_te->id, id, sizeof(new_te->id));
4124 static const struct got_error *
4125 write_tree(struct got_object_id **new_tree_id,
4126 struct got_tree_object *base_tree, const char *path_base_tree,
4127 struct got_pathlist_head *commitable_paths,
4128 got_worktree_status_cb status_cb, void *status_arg,
4129 struct got_repository *repo)
4131 const struct got_error *err = NULL;
4132 struct got_pathlist_head paths;
4133 struct got_tree_entry *te, *new_te = NULL;
4134 struct got_pathlist_entry *pe;
4139 /* Insert, and recurse into, newly added entries first. */
4140 TAILQ_FOREACH(pe, commitable_paths, entry) {
4141 struct got_commitable *ct = pe->data;
4142 char *child_path = NULL, *slash;
4144 if ((ct->status != GOT_STATUS_ADD &&
4145 ct->staged_status != GOT_STATUS_ADD) ||
4146 (ct->flags & GOT_COMMITABLE_ADDED))
4149 if (!got_path_is_child(pe->path, path_base_tree,
4150 strlen(path_base_tree)))
4153 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4158 slash = strchr(child_path, '/');
4159 if (slash == NULL) {
4160 err = alloc_added_blob_tree_entry(&new_te, ct);
4163 err = report_ct_status(ct, status_cb, status_arg);
4166 ct->flags |= GOT_COMMITABLE_ADDED;
4167 err = insert_tree_entry(new_te, &paths);
4172 *slash = '\0'; /* trim trailing path components */
4173 if (base_tree == NULL ||
4174 got_object_tree_find_entry(base_tree, child_path)
4176 err = make_subtree_for_added_blob(&new_te,
4177 child_path, path_base_tree,
4178 commitable_paths, status_cb, status_arg,
4182 err = insert_tree_entry(new_te, &paths);
4191 int i, nbase_entries;
4192 /* Handle modified and deleted entries. */
4193 nbase_entries = got_object_tree_get_nentries(base_tree);
4194 for (i = 0; i < nbase_entries; i++) {
4195 struct got_commitable *ct = NULL;
4197 te = got_object_tree_get_entry(base_tree, i);
4198 if (got_object_tree_entry_is_submodule(te)) {
4199 /* Entry is a submodule; just copy it. */
4200 err = got_object_tree_entry_dup(&new_te, te);
4203 err = insert_tree_entry(new_te, &paths);
4210 if (S_ISDIR(te->mode)) {
4212 err = got_object_tree_entry_dup(&new_te, te);
4215 err = match_modified_subtree(&modified, te,
4216 path_base_tree, commitable_paths);
4219 /* Avoid recursion into unmodified subtrees. */
4221 struct got_object_id *new_id;
4222 err = write_subtree(&new_id, te,
4223 path_base_tree, commitable_paths,
4224 status_cb, status_arg, repo);
4227 memcpy(&new_te->id, new_id,
4228 sizeof(new_te->id));
4231 err = insert_tree_entry(new_te, &paths);
4238 err = match_deleted_or_modified_ct(&ct, te,
4239 path_base_tree, commitable_paths);
4243 /* NB: Deleted entries get dropped here. */
4244 if (ct->status == GOT_STATUS_MODIFY ||
4245 ct->status == GOT_STATUS_MODE_CHANGE ||
4246 ct->staged_status == GOT_STATUS_MODIFY) {
4247 err = alloc_modified_blob_tree_entry(
4251 err = insert_tree_entry(new_te, &paths);
4256 err = report_ct_status(ct, status_cb,
4261 /* Entry is unchanged; just copy it. */
4262 err = got_object_tree_entry_dup(&new_te, te);
4265 err = insert_tree_entry(new_te, &paths);
4273 /* Write new list of entries; deleted entries have been dropped. */
4274 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4276 got_pathlist_free(&paths);
4280 static const struct got_error *
4281 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4282 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4283 int have_staged_files)
4285 const struct got_error *err = NULL;
4286 struct got_pathlist_entry *pe;
4288 TAILQ_FOREACH(pe, commitable_paths, entry) {
4289 struct got_fileindex_entry *ie;
4290 struct got_commitable *ct = pe->data;
4292 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4294 if (ct->status == GOT_STATUS_DELETE ||
4295 ct->staged_status == GOT_STATUS_DELETE) {
4296 got_fileindex_entry_remove(fileindex, ie);
4297 } else if (ct->staged_status == GOT_STATUS_ADD ||
4298 ct->staged_status == GOT_STATUS_MODIFY) {
4299 got_fileindex_entry_stage_set(ie,
4300 GOT_FILEIDX_STAGE_NONE);
4301 err = got_fileindex_entry_update(ie,
4302 ct->ondisk_path, ct->staged_blob_id->sha1,
4303 new_base_commit_id->sha1,
4304 !have_staged_files);
4306 err = got_fileindex_entry_update(ie,
4307 ct->ondisk_path, ct->blob_id->sha1,
4308 new_base_commit_id->sha1,
4309 !have_staged_files);
4311 err = got_fileindex_entry_alloc(&ie, pe->path);
4314 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4315 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4317 got_fileindex_entry_free(ie);
4320 err = got_fileindex_entry_add(fileindex, ie);
4322 got_fileindex_entry_free(ie);
4331 static const struct got_error *
4332 check_out_of_date(const char *in_repo_path, unsigned char status,
4333 unsigned char staged_status, struct got_object_id *base_blob_id,
4334 struct got_object_id *base_commit_id,
4335 struct got_object_id *head_commit_id, struct got_repository *repo,
4338 const struct got_error *err = NULL;
4339 struct got_object_id *id = NULL;
4341 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4342 /* Trivial case: base commit == head commit */
4343 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4346 * Ensure file content which local changes were based
4347 * on matches file content in the branch head.
4349 err = got_object_id_by_path(&id, repo, head_commit_id,
4352 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4353 err = got_error(ood_errcode);
4355 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4356 err = got_error(ood_errcode);
4358 /* Require that added files don't exist in the branch head. */
4359 err = got_object_id_by_path(&id, repo, head_commit_id,
4361 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4363 err = id ? got_error(ood_errcode) : NULL;
4370 const struct got_error *
4371 commit_worktree(struct got_object_id **new_commit_id,
4372 struct got_pathlist_head *commitable_paths,
4373 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4374 const char *author, const char *committer,
4375 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4376 got_worktree_status_cb status_cb, void *status_arg,
4377 struct got_repository *repo)
4379 const struct got_error *err = NULL, *unlockerr = NULL;
4380 struct got_pathlist_entry *pe;
4381 const char *head_ref_name = NULL;
4382 struct got_commit_object *head_commit = NULL;
4383 struct got_reference *head_ref2 = NULL;
4384 struct got_object_id *head_commit_id2 = NULL;
4385 struct got_tree_object *head_tree = NULL;
4386 struct got_object_id *new_tree_id = NULL;
4387 struct got_object_id_queue parent_ids;
4388 struct got_object_qid *pid = NULL;
4389 char *logmsg = NULL;
4391 *new_commit_id = NULL;
4393 SIMPLEQ_INIT(&parent_ids);
4395 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4399 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4403 if (commit_msg_cb != NULL) {
4404 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4409 if (logmsg == NULL || strlen(logmsg) == 0) {
4410 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4414 /* Create blobs from added and modified files and record their IDs. */
4415 TAILQ_FOREACH(pe, commitable_paths, entry) {
4416 struct got_commitable *ct = pe->data;
4419 /* Blobs for staged files already exist. */
4420 if (ct->staged_status == GOT_STATUS_ADD ||
4421 ct->staged_status == GOT_STATUS_MODIFY)
4424 if (ct->status != GOT_STATUS_ADD &&
4425 ct->status != GOT_STATUS_MODIFY &&
4426 ct->status != GOT_STATUS_MODE_CHANGE)
4429 if (asprintf(&ondisk_path, "%s/%s",
4430 worktree->root_path, pe->path) == -1) {
4431 err = got_error_from_errno("asprintf");
4434 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4440 /* Recursively write new tree objects. */
4441 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4442 status_cb, status_arg, repo);
4446 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4449 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4450 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4451 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4452 got_object_qid_free(pid);
4458 /* Check if a concurrent commit to our branch has occurred. */
4459 head_ref_name = got_worktree_get_head_ref_name(worktree);
4460 if (head_ref_name == NULL) {
4461 err = got_error_from_errno("got_worktree_get_head_ref_name");
4464 /* Lock the reference here to prevent concurrent modification. */
4465 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4468 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4471 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4472 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4475 /* Update branch head in repository. */
4476 err = got_ref_change_ref(head_ref2, *new_commit_id);
4479 err = got_ref_write(head_ref2, repo);
4483 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4487 err = ref_base_commit(worktree, repo);
4492 got_object_tree_close(head_tree);
4494 got_object_commit_close(head_commit);
4495 free(head_commit_id2);
4497 unlockerr = got_ref_unlock(head_ref2);
4498 if (unlockerr && err == NULL)
4500 got_ref_close(head_ref2);
4505 static const struct got_error *
4506 check_path_is_commitable(const char *path,
4507 struct got_pathlist_head *commitable_paths)
4509 struct got_pathlist_entry *cpe = NULL;
4510 size_t path_len = strlen(path);
4512 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4513 struct got_commitable *ct = cpe->data;
4514 const char *ct_path = ct->path;
4516 while (ct_path[0] == '/')
4519 if (strcmp(path, ct_path) == 0 ||
4520 got_path_is_child(ct_path, path, path_len))
4525 return got_error_path(path, GOT_ERR_BAD_PATH);
4530 static const struct got_error *
4531 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4533 int *have_staged_files = arg;
4535 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4536 *have_staged_files = 1;
4537 return got_error(GOT_ERR_CANCELLED);
4543 static const struct got_error *
4544 check_non_staged_files(struct got_fileindex *fileindex,
4545 struct got_pathlist_head *paths)
4547 struct got_pathlist_entry *pe;
4548 struct got_fileindex_entry *ie;
4550 TAILQ_FOREACH(pe, paths, entry) {
4551 if (pe->path[0] == '\0')
4553 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4555 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4556 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4557 return got_error_path(pe->path,
4558 GOT_ERR_FILE_NOT_STAGED);
4564 const struct got_error *
4565 got_worktree_commit(struct got_object_id **new_commit_id,
4566 struct got_worktree *worktree, struct got_pathlist_head *paths,
4567 const char *author, const char *committer,
4568 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4569 got_worktree_status_cb status_cb, void *status_arg,
4570 struct got_repository *repo)
4572 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4573 struct got_fileindex *fileindex = NULL;
4574 char *fileindex_path = NULL;
4575 struct got_pathlist_head commitable_paths;
4576 struct collect_commitables_arg cc_arg;
4577 struct got_pathlist_entry *pe;
4578 struct got_reference *head_ref = NULL;
4579 struct got_object_id *head_commit_id = NULL;
4580 int have_staged_files = 0;
4582 *new_commit_id = NULL;
4584 TAILQ_INIT(&commitable_paths);
4586 err = lock_worktree(worktree, LOCK_EX);
4590 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4594 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4598 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4602 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4603 &have_staged_files);
4604 if (err && err->code != GOT_ERR_CANCELLED)
4606 if (have_staged_files) {
4607 err = check_non_staged_files(fileindex, paths);
4612 cc_arg.commitable_paths = &commitable_paths;
4613 cc_arg.worktree = worktree;
4615 cc_arg.have_staged_files = have_staged_files;
4616 TAILQ_FOREACH(pe, paths, entry) {
4617 err = worktree_status(worktree, pe->path, fileindex, repo,
4618 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4623 if (TAILQ_EMPTY(&commitable_paths)) {
4624 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4628 TAILQ_FOREACH(pe, paths, entry) {
4629 err = check_path_is_commitable(pe->path, &commitable_paths);
4634 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4635 struct got_commitable *ct = pe->data;
4636 const char *ct_path = ct->in_repo_path;
4638 while (ct_path[0] == '/')
4640 err = check_out_of_date(ct_path, ct->status,
4641 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4642 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4648 err = commit_worktree(new_commit_id, &commitable_paths,
4649 head_commit_id, worktree, author, committer,
4650 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4654 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4655 fileindex, have_staged_files);
4656 sync_err = sync_fileindex(fileindex, fileindex_path);
4657 if (sync_err && err == NULL)
4661 got_fileindex_free(fileindex);
4662 free(fileindex_path);
4663 unlockerr = lock_worktree(worktree, LOCK_SH);
4664 if (unlockerr && err == NULL)
4666 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4667 struct got_commitable *ct = pe->data;
4668 free_commitable(ct);
4670 got_pathlist_free(&commitable_paths);
4675 got_commitable_get_path(struct got_commitable *ct)
4681 got_commitable_get_status(struct got_commitable *ct)
4686 struct check_rebase_ok_arg {
4687 struct got_worktree *worktree;
4688 struct got_repository *repo;
4691 static const struct got_error *
4692 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4694 const struct got_error *err = NULL;
4695 struct check_rebase_ok_arg *a = arg;
4696 unsigned char status;
4700 /* Reject rebase of a work tree with mixed base commits. */
4701 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4702 SHA1_DIGEST_LENGTH))
4703 return got_error(GOT_ERR_MIXED_COMMITS);
4705 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4707 return got_error_from_errno("asprintf");
4709 /* Reject rebase of a work tree with modified or staged files. */
4710 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4715 if (status != GOT_STATUS_NO_CHANGE)
4716 return got_error(GOT_ERR_MODIFIED);
4717 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4718 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4723 const struct got_error *
4724 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4725 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4726 struct got_worktree *worktree, struct got_reference *branch,
4727 struct got_repository *repo)
4729 const struct got_error *err = NULL;
4730 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4731 char *branch_ref_name = NULL;
4732 char *fileindex_path = NULL;
4733 struct check_rebase_ok_arg ok_arg;
4734 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4735 struct got_object_id *wt_branch_tip = NULL;
4737 *new_base_branch_ref = NULL;
4741 err = lock_worktree(worktree, LOCK_EX);
4745 err = open_fileindex(fileindex, &fileindex_path, worktree);
4749 ok_arg.worktree = worktree;
4751 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4756 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4760 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4764 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4768 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4773 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4776 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4777 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4781 err = got_ref_alloc_symref(new_base_branch_ref,
4782 new_base_branch_ref_name, wt_branch);
4785 err = got_ref_write(*new_base_branch_ref, repo);
4789 /* TODO Lock original branch's ref while rebasing? */
4791 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4795 err = got_ref_write(branch_ref, repo);
4799 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4800 worktree->base_commit_id);
4803 err = got_ref_write(*tmp_branch, repo);
4807 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4811 free(fileindex_path);
4812 free(tmp_branch_name);
4813 free(new_base_branch_ref_name);
4814 free(branch_ref_name);
4816 got_ref_close(branch_ref);
4818 got_ref_close(wt_branch);
4819 free(wt_branch_tip);
4821 if (*new_base_branch_ref) {
4822 got_ref_close(*new_base_branch_ref);
4823 *new_base_branch_ref = NULL;
4826 got_ref_close(*tmp_branch);
4830 got_fileindex_free(*fileindex);
4833 lock_worktree(worktree, LOCK_SH);
4838 const struct got_error *
4839 got_worktree_rebase_continue(struct got_object_id **commit_id,
4840 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4841 struct got_reference **branch, struct got_fileindex **fileindex,
4842 struct got_worktree *worktree, struct got_repository *repo)
4844 const struct got_error *err;
4845 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4846 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4847 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4848 char *fileindex_path = NULL;
4849 int have_staged_files = 0;
4852 *new_base_branch = NULL;
4857 err = lock_worktree(worktree, LOCK_EX);
4861 err = open_fileindex(fileindex, &fileindex_path, worktree);
4865 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4866 &have_staged_files);
4867 if (err && err->code != GOT_ERR_CANCELLED)
4869 if (have_staged_files) {
4870 err = got_error(GOT_ERR_STAGED_PATHS);
4874 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4878 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4882 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4886 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4890 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4894 err = got_ref_open(branch, repo,
4895 got_ref_get_symref_target(branch_ref), 0);
4899 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4903 err = got_ref_resolve(commit_id, repo, commit_ref);
4907 err = got_ref_open(new_base_branch, repo,
4908 new_base_branch_ref_name, 0);
4912 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4916 free(commit_ref_name);
4917 free(branch_ref_name);
4918 free(fileindex_path);
4920 got_ref_close(commit_ref);
4922 got_ref_close(branch_ref);
4927 got_ref_close(*tmp_branch);
4930 if (*new_base_branch) {
4931 got_ref_close(*new_base_branch);
4932 *new_base_branch = NULL;
4935 got_ref_close(*branch);
4939 got_fileindex_free(*fileindex);
4942 lock_worktree(worktree, LOCK_SH);
4947 const struct got_error *
4948 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4950 const struct got_error *err;
4951 char *tmp_branch_name = NULL;
4953 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4957 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4958 free(tmp_branch_name);
4962 static const struct got_error *
4963 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4964 char **logmsg, void *arg)
4970 static const struct got_error *
4971 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4972 const char *path, struct got_object_id *blob_id,
4973 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4974 int dirfd, const char *de_name)
4979 struct collect_merged_paths_arg {
4980 got_worktree_checkout_cb progress_cb;
4982 struct got_pathlist_head *merged_paths;
4985 static const struct got_error *
4986 collect_merged_paths(void *arg, unsigned char status, const char *path)
4988 const struct got_error *err;
4989 struct collect_merged_paths_arg *a = arg;
4991 struct got_pathlist_entry *new;
4993 err = (*a->progress_cb)(a->progress_arg, status, path);
4997 if (status != GOT_STATUS_MERGE &&
4998 status != GOT_STATUS_ADD &&
4999 status != GOT_STATUS_DELETE &&
5000 status != GOT_STATUS_CONFLICT)
5005 return got_error_from_errno("strdup");
5007 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5008 if (err || new == NULL)
5014 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5016 struct got_pathlist_entry *pe;
5018 TAILQ_FOREACH(pe, merged_paths, entry)
5019 free((char *)pe->path);
5021 got_pathlist_free(merged_paths);
5024 static const struct got_error *
5025 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5026 int is_rebase, struct got_repository *repo)
5028 const struct got_error *err;
5029 struct got_reference *commit_ref = NULL;
5031 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5033 if (err->code != GOT_ERR_NOT_REF)
5035 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5038 err = got_ref_write(commit_ref, repo);
5041 } else if (is_rebase) {
5042 struct got_object_id *stored_id;
5045 err = got_ref_resolve(&stored_id, repo, commit_ref);
5048 cmp = got_object_id_cmp(commit_id, stored_id);
5051 err = got_error(GOT_ERR_REBASE_COMMITID);
5057 got_ref_close(commit_ref);
5061 static const struct got_error *
5062 rebase_merge_files(struct got_pathlist_head *merged_paths,
5063 const char *commit_ref_name, struct got_worktree *worktree,
5064 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5065 struct got_object_id *commit_id, struct got_repository *repo,
5066 got_worktree_checkout_cb progress_cb, void *progress_arg,
5067 got_cancel_cb cancel_cb, void *cancel_arg)
5069 const struct got_error *err;
5070 struct got_reference *commit_ref = NULL;
5071 struct collect_merged_paths_arg cmp_arg;
5072 char *fileindex_path;
5074 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5076 err = get_fileindex_path(&fileindex_path, worktree);
5080 cmp_arg.progress_cb = progress_cb;
5081 cmp_arg.progress_arg = progress_arg;
5082 cmp_arg.merged_paths = merged_paths;
5083 err = merge_files(worktree, fileindex, fileindex_path,
5084 parent_commit_id, commit_id, repo, collect_merged_paths,
5085 &cmp_arg, cancel_cb, cancel_arg);
5087 got_ref_close(commit_ref);
5091 const struct got_error *
5092 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5093 struct got_worktree *worktree, struct got_fileindex *fileindex,
5094 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5095 struct got_repository *repo,
5096 got_worktree_checkout_cb progress_cb, void *progress_arg,
5097 got_cancel_cb cancel_cb, void *cancel_arg)
5099 const struct got_error *err;
5100 char *commit_ref_name;
5102 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5106 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5110 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5111 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5112 progress_arg, cancel_cb, cancel_arg);
5114 free(commit_ref_name);
5118 const struct got_error *
5119 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5120 struct got_worktree *worktree, struct got_fileindex *fileindex,
5121 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5122 struct got_repository *repo,
5123 got_worktree_checkout_cb progress_cb, void *progress_arg,
5124 got_cancel_cb cancel_cb, void *cancel_arg)
5126 const struct got_error *err;
5127 char *commit_ref_name;
5129 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5133 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5137 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5138 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5139 progress_arg, cancel_cb, cancel_arg);
5141 free(commit_ref_name);
5145 static const struct got_error *
5146 rebase_commit(struct got_object_id **new_commit_id,
5147 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5148 struct got_worktree *worktree, struct got_fileindex *fileindex,
5149 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5150 const char *new_logmsg, struct got_repository *repo)
5152 const struct got_error *err, *sync_err;
5153 struct got_pathlist_head commitable_paths;
5154 struct collect_commitables_arg cc_arg;
5155 char *fileindex_path = NULL;
5156 struct got_reference *head_ref = NULL;
5157 struct got_object_id *head_commit_id = NULL;
5158 char *logmsg = NULL;
5160 TAILQ_INIT(&commitable_paths);
5161 *new_commit_id = NULL;
5163 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5165 err = get_fileindex_path(&fileindex_path, worktree);
5169 cc_arg.commitable_paths = &commitable_paths;
5170 cc_arg.worktree = worktree;
5172 cc_arg.have_staged_files = 0;
5174 * If possible get the status of individual files directly to
5175 * avoid crawling the entire work tree once per rebased commit.
5176 * TODO: Ideally, merged_paths would contain a list of commitables
5177 * we could use so we could skip worktree_status() entirely.
5180 struct got_pathlist_entry *pe;
5181 if (TAILQ_EMPTY(merged_paths)) {
5182 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5185 TAILQ_FOREACH(pe, merged_paths, entry) {
5186 err = worktree_status(worktree, pe->path, fileindex,
5187 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5193 err = worktree_status(worktree, "", fileindex, repo,
5194 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5199 if (TAILQ_EMPTY(&commitable_paths)) {
5200 /* No-op change; commit will be elided. */
5201 err = got_ref_delete(commit_ref, repo);
5204 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5208 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5212 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5217 logmsg = strdup(new_logmsg);
5218 if (logmsg == NULL) {
5219 err = got_error_from_errno("strdup");
5223 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5228 /* NB: commit_worktree will call free(logmsg) */
5229 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5230 worktree, got_object_commit_get_author(orig_commit),
5231 got_object_commit_get_committer(orig_commit),
5232 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5236 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5240 err = got_ref_delete(commit_ref, repo);
5244 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5246 sync_err = sync_fileindex(fileindex, fileindex_path);
5247 if (sync_err && err == NULL)
5250 free(fileindex_path);
5251 free(head_commit_id);
5253 got_ref_close(head_ref);
5255 free(*new_commit_id);
5256 *new_commit_id = NULL;
5261 const struct got_error *
5262 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5263 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5264 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5265 struct got_commit_object *orig_commit,
5266 struct got_object_id *orig_commit_id, struct got_repository *repo)
5268 const struct got_error *err;
5269 char *commit_ref_name;
5270 struct got_reference *commit_ref = NULL;
5271 struct got_object_id *commit_id = NULL;
5273 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5277 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5280 err = got_ref_resolve(&commit_id, repo, commit_ref);
5283 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5284 err = got_error(GOT_ERR_REBASE_COMMITID);
5288 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5289 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5292 got_ref_close(commit_ref);
5293 free(commit_ref_name);
5298 const struct got_error *
5299 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5300 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5301 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5302 struct got_commit_object *orig_commit,
5303 struct got_object_id *orig_commit_id, const char *new_logmsg,
5304 struct got_repository *repo)
5306 const struct got_error *err;
5307 char *commit_ref_name;
5308 struct got_reference *commit_ref = NULL;
5310 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5314 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5318 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5319 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5322 got_ref_close(commit_ref);
5323 free(commit_ref_name);
5327 const struct got_error *
5328 got_worktree_rebase_postpone(struct got_worktree *worktree,
5329 struct got_fileindex *fileindex)
5332 got_fileindex_free(fileindex);
5333 return lock_worktree(worktree, LOCK_SH);
5336 static const struct got_error *
5337 delete_ref(const char *name, struct got_repository *repo)
5339 const struct got_error *err;
5340 struct got_reference *ref;
5342 err = got_ref_open(&ref, repo, name, 0);
5344 if (err->code == GOT_ERR_NOT_REF)
5349 err = got_ref_delete(ref, repo);
5354 static const struct got_error *
5355 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5357 const struct got_error *err;
5358 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5359 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5361 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5364 err = delete_ref(tmp_branch_name, repo);
5368 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5371 err = delete_ref(new_base_branch_ref_name, repo);
5375 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5378 err = delete_ref(branch_ref_name, repo);
5382 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5385 err = delete_ref(commit_ref_name, repo);
5390 free(tmp_branch_name);
5391 free(new_base_branch_ref_name);
5392 free(branch_ref_name);
5393 free(commit_ref_name);
5397 const struct got_error *
5398 got_worktree_rebase_complete(struct got_worktree *worktree,
5399 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5400 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5401 struct got_repository *repo)
5403 const struct got_error *err, *unlockerr;
5404 struct got_object_id *new_head_commit_id = NULL;
5406 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5410 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5414 err = got_ref_write(rebased_branch, repo);
5418 err = got_worktree_set_head_ref(worktree, rebased_branch);
5422 err = delete_rebase_refs(worktree, repo);
5425 got_fileindex_free(fileindex);
5426 free(new_head_commit_id);
5427 unlockerr = lock_worktree(worktree, LOCK_SH);
5428 if (unlockerr && err == NULL)
5433 const struct got_error *
5434 got_worktree_rebase_abort(struct got_worktree *worktree,
5435 struct got_fileindex *fileindex, struct got_repository *repo,
5436 struct got_reference *new_base_branch,
5437 got_worktree_checkout_cb progress_cb, void *progress_arg)
5439 const struct got_error *err, *unlockerr, *sync_err;
5440 struct got_reference *resolved = NULL;
5441 struct got_object_id *commit_id = NULL;
5442 char *fileindex_path = NULL;
5443 struct revert_file_args rfa;
5444 struct got_object_id *tree_id = NULL;
5446 err = lock_worktree(worktree, LOCK_EX);
5450 err = got_ref_open(&resolved, repo,
5451 got_ref_get_symref_target(new_base_branch), 0);
5455 err = got_worktree_set_head_ref(worktree, resolved);
5460 * XXX commits to the base branch could have happened while
5461 * we were busy rebasing; should we store the original commit ID
5462 * when rebase begins and read it back here?
5464 err = got_ref_resolve(&commit_id, repo, resolved);
5468 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5472 err = got_object_id_by_path(&tree_id, repo,
5473 worktree->base_commit_id, worktree->path_prefix);
5477 err = delete_rebase_refs(worktree, repo);
5481 err = get_fileindex_path(&fileindex_path, worktree);
5485 rfa.worktree = worktree;
5486 rfa.fileindex = fileindex;
5487 rfa.progress_cb = progress_cb;
5488 rfa.progress_arg = progress_arg;
5489 rfa.patch_cb = NULL;
5490 rfa.patch_arg = NULL;
5492 err = worktree_status(worktree, "", fileindex, repo,
5493 revert_file, &rfa, NULL, NULL, 0, 0);
5497 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5498 repo, progress_cb, progress_arg, NULL, NULL);
5500 sync_err = sync_fileindex(fileindex, fileindex_path);
5501 if (sync_err && err == NULL)
5504 got_ref_close(resolved);
5508 got_fileindex_free(fileindex);
5509 free(fileindex_path);
5511 unlockerr = lock_worktree(worktree, LOCK_SH);
5512 if (unlockerr && err == NULL)
5517 const struct got_error *
5518 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5519 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5520 struct got_fileindex **fileindex, struct got_worktree *worktree,
5521 struct got_repository *repo)
5523 const struct got_error *err = NULL;
5524 char *tmp_branch_name = NULL;
5525 char *branch_ref_name = NULL;
5526 char *base_commit_ref_name = NULL;
5527 char *fileindex_path = NULL;
5528 struct check_rebase_ok_arg ok_arg;
5529 struct got_reference *wt_branch = NULL;
5530 struct got_reference *base_commit_ref = NULL;
5534 *base_commit_id = NULL;
5537 err = lock_worktree(worktree, LOCK_EX);
5541 err = open_fileindex(fileindex, &fileindex_path, worktree);
5545 ok_arg.worktree = worktree;
5547 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5552 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5556 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5560 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5565 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5570 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5574 err = got_ref_write(*branch_ref, repo);
5578 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5579 worktree->base_commit_id);
5582 err = got_ref_write(base_commit_ref, repo);
5585 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5586 if (*base_commit_id == NULL) {
5587 err = got_error_from_errno("got_object_id_dup");
5591 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5592 worktree->base_commit_id);
5595 err = got_ref_write(*tmp_branch, repo);
5599 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5603 free(fileindex_path);
5604 free(tmp_branch_name);
5605 free(branch_ref_name);
5606 free(base_commit_ref_name);
5608 got_ref_close(wt_branch);
5611 got_ref_close(*branch_ref);
5615 got_ref_close(*tmp_branch);
5618 free(*base_commit_id);
5620 got_fileindex_free(*fileindex);
5623 lock_worktree(worktree, LOCK_SH);
5628 const struct got_error *
5629 got_worktree_histedit_postpone(struct got_worktree *worktree,
5630 struct got_fileindex *fileindex)
5633 got_fileindex_free(fileindex);
5634 return lock_worktree(worktree, LOCK_SH);
5637 const struct got_error *
5638 got_worktree_histedit_in_progress(int *in_progress,
5639 struct got_worktree *worktree)
5641 const struct got_error *err;
5642 char *tmp_branch_name = NULL;
5644 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5648 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5649 free(tmp_branch_name);
5653 const struct got_error *
5654 got_worktree_histedit_continue(struct got_object_id **commit_id,
5655 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5656 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5657 struct got_worktree *worktree, struct got_repository *repo)
5659 const struct got_error *err;
5660 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5661 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5662 struct got_reference *commit_ref = NULL;
5663 struct got_reference *base_commit_ref = NULL;
5664 char *fileindex_path = NULL;
5665 int have_staged_files = 0;
5669 *base_commit_id = NULL;
5672 err = lock_worktree(worktree, LOCK_EX);
5676 err = open_fileindex(fileindex, &fileindex_path, worktree);
5680 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5681 &have_staged_files);
5682 if (err && err->code != GOT_ERR_CANCELLED)
5684 if (have_staged_files) {
5685 err = got_error(GOT_ERR_STAGED_PATHS);
5689 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5693 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5697 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5701 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5706 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5710 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5713 err = got_ref_resolve(commit_id, repo, commit_ref);
5717 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5720 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5724 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5728 free(commit_ref_name);
5729 free(branch_ref_name);
5730 free(fileindex_path);
5732 got_ref_close(commit_ref);
5733 if (base_commit_ref)
5734 got_ref_close(base_commit_ref);
5738 free(*base_commit_id);
5739 *base_commit_id = NULL;
5741 got_ref_close(*tmp_branch);
5745 got_fileindex_free(*fileindex);
5748 lock_worktree(worktree, LOCK_EX);
5753 static const struct got_error *
5754 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5756 const struct got_error *err;
5757 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5758 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5760 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5763 err = delete_ref(tmp_branch_name, repo);
5767 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5771 err = delete_ref(base_commit_ref_name, repo);
5775 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5778 err = delete_ref(branch_ref_name, repo);
5782 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5785 err = delete_ref(commit_ref_name, repo);
5789 free(tmp_branch_name);
5790 free(base_commit_ref_name);
5791 free(branch_ref_name);
5792 free(commit_ref_name);
5796 const struct got_error *
5797 got_worktree_histedit_abort(struct got_worktree *worktree,
5798 struct got_fileindex *fileindex, struct got_repository *repo,
5799 struct got_reference *branch, struct got_object_id *base_commit_id,
5800 got_worktree_checkout_cb progress_cb, void *progress_arg)
5802 const struct got_error *err, *unlockerr, *sync_err;
5803 struct got_reference *resolved = NULL;
5804 char *fileindex_path = NULL;
5805 struct got_object_id *tree_id = NULL;
5806 struct revert_file_args rfa;
5808 err = lock_worktree(worktree, LOCK_EX);
5812 err = got_ref_open(&resolved, repo,
5813 got_ref_get_symref_target(branch), 0);
5817 err = got_worktree_set_head_ref(worktree, resolved);
5821 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5825 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5826 worktree->path_prefix);
5830 err = delete_histedit_refs(worktree, repo);
5834 err = get_fileindex_path(&fileindex_path, worktree);
5838 rfa.worktree = worktree;
5839 rfa.fileindex = fileindex;
5840 rfa.progress_cb = progress_cb;
5841 rfa.progress_arg = progress_arg;
5842 rfa.patch_cb = NULL;
5843 rfa.patch_arg = NULL;
5845 err = worktree_status(worktree, "", fileindex, repo,
5846 revert_file, &rfa, NULL, NULL, 0, 0);
5850 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5851 repo, progress_cb, progress_arg, NULL, NULL);
5853 sync_err = sync_fileindex(fileindex, fileindex_path);
5854 if (sync_err && err == NULL)
5857 got_ref_close(resolved);
5859 free(fileindex_path);
5861 unlockerr = lock_worktree(worktree, LOCK_SH);
5862 if (unlockerr && err == NULL)
5867 const struct got_error *
5868 got_worktree_histedit_complete(struct got_worktree *worktree,
5869 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5870 struct got_reference *edited_branch, struct got_repository *repo)
5872 const struct got_error *err, *unlockerr;
5873 struct got_object_id *new_head_commit_id = NULL;
5874 struct got_reference *resolved = NULL;
5876 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5880 err = got_ref_open(&resolved, repo,
5881 got_ref_get_symref_target(edited_branch), 0);
5885 err = got_ref_change_ref(resolved, new_head_commit_id);
5889 err = got_ref_write(resolved, repo);
5893 err = got_worktree_set_head_ref(worktree, resolved);
5897 err = delete_histedit_refs(worktree, repo);
5900 got_fileindex_free(fileindex);
5901 free(new_head_commit_id);
5902 unlockerr = lock_worktree(worktree, LOCK_SH);
5903 if (unlockerr && err == NULL)
5908 const struct got_error *
5909 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5910 struct got_object_id *commit_id, struct got_repository *repo)
5912 const struct got_error *err;
5913 char *commit_ref_name;
5915 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5919 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5923 err = delete_ref(commit_ref_name, repo);
5925 free(commit_ref_name);
5929 const struct got_error *
5930 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5931 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5932 struct got_worktree *worktree, const char *refname,
5933 struct got_repository *repo)
5935 const struct got_error *err = NULL;
5936 char *fileindex_path = NULL;
5937 struct check_rebase_ok_arg ok_arg;
5941 *base_branch_ref = NULL;
5943 err = lock_worktree(worktree, LOCK_EX);
5947 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5948 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5949 "cannot integrate a branch into itself; "
5950 "update -b or different branch name required");
5954 err = open_fileindex(fileindex, &fileindex_path, worktree);
5958 /* Preconditions are the same as for rebase. */
5959 ok_arg.worktree = worktree;
5961 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5966 err = got_ref_open(branch_ref, repo, refname, 1);
5970 err = got_ref_open(base_branch_ref, repo,
5971 got_worktree_get_head_ref_name(worktree), 1);
5975 got_ref_close(*branch_ref);
5978 if (*base_branch_ref) {
5979 got_ref_close(*base_branch_ref);
5980 *base_branch_ref = NULL;
5983 got_fileindex_free(*fileindex);
5986 lock_worktree(worktree, LOCK_SH);
5991 const struct got_error *
5992 got_worktree_integrate_continue(struct got_worktree *worktree,
5993 struct got_fileindex *fileindex, struct got_repository *repo,
5994 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5995 got_worktree_checkout_cb progress_cb, void *progress_arg,
5996 got_cancel_cb cancel_cb, void *cancel_arg)
5998 const struct got_error *err = NULL, *sync_err, *unlockerr;
5999 char *fileindex_path = NULL;
6000 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6002 err = get_fileindex_path(&fileindex_path, worktree);
6006 err = got_ref_resolve(&commit_id, repo, branch_ref);
6010 err = got_object_id_by_path(&tree_id, repo, commit_id,
6011 worktree->path_prefix);
6015 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6019 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6020 progress_cb, progress_arg, cancel_cb, cancel_arg);
6024 err = got_ref_change_ref(base_branch_ref, commit_id);
6028 err = got_ref_write(base_branch_ref, repo);
6030 sync_err = sync_fileindex(fileindex, fileindex_path);
6031 if (sync_err && err == NULL)
6035 unlockerr = got_ref_unlock(branch_ref);
6036 if (unlockerr && err == NULL)
6038 got_ref_close(branch_ref);
6040 unlockerr = got_ref_unlock(base_branch_ref);
6041 if (unlockerr && err == NULL)
6043 got_ref_close(base_branch_ref);
6045 got_fileindex_free(fileindex);
6046 free(fileindex_path);
6049 unlockerr = lock_worktree(worktree, LOCK_SH);
6050 if (unlockerr && err == NULL)
6055 const struct got_error *
6056 got_worktree_integrate_abort(struct got_worktree *worktree,
6057 struct got_fileindex *fileindex, struct got_repository *repo,
6058 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6060 const struct got_error *err = NULL, *unlockerr = NULL;
6062 got_fileindex_free(fileindex);
6064 err = lock_worktree(worktree, LOCK_SH);
6066 unlockerr = got_ref_unlock(branch_ref);
6067 if (unlockerr && err == NULL)
6069 got_ref_close(branch_ref);
6071 unlockerr = got_ref_unlock(base_branch_ref);
6072 if (unlockerr && err == NULL)
6074 got_ref_close(base_branch_ref);
6079 struct check_stage_ok_arg {
6080 struct got_object_id *head_commit_id;
6081 struct got_worktree *worktree;
6082 struct got_fileindex *fileindex;
6083 struct got_repository *repo;
6087 const struct got_error *
6088 check_stage_ok(void *arg, unsigned char status,
6089 unsigned char staged_status, const char *relpath,
6090 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6091 struct got_object_id *commit_id, int dirfd, const char *de_name)
6093 struct check_stage_ok_arg *a = arg;
6094 const struct got_error *err = NULL;
6095 struct got_fileindex_entry *ie;
6096 struct got_object_id base_commit_id;
6097 struct got_object_id *base_commit_idp = NULL;
6098 char *in_repo_path = NULL, *p;
6100 if (status == GOT_STATUS_UNVERSIONED ||
6101 status == GOT_STATUS_NO_CHANGE)
6103 if (status == GOT_STATUS_NONEXISTENT)
6104 return got_error_set_errno(ENOENT, relpath);
6106 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6108 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6110 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6111 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6113 return got_error_from_errno("asprintf");
6115 if (got_fileindex_entry_has_commit(ie)) {
6116 memcpy(base_commit_id.sha1, ie->commit_sha1,
6117 SHA1_DIGEST_LENGTH);
6118 base_commit_idp = &base_commit_id;
6121 if (status == GOT_STATUS_CONFLICT) {
6122 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6124 } else if (status != GOT_STATUS_ADD &&
6125 status != GOT_STATUS_MODIFY &&
6126 status != GOT_STATUS_DELETE) {
6127 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6131 a->have_changes = 1;
6136 err = check_out_of_date(p, status, staged_status,
6137 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6138 GOT_ERR_STAGE_OUT_OF_DATE);
6144 struct stage_path_arg {
6145 struct got_worktree *worktree;
6146 struct got_fileindex *fileindex;
6147 struct got_repository *repo;
6148 got_worktree_status_cb status_cb;
6150 got_worktree_patch_cb patch_cb;
6152 int staged_something;
6155 static const struct got_error *
6156 stage_path(void *arg, unsigned char status,
6157 unsigned char staged_status, const char *relpath,
6158 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6159 struct got_object_id *commit_id, int dirfd, const char *de_name)
6161 struct stage_path_arg *a = arg;
6162 const struct got_error *err = NULL;
6163 struct got_fileindex_entry *ie;
6164 char *ondisk_path = NULL, *path_content = NULL;
6166 struct got_object_id *new_staged_blob_id = NULL;
6168 if (status == GOT_STATUS_UNVERSIONED)
6171 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6173 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6175 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6177 return got_error_from_errno("asprintf");
6180 case GOT_STATUS_ADD:
6181 case GOT_STATUS_MODIFY:
6183 if (status == GOT_STATUS_ADD) {
6184 int choice = GOT_PATCH_CHOICE_NONE;
6185 err = (*a->patch_cb)(&choice, a->patch_arg,
6186 status, ie->path, NULL, 1, 1);
6189 if (choice != GOT_PATCH_CHOICE_YES)
6192 err = create_patched_content(&path_content, 0,
6193 staged_blob_id ? staged_blob_id : blob_id,
6194 ondisk_path, dirfd, de_name, ie->path,
6195 a->repo, a->patch_cb, a->patch_arg);
6196 if (err || path_content == NULL)
6200 err = got_object_blob_create(&new_staged_blob_id,
6201 path_content ? path_content : ondisk_path, a->repo);
6204 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6205 SHA1_DIGEST_LENGTH);
6206 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6207 stage = GOT_FILEIDX_STAGE_ADD;
6209 stage = GOT_FILEIDX_STAGE_MODIFY;
6210 got_fileindex_entry_stage_set(ie, stage);
6211 a->staged_something = 1;
6212 if (a->status_cb == NULL)
6214 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6215 get_staged_status(ie), relpath, blob_id,
6216 new_staged_blob_id, NULL, dirfd, de_name);
6218 case GOT_STATUS_DELETE:
6219 if (staged_status == GOT_STATUS_DELETE)
6222 int choice = GOT_PATCH_CHOICE_NONE;
6223 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6224 ie->path, NULL, 1, 1);
6227 if (choice == GOT_PATCH_CHOICE_NO)
6229 if (choice != GOT_PATCH_CHOICE_YES) {
6230 err = got_error(GOT_ERR_PATCH_CHOICE);
6234 stage = GOT_FILEIDX_STAGE_DELETE;
6235 got_fileindex_entry_stage_set(ie, stage);
6236 a->staged_something = 1;
6237 if (a->status_cb == NULL)
6239 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6240 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6243 case GOT_STATUS_NO_CHANGE:
6245 case GOT_STATUS_CONFLICT:
6246 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6248 case GOT_STATUS_NONEXISTENT:
6249 err = got_error_set_errno(ENOENT, relpath);
6252 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6256 if (path_content && unlink(path_content) == -1 && err == NULL)
6257 err = got_error_from_errno2("unlink", path_content);
6260 free(new_staged_blob_id);
6264 const struct got_error *
6265 got_worktree_stage(struct got_worktree *worktree,
6266 struct got_pathlist_head *paths,
6267 got_worktree_status_cb status_cb, void *status_arg,
6268 got_worktree_patch_cb patch_cb, void *patch_arg,
6269 struct got_repository *repo)
6271 const struct got_error *err = NULL, *sync_err, *unlockerr;
6272 struct got_pathlist_entry *pe;
6273 struct got_fileindex *fileindex = NULL;
6274 char *fileindex_path = NULL;
6275 struct got_reference *head_ref = NULL;
6276 struct got_object_id *head_commit_id = NULL;
6277 struct check_stage_ok_arg oka;
6278 struct stage_path_arg spa;
6280 err = lock_worktree(worktree, LOCK_EX);
6284 err = got_ref_open(&head_ref, repo,
6285 got_worktree_get_head_ref_name(worktree), 0);
6288 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6291 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6295 /* Check pre-conditions before staging anything. */
6296 oka.head_commit_id = head_commit_id;
6297 oka.worktree = worktree;
6298 oka.fileindex = fileindex;
6300 oka.have_changes = 0;
6301 TAILQ_FOREACH(pe, paths, entry) {
6302 err = worktree_status(worktree, pe->path, fileindex, repo,
6303 check_stage_ok, &oka, NULL, NULL, 0, 0);
6307 if (!oka.have_changes) {
6308 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6312 spa.worktree = worktree;
6313 spa.fileindex = fileindex;
6315 spa.patch_cb = patch_cb;
6316 spa.patch_arg = patch_arg;
6317 spa.status_cb = status_cb;
6318 spa.status_arg = status_arg;
6319 spa.staged_something = 0;
6320 TAILQ_FOREACH(pe, paths, entry) {
6321 err = worktree_status(worktree, pe->path, fileindex, repo,
6322 stage_path, &spa, NULL, NULL, 0, 0);
6326 if (!spa.staged_something) {
6327 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6331 sync_err = sync_fileindex(fileindex, fileindex_path);
6332 if (sync_err && err == NULL)
6336 got_ref_close(head_ref);
6337 free(head_commit_id);
6338 free(fileindex_path);
6340 got_fileindex_free(fileindex);
6341 unlockerr = lock_worktree(worktree, LOCK_SH);
6342 if (unlockerr && err == NULL)
6347 struct unstage_path_arg {
6348 struct got_worktree *worktree;
6349 struct got_fileindex *fileindex;
6350 struct got_repository *repo;
6351 got_worktree_checkout_cb progress_cb;
6353 got_worktree_patch_cb patch_cb;
6357 static const struct got_error *
6358 create_unstaged_content(char **path_unstaged_content,
6359 char **path_new_staged_content, struct got_object_id *blob_id,
6360 struct got_object_id *staged_blob_id, const char *relpath,
6361 struct got_repository *repo,
6362 got_worktree_patch_cb patch_cb, void *patch_arg)
6364 const struct got_error *err;
6365 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6366 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6367 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6368 struct stat sb1, sb2;
6369 struct got_diff_changes *changes = NULL;
6370 struct got_diff_state *ds = NULL;
6371 struct got_diff_args *args = NULL;
6372 struct got_diff_change *change;
6373 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6374 int have_content = 0, have_rejected_content = 0;
6376 *path_unstaged_content = NULL;
6377 *path_new_staged_content = NULL;
6379 err = got_object_id_str(&label1, blob_id);
6382 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6386 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6390 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6394 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6398 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6402 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6406 if (stat(path1, &sb1) == -1) {
6407 err = got_error_from_errno2("stat", path1);
6411 if (stat(path2, &sb2) == -1) {
6412 err = got_error_from_errno2("stat", path2);
6416 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6417 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6421 err = got_opentemp_named(path_unstaged_content, &outfile,
6422 "got-unstaged-content");
6425 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6426 "got-new-staged-content");
6430 if (fseek(f1, 0L, SEEK_SET) == -1) {
6431 err = got_ferror(f1, GOT_ERR_IO);
6434 if (fseek(f2, 0L, SEEK_SET) == -1) {
6435 err = got_ferror(f2, GOT_ERR_IO);
6438 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6440 err = apply_or_reject_change(&choice, change, ++n,
6441 changes->nchanges, ds, args, diff_flags, relpath,
6442 f1, f2, &line_cur1, &line_cur2,
6443 outfile, rejectfile, patch_cb, patch_arg);
6446 if (choice == GOT_PATCH_CHOICE_YES)
6449 have_rejected_content = 1;
6450 if (choice == GOT_PATCH_CHOICE_QUIT)
6453 if (have_content || have_rejected_content)
6454 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6455 outfile, rejectfile);
6459 got_object_blob_close(blob);
6461 got_object_blob_close(staged_blob);
6462 if (f1 && fclose(f1) == EOF && err == NULL)
6463 err = got_error_from_errno2("fclose", path1);
6464 if (f2 && fclose(f2) == EOF && err == NULL)
6465 err = got_error_from_errno2("fclose", path2);
6466 if (outfile && fclose(outfile) == EOF && err == NULL)
6467 err = got_error_from_errno2("fclose", *path_unstaged_content);
6468 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6469 err = got_error_from_errno2("fclose", *path_new_staged_content);
6470 if (path1 && unlink(path1) == -1 && err == NULL)
6471 err = got_error_from_errno2("unlink", path1);
6472 if (path2 && unlink(path2) == -1 && err == NULL)
6473 err = got_error_from_errno2("unlink", path2);
6474 if (err || !have_content) {
6475 if (*path_unstaged_content &&
6476 unlink(*path_unstaged_content) == -1 && err == NULL)
6477 err = got_error_from_errno2("unlink",
6478 *path_unstaged_content);
6479 free(*path_unstaged_content);
6480 *path_unstaged_content = NULL;
6482 if (err || !have_rejected_content) {
6483 if (*path_new_staged_content &&
6484 unlink(*path_new_staged_content) == -1 && err == NULL)
6485 err = got_error_from_errno2("unlink",
6486 *path_new_staged_content);
6487 free(*path_new_staged_content);
6488 *path_new_staged_content = NULL;
6492 got_diff_state_free(ds);
6496 got_diff_free_changes(changes);
6502 static const struct got_error *
6503 unstage_path(void *arg, unsigned char status,
6504 unsigned char staged_status, const char *relpath,
6505 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6506 struct got_object_id *commit_id, int dirfd, const char *de_name)
6508 const struct got_error *err = NULL;
6509 struct unstage_path_arg *a = arg;
6510 struct got_fileindex_entry *ie;
6511 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6512 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6513 char *path_new_staged_content = NULL;
6514 char *id_str = NULL, *label_orig = NULL;
6515 int local_changes_subsumed;
6518 if (staged_status != GOT_STATUS_ADD &&
6519 staged_status != GOT_STATUS_MODIFY &&
6520 staged_status != GOT_STATUS_DELETE)
6523 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6525 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6527 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6529 return got_error_from_errno("asprintf");
6531 err = got_object_id_str(&id_str,
6532 commit_id ? commit_id : a->worktree->base_commit_id);
6535 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6537 err = got_error_from_errno("asprintf");
6541 switch (staged_status) {
6542 case GOT_STATUS_MODIFY:
6543 err = got_object_open_as_blob(&blob_base, a->repo,
6548 case GOT_STATUS_ADD:
6550 if (staged_status == GOT_STATUS_ADD) {
6551 int choice = GOT_PATCH_CHOICE_NONE;
6552 err = (*a->patch_cb)(&choice, a->patch_arg,
6553 staged_status, ie->path, NULL, 1, 1);
6556 if (choice != GOT_PATCH_CHOICE_YES)
6559 err = create_unstaged_content(
6560 &path_unstaged_content,
6561 &path_new_staged_content, blob_id,
6562 staged_blob_id, ie->path, a->repo,
6563 a->patch_cb, a->patch_arg);
6564 if (err || path_unstaged_content == NULL)
6566 if (path_new_staged_content) {
6567 err = got_object_blob_create(
6569 path_new_staged_content,
6573 memcpy(ie->staged_blob_sha1,
6574 staged_blob_id->sha1,
6575 SHA1_DIGEST_LENGTH);
6577 err = merge_file(&local_changes_subsumed,
6578 a->worktree, blob_base, ondisk_path,
6579 relpath, got_fileindex_perms_to_st(ie),
6580 path_unstaged_content, label_orig,
6581 "unstaged", a->repo, a->progress_cb,
6584 path_new_staged_content == NULL)
6585 got_fileindex_entry_stage_set(ie,
6586 GOT_FILEIDX_STAGE_NONE);
6587 break; /* Done with this file. */
6590 err = got_object_open_as_blob(&blob_staged, a->repo,
6591 staged_blob_id, 8192);
6594 err = merge_blob(&local_changes_subsumed, a->worktree,
6595 blob_base, ondisk_path, relpath,
6596 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6597 commit_id ? commit_id : a->worktree->base_commit_id,
6598 a->repo, a->progress_cb, a->progress_arg);
6600 got_fileindex_entry_stage_set(ie,
6601 GOT_FILEIDX_STAGE_NONE);
6603 case GOT_STATUS_DELETE:
6605 int choice = GOT_PATCH_CHOICE_NONE;
6606 err = (*a->patch_cb)(&choice, a->patch_arg,
6607 staged_status, ie->path, NULL, 1, 1);
6610 if (choice == GOT_PATCH_CHOICE_NO)
6612 if (choice != GOT_PATCH_CHOICE_YES) {
6613 err = got_error(GOT_ERR_PATCH_CHOICE);
6617 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6618 err = get_file_status(&status, &sb, ie, ondisk_path,
6619 dirfd, de_name, a->repo);
6622 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6627 if (path_unstaged_content &&
6628 unlink(path_unstaged_content) == -1 && err == NULL)
6629 err = got_error_from_errno2("unlink", path_unstaged_content);
6630 if (path_new_staged_content &&
6631 unlink(path_new_staged_content) == -1 && err == NULL)
6632 err = got_error_from_errno2("unlink", path_new_staged_content);
6633 free(path_unstaged_content);
6634 free(path_new_staged_content);
6636 got_object_blob_close(blob_base);
6638 got_object_blob_close(blob_staged);
6644 const struct got_error *
6645 got_worktree_unstage(struct got_worktree *worktree,
6646 struct got_pathlist_head *paths,
6647 got_worktree_checkout_cb progress_cb, void *progress_arg,
6648 got_worktree_patch_cb patch_cb, void *patch_arg,
6649 struct got_repository *repo)
6651 const struct got_error *err = NULL, *sync_err, *unlockerr;
6652 struct got_pathlist_entry *pe;
6653 struct got_fileindex *fileindex = NULL;
6654 char *fileindex_path = NULL;
6655 struct unstage_path_arg upa;
6657 err = lock_worktree(worktree, LOCK_EX);
6661 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6665 upa.worktree = worktree;
6666 upa.fileindex = fileindex;
6668 upa.progress_cb = progress_cb;
6669 upa.progress_arg = progress_arg;
6670 upa.patch_cb = patch_cb;
6671 upa.patch_arg = patch_arg;
6672 TAILQ_FOREACH(pe, paths, entry) {
6673 err = worktree_status(worktree, pe->path, fileindex, repo,
6674 unstage_path, &upa, NULL, NULL, 0, 0);
6679 sync_err = sync_fileindex(fileindex, fileindex_path);
6680 if (sync_err && err == NULL)
6683 free(fileindex_path);
6685 got_fileindex_free(fileindex);
6686 unlockerr = lock_worktree(worktree, LOCK_SH);
6687 if (unlockerr && err == NULL)