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);
1306 if (status == GOT_STATUS_CONFLICT) {
1307 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1312 if (ie && status != GOT_STATUS_MISSING &&
1313 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1314 if (got_fileindex_entry_has_commit(ie) &&
1315 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1316 SHA1_DIGEST_LENGTH) == 0) {
1317 err = sync_timestamps(ondisk_path, status, ie, &sb);
1320 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1324 if (got_fileindex_entry_has_blob(ie) &&
1325 memcmp(ie->blob_sha1, te->id.sha1,
1326 SHA1_DIGEST_LENGTH) == 0) {
1327 err = sync_timestamps(ondisk_path, status, ie, &sb);
1332 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1336 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1337 int update_timestamps;
1338 struct got_blob_object *blob2 = NULL;
1339 char *label_orig = NULL;
1340 if (got_fileindex_entry_has_blob(ie)) {
1341 struct got_object_id id2;
1342 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1343 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1347 if (got_fileindex_entry_has_commit(ie)) {
1348 char id_str[SHA1_DIGEST_STRING_LENGTH];
1349 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1350 sizeof(id_str)) == NULL) {
1351 err = got_error_path(id_str,
1352 GOT_ERR_BAD_OBJ_ID_STR);
1355 if (asprintf(&label_orig, "%s: commit %s",
1356 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1357 err = got_error_from_errno("asprintf");
1361 err = merge_blob(&update_timestamps, worktree, blob2,
1362 ondisk_path, path, sb.st_mode, label_orig, blob,
1363 worktree->base_commit_id, repo,
1364 progress_cb, progress_arg);
1367 got_object_blob_close(blob2);
1371 * Do not update timestamps of files with local changes.
1372 * Otherwise, a future status walk would treat them as
1373 * unmodified files again.
1375 err = got_fileindex_entry_update(ie, ondisk_path,
1376 blob->id.sha1, worktree->base_commit_id->sha1,
1378 } else if (status == GOT_STATUS_MODE_CHANGE) {
1379 err = got_fileindex_entry_update(ie, ondisk_path,
1380 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1381 } else if (status == GOT_STATUS_DELETE) {
1382 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1385 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1386 ondisk_path, path, blob, 0);
1390 err = install_blob(worktree, ondisk_path, path, te->mode,
1391 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1392 repo, progress_cb, progress_arg);
1395 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1396 ondisk_path, path, blob, 1);
1400 got_object_blob_close(blob);
1406 static const struct got_error *
1407 remove_ondisk_file(const char *root_path, const char *path)
1409 const struct got_error *err = NULL;
1410 char *ondisk_path = NULL;
1412 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1413 return got_error_from_errno("asprintf");
1415 if (unlink(ondisk_path) == -1) {
1416 if (errno != ENOENT)
1417 err = got_error_from_errno2("unlink", ondisk_path);
1419 char *parent = dirname(ondisk_path);
1420 while (parent && strcmp(parent, root_path) != 0) {
1421 if (rmdir(parent) == -1) {
1422 if (errno != ENOTEMPTY)
1423 err = got_error_from_errno2("rmdir",
1427 parent = dirname(parent);
1434 static const struct got_error *
1435 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1436 struct got_fileindex_entry *ie, struct got_repository *repo,
1437 got_worktree_checkout_cb progress_cb, void *progress_arg)
1439 const struct got_error *err = NULL;
1440 unsigned char status;
1444 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1445 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1447 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1449 return got_error_from_errno("asprintf");
1451 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1455 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1456 status == GOT_STATUS_ADD) {
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1461 * Preserve the working file and change the deleted blob's
1462 * entry into a schedule-add entry.
1464 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1469 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1472 if (status == GOT_STATUS_NO_CHANGE) {
1473 err = remove_ondisk_file(worktree->root_path, ie->path);
1477 got_fileindex_entry_remove(fileindex, ie);
1483 struct diff_cb_arg {
1484 struct got_fileindex *fileindex;
1485 struct got_worktree *worktree;
1486 struct got_repository *repo;
1487 got_worktree_checkout_cb progress_cb;
1489 got_cancel_cb cancel_cb;
1493 static const struct got_error *
1494 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1495 struct got_tree_entry *te, const char *parent_path)
1497 struct diff_cb_arg *a = arg;
1499 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1500 return got_error(GOT_ERR_CANCELLED);
1502 return update_blob(a->worktree, a->fileindex, ie, te,
1503 ie->path, a->repo, a->progress_cb, a->progress_arg);
1506 static const struct got_error *
1507 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1509 struct diff_cb_arg *a = arg;
1511 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1512 return got_error(GOT_ERR_CANCELLED);
1514 return delete_blob(a->worktree, a->fileindex, ie,
1515 a->repo, a->progress_cb, a->progress_arg);
1518 static const struct got_error *
1519 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1521 struct diff_cb_arg *a = arg;
1522 const struct got_error *err;
1525 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1526 return got_error(GOT_ERR_CANCELLED);
1528 if (got_object_tree_entry_is_submodule(te))
1531 if (asprintf(&path, "%s%s%s", parent_path,
1532 parent_path[0] ? "/" : "", te->name)
1534 return got_error_from_errno("asprintf");
1536 if (S_ISDIR(te->mode))
1537 err = add_dir_on_disk(a->worktree, path);
1539 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1540 a->repo, a->progress_cb, a->progress_arg);
1546 static const struct got_error *
1547 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1549 const struct got_error *err = NULL;
1550 char *uuidstr = NULL;
1551 uint32_t uuid_status;
1555 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1556 if (uuid_status != uuid_s_ok)
1557 return got_error_uuid(uuid_status, "uuid_to_string");
1559 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1561 err = got_error_from_errno("asprintf");
1568 const struct got_error *
1569 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1571 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1574 static const struct got_error *
1575 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1577 return get_ref_name(refname, worktree,
1578 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1581 static const struct got_error *
1582 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1584 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1587 static const struct got_error *
1588 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1590 return get_ref_name(refname, worktree,
1591 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1594 static const struct got_error *
1595 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1597 return get_ref_name(refname, worktree,
1598 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1601 static const struct got_error *
1602 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1604 return get_ref_name(refname, worktree,
1605 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1608 static const struct got_error *
1609 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1611 return get_ref_name(refname, worktree,
1612 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1615 static const struct got_error *
1616 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1618 return get_ref_name(refname, worktree,
1619 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1622 static const struct got_error *
1623 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1625 return get_ref_name(refname, worktree,
1626 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1629 const struct got_error *
1630 got_worktree_get_histedit_script_path(char **path,
1631 struct got_worktree *worktree)
1633 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1634 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1636 return got_error_from_errno("asprintf");
1642 * Prevent Git's garbage collector from deleting our base commit by
1643 * setting a reference to our base commit's ID.
1645 static const struct got_error *
1646 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1648 const struct got_error *err = NULL;
1649 struct got_reference *ref = NULL;
1652 err = got_worktree_get_base_ref_name(&refname, worktree);
1656 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1660 err = got_ref_write(ref, repo);
1668 static const struct got_error *
1669 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1671 const struct got_error *err = NULL;
1673 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1674 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1675 err = got_error_from_errno("asprintf");
1676 *fileindex_path = NULL;
1682 static const struct got_error *
1683 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1684 struct got_worktree *worktree)
1686 const struct got_error *err = NULL;
1689 *fileindex_path = NULL;
1690 *fileindex = got_fileindex_alloc();
1691 if (*fileindex == NULL)
1692 return got_error_from_errno("got_fileindex_alloc");
1694 err = get_fileindex_path(fileindex_path, worktree);
1698 index = fopen(*fileindex_path, "rb");
1699 if (index == NULL) {
1700 if (errno != ENOENT)
1701 err = got_error_from_errno2("fopen", *fileindex_path);
1703 err = got_fileindex_read(*fileindex, index);
1704 if (fclose(index) != 0 && err == NULL)
1705 err = got_error_from_errno("fclose");
1709 free(*fileindex_path);
1710 *fileindex_path = NULL;
1711 got_fileindex_free(*fileindex);
1717 struct bump_base_commit_id_arg {
1718 struct got_object_id *base_commit_id;
1721 const char *entry_name;
1722 got_worktree_checkout_cb progress_cb;
1726 /* Bump base commit ID of all files within an updated part of the work tree. */
1727 static const struct got_error *
1728 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1730 const struct got_error *err;
1731 struct bump_base_commit_id_arg *a = arg;
1733 if (a->entry_name) {
1734 if (strcmp(ie->path, a->path) != 0)
1736 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1739 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1740 SHA1_DIGEST_LENGTH) == 0)
1743 if (a->progress_cb) {
1744 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1749 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1753 static const struct got_error *
1754 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1756 const struct got_error *err = NULL;
1757 char *new_fileindex_path = NULL;
1758 FILE *new_index = NULL;
1759 struct timespec timeout;
1761 err = got_opentemp_named(&new_fileindex_path, &new_index,
1766 err = got_fileindex_write(fileindex, new_index);
1770 if (rename(new_fileindex_path, fileindex_path) != 0) {
1771 err = got_error_from_errno3("rename", new_fileindex_path,
1773 unlink(new_fileindex_path);
1777 * Sleep for a short amount of time to ensure that files modified after
1778 * this program exits have a different time stamp from the one which
1779 * was recorded in the file index.
1782 timeout.tv_nsec = 1;
1783 nanosleep(&timeout, NULL);
1787 free(new_fileindex_path);
1791 static const struct got_error *
1792 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1793 struct got_object_id **tree_id, const char *wt_relpath,
1794 struct got_worktree *worktree, struct got_repository *repo)
1796 const struct got_error *err = NULL;
1797 struct got_object_id *id = NULL;
1798 char *in_repo_path = NULL;
1799 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1801 *entry_type = GOT_OBJ_TYPE_ANY;
1802 *tree_relpath = NULL;
1805 if (wt_relpath[0] == '\0') {
1806 /* Check out all files within the work tree. */
1807 *entry_type = GOT_OBJ_TYPE_TREE;
1808 *tree_relpath = strdup("");
1809 if (*tree_relpath == NULL) {
1810 err = got_error_from_errno("strdup");
1813 err = got_object_id_by_path(tree_id, repo,
1814 worktree->base_commit_id, worktree->path_prefix);
1820 /* Check out a subset of files in the work tree. */
1822 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1823 is_root_wt ? "" : "/", wt_relpath) == -1) {
1824 err = got_error_from_errno("asprintf");
1828 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1834 in_repo_path = NULL;
1836 err = got_object_get_type(entry_type, repo, id);
1840 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1841 /* Check out a single file. */
1842 if (strchr(wt_relpath, '/') == NULL) {
1843 /* Check out a single file in work tree's root dir. */
1844 in_repo_path = strdup(worktree->path_prefix);
1845 if (in_repo_path == NULL) {
1846 err = got_error_from_errno("strdup");
1849 *tree_relpath = strdup("");
1850 if (*tree_relpath == NULL) {
1851 err = got_error_from_errno("strdup");
1855 /* Check out a single file in a subdirectory. */
1856 err = got_path_dirname(tree_relpath, wt_relpath);
1859 if (asprintf(&in_repo_path, "%s%s%s",
1860 worktree->path_prefix, is_root_wt ? "" : "/",
1861 *tree_relpath) == -1) {
1862 err = got_error_from_errno("asprintf");
1866 err = got_object_id_by_path(tree_id, repo,
1867 worktree->base_commit_id, in_repo_path);
1869 /* Check out all files within a subdirectory. */
1870 *tree_id = got_object_id_dup(id);
1871 if (*tree_id == NULL) {
1872 err = got_error_from_errno("got_object_id_dup");
1875 *tree_relpath = strdup(wt_relpath);
1876 if (*tree_relpath == NULL) {
1877 err = got_error_from_errno("strdup");
1885 *entry_type = GOT_OBJ_TYPE_ANY;
1886 free(*tree_relpath);
1887 *tree_relpath = NULL;
1894 static const struct got_error *
1895 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1896 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1897 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1898 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1900 const struct got_error *err = NULL;
1901 struct got_commit_object *commit = NULL;
1902 struct got_tree_object *tree = NULL;
1903 struct got_fileindex_diff_tree_cb diff_cb;
1904 struct diff_cb_arg arg;
1906 err = ref_base_commit(worktree, repo);
1908 if (!(err->code == GOT_ERR_ERRNO &&
1909 (errno == EACCES || errno == EROFS)))
1911 err = (*progress_cb)(progress_arg,
1912 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1917 err = got_object_open_as_commit(&commit, repo,
1918 worktree->base_commit_id);
1922 err = got_object_open_as_tree(&tree, repo, tree_id);
1927 got_object_tree_find_entry(tree, entry_name) == NULL) {
1928 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1932 diff_cb.diff_old_new = diff_old_new;
1933 diff_cb.diff_old = diff_old;
1934 diff_cb.diff_new = diff_new;
1935 arg.fileindex = fileindex;
1936 arg.worktree = worktree;
1938 arg.progress_cb = progress_cb;
1939 arg.progress_arg = progress_arg;
1940 arg.cancel_cb = cancel_cb;
1941 arg.cancel_arg = cancel_arg;
1942 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1943 entry_name, repo, &diff_cb, &arg);
1946 got_object_tree_close(tree);
1948 got_object_commit_close(commit);
1952 const struct got_error *
1953 got_worktree_checkout_files(struct got_worktree *worktree,
1954 struct got_pathlist_head *paths, struct got_repository *repo,
1955 got_worktree_checkout_cb progress_cb, void *progress_arg,
1956 got_cancel_cb cancel_cb, void *cancel_arg)
1958 const struct got_error *err = NULL, *sync_err, *unlockerr;
1959 struct got_commit_object *commit = NULL;
1960 struct got_tree_object *tree = NULL;
1961 struct got_fileindex *fileindex = NULL;
1962 char *fileindex_path = NULL;
1963 struct got_pathlist_entry *pe;
1964 struct tree_path_data {
1965 SIMPLEQ_ENTRY(tree_path_data) entry;
1966 struct got_object_id *tree_id;
1971 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1973 SIMPLEQ_INIT(&tree_paths);
1975 err = lock_worktree(worktree, LOCK_EX);
1979 /* Map all specified paths to in-repository trees. */
1980 TAILQ_FOREACH(pe, paths, entry) {
1981 tpd = malloc(sizeof(*tpd));
1983 err = got_error_from_errno("malloc");
1987 err = find_tree_entry_for_checkout(&tpd->entry_type,
1988 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1994 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1995 err = got_path_basename(&tpd->entry_name, pe->path);
2003 tpd->entry_name = NULL;
2005 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2009 * Read the file index.
2010 * Checking out files is supposed to be an idempotent operation.
2011 * If the on-disk file index is incomplete we will try to complete it.
2013 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2017 tpd = SIMPLEQ_FIRST(&tree_paths);
2018 TAILQ_FOREACH(pe, paths, entry) {
2019 struct bump_base_commit_id_arg bbc_arg;
2021 err = checkout_files(worktree, fileindex, tpd->relpath,
2022 tpd->tree_id, tpd->entry_name, repo,
2023 progress_cb, progress_arg, cancel_cb, cancel_arg);
2027 bbc_arg.base_commit_id = worktree->base_commit_id;
2028 bbc_arg.entry_name = tpd->entry_name;
2029 bbc_arg.path = pe->path;
2030 bbc_arg.path_len = pe->path_len;
2031 bbc_arg.progress_cb = progress_cb;
2032 bbc_arg.progress_arg = progress_arg;
2033 err = got_fileindex_for_each_entry_safe(fileindex,
2034 bump_base_commit_id, &bbc_arg);
2038 tpd = SIMPLEQ_NEXT(tpd, entry);
2040 sync_err = sync_fileindex(fileindex, fileindex_path);
2041 if (sync_err && err == NULL)
2044 free(fileindex_path);
2046 got_object_tree_close(tree);
2048 got_object_commit_close(commit);
2050 got_fileindex_free(fileindex);
2051 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2052 tpd = SIMPLEQ_FIRST(&tree_paths);
2053 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2058 unlockerr = lock_worktree(worktree, LOCK_SH);
2059 if (unlockerr && err == NULL)
2064 struct merge_file_cb_arg {
2065 struct got_worktree *worktree;
2066 struct got_fileindex *fileindex;
2067 got_worktree_checkout_cb progress_cb;
2069 got_cancel_cb cancel_cb;
2071 const char *label_orig;
2072 struct got_object_id *commit_id2;
2075 static const struct got_error *
2076 merge_file_cb(void *arg, struct got_blob_object *blob1,
2077 struct got_blob_object *blob2, struct got_object_id *id1,
2078 struct got_object_id *id2, const char *path1, const char *path2,
2079 mode_t mode1, mode_t mode2, struct got_repository *repo)
2081 static const struct got_error *err = NULL;
2082 struct merge_file_cb_arg *a = arg;
2083 struct got_fileindex_entry *ie;
2084 char *ondisk_path = NULL;
2086 unsigned char status;
2087 int local_changes_subsumed;
2089 if (blob1 && blob2) {
2090 ie = got_fileindex_entry_get(a->fileindex, path2,
2093 return (*a->progress_cb)(a->progress_arg,
2094 GOT_STATUS_MISSING, path2);
2096 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2098 return got_error_from_errno("asprintf");
2100 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2105 if (status == GOT_STATUS_DELETE) {
2106 err = (*a->progress_cb)(a->progress_arg,
2107 GOT_STATUS_MERGE, path2);
2110 if (status != GOT_STATUS_NO_CHANGE &&
2111 status != GOT_STATUS_MODIFY &&
2112 status != GOT_STATUS_CONFLICT &&
2113 status != GOT_STATUS_ADD) {
2114 err = (*a->progress_cb)(a->progress_arg, status, path2);
2118 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2119 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2120 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2122 ie = got_fileindex_entry_get(a->fileindex, path1,
2125 return (*a->progress_cb)(a->progress_arg,
2126 GOT_STATUS_MISSING, path1);
2128 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2130 return got_error_from_errno("asprintf");
2132 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2138 case GOT_STATUS_NO_CHANGE:
2139 err = (*a->progress_cb)(a->progress_arg,
2140 GOT_STATUS_DELETE, path1);
2143 err = remove_ondisk_file(a->worktree->root_path, path1);
2147 got_fileindex_entry_mark_deleted_from_disk(ie);
2149 case GOT_STATUS_DELETE:
2150 case GOT_STATUS_MISSING:
2151 err = (*a->progress_cb)(a->progress_arg,
2152 GOT_STATUS_DELETE, path1);
2156 got_fileindex_entry_mark_deleted_from_disk(ie);
2158 case GOT_STATUS_ADD:
2159 case GOT_STATUS_MODIFY:
2160 case GOT_STATUS_CONFLICT:
2161 err = (*a->progress_cb)(a->progress_arg,
2162 GOT_STATUS_CANNOT_DELETE, path1);
2166 case GOT_STATUS_OBSTRUCTED:
2167 err = (*a->progress_cb)(a->progress_arg, status, path1);
2175 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2177 return got_error_from_errno("asprintf");
2178 ie = got_fileindex_entry_get(a->fileindex, path2,
2181 err = get_file_status(&status, &sb, ie, ondisk_path,
2185 if (status != GOT_STATUS_NO_CHANGE &&
2186 status != GOT_STATUS_MODIFY &&
2187 status != GOT_STATUS_CONFLICT &&
2188 status != GOT_STATUS_ADD) {
2189 err = (*a->progress_cb)(a->progress_arg,
2193 err = merge_blob(&local_changes_subsumed, a->worktree,
2194 NULL, ondisk_path, path2, sb.st_mode,
2195 a->label_orig, blob2, a->commit_id2, repo,
2198 if (status == GOT_STATUS_DELETE) {
2199 err = update_blob_fileindex_entry(a->worktree,
2200 a->fileindex, ie, ondisk_path, ie->path,
2206 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2207 err = install_blob(a->worktree, ondisk_path, path2,
2208 /* XXX get this from parent tree! */
2209 GOT_DEFAULT_FILE_MODE,
2210 sb.st_mode, blob2, 0, 0, repo,
2211 a->progress_cb, a->progress_arg);
2214 err = got_fileindex_entry_alloc(&ie, path2);
2217 err = got_fileindex_entry_update(ie, ondisk_path,
2220 got_fileindex_entry_free(ie);
2223 err = got_fileindex_entry_add(a->fileindex, ie);
2225 got_fileindex_entry_free(ie);
2235 struct check_merge_ok_arg {
2236 struct got_worktree *worktree;
2237 struct got_repository *repo;
2240 static const struct got_error *
2241 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2243 const struct got_error *err = NULL;
2244 struct check_merge_ok_arg *a = arg;
2245 unsigned char status;
2249 /* Reject merges into a work tree with mixed base commits. */
2250 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2251 SHA1_DIGEST_LENGTH))
2252 return got_error(GOT_ERR_MIXED_COMMITS);
2254 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2256 return got_error_from_errno("asprintf");
2258 /* Reject merges into a work tree with conflicted files. */
2259 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2262 if (status == GOT_STATUS_CONFLICT)
2263 return got_error(GOT_ERR_CONFLICTS);
2268 static const struct got_error *
2269 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2270 const char *fileindex_path, struct got_object_id *commit_id1,
2271 struct got_object_id *commit_id2, struct got_repository *repo,
2272 got_worktree_checkout_cb progress_cb, void *progress_arg,
2273 got_cancel_cb cancel_cb, void *cancel_arg)
2275 const struct got_error *err = NULL, *sync_err;
2276 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2277 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2278 struct merge_file_cb_arg arg;
2279 char *label_orig = NULL;
2284 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2285 worktree->path_prefix);
2289 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2293 err = got_object_id_str(&id_str, commit_id1);
2297 if (asprintf(&label_orig, "%s: commit %s",
2298 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2299 err = got_error_from_errno("asprintf");
2306 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2307 worktree->path_prefix);
2311 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2315 arg.worktree = worktree;
2316 arg.fileindex = fileindex;
2317 arg.progress_cb = progress_cb;
2318 arg.progress_arg = progress_arg;
2319 arg.cancel_cb = cancel_cb;
2320 arg.cancel_arg = cancel_arg;
2321 arg.label_orig = label_orig;
2322 arg.commit_id2 = commit_id2;
2323 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2324 sync_err = sync_fileindex(fileindex, fileindex_path);
2325 if (sync_err && err == NULL)
2329 got_object_tree_close(tree1);
2331 got_object_tree_close(tree2);
2336 const struct got_error *
2337 got_worktree_merge_files(struct got_worktree *worktree,
2338 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2339 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2340 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2342 const struct got_error *err, *unlockerr;
2343 char *fileindex_path = NULL;
2344 struct got_fileindex *fileindex = NULL;
2345 struct check_merge_ok_arg mok_arg;
2347 err = lock_worktree(worktree, LOCK_EX);
2351 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2355 mok_arg.worktree = worktree;
2356 mok_arg.repo = repo;
2357 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2362 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2363 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2366 got_fileindex_free(fileindex);
2367 free(fileindex_path);
2368 unlockerr = lock_worktree(worktree, LOCK_SH);
2369 if (unlockerr && err == NULL)
2374 struct diff_dir_cb_arg {
2375 struct got_fileindex *fileindex;
2376 struct got_worktree *worktree;
2377 const char *status_path;
2378 size_t status_path_len;
2379 struct got_repository *repo;
2380 got_worktree_status_cb status_cb;
2382 got_cancel_cb cancel_cb;
2384 /* A pathlist containing per-directory pathlists of ignore patterns. */
2385 struct got_pathlist_head ignores;
2386 int report_unchanged;
2389 static const struct got_error *
2390 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2391 int dirfd, const char *de_name,
2392 got_worktree_status_cb status_cb, void *status_arg,
2393 struct got_repository *repo, int report_unchanged)
2395 const struct got_error *err = NULL;
2396 unsigned char status = GOT_STATUS_NO_CHANGE;
2397 unsigned char staged_status = get_staged_status(ie);
2399 struct got_object_id blob_id, commit_id, staged_blob_id;
2400 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2401 struct got_object_id *staged_blob_idp = NULL;
2403 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2407 if (status == GOT_STATUS_NO_CHANGE &&
2408 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2411 if (got_fileindex_entry_has_blob(ie)) {
2412 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2413 blob_idp = &blob_id;
2415 if (got_fileindex_entry_has_commit(ie)) {
2416 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2417 commit_idp = &commit_id;
2419 if (staged_status == GOT_STATUS_ADD ||
2420 staged_status == GOT_STATUS_MODIFY) {
2421 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2422 SHA1_DIGEST_LENGTH);
2423 staged_blob_idp = &staged_blob_id;
2426 return (*status_cb)(status_arg, status, staged_status,
2427 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2430 static const struct got_error *
2431 status_old_new(void *arg, struct got_fileindex_entry *ie,
2432 struct dirent *de, const char *parent_path, int dirfd)
2434 const struct got_error *err = NULL;
2435 struct diff_dir_cb_arg *a = arg;
2438 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2439 return got_error(GOT_ERR_CANCELLED);
2441 if (got_path_cmp(parent_path, a->status_path,
2442 strlen(parent_path), a->status_path_len) != 0 &&
2443 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2446 if (parent_path[0]) {
2447 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2448 parent_path, de->d_name) == -1)
2449 return got_error_from_errno("asprintf");
2451 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2453 return got_error_from_errno("asprintf");
2456 err = report_file_status(ie, abspath, dirfd, de->d_name,
2457 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2462 static const struct got_error *
2463 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2465 struct diff_dir_cb_arg *a = arg;
2466 struct got_object_id blob_id, commit_id;
2467 unsigned char status;
2469 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2470 return got_error(GOT_ERR_CANCELLED);
2472 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2475 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2476 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2477 if (got_fileindex_entry_has_file_on_disk(ie))
2478 status = GOT_STATUS_MISSING;
2480 status = GOT_STATUS_DELETE;
2481 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2482 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2486 free_ignorelist(struct got_pathlist_head *ignorelist)
2488 struct got_pathlist_entry *pe;
2490 TAILQ_FOREACH(pe, ignorelist, entry)
2491 free((char *)pe->path);
2492 got_pathlist_free(ignorelist);
2496 free_ignores(struct got_pathlist_head *ignores)
2498 struct got_pathlist_entry *pe;
2500 TAILQ_FOREACH(pe, ignores, entry) {
2501 struct got_pathlist_head *ignorelist = pe->data;
2502 free_ignorelist(ignorelist);
2503 free((char *)pe->path);
2505 got_pathlist_free(ignores);
2508 static const struct got_error *
2509 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2511 const struct got_error *err = NULL;
2512 struct got_pathlist_entry *pe = NULL;
2513 struct got_pathlist_head *ignorelist;
2514 char *line = NULL, *pattern, *dirpath = NULL;
2515 size_t linesize = 0;
2518 ignorelist = calloc(1, sizeof(*ignorelist));
2519 if (ignorelist == NULL)
2520 return got_error_from_errno("calloc");
2521 TAILQ_INIT(ignorelist);
2523 while ((linelen = getline(&line, &linesize, f)) != -1) {
2524 if (linelen > 0 && line[linelen - 1] == '\n')
2525 line[linelen - 1] = '\0';
2527 /* Git's ignores may contain comments. */
2531 /* Git's negated patterns are not (yet?) supported. */
2535 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2537 err = got_error_from_errno("asprintf");
2540 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2545 err = got_error_from_errno("getline");
2549 dirpath = strdup(path);
2550 if (dirpath == NULL) {
2551 err = got_error_from_errno("strdup");
2554 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2557 if (err || pe == NULL) {
2559 free_ignorelist(ignorelist);
2565 match_ignores(struct got_pathlist_head *ignores, const char *path)
2567 struct got_pathlist_entry *pe;
2569 /* Handle patterns which match in all directories. */
2570 TAILQ_FOREACH(pe, ignores, entry) {
2571 struct got_pathlist_head *ignorelist = pe->data;
2572 struct got_pathlist_entry *pi;
2574 TAILQ_FOREACH(pi, ignorelist, entry) {
2575 const char *p, *pattern = pi->path;
2577 if (strncmp(pattern, "**/", 3) != 0)
2582 if (fnmatch(pattern, p,
2583 FNM_PATHNAME | FNM_LEADING_DIR)) {
2584 /* Retry in next directory. */
2585 while (*p && *p != '/')
2597 * The ignores pathlist contains ignore lists from children before
2598 * parents, so we can find the most specific ignorelist by walking
2599 * ignores backwards.
2601 pe = TAILQ_LAST(ignores, got_pathlist_head);
2603 if (got_path_is_child(path, pe->path, pe->path_len)) {
2604 struct got_pathlist_head *ignorelist = pe->data;
2605 struct got_pathlist_entry *pi;
2606 TAILQ_FOREACH(pi, ignorelist, entry) {
2607 const char *pattern = pi->path;
2608 int flags = FNM_LEADING_DIR;
2609 if (strstr(pattern, "/**/") == NULL)
2610 flags |= FNM_PATHNAME;
2611 if (fnmatch(pattern, path, flags))
2616 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2622 static const struct got_error *
2623 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2624 const char *path, int dirfd, const char *ignores_filename)
2626 const struct got_error *err = NULL;
2629 FILE *ignoresfile = NULL;
2631 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2632 path[0] ? "/" : "", ignores_filename) == -1)
2633 return got_error_from_errno("asprintf");
2636 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2638 if (errno != ENOENT && errno != EACCES)
2639 err = got_error_from_errno2("openat",
2642 ignoresfile = fdopen(fd, "r");
2643 if (ignoresfile == NULL)
2644 err = got_error_from_errno2("fdopen",
2648 err = read_ignores(ignores, path, ignoresfile);
2652 ignoresfile = fopen(ignorespath, "r");
2653 if (ignoresfile == NULL) {
2654 if (errno != ENOENT && errno != EACCES)
2655 err = got_error_from_errno2("fopen",
2658 err = read_ignores(ignores, path, ignoresfile);
2661 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2662 err = got_error_from_errno2("fclose", path);
2663 if (fd != -1 && close(fd) == -1 && err == NULL)
2664 err = got_error_from_errno2("close", path);
2669 static const struct got_error *
2670 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2672 const struct got_error *err = NULL;
2673 struct diff_dir_cb_arg *a = arg;
2676 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2677 return got_error(GOT_ERR_CANCELLED);
2679 /* XXX ignore symlinks for now */
2680 if (de->d_type == DT_LNK)
2683 if (parent_path[0]) {
2684 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2685 return got_error_from_errno("asprintf");
2690 if (de->d_type == DT_DIR) {
2691 int subdirfd = openat(dirfd, de->d_name,
2692 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2693 if (subdirfd == -1) {
2694 if (errno != ENOENT && errno != EACCES)
2695 err = got_error_from_errno2("openat", path);
2697 err = add_ignores(&a->ignores, a->worktree->root_path,
2698 path, subdirfd, ".cvsignore");
2700 err = add_ignores(&a->ignores,
2701 a->worktree->root_path, path,
2702 subdirfd, ".gitignore");
2703 if (close(subdirfd) == -1 && err == NULL)
2704 err = got_error_from_errno2("close", path);
2706 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2707 && !match_ignores(&a->ignores, path))
2708 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2709 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2715 static const struct got_error *
2716 report_single_file_status(const char *path, const char *ondisk_path,
2717 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2718 void *status_arg, struct got_repository *repo, int report_unchanged)
2720 struct got_fileindex_entry *ie;
2723 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2725 return report_file_status(ie, ondisk_path, -1, NULL,
2726 status_cb, status_arg, repo, report_unchanged);
2728 if (lstat(ondisk_path, &sb) == -1) {
2729 if (errno != ENOENT)
2730 return got_error_from_errno2("lstat", ondisk_path);
2731 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2732 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2736 if (S_ISREG(sb.st_mode))
2737 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2738 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2743 static const struct got_error *
2744 worktree_status(struct got_worktree *worktree, const char *path,
2745 struct got_fileindex *fileindex, struct got_repository *repo,
2746 got_worktree_status_cb status_cb, void *status_arg,
2747 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2748 int report_unchanged)
2750 const struct got_error *err = NULL;
2752 struct got_fileindex_diff_dir_cb fdiff_cb;
2753 struct diff_dir_cb_arg arg;
2754 char *ondisk_path = NULL;
2756 if (asprintf(&ondisk_path, "%s%s%s",
2757 worktree->root_path, path[0] ? "/" : "", path) == -1)
2758 return got_error_from_errno("asprintf");
2760 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2762 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2763 err = got_error_from_errno2("open", ondisk_path);
2765 err = report_single_file_status(path, ondisk_path,
2766 fileindex, status_cb, status_arg, repo,
2769 fdiff_cb.diff_old_new = status_old_new;
2770 fdiff_cb.diff_old = status_old;
2771 fdiff_cb.diff_new = status_new;
2772 arg.fileindex = fileindex;
2773 arg.worktree = worktree;
2774 arg.status_path = path;
2775 arg.status_path_len = strlen(path);
2777 arg.status_cb = status_cb;
2778 arg.status_arg = status_arg;
2779 arg.cancel_cb = cancel_cb;
2780 arg.cancel_arg = cancel_arg;
2781 arg.report_unchanged = report_unchanged;
2782 TAILQ_INIT(&arg.ignores);
2784 err = add_ignores(&arg.ignores, worktree->root_path,
2785 path, fd, ".cvsignore");
2787 err = add_ignores(&arg.ignores,
2788 worktree->root_path, path, fd,
2792 err = got_fileindex_diff_dir(fileindex, fd,
2793 worktree->root_path, path, repo, &fdiff_cb, &arg);
2794 free_ignores(&arg.ignores);
2797 if (fd != -1 && close(fd) != 0 && err == NULL)
2798 err = got_error_from_errno("close");
2803 const struct got_error *
2804 got_worktree_status(struct got_worktree *worktree,
2805 struct got_pathlist_head *paths, struct got_repository *repo,
2806 got_worktree_status_cb status_cb, void *status_arg,
2807 got_cancel_cb cancel_cb, void *cancel_arg)
2809 const struct got_error *err = NULL;
2810 char *fileindex_path = NULL;
2811 struct got_fileindex *fileindex = NULL;
2812 struct got_pathlist_entry *pe;
2814 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2818 TAILQ_FOREACH(pe, paths, entry) {
2819 err = worktree_status(worktree, pe->path, fileindex, repo,
2820 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2824 free(fileindex_path);
2825 got_fileindex_free(fileindex);
2829 const struct got_error *
2830 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2833 const struct got_error *err = NULL;
2834 char *resolved, *cwd = NULL, *path = NULL;
2839 resolved = realpath(arg, NULL);
2840 if (resolved == NULL) {
2841 if (errno != ENOENT)
2842 return got_error_from_errno2("realpath", arg);
2843 cwd = getcwd(NULL, 0);
2845 return got_error_from_errno("getcwd");
2846 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2847 err = got_error_from_errno("asprintf");
2852 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2853 strlen(got_worktree_get_root_path(worktree)))) {
2854 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
2858 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2859 err = got_path_skip_common_ancestor(&path,
2860 got_worktree_get_root_path(worktree), resolved);
2866 err = got_error_from_errno("strdup");
2871 /* XXX status walk can't deal with trailing slash! */
2873 while (len > 0 && path[len - 1] == '/') {
2874 path[len - 1] = '\0';
2887 struct schedule_addition_args {
2888 struct got_worktree *worktree;
2889 struct got_fileindex *fileindex;
2890 got_worktree_checkout_cb progress_cb;
2892 struct got_repository *repo;
2895 static const struct got_error *
2896 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2897 const char *relpath, struct got_object_id *blob_id,
2898 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2899 int dirfd, const char *de_name)
2901 struct schedule_addition_args *a = arg;
2902 const struct got_error *err = NULL;
2903 struct got_fileindex_entry *ie;
2907 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2909 return got_error_from_errno("asprintf");
2911 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2913 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2917 /* Re-adding an existing entry is a no-op. */
2918 if (status == GOT_STATUS_ADD)
2920 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2925 if (status != GOT_STATUS_UNVERSIONED) {
2926 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2930 err = got_fileindex_entry_alloc(&ie, relpath);
2933 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
2935 got_fileindex_entry_free(ie);
2938 err = got_fileindex_entry_add(a->fileindex, ie);
2940 got_fileindex_entry_free(ie);
2947 if (status == GOT_STATUS_ADD)
2949 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2952 const struct got_error *
2953 got_worktree_schedule_add(struct got_worktree *worktree,
2954 struct got_pathlist_head *paths,
2955 got_worktree_checkout_cb progress_cb, void *progress_arg,
2956 struct got_repository *repo, int no_ignores)
2958 struct got_fileindex *fileindex = NULL;
2959 char *fileindex_path = NULL;
2960 const struct got_error *err = NULL, *sync_err, *unlockerr;
2961 struct got_pathlist_entry *pe;
2962 struct schedule_addition_args saa;
2964 err = lock_worktree(worktree, LOCK_EX);
2968 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2972 saa.worktree = worktree;
2973 saa.fileindex = fileindex;
2974 saa.progress_cb = progress_cb;
2975 saa.progress_arg = progress_arg;
2978 TAILQ_FOREACH(pe, paths, entry) {
2979 err = worktree_status(worktree, pe->path, fileindex, repo,
2980 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2984 sync_err = sync_fileindex(fileindex, fileindex_path);
2985 if (sync_err && err == NULL)
2988 free(fileindex_path);
2990 got_fileindex_free(fileindex);
2991 unlockerr = lock_worktree(worktree, LOCK_SH);
2992 if (unlockerr && err == NULL)
2997 struct schedule_deletion_args {
2998 struct got_worktree *worktree;
2999 struct got_fileindex *fileindex;
3000 got_worktree_delete_cb progress_cb;
3002 struct got_repository *repo;
3003 int delete_local_mods;
3007 static const struct got_error *
3008 schedule_for_deletion(void *arg, unsigned char status,
3009 unsigned char staged_status, const char *relpath,
3010 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3011 struct got_object_id *commit_id, int dirfd, const char *de_name)
3013 struct schedule_deletion_args *a = arg;
3014 const struct got_error *err = NULL;
3015 struct got_fileindex_entry *ie = NULL;
3017 char *ondisk_path, *parent = NULL;
3019 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3021 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3023 staged_status = get_staged_status(ie);
3024 if (staged_status != GOT_STATUS_NO_CHANGE) {
3025 if (staged_status == GOT_STATUS_DELETE)
3027 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3030 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3032 return got_error_from_errno("asprintf");
3034 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3039 if (status != GOT_STATUS_NO_CHANGE) {
3040 if (status == GOT_STATUS_DELETE)
3042 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3043 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3046 if (status != GOT_STATUS_MODIFY &&
3047 status != GOT_STATUS_MISSING) {
3048 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3053 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3055 if (unlinkat(dirfd, de_name, 0) != 0) {
3056 err = got_error_from_errno2("unlinkat",
3060 } else if (unlink(ondisk_path) != 0) {
3061 err = got_error_from_errno2("unlink", ondisk_path);
3065 parent = dirname(ondisk_path);
3067 if (parent == NULL) {
3068 err = got_error_from_errno2("dirname", ondisk_path);
3071 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3072 if (rmdir(parent) == -1) {
3073 if (errno != ENOTEMPTY)
3074 err = got_error_from_errno2("rmdir",
3078 parent = dirname(parent);
3079 if (parent == NULL) {
3080 err = got_error_from_errno2("dirname", parent);
3086 got_fileindex_entry_mark_deleted_from_disk(ie);
3091 if (status == GOT_STATUS_DELETE)
3093 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3094 staged_status, relpath);
3097 const struct got_error *
3098 got_worktree_schedule_delete(struct got_worktree *worktree,
3099 struct got_pathlist_head *paths, int delete_local_mods,
3100 got_worktree_delete_cb progress_cb, void *progress_arg,
3101 struct got_repository *repo, int keep_on_disk)
3103 struct got_fileindex *fileindex = NULL;
3104 char *fileindex_path = NULL;
3105 const struct got_error *err = NULL, *sync_err, *unlockerr;
3106 struct got_pathlist_entry *pe;
3107 struct schedule_deletion_args sda;
3109 err = lock_worktree(worktree, LOCK_EX);
3113 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3117 sda.worktree = worktree;
3118 sda.fileindex = fileindex;
3119 sda.progress_cb = progress_cb;
3120 sda.progress_arg = progress_arg;
3122 sda.delete_local_mods = delete_local_mods;
3123 sda.keep_on_disk = keep_on_disk;
3125 TAILQ_FOREACH(pe, paths, entry) {
3126 err = worktree_status(worktree, pe->path, fileindex, repo,
3127 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3131 sync_err = sync_fileindex(fileindex, fileindex_path);
3132 if (sync_err && err == NULL)
3135 free(fileindex_path);
3137 got_fileindex_free(fileindex);
3138 unlockerr = lock_worktree(worktree, LOCK_SH);
3139 if (unlockerr && err == NULL)
3144 static const struct got_error *
3145 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3147 const struct got_error *err = NULL;
3149 size_t linesize = 0, n;
3152 linelen = getline(&line, &linesize, infile);
3153 if (linelen == -1) {
3154 if (ferror(infile)) {
3155 err = got_error_from_errno("getline");
3161 n = fwrite(line, 1, linelen, outfile);
3163 err = got_ferror(outfile, GOT_ERR_IO);
3168 n = fwrite(line, 1, linelen, rejectfile);
3170 err = got_ferror(outfile, GOT_ERR_IO);
3177 static const struct got_error *
3178 skip_one_line(FILE *f)
3181 size_t linesize = 0;
3184 linelen = getline(&line, &linesize, f);
3185 if (linelen == -1) {
3187 return got_error_from_errno("getline");
3194 static const struct got_error *
3195 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3196 int start_old, int end_old, int start_new, int end_new,
3197 FILE *outfile, FILE *rejectfile)
3199 const struct got_error *err;
3201 /* Copy old file's lines leading up to patch. */
3202 while (!feof(f1) && *line_cur1 < start_old) {
3203 err = copy_one_line(f1, outfile, NULL);
3208 /* Skip new file's lines leading up to patch. */
3209 while (!feof(f2) && *line_cur2 < start_new) {
3211 err = copy_one_line(f2, NULL, rejectfile);
3213 err = skip_one_line(f2);
3218 /* Copy patched lines. */
3219 while (!feof(f2) && *line_cur2 <= end_new) {
3220 err = copy_one_line(f2, outfile, NULL);
3225 /* Skip over old file's replaced lines. */
3226 while (!feof(f1) && *line_cur1 <= end_old) {
3228 err = copy_one_line(f1, NULL, rejectfile);
3230 err = skip_one_line(f1);
3239 static const struct got_error *
3240 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3241 FILE *outfile, FILE *rejectfile)
3243 const struct got_error *err;
3246 /* Copy old file's lines until EOF. */
3248 err = copy_one_line(f1, outfile, NULL);
3255 /* Copy new file's lines until EOF. */
3257 err = copy_one_line(f2, NULL, rejectfile);
3267 static const struct got_error *
3268 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3269 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3270 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3271 int *line_cur2, FILE *outfile, FILE *rejectfile,
3272 got_worktree_patch_cb patch_cb, void *patch_arg)
3274 const struct got_error *err = NULL;
3275 int start_old = change->cv.a;
3276 int end_old = change->cv.b;
3277 int start_new = change->cv.c;
3278 int end_new = change->cv.d;
3282 *choice = GOT_PATCH_CHOICE_NONE;
3284 hunkfile = got_opentemp();
3285 if (hunkfile == NULL)
3286 return got_error_from_errno("got_opentemp");
3291 /* XXX TODO needs error checking */
3292 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3294 if (fseek(f1, pos1, SEEK_SET) == -1) {
3295 err = got_ferror(f1, GOT_ERR_IO);
3298 if (fseek(f2, pos2, SEEK_SET) == -1) {
3299 err = got_ferror(f1, GOT_ERR_IO);
3302 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3303 err = got_ferror(hunkfile, GOT_ERR_IO);
3307 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3308 hunkfile, n, nchanges);
3313 case GOT_PATCH_CHOICE_YES:
3314 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3315 end_old, start_new, end_new, outfile, rejectfile);
3317 case GOT_PATCH_CHOICE_NO:
3318 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3319 end_old, start_new, end_new, rejectfile, outfile);
3321 case GOT_PATCH_CHOICE_QUIT:
3324 err = got_error(GOT_ERR_PATCH_CHOICE);
3328 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3329 err = got_error_from_errno("fclose");
3333 struct revert_file_args {
3334 struct got_worktree *worktree;
3335 struct got_fileindex *fileindex;
3336 got_worktree_checkout_cb progress_cb;
3338 got_worktree_patch_cb patch_cb;
3340 struct got_repository *repo;
3343 static const struct got_error *
3344 create_patched_content(char **path_outfile, int reverse_patch,
3345 struct got_object_id *blob_id, const char *path2,
3346 int dirfd2, const char *de_name2,
3347 const char *relpath, struct got_repository *repo,
3348 got_worktree_patch_cb patch_cb, void *patch_arg)
3350 const struct got_error *err;
3351 struct got_blob_object *blob = NULL;
3352 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3354 char *path1 = NULL, *id_str = NULL;
3355 struct stat sb1, sb2;
3356 struct got_diff_changes *changes = NULL;
3357 struct got_diff_state *ds = NULL;
3358 struct got_diff_args *args = NULL;
3359 struct got_diff_change *change;
3360 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3363 *path_outfile = NULL;
3365 err = got_object_id_str(&id_str, blob_id);
3370 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3372 err = got_error_from_errno2("openat", path2);
3376 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3378 err = got_error_from_errno2("open", path2);
3382 if (fstat(fd2, &sb2) == -1) {
3383 err = got_error_from_errno2("fstat", path2);
3387 f2 = fdopen(fd2, "r");
3389 err = got_error_from_errno2("fdopen", path2);
3394 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3398 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3402 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3406 if (stat(path1, &sb1) == -1) {
3407 err = got_error_from_errno2("stat", path1);
3411 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3412 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3416 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3420 if (fseek(f1, 0L, SEEK_SET) == -1)
3421 return got_ferror(f1, GOT_ERR_IO);
3422 if (fseek(f2, 0L, SEEK_SET) == -1)
3423 return got_ferror(f2, GOT_ERR_IO);
3424 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3426 err = apply_or_reject_change(&choice, change, ++n,
3427 changes->nchanges, ds, args, diff_flags, relpath,
3428 f1, f2, &line_cur1, &line_cur2,
3429 reverse_patch ? NULL : outfile,
3430 reverse_patch ? outfile : NULL,
3431 patch_cb, patch_arg);
3434 if (choice == GOT_PATCH_CHOICE_YES)
3436 else if (choice == GOT_PATCH_CHOICE_QUIT)
3440 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3441 reverse_patch ? NULL : outfile,
3442 reverse_patch ? outfile : NULL);
3446 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3447 err = got_error_from_errno2("chmod", path2);
3454 got_object_blob_close(blob);
3455 if (f1 && fclose(f1) == EOF && err == NULL)
3456 err = got_error_from_errno2("fclose", path1);
3457 if (f2 && fclose(f2) == EOF && err == NULL)
3458 err = got_error_from_errno2("fclose", path2);
3459 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3460 err = got_error_from_errno2("close", path2);
3461 if (outfile && fclose(outfile) == EOF && err == NULL)
3462 err = got_error_from_errno2("fclose", *path_outfile);
3463 if (path1 && unlink(path1) == -1 && err == NULL)
3464 err = got_error_from_errno2("unlink", path1);
3465 if (err || !have_content) {
3466 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3467 err = got_error_from_errno2("unlink", *path_outfile);
3468 free(*path_outfile);
3469 *path_outfile = NULL;
3473 got_diff_state_free(ds);
3477 got_diff_free_changes(changes);
3482 static const struct got_error *
3483 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3484 const char *relpath, struct got_object_id *blob_id,
3485 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3486 int dirfd, const char *de_name)
3488 struct revert_file_args *a = arg;
3489 const struct got_error *err = NULL;
3490 char *parent_path = NULL;
3491 struct got_fileindex_entry *ie;
3492 struct got_tree_object *tree = NULL;
3493 struct got_object_id *tree_id = NULL;
3494 const struct got_tree_entry *te = NULL;
3495 char *tree_path = NULL, *te_name;
3496 char *ondisk_path = NULL, *path_content = NULL;
3497 struct got_blob_object *blob = NULL;
3499 /* Reverting a staged deletion is a no-op. */
3500 if (status == GOT_STATUS_DELETE &&
3501 staged_status != GOT_STATUS_NO_CHANGE)
3504 if (status == GOT_STATUS_UNVERSIONED)
3505 return (*a->progress_cb)(a->progress_arg,
3506 GOT_STATUS_UNVERSIONED, relpath);
3508 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3510 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3512 /* Construct in-repository path of tree which contains this blob. */
3513 err = got_path_dirname(&parent_path, ie->path);
3515 if (err->code != GOT_ERR_BAD_PATH)
3517 parent_path = strdup("/");
3518 if (parent_path == NULL) {
3519 err = got_error_from_errno("strdup");
3523 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3524 tree_path = strdup(parent_path);
3525 if (tree_path == NULL) {
3526 err = got_error_from_errno("strdup");
3530 if (got_path_is_root_dir(parent_path)) {
3531 tree_path = strdup(a->worktree->path_prefix);
3532 if (tree_path == NULL) {
3533 err = got_error_from_errno("strdup");
3537 if (asprintf(&tree_path, "%s/%s",
3538 a->worktree->path_prefix, parent_path) == -1) {
3539 err = got_error_from_errno("asprintf");
3545 err = got_object_id_by_path(&tree_id, a->repo,
3546 a->worktree->base_commit_id, tree_path);
3548 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3549 (status == GOT_STATUS_ADD ||
3550 staged_status == GOT_STATUS_ADD)))
3553 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3557 te_name = basename(ie->path);
3558 if (te_name == NULL) {
3559 err = got_error_from_errno2("basename", ie->path);
3563 te = got_object_tree_find_entry(tree, te_name);
3564 if (te == NULL && status != GOT_STATUS_ADD &&
3565 staged_status != GOT_STATUS_ADD) {
3566 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3572 case GOT_STATUS_ADD:
3574 int choice = GOT_PATCH_CHOICE_NONE;
3575 err = (*a->patch_cb)(&choice, a->patch_arg,
3576 status, ie->path, NULL, 1, 1);
3579 if (choice != GOT_PATCH_CHOICE_YES)
3582 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3586 got_fileindex_entry_remove(a->fileindex, ie);
3588 case GOT_STATUS_DELETE:
3590 int choice = GOT_PATCH_CHOICE_NONE;
3591 err = (*a->patch_cb)(&choice, a->patch_arg,
3592 status, ie->path, NULL, 1, 1);
3595 if (choice != GOT_PATCH_CHOICE_YES)
3599 case GOT_STATUS_MODIFY:
3600 case GOT_STATUS_MODE_CHANGE:
3601 case GOT_STATUS_CONFLICT:
3602 case GOT_STATUS_MISSING: {
3603 struct got_object_id id;
3604 if (staged_status == GOT_STATUS_ADD ||
3605 staged_status == GOT_STATUS_MODIFY) {
3606 memcpy(id.sha1, ie->staged_blob_sha1,
3607 SHA1_DIGEST_LENGTH);
3609 memcpy(id.sha1, ie->blob_sha1,
3610 SHA1_DIGEST_LENGTH);
3611 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3615 if (asprintf(&ondisk_path, "%s/%s",
3616 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3617 err = got_error_from_errno("asprintf");
3621 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3622 status == GOT_STATUS_CONFLICT)) {
3623 err = create_patched_content(&path_content, 1, &id,
3624 ondisk_path, dirfd, de_name, ie->path, a->repo,
3625 a->patch_cb, a->patch_arg);
3626 if (err || path_content == NULL)
3628 if (rename(path_content, ondisk_path) == -1) {
3629 err = got_error_from_errno3("rename",
3630 path_content, ondisk_path);
3634 err = install_blob(a->worktree, ondisk_path, ie->path,
3635 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3636 got_fileindex_perms_to_st(ie), blob, 0, 1,
3637 a->repo, a->progress_cb, a->progress_arg);
3640 if (status == GOT_STATUS_DELETE ||
3641 status == GOT_STATUS_MODE_CHANGE) {
3642 err = update_blob_fileindex_entry(a->worktree,
3643 a->fileindex, ie, ondisk_path, ie->path,
3660 got_object_blob_close(blob);
3662 got_object_tree_close(tree);
3667 const struct got_error *
3668 got_worktree_revert(struct got_worktree *worktree,
3669 struct got_pathlist_head *paths,
3670 got_worktree_checkout_cb progress_cb, void *progress_arg,
3671 got_worktree_patch_cb patch_cb, void *patch_arg,
3672 struct got_repository *repo)
3674 struct got_fileindex *fileindex = NULL;
3675 char *fileindex_path = NULL;
3676 const struct got_error *err = NULL, *unlockerr = NULL;
3677 const struct got_error *sync_err = NULL;
3678 struct got_pathlist_entry *pe;
3679 struct revert_file_args rfa;
3681 err = lock_worktree(worktree, LOCK_EX);
3685 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3689 rfa.worktree = worktree;
3690 rfa.fileindex = fileindex;
3691 rfa.progress_cb = progress_cb;
3692 rfa.progress_arg = progress_arg;
3693 rfa.patch_cb = patch_cb;
3694 rfa.patch_arg = patch_arg;
3696 TAILQ_FOREACH(pe, paths, entry) {
3697 err = worktree_status(worktree, pe->path, fileindex, repo,
3698 revert_file, &rfa, NULL, NULL, 0, 0);
3702 sync_err = sync_fileindex(fileindex, fileindex_path);
3703 if (sync_err && err == NULL)
3706 free(fileindex_path);
3708 got_fileindex_free(fileindex);
3709 unlockerr = lock_worktree(worktree, LOCK_SH);
3710 if (unlockerr && err == NULL)
3716 free_commitable(struct got_commitable *ct)
3719 free(ct->in_repo_path);
3720 free(ct->ondisk_path);
3722 free(ct->base_blob_id);
3723 free(ct->staged_blob_id);
3724 free(ct->base_commit_id);
3728 struct collect_commitables_arg {
3729 struct got_pathlist_head *commitable_paths;
3730 struct got_repository *repo;
3731 struct got_worktree *worktree;
3732 int have_staged_files;
3735 static const struct got_error *
3736 collect_commitables(void *arg, unsigned char status,
3737 unsigned char staged_status, const char *relpath,
3738 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3739 struct got_object_id *commit_id, int dirfd, const char *de_name)
3741 struct collect_commitables_arg *a = arg;
3742 const struct got_error *err = NULL;
3743 struct got_commitable *ct = NULL;
3744 struct got_pathlist_entry *new = NULL;
3745 char *parent_path = NULL, *path = NULL;
3748 if (a->have_staged_files) {
3749 if (staged_status != GOT_STATUS_MODIFY &&
3750 staged_status != GOT_STATUS_ADD &&
3751 staged_status != GOT_STATUS_DELETE)
3754 if (status == GOT_STATUS_CONFLICT)
3755 return got_error(GOT_ERR_COMMIT_CONFLICT);
3757 if (status != GOT_STATUS_MODIFY &&
3758 status != GOT_STATUS_MODE_CHANGE &&
3759 status != GOT_STATUS_ADD &&
3760 status != GOT_STATUS_DELETE)
3764 if (asprintf(&path, "/%s", relpath) == -1) {
3765 err = got_error_from_errno("asprintf");
3768 if (strcmp(path, "/") == 0) {
3769 parent_path = strdup("");
3770 if (parent_path == NULL)
3771 return got_error_from_errno("strdup");
3773 err = got_path_dirname(&parent_path, path);
3778 ct = calloc(1, sizeof(*ct));
3780 err = got_error_from_errno("calloc");
3784 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3786 err = got_error_from_errno("asprintf");
3789 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3790 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3793 if (fstatat(dirfd, de_name, &sb,
3794 AT_SYMLINK_NOFOLLOW) == -1) {
3795 err = got_error_from_errno2("fstatat",
3799 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3800 err = got_error_from_errno2("lstat", ct->ondisk_path);
3803 ct->mode = sb.st_mode;
3806 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3807 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3809 err = got_error_from_errno("asprintf");
3813 ct->status = status;
3814 ct->staged_status = staged_status;
3815 ct->blob_id = NULL; /* will be filled in when blob gets created */
3816 if (ct->status != GOT_STATUS_ADD &&
3817 ct->staged_status != GOT_STATUS_ADD) {
3818 ct->base_blob_id = got_object_id_dup(blob_id);
3819 if (ct->base_blob_id == NULL) {
3820 err = got_error_from_errno("got_object_id_dup");
3823 ct->base_commit_id = got_object_id_dup(commit_id);
3824 if (ct->base_commit_id == NULL) {
3825 err = got_error_from_errno("got_object_id_dup");
3829 if (ct->staged_status == GOT_STATUS_ADD ||
3830 ct->staged_status == GOT_STATUS_MODIFY) {
3831 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3832 if (ct->staged_blob_id == NULL) {
3833 err = got_error_from_errno("got_object_id_dup");
3837 ct->path = strdup(path);
3838 if (ct->path == NULL) {
3839 err = got_error_from_errno("strdup");
3842 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3844 if (ct && (err || new == NULL))
3845 free_commitable(ct);
3851 static const struct got_error *write_tree(struct got_object_id **, int *,
3852 struct got_tree_object *, const char *, struct got_pathlist_head *,
3853 got_worktree_status_cb status_cb, void *status_arg,
3854 struct got_repository *);
3856 static const struct got_error *
3857 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
3858 struct got_tree_entry *te, const char *parent_path,
3859 struct got_pathlist_head *commitable_paths,
3860 got_worktree_status_cb status_cb, void *status_arg,
3861 struct got_repository *repo)
3863 const struct got_error *err = NULL;
3864 struct got_tree_object *subtree;
3867 if (asprintf(&subpath, "%s%s%s", parent_path,
3868 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3869 return got_error_from_errno("asprintf");
3871 err = got_object_open_as_tree(&subtree, repo, &te->id);
3875 err = write_tree(new_subtree_id, nentries, subtree, subpath,
3876 commitable_paths, status_cb, status_arg, repo);
3877 got_object_tree_close(subtree);
3882 static const struct got_error *
3883 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3885 const struct got_error *err = NULL;
3886 char *ct_parent_path = NULL;
3890 if (strchr(ct->in_repo_path, '/') == NULL) {
3891 *match = got_path_is_root_dir(path);
3895 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3898 *match = (strcmp(path, ct_parent_path) == 0);
3899 free(ct_parent_path);
3904 get_ct_file_mode(struct got_commitable *ct)
3906 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3909 static const struct got_error *
3910 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3911 struct got_tree_entry *te, struct got_commitable *ct)
3913 const struct got_error *err = NULL;
3917 err = got_object_tree_entry_dup(new_te, te);
3921 (*new_te)->mode = get_ct_file_mode(ct);
3923 if (ct->staged_status == GOT_STATUS_MODIFY)
3924 memcpy(&(*new_te)->id, ct->staged_blob_id,
3925 sizeof((*new_te)->id));
3927 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3929 if (err && *new_te) {
3936 static const struct got_error *
3937 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3938 struct got_commitable *ct)
3940 const struct got_error *err = NULL;
3945 *new_te = calloc(1, sizeof(**new_te));
3946 if (*new_te == NULL)
3947 return got_error_from_errno("calloc");
3949 ct_name = basename(ct->path);
3950 if (ct_name == NULL) {
3951 err = got_error_from_errno2("basename", ct->path);
3954 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3955 sizeof((*new_te)->name)) {
3956 err = got_error(GOT_ERR_NO_SPACE);
3960 (*new_te)->mode = get_ct_file_mode(ct);
3962 if (ct->staged_status == GOT_STATUS_ADD)
3963 memcpy(&(*new_te)->id, ct->staged_blob_id,
3964 sizeof((*new_te)->id));
3966 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3968 if (err && *new_te) {
3975 static const struct got_error *
3976 insert_tree_entry(struct got_tree_entry *new_te,
3977 struct got_pathlist_head *paths)
3979 const struct got_error *err = NULL;
3980 struct got_pathlist_entry *new_pe;
3982 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3986 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3990 static const struct got_error *
3991 report_ct_status(struct got_commitable *ct,
3992 got_worktree_status_cb status_cb, void *status_arg)
3994 const char *ct_path = ct->path;
3995 unsigned char status;
3997 while (ct_path[0] == '/')
4000 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4001 status = ct->staged_status;
4003 status = ct->status;
4005 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4006 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4009 static const struct got_error *
4010 match_modified_subtree(int *modified, struct got_tree_entry *te,
4011 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4013 const struct got_error *err = NULL;
4014 struct got_pathlist_entry *pe;
4019 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4020 got_path_is_root_dir(base_tree_path) ? "" : "/",
4022 return got_error_from_errno("asprintf");
4024 TAILQ_FOREACH(pe, commitable_paths, entry) {
4025 struct got_commitable *ct = pe->data;
4026 *modified = got_path_is_child(ct->in_repo_path, te_path,
4036 static const struct got_error *
4037 match_deleted_or_modified_ct(struct got_commitable **ctp,
4038 struct got_tree_entry *te, const char *base_tree_path,
4039 struct got_pathlist_head *commitable_paths)
4041 const struct got_error *err = NULL;
4042 struct got_pathlist_entry *pe;
4046 TAILQ_FOREACH(pe, commitable_paths, entry) {
4047 struct got_commitable *ct = pe->data;
4048 char *ct_name = NULL;
4051 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4052 if (ct->status != GOT_STATUS_MODIFY &&
4053 ct->status != GOT_STATUS_MODE_CHANGE &&
4054 ct->status != GOT_STATUS_DELETE)
4057 if (ct->staged_status != GOT_STATUS_MODIFY &&
4058 ct->staged_status != GOT_STATUS_DELETE)
4062 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4065 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4071 ct_name = basename(pe->path);
4072 if (ct_name == NULL)
4073 return got_error_from_errno2("basename", pe->path);
4075 if (strcmp(te->name, ct_name) != 0)
4085 static const struct got_error *
4086 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4087 const char *child_path, const char *path_base_tree,
4088 struct got_pathlist_head *commitable_paths,
4089 got_worktree_status_cb status_cb, void *status_arg,
4090 struct got_repository *repo)
4092 const struct got_error *err = NULL;
4093 struct got_tree_entry *new_te;
4095 struct got_object_id *id = NULL;
4100 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4101 got_path_is_root_dir(path_base_tree) ? "" : "/",
4103 return got_error_from_errno("asprintf");
4105 new_te = calloc(1, sizeof(*new_te));
4107 return got_error_from_errno("calloc");
4108 new_te->mode = S_IFDIR;
4110 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4111 sizeof(new_te->name)) {
4112 err = got_error(GOT_ERR_NO_SPACE);
4115 err = write_tree(&id, &nentries, NULL, subtree_path,
4116 commitable_paths, status_cb, status_arg, repo);
4121 memcpy(&new_te->id, id, sizeof(new_te->id));
4130 static const struct got_error *
4131 write_tree(struct got_object_id **new_tree_id, int *nentries,
4132 struct got_tree_object *base_tree, const char *path_base_tree,
4133 struct got_pathlist_head *commitable_paths,
4134 got_worktree_status_cb status_cb, void *status_arg,
4135 struct got_repository *repo)
4137 const struct got_error *err = NULL;
4138 struct got_pathlist_head paths;
4139 struct got_tree_entry *te, *new_te = NULL;
4140 struct got_pathlist_entry *pe;
4145 /* Insert, and recurse into, newly added entries first. */
4146 TAILQ_FOREACH(pe, commitable_paths, entry) {
4147 struct got_commitable *ct = pe->data;
4148 char *child_path = NULL, *slash;
4150 if ((ct->status != GOT_STATUS_ADD &&
4151 ct->staged_status != GOT_STATUS_ADD) ||
4152 (ct->flags & GOT_COMMITABLE_ADDED))
4155 if (!got_path_is_child(pe->path, path_base_tree,
4156 strlen(path_base_tree)))
4159 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4164 slash = strchr(child_path, '/');
4165 if (slash == NULL) {
4166 err = alloc_added_blob_tree_entry(&new_te, ct);
4169 err = report_ct_status(ct, status_cb, status_arg);
4172 ct->flags |= GOT_COMMITABLE_ADDED;
4173 err = insert_tree_entry(new_te, &paths);
4178 *slash = '\0'; /* trim trailing path components */
4179 if (base_tree == NULL ||
4180 got_object_tree_find_entry(base_tree, child_path)
4182 err = make_subtree_for_added_blob(&new_te,
4183 child_path, path_base_tree,
4184 commitable_paths, status_cb, status_arg,
4188 err = insert_tree_entry(new_te, &paths);
4197 int i, nbase_entries;
4198 /* Handle modified and deleted entries. */
4199 nbase_entries = got_object_tree_get_nentries(base_tree);
4200 for (i = 0; i < nbase_entries; i++) {
4201 struct got_commitable *ct = NULL;
4203 te = got_object_tree_get_entry(base_tree, i);
4204 if (got_object_tree_entry_is_submodule(te)) {
4205 /* Entry is a submodule; just copy it. */
4206 err = got_object_tree_entry_dup(&new_te, te);
4209 err = insert_tree_entry(new_te, &paths);
4216 if (S_ISDIR(te->mode)) {
4218 err = got_object_tree_entry_dup(&new_te, te);
4221 err = match_modified_subtree(&modified, te,
4222 path_base_tree, commitable_paths);
4225 /* Avoid recursion into unmodified subtrees. */
4227 struct got_object_id *new_id;
4229 err = write_subtree(&new_id,
4231 path_base_tree, commitable_paths,
4232 status_cb, status_arg, repo);
4235 if (nsubentries == 0) {
4236 /* All entries were deleted. */
4240 memcpy(&new_te->id, new_id,
4241 sizeof(new_te->id));
4244 err = insert_tree_entry(new_te, &paths);
4251 err = match_deleted_or_modified_ct(&ct, te,
4252 path_base_tree, commitable_paths);
4256 /* NB: Deleted entries get dropped here. */
4257 if (ct->status == GOT_STATUS_MODIFY ||
4258 ct->status == GOT_STATUS_MODE_CHANGE ||
4259 ct->staged_status == GOT_STATUS_MODIFY) {
4260 err = alloc_modified_blob_tree_entry(
4264 err = insert_tree_entry(new_te, &paths);
4269 err = report_ct_status(ct, status_cb,
4274 /* Entry is unchanged; just copy it. */
4275 err = got_object_tree_entry_dup(&new_te, te);
4278 err = insert_tree_entry(new_te, &paths);
4286 /* Write new list of entries; deleted entries have been dropped. */
4287 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4289 got_pathlist_free(&paths);
4293 static const struct got_error *
4294 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4295 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4296 int have_staged_files)
4298 const struct got_error *err = NULL;
4299 struct got_pathlist_entry *pe;
4301 TAILQ_FOREACH(pe, commitable_paths, entry) {
4302 struct got_fileindex_entry *ie;
4303 struct got_commitable *ct = pe->data;
4305 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4307 if (ct->status == GOT_STATUS_DELETE ||
4308 ct->staged_status == GOT_STATUS_DELETE) {
4309 got_fileindex_entry_remove(fileindex, ie);
4310 } else if (ct->staged_status == GOT_STATUS_ADD ||
4311 ct->staged_status == GOT_STATUS_MODIFY) {
4312 got_fileindex_entry_stage_set(ie,
4313 GOT_FILEIDX_STAGE_NONE);
4314 err = got_fileindex_entry_update(ie,
4315 ct->ondisk_path, ct->staged_blob_id->sha1,
4316 new_base_commit_id->sha1,
4317 !have_staged_files);
4319 err = got_fileindex_entry_update(ie,
4320 ct->ondisk_path, ct->blob_id->sha1,
4321 new_base_commit_id->sha1,
4322 !have_staged_files);
4324 err = got_fileindex_entry_alloc(&ie, pe->path);
4327 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4328 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4330 got_fileindex_entry_free(ie);
4333 err = got_fileindex_entry_add(fileindex, ie);
4335 got_fileindex_entry_free(ie);
4344 static const struct got_error *
4345 check_out_of_date(const char *in_repo_path, unsigned char status,
4346 unsigned char staged_status, struct got_object_id *base_blob_id,
4347 struct got_object_id *base_commit_id,
4348 struct got_object_id *head_commit_id, struct got_repository *repo,
4351 const struct got_error *err = NULL;
4352 struct got_object_id *id = NULL;
4354 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4355 /* Trivial case: base commit == head commit */
4356 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4359 * Ensure file content which local changes were based
4360 * on matches file content in the branch head.
4362 err = got_object_id_by_path(&id, repo, head_commit_id,
4365 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4366 err = got_error(ood_errcode);
4368 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4369 err = got_error(ood_errcode);
4371 /* Require that added files don't exist in the branch head. */
4372 err = got_object_id_by_path(&id, repo, head_commit_id,
4374 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4376 err = id ? got_error(ood_errcode) : NULL;
4383 const struct got_error *
4384 commit_worktree(struct got_object_id **new_commit_id,
4385 struct got_pathlist_head *commitable_paths,
4386 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4387 const char *author, const char *committer,
4388 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4389 got_worktree_status_cb status_cb, void *status_arg,
4390 struct got_repository *repo)
4392 const struct got_error *err = NULL, *unlockerr = NULL;
4393 struct got_pathlist_entry *pe;
4394 const char *head_ref_name = NULL;
4395 struct got_commit_object *head_commit = NULL;
4396 struct got_reference *head_ref2 = NULL;
4397 struct got_object_id *head_commit_id2 = NULL;
4398 struct got_tree_object *head_tree = NULL;
4399 struct got_object_id *new_tree_id = NULL;
4401 struct got_object_id_queue parent_ids;
4402 struct got_object_qid *pid = NULL;
4403 char *logmsg = NULL;
4405 *new_commit_id = NULL;
4407 SIMPLEQ_INIT(&parent_ids);
4409 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4413 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4417 if (commit_msg_cb != NULL) {
4418 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4423 if (logmsg == NULL || strlen(logmsg) == 0) {
4424 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4428 /* Create blobs from added and modified files and record their IDs. */
4429 TAILQ_FOREACH(pe, commitable_paths, entry) {
4430 struct got_commitable *ct = pe->data;
4433 /* Blobs for staged files already exist. */
4434 if (ct->staged_status == GOT_STATUS_ADD ||
4435 ct->staged_status == GOT_STATUS_MODIFY)
4438 if (ct->status != GOT_STATUS_ADD &&
4439 ct->status != GOT_STATUS_MODIFY &&
4440 ct->status != GOT_STATUS_MODE_CHANGE)
4443 if (asprintf(&ondisk_path, "%s/%s",
4444 worktree->root_path, pe->path) == -1) {
4445 err = got_error_from_errno("asprintf");
4448 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4454 /* Recursively write new tree objects. */
4455 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4456 commitable_paths, status_cb, status_arg, repo);
4460 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4463 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4464 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4465 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4466 got_object_qid_free(pid);
4472 /* Check if a concurrent commit to our branch has occurred. */
4473 head_ref_name = got_worktree_get_head_ref_name(worktree);
4474 if (head_ref_name == NULL) {
4475 err = got_error_from_errno("got_worktree_get_head_ref_name");
4478 /* Lock the reference here to prevent concurrent modification. */
4479 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4482 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4485 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4486 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4489 /* Update branch head in repository. */
4490 err = got_ref_change_ref(head_ref2, *new_commit_id);
4493 err = got_ref_write(head_ref2, repo);
4497 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4501 err = ref_base_commit(worktree, repo);
4506 got_object_tree_close(head_tree);
4508 got_object_commit_close(head_commit);
4509 free(head_commit_id2);
4511 unlockerr = got_ref_unlock(head_ref2);
4512 if (unlockerr && err == NULL)
4514 got_ref_close(head_ref2);
4519 static const struct got_error *
4520 check_path_is_commitable(const char *path,
4521 struct got_pathlist_head *commitable_paths)
4523 struct got_pathlist_entry *cpe = NULL;
4524 size_t path_len = strlen(path);
4526 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4527 struct got_commitable *ct = cpe->data;
4528 const char *ct_path = ct->path;
4530 while (ct_path[0] == '/')
4533 if (strcmp(path, ct_path) == 0 ||
4534 got_path_is_child(ct_path, path, path_len))
4539 return got_error_path(path, GOT_ERR_BAD_PATH);
4544 static const struct got_error *
4545 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4547 int *have_staged_files = arg;
4549 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4550 *have_staged_files = 1;
4551 return got_error(GOT_ERR_CANCELLED);
4557 static const struct got_error *
4558 check_non_staged_files(struct got_fileindex *fileindex,
4559 struct got_pathlist_head *paths)
4561 struct got_pathlist_entry *pe;
4562 struct got_fileindex_entry *ie;
4564 TAILQ_FOREACH(pe, paths, entry) {
4565 if (pe->path[0] == '\0')
4567 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4569 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4570 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4571 return got_error_path(pe->path,
4572 GOT_ERR_FILE_NOT_STAGED);
4578 const struct got_error *
4579 got_worktree_commit(struct got_object_id **new_commit_id,
4580 struct got_worktree *worktree, struct got_pathlist_head *paths,
4581 const char *author, const char *committer,
4582 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4583 got_worktree_status_cb status_cb, void *status_arg,
4584 struct got_repository *repo)
4586 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4587 struct got_fileindex *fileindex = NULL;
4588 char *fileindex_path = NULL;
4589 struct got_pathlist_head commitable_paths;
4590 struct collect_commitables_arg cc_arg;
4591 struct got_pathlist_entry *pe;
4592 struct got_reference *head_ref = NULL;
4593 struct got_object_id *head_commit_id = NULL;
4594 int have_staged_files = 0;
4596 *new_commit_id = NULL;
4598 TAILQ_INIT(&commitable_paths);
4600 err = lock_worktree(worktree, LOCK_EX);
4604 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4608 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4612 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4616 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4617 &have_staged_files);
4618 if (err && err->code != GOT_ERR_CANCELLED)
4620 if (have_staged_files) {
4621 err = check_non_staged_files(fileindex, paths);
4626 cc_arg.commitable_paths = &commitable_paths;
4627 cc_arg.worktree = worktree;
4629 cc_arg.have_staged_files = have_staged_files;
4630 TAILQ_FOREACH(pe, paths, entry) {
4631 err = worktree_status(worktree, pe->path, fileindex, repo,
4632 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4637 if (TAILQ_EMPTY(&commitable_paths)) {
4638 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4642 TAILQ_FOREACH(pe, paths, entry) {
4643 err = check_path_is_commitable(pe->path, &commitable_paths);
4648 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4649 struct got_commitable *ct = pe->data;
4650 const char *ct_path = ct->in_repo_path;
4652 while (ct_path[0] == '/')
4654 err = check_out_of_date(ct_path, ct->status,
4655 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4656 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4662 err = commit_worktree(new_commit_id, &commitable_paths,
4663 head_commit_id, worktree, author, committer,
4664 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4668 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4669 fileindex, have_staged_files);
4670 sync_err = sync_fileindex(fileindex, fileindex_path);
4671 if (sync_err && err == NULL)
4675 got_fileindex_free(fileindex);
4676 free(fileindex_path);
4677 unlockerr = lock_worktree(worktree, LOCK_SH);
4678 if (unlockerr && err == NULL)
4680 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4681 struct got_commitable *ct = pe->data;
4682 free_commitable(ct);
4684 got_pathlist_free(&commitable_paths);
4689 got_commitable_get_path(struct got_commitable *ct)
4695 got_commitable_get_status(struct got_commitable *ct)
4700 struct check_rebase_ok_arg {
4701 struct got_worktree *worktree;
4702 struct got_repository *repo;
4705 static const struct got_error *
4706 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4708 const struct got_error *err = NULL;
4709 struct check_rebase_ok_arg *a = arg;
4710 unsigned char status;
4714 /* Reject rebase of a work tree with mixed base commits. */
4715 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4716 SHA1_DIGEST_LENGTH))
4717 return got_error(GOT_ERR_MIXED_COMMITS);
4719 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4721 return got_error_from_errno("asprintf");
4723 /* Reject rebase of a work tree with modified or staged files. */
4724 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4729 if (status != GOT_STATUS_NO_CHANGE)
4730 return got_error(GOT_ERR_MODIFIED);
4731 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4732 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4737 const struct got_error *
4738 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4739 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4740 struct got_worktree *worktree, struct got_reference *branch,
4741 struct got_repository *repo)
4743 const struct got_error *err = NULL;
4744 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4745 char *branch_ref_name = NULL;
4746 char *fileindex_path = NULL;
4747 struct check_rebase_ok_arg ok_arg;
4748 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4749 struct got_object_id *wt_branch_tip = NULL;
4751 *new_base_branch_ref = NULL;
4755 err = lock_worktree(worktree, LOCK_EX);
4759 err = open_fileindex(fileindex, &fileindex_path, worktree);
4763 ok_arg.worktree = worktree;
4765 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4770 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4774 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4778 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4782 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4787 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4790 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4791 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4795 err = got_ref_alloc_symref(new_base_branch_ref,
4796 new_base_branch_ref_name, wt_branch);
4799 err = got_ref_write(*new_base_branch_ref, repo);
4803 /* TODO Lock original branch's ref while rebasing? */
4805 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4809 err = got_ref_write(branch_ref, repo);
4813 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4814 worktree->base_commit_id);
4817 err = got_ref_write(*tmp_branch, repo);
4821 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4825 free(fileindex_path);
4826 free(tmp_branch_name);
4827 free(new_base_branch_ref_name);
4828 free(branch_ref_name);
4830 got_ref_close(branch_ref);
4832 got_ref_close(wt_branch);
4833 free(wt_branch_tip);
4835 if (*new_base_branch_ref) {
4836 got_ref_close(*new_base_branch_ref);
4837 *new_base_branch_ref = NULL;
4840 got_ref_close(*tmp_branch);
4844 got_fileindex_free(*fileindex);
4847 lock_worktree(worktree, LOCK_SH);
4852 const struct got_error *
4853 got_worktree_rebase_continue(struct got_object_id **commit_id,
4854 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4855 struct got_reference **branch, struct got_fileindex **fileindex,
4856 struct got_worktree *worktree, struct got_repository *repo)
4858 const struct got_error *err;
4859 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4860 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4861 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4862 char *fileindex_path = NULL;
4863 int have_staged_files = 0;
4866 *new_base_branch = NULL;
4871 err = lock_worktree(worktree, LOCK_EX);
4875 err = open_fileindex(fileindex, &fileindex_path, worktree);
4879 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4880 &have_staged_files);
4881 if (err && err->code != GOT_ERR_CANCELLED)
4883 if (have_staged_files) {
4884 err = got_error(GOT_ERR_STAGED_PATHS);
4888 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4892 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4896 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4900 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4904 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4908 err = got_ref_open(branch, repo,
4909 got_ref_get_symref_target(branch_ref), 0);
4913 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4917 err = got_ref_resolve(commit_id, repo, commit_ref);
4921 err = got_ref_open(new_base_branch, repo,
4922 new_base_branch_ref_name, 0);
4926 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4930 free(commit_ref_name);
4931 free(branch_ref_name);
4932 free(fileindex_path);
4934 got_ref_close(commit_ref);
4936 got_ref_close(branch_ref);
4941 got_ref_close(*tmp_branch);
4944 if (*new_base_branch) {
4945 got_ref_close(*new_base_branch);
4946 *new_base_branch = NULL;
4949 got_ref_close(*branch);
4953 got_fileindex_free(*fileindex);
4956 lock_worktree(worktree, LOCK_SH);
4961 const struct got_error *
4962 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4964 const struct got_error *err;
4965 char *tmp_branch_name = NULL;
4967 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4971 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4972 free(tmp_branch_name);
4976 static const struct got_error *
4977 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4978 char **logmsg, void *arg)
4984 static const struct got_error *
4985 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4986 const char *path, struct got_object_id *blob_id,
4987 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4988 int dirfd, const char *de_name)
4993 struct collect_merged_paths_arg {
4994 got_worktree_checkout_cb progress_cb;
4996 struct got_pathlist_head *merged_paths;
4999 static const struct got_error *
5000 collect_merged_paths(void *arg, unsigned char status, const char *path)
5002 const struct got_error *err;
5003 struct collect_merged_paths_arg *a = arg;
5005 struct got_pathlist_entry *new;
5007 err = (*a->progress_cb)(a->progress_arg, status, path);
5011 if (status != GOT_STATUS_MERGE &&
5012 status != GOT_STATUS_ADD &&
5013 status != GOT_STATUS_DELETE &&
5014 status != GOT_STATUS_CONFLICT)
5019 return got_error_from_errno("strdup");
5021 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5022 if (err || new == NULL)
5028 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5030 struct got_pathlist_entry *pe;
5032 TAILQ_FOREACH(pe, merged_paths, entry)
5033 free((char *)pe->path);
5035 got_pathlist_free(merged_paths);
5038 static const struct got_error *
5039 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5040 int is_rebase, struct got_repository *repo)
5042 const struct got_error *err;
5043 struct got_reference *commit_ref = NULL;
5045 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5047 if (err->code != GOT_ERR_NOT_REF)
5049 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5052 err = got_ref_write(commit_ref, repo);
5055 } else if (is_rebase) {
5056 struct got_object_id *stored_id;
5059 err = got_ref_resolve(&stored_id, repo, commit_ref);
5062 cmp = got_object_id_cmp(commit_id, stored_id);
5065 err = got_error(GOT_ERR_REBASE_COMMITID);
5071 got_ref_close(commit_ref);
5075 static const struct got_error *
5076 rebase_merge_files(struct got_pathlist_head *merged_paths,
5077 const char *commit_ref_name, struct got_worktree *worktree,
5078 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5079 struct got_object_id *commit_id, struct got_repository *repo,
5080 got_worktree_checkout_cb progress_cb, void *progress_arg,
5081 got_cancel_cb cancel_cb, void *cancel_arg)
5083 const struct got_error *err;
5084 struct got_reference *commit_ref = NULL;
5085 struct collect_merged_paths_arg cmp_arg;
5086 char *fileindex_path;
5088 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5090 err = get_fileindex_path(&fileindex_path, worktree);
5094 cmp_arg.progress_cb = progress_cb;
5095 cmp_arg.progress_arg = progress_arg;
5096 cmp_arg.merged_paths = merged_paths;
5097 err = merge_files(worktree, fileindex, fileindex_path,
5098 parent_commit_id, commit_id, repo, collect_merged_paths,
5099 &cmp_arg, cancel_cb, cancel_arg);
5101 got_ref_close(commit_ref);
5105 const struct got_error *
5106 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5107 struct got_worktree *worktree, struct got_fileindex *fileindex,
5108 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5109 struct got_repository *repo,
5110 got_worktree_checkout_cb progress_cb, void *progress_arg,
5111 got_cancel_cb cancel_cb, void *cancel_arg)
5113 const struct got_error *err;
5114 char *commit_ref_name;
5116 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5120 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5124 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5125 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5126 progress_arg, cancel_cb, cancel_arg);
5128 free(commit_ref_name);
5132 const struct got_error *
5133 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5134 struct got_worktree *worktree, struct got_fileindex *fileindex,
5135 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5136 struct got_repository *repo,
5137 got_worktree_checkout_cb progress_cb, void *progress_arg,
5138 got_cancel_cb cancel_cb, void *cancel_arg)
5140 const struct got_error *err;
5141 char *commit_ref_name;
5143 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5147 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5151 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5152 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5153 progress_arg, cancel_cb, cancel_arg);
5155 free(commit_ref_name);
5159 static const struct got_error *
5160 rebase_commit(struct got_object_id **new_commit_id,
5161 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5162 struct got_worktree *worktree, struct got_fileindex *fileindex,
5163 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5164 const char *new_logmsg, struct got_repository *repo)
5166 const struct got_error *err, *sync_err;
5167 struct got_pathlist_head commitable_paths;
5168 struct collect_commitables_arg cc_arg;
5169 char *fileindex_path = NULL;
5170 struct got_reference *head_ref = NULL;
5171 struct got_object_id *head_commit_id = NULL;
5172 char *logmsg = NULL;
5174 TAILQ_INIT(&commitable_paths);
5175 *new_commit_id = NULL;
5177 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5179 err = get_fileindex_path(&fileindex_path, worktree);
5183 cc_arg.commitable_paths = &commitable_paths;
5184 cc_arg.worktree = worktree;
5186 cc_arg.have_staged_files = 0;
5188 * If possible get the status of individual files directly to
5189 * avoid crawling the entire work tree once per rebased commit.
5190 * TODO: Ideally, merged_paths would contain a list of commitables
5191 * we could use so we could skip worktree_status() entirely.
5194 struct got_pathlist_entry *pe;
5195 TAILQ_FOREACH(pe, merged_paths, entry) {
5196 err = worktree_status(worktree, pe->path, fileindex,
5197 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5203 err = worktree_status(worktree, "", fileindex, repo,
5204 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5209 if (TAILQ_EMPTY(&commitable_paths)) {
5210 /* No-op change; commit will be elided. */
5211 err = got_ref_delete(commit_ref, repo);
5214 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5218 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5222 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5227 logmsg = strdup(new_logmsg);
5228 if (logmsg == NULL) {
5229 err = got_error_from_errno("strdup");
5233 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5238 /* NB: commit_worktree will call free(logmsg) */
5239 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5240 worktree, got_object_commit_get_author(orig_commit),
5241 got_object_commit_get_committer(orig_commit),
5242 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5246 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5250 err = got_ref_delete(commit_ref, repo);
5254 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5256 sync_err = sync_fileindex(fileindex, fileindex_path);
5257 if (sync_err && err == NULL)
5260 free(fileindex_path);
5261 free(head_commit_id);
5263 got_ref_close(head_ref);
5265 free(*new_commit_id);
5266 *new_commit_id = NULL;
5271 const struct got_error *
5272 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5273 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5274 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5275 struct got_commit_object *orig_commit,
5276 struct got_object_id *orig_commit_id, struct got_repository *repo)
5278 const struct got_error *err;
5279 char *commit_ref_name;
5280 struct got_reference *commit_ref = NULL;
5281 struct got_object_id *commit_id = NULL;
5283 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5287 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5290 err = got_ref_resolve(&commit_id, repo, commit_ref);
5293 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5294 err = got_error(GOT_ERR_REBASE_COMMITID);
5298 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5299 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5302 got_ref_close(commit_ref);
5303 free(commit_ref_name);
5308 const struct got_error *
5309 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5310 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5311 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5312 struct got_commit_object *orig_commit,
5313 struct got_object_id *orig_commit_id, const char *new_logmsg,
5314 struct got_repository *repo)
5316 const struct got_error *err;
5317 char *commit_ref_name;
5318 struct got_reference *commit_ref = NULL;
5320 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5324 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5328 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5329 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5332 got_ref_close(commit_ref);
5333 free(commit_ref_name);
5337 const struct got_error *
5338 got_worktree_rebase_postpone(struct got_worktree *worktree,
5339 struct got_fileindex *fileindex)
5342 got_fileindex_free(fileindex);
5343 return lock_worktree(worktree, LOCK_SH);
5346 static const struct got_error *
5347 delete_ref(const char *name, struct got_repository *repo)
5349 const struct got_error *err;
5350 struct got_reference *ref;
5352 err = got_ref_open(&ref, repo, name, 0);
5354 if (err->code == GOT_ERR_NOT_REF)
5359 err = got_ref_delete(ref, repo);
5364 static const struct got_error *
5365 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5367 const struct got_error *err;
5368 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5369 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5371 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5374 err = delete_ref(tmp_branch_name, repo);
5378 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5381 err = delete_ref(new_base_branch_ref_name, repo);
5385 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5388 err = delete_ref(branch_ref_name, repo);
5392 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5395 err = delete_ref(commit_ref_name, repo);
5400 free(tmp_branch_name);
5401 free(new_base_branch_ref_name);
5402 free(branch_ref_name);
5403 free(commit_ref_name);
5407 const struct got_error *
5408 got_worktree_rebase_complete(struct got_worktree *worktree,
5409 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5410 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5411 struct got_repository *repo)
5413 const struct got_error *err, *unlockerr;
5414 struct got_object_id *new_head_commit_id = NULL;
5416 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5420 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5424 err = got_ref_write(rebased_branch, repo);
5428 err = got_worktree_set_head_ref(worktree, rebased_branch);
5432 err = delete_rebase_refs(worktree, repo);
5435 got_fileindex_free(fileindex);
5436 free(new_head_commit_id);
5437 unlockerr = lock_worktree(worktree, LOCK_SH);
5438 if (unlockerr && err == NULL)
5443 const struct got_error *
5444 got_worktree_rebase_abort(struct got_worktree *worktree,
5445 struct got_fileindex *fileindex, struct got_repository *repo,
5446 struct got_reference *new_base_branch,
5447 got_worktree_checkout_cb progress_cb, void *progress_arg)
5449 const struct got_error *err, *unlockerr, *sync_err;
5450 struct got_reference *resolved = NULL;
5451 struct got_object_id *commit_id = NULL;
5452 char *fileindex_path = NULL;
5453 struct revert_file_args rfa;
5454 struct got_object_id *tree_id = NULL;
5456 err = lock_worktree(worktree, LOCK_EX);
5460 err = got_ref_open(&resolved, repo,
5461 got_ref_get_symref_target(new_base_branch), 0);
5465 err = got_worktree_set_head_ref(worktree, resolved);
5470 * XXX commits to the base branch could have happened while
5471 * we were busy rebasing; should we store the original commit ID
5472 * when rebase begins and read it back here?
5474 err = got_ref_resolve(&commit_id, repo, resolved);
5478 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5482 err = got_object_id_by_path(&tree_id, repo,
5483 worktree->base_commit_id, worktree->path_prefix);
5487 err = delete_rebase_refs(worktree, repo);
5491 err = get_fileindex_path(&fileindex_path, worktree);
5495 rfa.worktree = worktree;
5496 rfa.fileindex = fileindex;
5497 rfa.progress_cb = progress_cb;
5498 rfa.progress_arg = progress_arg;
5499 rfa.patch_cb = NULL;
5500 rfa.patch_arg = NULL;
5502 err = worktree_status(worktree, "", fileindex, repo,
5503 revert_file, &rfa, NULL, NULL, 0, 0);
5507 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5508 repo, progress_cb, progress_arg, NULL, NULL);
5510 sync_err = sync_fileindex(fileindex, fileindex_path);
5511 if (sync_err && err == NULL)
5514 got_ref_close(resolved);
5518 got_fileindex_free(fileindex);
5519 free(fileindex_path);
5521 unlockerr = lock_worktree(worktree, LOCK_SH);
5522 if (unlockerr && err == NULL)
5527 const struct got_error *
5528 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5529 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5530 struct got_fileindex **fileindex, struct got_worktree *worktree,
5531 struct got_repository *repo)
5533 const struct got_error *err = NULL;
5534 char *tmp_branch_name = NULL;
5535 char *branch_ref_name = NULL;
5536 char *base_commit_ref_name = NULL;
5537 char *fileindex_path = NULL;
5538 struct check_rebase_ok_arg ok_arg;
5539 struct got_reference *wt_branch = NULL;
5540 struct got_reference *base_commit_ref = NULL;
5544 *base_commit_id = NULL;
5547 err = lock_worktree(worktree, LOCK_EX);
5551 err = open_fileindex(fileindex, &fileindex_path, worktree);
5555 ok_arg.worktree = worktree;
5557 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5562 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5566 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5570 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5575 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5580 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5584 err = got_ref_write(*branch_ref, repo);
5588 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5589 worktree->base_commit_id);
5592 err = got_ref_write(base_commit_ref, repo);
5595 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5596 if (*base_commit_id == NULL) {
5597 err = got_error_from_errno("got_object_id_dup");
5601 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5602 worktree->base_commit_id);
5605 err = got_ref_write(*tmp_branch, repo);
5609 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5613 free(fileindex_path);
5614 free(tmp_branch_name);
5615 free(branch_ref_name);
5616 free(base_commit_ref_name);
5618 got_ref_close(wt_branch);
5621 got_ref_close(*branch_ref);
5625 got_ref_close(*tmp_branch);
5628 free(*base_commit_id);
5630 got_fileindex_free(*fileindex);
5633 lock_worktree(worktree, LOCK_SH);
5638 const struct got_error *
5639 got_worktree_histedit_postpone(struct got_worktree *worktree,
5640 struct got_fileindex *fileindex)
5643 got_fileindex_free(fileindex);
5644 return lock_worktree(worktree, LOCK_SH);
5647 const struct got_error *
5648 got_worktree_histedit_in_progress(int *in_progress,
5649 struct got_worktree *worktree)
5651 const struct got_error *err;
5652 char *tmp_branch_name = NULL;
5654 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5658 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5659 free(tmp_branch_name);
5663 const struct got_error *
5664 got_worktree_histedit_continue(struct got_object_id **commit_id,
5665 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5666 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5667 struct got_worktree *worktree, struct got_repository *repo)
5669 const struct got_error *err;
5670 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5671 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5672 struct got_reference *commit_ref = NULL;
5673 struct got_reference *base_commit_ref = NULL;
5674 char *fileindex_path = NULL;
5675 int have_staged_files = 0;
5679 *base_commit_id = NULL;
5682 err = lock_worktree(worktree, LOCK_EX);
5686 err = open_fileindex(fileindex, &fileindex_path, worktree);
5690 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5691 &have_staged_files);
5692 if (err && err->code != GOT_ERR_CANCELLED)
5694 if (have_staged_files) {
5695 err = got_error(GOT_ERR_STAGED_PATHS);
5699 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5703 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5707 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5711 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5716 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5720 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5723 err = got_ref_resolve(commit_id, repo, commit_ref);
5727 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5730 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5734 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5738 free(commit_ref_name);
5739 free(branch_ref_name);
5740 free(fileindex_path);
5742 got_ref_close(commit_ref);
5743 if (base_commit_ref)
5744 got_ref_close(base_commit_ref);
5748 free(*base_commit_id);
5749 *base_commit_id = NULL;
5751 got_ref_close(*tmp_branch);
5755 got_fileindex_free(*fileindex);
5758 lock_worktree(worktree, LOCK_EX);
5763 static const struct got_error *
5764 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5766 const struct got_error *err;
5767 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5768 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5770 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5773 err = delete_ref(tmp_branch_name, repo);
5777 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5781 err = delete_ref(base_commit_ref_name, repo);
5785 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5788 err = delete_ref(branch_ref_name, repo);
5792 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5795 err = delete_ref(commit_ref_name, repo);
5799 free(tmp_branch_name);
5800 free(base_commit_ref_name);
5801 free(branch_ref_name);
5802 free(commit_ref_name);
5806 const struct got_error *
5807 got_worktree_histedit_abort(struct got_worktree *worktree,
5808 struct got_fileindex *fileindex, struct got_repository *repo,
5809 struct got_reference *branch, struct got_object_id *base_commit_id,
5810 got_worktree_checkout_cb progress_cb, void *progress_arg)
5812 const struct got_error *err, *unlockerr, *sync_err;
5813 struct got_reference *resolved = NULL;
5814 char *fileindex_path = NULL;
5815 struct got_object_id *tree_id = NULL;
5816 struct revert_file_args rfa;
5818 err = lock_worktree(worktree, LOCK_EX);
5822 err = got_ref_open(&resolved, repo,
5823 got_ref_get_symref_target(branch), 0);
5827 err = got_worktree_set_head_ref(worktree, resolved);
5831 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5835 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5836 worktree->path_prefix);
5840 err = delete_histedit_refs(worktree, repo);
5844 err = get_fileindex_path(&fileindex_path, worktree);
5848 rfa.worktree = worktree;
5849 rfa.fileindex = fileindex;
5850 rfa.progress_cb = progress_cb;
5851 rfa.progress_arg = progress_arg;
5852 rfa.patch_cb = NULL;
5853 rfa.patch_arg = NULL;
5855 err = worktree_status(worktree, "", fileindex, repo,
5856 revert_file, &rfa, NULL, NULL, 0, 0);
5860 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5861 repo, progress_cb, progress_arg, NULL, NULL);
5863 sync_err = sync_fileindex(fileindex, fileindex_path);
5864 if (sync_err && err == NULL)
5867 got_ref_close(resolved);
5869 free(fileindex_path);
5871 unlockerr = lock_worktree(worktree, LOCK_SH);
5872 if (unlockerr && err == NULL)
5877 const struct got_error *
5878 got_worktree_histedit_complete(struct got_worktree *worktree,
5879 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5880 struct got_reference *edited_branch, struct got_repository *repo)
5882 const struct got_error *err, *unlockerr;
5883 struct got_object_id *new_head_commit_id = NULL;
5884 struct got_reference *resolved = NULL;
5886 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5890 err = got_ref_open(&resolved, repo,
5891 got_ref_get_symref_target(edited_branch), 0);
5895 err = got_ref_change_ref(resolved, new_head_commit_id);
5899 err = got_ref_write(resolved, repo);
5903 err = got_worktree_set_head_ref(worktree, resolved);
5907 err = delete_histedit_refs(worktree, repo);
5910 got_fileindex_free(fileindex);
5911 free(new_head_commit_id);
5912 unlockerr = lock_worktree(worktree, LOCK_SH);
5913 if (unlockerr && err == NULL)
5918 const struct got_error *
5919 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5920 struct got_object_id *commit_id, struct got_repository *repo)
5922 const struct got_error *err;
5923 char *commit_ref_name;
5925 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5929 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5933 err = delete_ref(commit_ref_name, repo);
5935 free(commit_ref_name);
5939 const struct got_error *
5940 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5941 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5942 struct got_worktree *worktree, const char *refname,
5943 struct got_repository *repo)
5945 const struct got_error *err = NULL;
5946 char *fileindex_path = NULL;
5947 struct check_rebase_ok_arg ok_arg;
5951 *base_branch_ref = NULL;
5953 err = lock_worktree(worktree, LOCK_EX);
5957 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5958 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5959 "cannot integrate a branch into itself; "
5960 "update -b or different branch name required");
5964 err = open_fileindex(fileindex, &fileindex_path, worktree);
5968 /* Preconditions are the same as for rebase. */
5969 ok_arg.worktree = worktree;
5971 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5976 err = got_ref_open(branch_ref, repo, refname, 1);
5980 err = got_ref_open(base_branch_ref, repo,
5981 got_worktree_get_head_ref_name(worktree), 1);
5985 got_ref_close(*branch_ref);
5988 if (*base_branch_ref) {
5989 got_ref_close(*base_branch_ref);
5990 *base_branch_ref = NULL;
5993 got_fileindex_free(*fileindex);
5996 lock_worktree(worktree, LOCK_SH);
6001 const struct got_error *
6002 got_worktree_integrate_continue(struct got_worktree *worktree,
6003 struct got_fileindex *fileindex, struct got_repository *repo,
6004 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6005 got_worktree_checkout_cb progress_cb, void *progress_arg,
6006 got_cancel_cb cancel_cb, void *cancel_arg)
6008 const struct got_error *err = NULL, *sync_err, *unlockerr;
6009 char *fileindex_path = NULL;
6010 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6012 err = get_fileindex_path(&fileindex_path, worktree);
6016 err = got_ref_resolve(&commit_id, repo, branch_ref);
6020 err = got_object_id_by_path(&tree_id, repo, commit_id,
6021 worktree->path_prefix);
6025 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6029 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6030 progress_cb, progress_arg, cancel_cb, cancel_arg);
6034 err = got_ref_change_ref(base_branch_ref, commit_id);
6038 err = got_ref_write(base_branch_ref, repo);
6040 sync_err = sync_fileindex(fileindex, fileindex_path);
6041 if (sync_err && err == NULL)
6045 unlockerr = got_ref_unlock(branch_ref);
6046 if (unlockerr && err == NULL)
6048 got_ref_close(branch_ref);
6050 unlockerr = got_ref_unlock(base_branch_ref);
6051 if (unlockerr && err == NULL)
6053 got_ref_close(base_branch_ref);
6055 got_fileindex_free(fileindex);
6056 free(fileindex_path);
6059 unlockerr = lock_worktree(worktree, LOCK_SH);
6060 if (unlockerr && err == NULL)
6065 const struct got_error *
6066 got_worktree_integrate_abort(struct got_worktree *worktree,
6067 struct got_fileindex *fileindex, struct got_repository *repo,
6068 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6070 const struct got_error *err = NULL, *unlockerr = NULL;
6072 got_fileindex_free(fileindex);
6074 err = lock_worktree(worktree, LOCK_SH);
6076 unlockerr = got_ref_unlock(branch_ref);
6077 if (unlockerr && err == NULL)
6079 got_ref_close(branch_ref);
6081 unlockerr = got_ref_unlock(base_branch_ref);
6082 if (unlockerr && err == NULL)
6084 got_ref_close(base_branch_ref);
6089 struct check_stage_ok_arg {
6090 struct got_object_id *head_commit_id;
6091 struct got_worktree *worktree;
6092 struct got_fileindex *fileindex;
6093 struct got_repository *repo;
6097 const struct got_error *
6098 check_stage_ok(void *arg, unsigned char status,
6099 unsigned char staged_status, const char *relpath,
6100 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6101 struct got_object_id *commit_id, int dirfd, const char *de_name)
6103 struct check_stage_ok_arg *a = arg;
6104 const struct got_error *err = NULL;
6105 struct got_fileindex_entry *ie;
6106 struct got_object_id base_commit_id;
6107 struct got_object_id *base_commit_idp = NULL;
6108 char *in_repo_path = NULL, *p;
6110 if (status == GOT_STATUS_UNVERSIONED ||
6111 status == GOT_STATUS_NO_CHANGE)
6113 if (status == GOT_STATUS_NONEXISTENT)
6114 return got_error_set_errno(ENOENT, relpath);
6116 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6118 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6120 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6121 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6123 return got_error_from_errno("asprintf");
6125 if (got_fileindex_entry_has_commit(ie)) {
6126 memcpy(base_commit_id.sha1, ie->commit_sha1,
6127 SHA1_DIGEST_LENGTH);
6128 base_commit_idp = &base_commit_id;
6131 if (status == GOT_STATUS_CONFLICT) {
6132 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6134 } else if (status != GOT_STATUS_ADD &&
6135 status != GOT_STATUS_MODIFY &&
6136 status != GOT_STATUS_DELETE) {
6137 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6141 a->have_changes = 1;
6146 err = check_out_of_date(p, status, staged_status,
6147 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6148 GOT_ERR_STAGE_OUT_OF_DATE);
6154 struct stage_path_arg {
6155 struct got_worktree *worktree;
6156 struct got_fileindex *fileindex;
6157 struct got_repository *repo;
6158 got_worktree_status_cb status_cb;
6160 got_worktree_patch_cb patch_cb;
6162 int staged_something;
6165 static const struct got_error *
6166 stage_path(void *arg, unsigned char status,
6167 unsigned char staged_status, const char *relpath,
6168 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6169 struct got_object_id *commit_id, int dirfd, const char *de_name)
6171 struct stage_path_arg *a = arg;
6172 const struct got_error *err = NULL;
6173 struct got_fileindex_entry *ie;
6174 char *ondisk_path = NULL, *path_content = NULL;
6176 struct got_object_id *new_staged_blob_id = NULL;
6178 if (status == GOT_STATUS_UNVERSIONED)
6181 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6183 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6185 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6187 return got_error_from_errno("asprintf");
6190 case GOT_STATUS_ADD:
6191 case GOT_STATUS_MODIFY:
6193 if (status == GOT_STATUS_ADD) {
6194 int choice = GOT_PATCH_CHOICE_NONE;
6195 err = (*a->patch_cb)(&choice, a->patch_arg,
6196 status, ie->path, NULL, 1, 1);
6199 if (choice != GOT_PATCH_CHOICE_YES)
6202 err = create_patched_content(&path_content, 0,
6203 staged_blob_id ? staged_blob_id : blob_id,
6204 ondisk_path, dirfd, de_name, ie->path,
6205 a->repo, a->patch_cb, a->patch_arg);
6206 if (err || path_content == NULL)
6210 err = got_object_blob_create(&new_staged_blob_id,
6211 path_content ? path_content : ondisk_path, a->repo);
6214 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6215 SHA1_DIGEST_LENGTH);
6216 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6217 stage = GOT_FILEIDX_STAGE_ADD;
6219 stage = GOT_FILEIDX_STAGE_MODIFY;
6220 got_fileindex_entry_stage_set(ie, stage);
6221 a->staged_something = 1;
6222 if (a->status_cb == NULL)
6224 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6225 get_staged_status(ie), relpath, blob_id,
6226 new_staged_blob_id, NULL, dirfd, de_name);
6228 case GOT_STATUS_DELETE:
6229 if (staged_status == GOT_STATUS_DELETE)
6232 int choice = GOT_PATCH_CHOICE_NONE;
6233 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6234 ie->path, NULL, 1, 1);
6237 if (choice == GOT_PATCH_CHOICE_NO)
6239 if (choice != GOT_PATCH_CHOICE_YES) {
6240 err = got_error(GOT_ERR_PATCH_CHOICE);
6244 stage = GOT_FILEIDX_STAGE_DELETE;
6245 got_fileindex_entry_stage_set(ie, stage);
6246 a->staged_something = 1;
6247 if (a->status_cb == NULL)
6249 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6250 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6253 case GOT_STATUS_NO_CHANGE:
6255 case GOT_STATUS_CONFLICT:
6256 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6258 case GOT_STATUS_NONEXISTENT:
6259 err = got_error_set_errno(ENOENT, relpath);
6262 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6266 if (path_content && unlink(path_content) == -1 && err == NULL)
6267 err = got_error_from_errno2("unlink", path_content);
6270 free(new_staged_blob_id);
6274 const struct got_error *
6275 got_worktree_stage(struct got_worktree *worktree,
6276 struct got_pathlist_head *paths,
6277 got_worktree_status_cb status_cb, void *status_arg,
6278 got_worktree_patch_cb patch_cb, void *patch_arg,
6279 struct got_repository *repo)
6281 const struct got_error *err = NULL, *sync_err, *unlockerr;
6282 struct got_pathlist_entry *pe;
6283 struct got_fileindex *fileindex = NULL;
6284 char *fileindex_path = NULL;
6285 struct got_reference *head_ref = NULL;
6286 struct got_object_id *head_commit_id = NULL;
6287 struct check_stage_ok_arg oka;
6288 struct stage_path_arg spa;
6290 err = lock_worktree(worktree, LOCK_EX);
6294 err = got_ref_open(&head_ref, repo,
6295 got_worktree_get_head_ref_name(worktree), 0);
6298 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6301 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6305 /* Check pre-conditions before staging anything. */
6306 oka.head_commit_id = head_commit_id;
6307 oka.worktree = worktree;
6308 oka.fileindex = fileindex;
6310 oka.have_changes = 0;
6311 TAILQ_FOREACH(pe, paths, entry) {
6312 err = worktree_status(worktree, pe->path, fileindex, repo,
6313 check_stage_ok, &oka, NULL, NULL, 0, 0);
6317 if (!oka.have_changes) {
6318 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6322 spa.worktree = worktree;
6323 spa.fileindex = fileindex;
6325 spa.patch_cb = patch_cb;
6326 spa.patch_arg = patch_arg;
6327 spa.status_cb = status_cb;
6328 spa.status_arg = status_arg;
6329 spa.staged_something = 0;
6330 TAILQ_FOREACH(pe, paths, entry) {
6331 err = worktree_status(worktree, pe->path, fileindex, repo,
6332 stage_path, &spa, NULL, NULL, 0, 0);
6336 if (!spa.staged_something) {
6337 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6341 sync_err = sync_fileindex(fileindex, fileindex_path);
6342 if (sync_err && err == NULL)
6346 got_ref_close(head_ref);
6347 free(head_commit_id);
6348 free(fileindex_path);
6350 got_fileindex_free(fileindex);
6351 unlockerr = lock_worktree(worktree, LOCK_SH);
6352 if (unlockerr && err == NULL)
6357 struct unstage_path_arg {
6358 struct got_worktree *worktree;
6359 struct got_fileindex *fileindex;
6360 struct got_repository *repo;
6361 got_worktree_checkout_cb progress_cb;
6363 got_worktree_patch_cb patch_cb;
6367 static const struct got_error *
6368 create_unstaged_content(char **path_unstaged_content,
6369 char **path_new_staged_content, struct got_object_id *blob_id,
6370 struct got_object_id *staged_blob_id, const char *relpath,
6371 struct got_repository *repo,
6372 got_worktree_patch_cb patch_cb, void *patch_arg)
6374 const struct got_error *err;
6375 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6376 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6377 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6378 struct stat sb1, sb2;
6379 struct got_diff_changes *changes = NULL;
6380 struct got_diff_state *ds = NULL;
6381 struct got_diff_args *args = NULL;
6382 struct got_diff_change *change;
6383 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6384 int have_content = 0, have_rejected_content = 0;
6386 *path_unstaged_content = NULL;
6387 *path_new_staged_content = NULL;
6389 err = got_object_id_str(&label1, blob_id);
6392 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6396 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6400 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6404 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6408 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6412 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6416 if (stat(path1, &sb1) == -1) {
6417 err = got_error_from_errno2("stat", path1);
6421 if (stat(path2, &sb2) == -1) {
6422 err = got_error_from_errno2("stat", path2);
6426 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6427 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6431 err = got_opentemp_named(path_unstaged_content, &outfile,
6432 "got-unstaged-content");
6435 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6436 "got-new-staged-content");
6440 if (fseek(f1, 0L, SEEK_SET) == -1) {
6441 err = got_ferror(f1, GOT_ERR_IO);
6444 if (fseek(f2, 0L, SEEK_SET) == -1) {
6445 err = got_ferror(f2, GOT_ERR_IO);
6448 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6450 err = apply_or_reject_change(&choice, change, ++n,
6451 changes->nchanges, ds, args, diff_flags, relpath,
6452 f1, f2, &line_cur1, &line_cur2,
6453 outfile, rejectfile, patch_cb, patch_arg);
6456 if (choice == GOT_PATCH_CHOICE_YES)
6459 have_rejected_content = 1;
6460 if (choice == GOT_PATCH_CHOICE_QUIT)
6463 if (have_content || have_rejected_content)
6464 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6465 outfile, rejectfile);
6469 got_object_blob_close(blob);
6471 got_object_blob_close(staged_blob);
6472 if (f1 && fclose(f1) == EOF && err == NULL)
6473 err = got_error_from_errno2("fclose", path1);
6474 if (f2 && fclose(f2) == EOF && err == NULL)
6475 err = got_error_from_errno2("fclose", path2);
6476 if (outfile && fclose(outfile) == EOF && err == NULL)
6477 err = got_error_from_errno2("fclose", *path_unstaged_content);
6478 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6479 err = got_error_from_errno2("fclose", *path_new_staged_content);
6480 if (path1 && unlink(path1) == -1 && err == NULL)
6481 err = got_error_from_errno2("unlink", path1);
6482 if (path2 && unlink(path2) == -1 && err == NULL)
6483 err = got_error_from_errno2("unlink", path2);
6484 if (err || !have_content) {
6485 if (*path_unstaged_content &&
6486 unlink(*path_unstaged_content) == -1 && err == NULL)
6487 err = got_error_from_errno2("unlink",
6488 *path_unstaged_content);
6489 free(*path_unstaged_content);
6490 *path_unstaged_content = NULL;
6492 if (err || !have_rejected_content) {
6493 if (*path_new_staged_content &&
6494 unlink(*path_new_staged_content) == -1 && err == NULL)
6495 err = got_error_from_errno2("unlink",
6496 *path_new_staged_content);
6497 free(*path_new_staged_content);
6498 *path_new_staged_content = NULL;
6502 got_diff_state_free(ds);
6506 got_diff_free_changes(changes);
6512 static const struct got_error *
6513 unstage_path(void *arg, unsigned char status,
6514 unsigned char staged_status, const char *relpath,
6515 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6516 struct got_object_id *commit_id, int dirfd, const char *de_name)
6518 const struct got_error *err = NULL;
6519 struct unstage_path_arg *a = arg;
6520 struct got_fileindex_entry *ie;
6521 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6522 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6523 char *path_new_staged_content = NULL;
6524 char *id_str = NULL, *label_orig = NULL;
6525 int local_changes_subsumed;
6528 if (staged_status != GOT_STATUS_ADD &&
6529 staged_status != GOT_STATUS_MODIFY &&
6530 staged_status != GOT_STATUS_DELETE)
6533 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6535 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6537 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6539 return got_error_from_errno("asprintf");
6541 err = got_object_id_str(&id_str,
6542 commit_id ? commit_id : a->worktree->base_commit_id);
6545 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6547 err = got_error_from_errno("asprintf");
6551 switch (staged_status) {
6552 case GOT_STATUS_MODIFY:
6553 err = got_object_open_as_blob(&blob_base, a->repo,
6558 case GOT_STATUS_ADD:
6560 if (staged_status == GOT_STATUS_ADD) {
6561 int choice = GOT_PATCH_CHOICE_NONE;
6562 err = (*a->patch_cb)(&choice, a->patch_arg,
6563 staged_status, ie->path, NULL, 1, 1);
6566 if (choice != GOT_PATCH_CHOICE_YES)
6569 err = create_unstaged_content(
6570 &path_unstaged_content,
6571 &path_new_staged_content, blob_id,
6572 staged_blob_id, ie->path, a->repo,
6573 a->patch_cb, a->patch_arg);
6574 if (err || path_unstaged_content == NULL)
6576 if (path_new_staged_content) {
6577 err = got_object_blob_create(
6579 path_new_staged_content,
6583 memcpy(ie->staged_blob_sha1,
6584 staged_blob_id->sha1,
6585 SHA1_DIGEST_LENGTH);
6587 err = merge_file(&local_changes_subsumed,
6588 a->worktree, blob_base, ondisk_path,
6589 relpath, got_fileindex_perms_to_st(ie),
6590 path_unstaged_content, label_orig,
6591 "unstaged", a->repo, a->progress_cb,
6594 path_new_staged_content == NULL)
6595 got_fileindex_entry_stage_set(ie,
6596 GOT_FILEIDX_STAGE_NONE);
6597 break; /* Done with this file. */
6600 err = got_object_open_as_blob(&blob_staged, a->repo,
6601 staged_blob_id, 8192);
6604 err = merge_blob(&local_changes_subsumed, a->worktree,
6605 blob_base, ondisk_path, relpath,
6606 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6607 commit_id ? commit_id : a->worktree->base_commit_id,
6608 a->repo, a->progress_cb, a->progress_arg);
6610 got_fileindex_entry_stage_set(ie,
6611 GOT_FILEIDX_STAGE_NONE);
6613 case GOT_STATUS_DELETE:
6615 int choice = GOT_PATCH_CHOICE_NONE;
6616 err = (*a->patch_cb)(&choice, a->patch_arg,
6617 staged_status, ie->path, NULL, 1, 1);
6620 if (choice == GOT_PATCH_CHOICE_NO)
6622 if (choice != GOT_PATCH_CHOICE_YES) {
6623 err = got_error(GOT_ERR_PATCH_CHOICE);
6627 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6628 err = get_file_status(&status, &sb, ie, ondisk_path,
6629 dirfd, de_name, a->repo);
6632 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6637 if (path_unstaged_content &&
6638 unlink(path_unstaged_content) == -1 && err == NULL)
6639 err = got_error_from_errno2("unlink", path_unstaged_content);
6640 if (path_new_staged_content &&
6641 unlink(path_new_staged_content) == -1 && err == NULL)
6642 err = got_error_from_errno2("unlink", path_new_staged_content);
6643 free(path_unstaged_content);
6644 free(path_new_staged_content);
6646 got_object_blob_close(blob_base);
6648 got_object_blob_close(blob_staged);
6654 const struct got_error *
6655 got_worktree_unstage(struct got_worktree *worktree,
6656 struct got_pathlist_head *paths,
6657 got_worktree_checkout_cb progress_cb, void *progress_arg,
6658 got_worktree_patch_cb patch_cb, void *patch_arg,
6659 struct got_repository *repo)
6661 const struct got_error *err = NULL, *sync_err, *unlockerr;
6662 struct got_pathlist_entry *pe;
6663 struct got_fileindex *fileindex = NULL;
6664 char *fileindex_path = NULL;
6665 struct unstage_path_arg upa;
6667 err = lock_worktree(worktree, LOCK_EX);
6671 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6675 upa.worktree = worktree;
6676 upa.fileindex = fileindex;
6678 upa.progress_cb = progress_cb;
6679 upa.progress_arg = progress_arg;
6680 upa.patch_cb = patch_cb;
6681 upa.patch_arg = patch_arg;
6682 TAILQ_FOREACH(pe, paths, entry) {
6683 err = worktree_status(worktree, pe->path, fileindex, repo,
6684 unstage_path, &upa, NULL, NULL, 0, 0);
6689 sync_err = sync_fileindex(fileindex, fileindex_path);
6690 if (sync_err && err == NULL)
6693 free(fileindex_path);
6695 got_fileindex_free(fileindex);
6696 unlockerr = lock_worktree(worktree, LOCK_SH);
6697 if (unlockerr && err == NULL)