2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
69 const struct got_error *err = NULL;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
83 const struct got_error *err = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 err = got_error_from_errno2("open", path);
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
170 (*content)[sb.st_size - 1] = '\0';
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
192 return got_error_from_errno("got_ref_to_str");
194 refstr = strdup(got_ref_get_name(head_ref));
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
210 uint32_t uuid_status;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
223 err = got_ref_resolve(&commit_id, repo, head_ref);
226 err = got_object_get_type(&obj_type, repo, commit_id);
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
421 got_repo_close(repo);
424 free(base_commit_id_str);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
434 (*worktree)->lockfd = fd;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
450 path = dirname(path);
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
535 err = write_head_ref(path_got, head_ref);
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
570 err = got_object_open(&obj, repo, commit_id);
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
595 got_object_close(obj);
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
641 size_t flen1 = 0, flen2 = 0;
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
660 } else if (flen2 == 0) {
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
684 FILE *f1 = NULL, *f2 = NULL;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
700 if (size1 != size2) {
705 f1 = fopen(f1_path, "r");
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
711 err = got_error_from_errno2("fopen", f2_path);
715 err = check_file_contents_equal(same, f1, f2);
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
805 if (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", merged_path);
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
834 * Perform a 3-way merge where blob_orig acts as the common ancestor,
835 * blob_deriv acts as the first derived version, and the file on disk
836 * acts as the second derived version.
838 static const struct got_error *
839 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
840 struct got_blob_object *blob_orig, const char *ondisk_path,
841 const char *path, uint16_t st_mode, const char *label_orig,
842 struct got_blob_object *blob_deriv,
843 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
844 got_worktree_checkout_cb progress_cb, void *progress_arg)
846 const struct got_error *err = NULL;
847 FILE *f_deriv = NULL;
848 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
849 char *label_deriv = NULL, *parent;
851 *local_changes_subsumed = 0;
853 parent = dirname(ondisk_path);
855 return got_error_from_errno2("dirname", ondisk_path);
858 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
859 err = got_error_from_errno("asprintf");
864 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
867 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
872 err = got_object_id_str(&id_str, deriv_base_commit_id);
875 if (asprintf(&label_deriv, "%s: commit %s",
876 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
877 err = got_error_from_errno("asprintf");
881 err = merge_file(local_changes_subsumed, worktree, blob_orig,
882 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
883 label_deriv, repo, progress_cb, progress_arg);
885 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
886 err = got_error_from_errno("fclose");
888 if (blob_deriv_path) {
889 unlink(blob_deriv_path);
890 free(blob_deriv_path);
897 static const struct got_error *
898 update_blob_fileindex_entry(struct got_worktree *worktree,
899 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
900 const char *ondisk_path, const char *path, struct got_blob_object *blob,
901 int update_timestamps)
903 const struct got_error *err = NULL;
906 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
908 err = got_fileindex_entry_update(ie, ondisk_path,
909 blob->id.sha1, worktree->base_commit_id->sha1,
912 struct got_fileindex_entry *new_ie;
913 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
914 path, blob->id.sha1, worktree->base_commit_id->sha1);
916 err = got_fileindex_entry_add(fileindex, new_ie);
922 get_ondisk_perms(int executable, mode_t st_mode)
924 mode_t xbits = S_IXUSR;
927 /* Map read bits to execute bits. */
928 if (st_mode & S_IRGRP)
930 if (st_mode & S_IROTH)
932 return st_mode | xbits;
935 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
938 static const struct got_error *
939 install_blob(struct got_worktree *worktree, const char *ondisk_path,
940 const char *path, mode_t te_mode, mode_t st_mode,
941 struct got_blob_object *blob, int restoring_missing_file,
942 int reverting_versioned_file, struct got_repository *repo,
943 got_worktree_checkout_cb progress_cb, void *progress_arg)
945 const struct got_error *err = NULL;
949 char *tmppath = NULL;
951 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
952 GOT_DEFAULT_FILE_MODE);
954 if (errno == ENOENT) {
955 char *parent = dirname(path);
957 return got_error_from_errno2("dirname", path);
958 err = add_dir_on_disk(worktree, parent);
961 fd = open(ondisk_path,
962 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
963 GOT_DEFAULT_FILE_MODE);
965 return got_error_from_errno2("open",
967 } else if (errno == EEXIST) {
968 if (!S_ISREG(st_mode)) {
969 /* TODO file is obstructed; do something */
970 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
973 err = got_opentemp_named_fd(&tmppath, &fd,
980 return got_error_from_errno2("open", ondisk_path);
983 if (restoring_missing_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
985 else if (reverting_versioned_file)
986 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
988 err = (*progress_cb)(progress_arg,
989 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
993 hdrlen = got_object_blob_get_hdrlen(blob);
995 const uint8_t *buf = got_object_blob_get_read_buf(blob);
996 err = got_object_blob_read_block(&len, blob);
1000 /* Skip blob object header first time around. */
1001 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1003 err = got_error_from_errno("write");
1005 } else if (outlen != len - hdrlen) {
1006 err = got_error(GOT_ERR_IO);
1013 if (fsync(fd) != 0) {
1014 err = got_error_from_errno("fsync");
1019 if (rename(tmppath, ondisk_path) != 0) {
1020 err = got_error_from_errno3("rename", tmppath,
1027 if (chmod(ondisk_path,
1028 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1029 err = got_error_from_errno2("chmod", ondisk_path);
1034 if (fd != -1 && close(fd) != 0 && err == NULL)
1035 err = got_error_from_errno("close");
1040 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1041 static const struct got_error *
1042 get_modified_file_content_status(unsigned char *status, FILE *f)
1044 const struct got_error *err = NULL;
1045 const char *markers[3] = {
1046 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1047 GOT_DIFF_CONFLICT_MARKER_SEP,
1048 GOT_DIFF_CONFLICT_MARKER_END
1053 const char delim[3] = {'\0', '\0', '\0'};
1055 while (*status == GOT_STATUS_MODIFY) {
1056 line = fparseln(f, &len, NULL, delim, 0);
1060 err = got_ferror(f, GOT_ERR_IO);
1064 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1065 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1067 *status = GOT_STATUS_CONFLICT;
1077 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1079 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1080 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1084 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1086 return !(ie->ctime_sec == sb->st_ctime &&
1087 ie->ctime_nsec == sb->st_ctimensec &&
1088 ie->mtime_sec == sb->st_mtime &&
1089 ie->mtime_nsec == sb->st_mtimensec &&
1090 ie->size == (sb->st_size & 0xffffffff) &&
1091 !xbit_differs(ie, sb->st_mode));
1094 static unsigned char
1095 get_staged_status(struct got_fileindex_entry *ie)
1097 switch (got_fileindex_entry_stage_get(ie)) {
1098 case GOT_FILEIDX_STAGE_ADD:
1099 return GOT_STATUS_ADD;
1100 case GOT_FILEIDX_STAGE_DELETE:
1101 return GOT_STATUS_DELETE;
1102 case GOT_FILEIDX_STAGE_MODIFY:
1103 return GOT_STATUS_MODIFY;
1105 return GOT_STATUS_NO_CHANGE;
1109 static const struct got_error *
1110 get_file_status(unsigned char *status, struct stat *sb,
1111 struct got_fileindex_entry *ie, const char *abspath,
1112 int dirfd, const char *de_name, struct got_repository *repo)
1114 const struct got_error *err = NULL;
1115 struct got_object_id id;
1120 struct got_blob_object *blob = NULL;
1122 unsigned char staged_status = get_staged_status(ie);
1124 *status = GOT_STATUS_NO_CHANGE;
1127 * Whenever the caller provides a directory descriptor and a
1128 * directory entry name for the file, use them! This prevents
1129 * race conditions if filesystem paths change beneath our feet.
1132 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1133 if (errno == ENOENT) {
1134 if (got_fileindex_entry_has_file_on_disk(ie))
1135 *status = GOT_STATUS_MISSING;
1137 *status = GOT_STATUS_DELETE;
1140 err = got_error_from_errno2("fstatat", abspath);
1144 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1145 if (fd == -1 && errno != ENOENT)
1146 return got_error_from_errno2("open", abspath);
1147 if (fd == -1 || fstat(fd, sb) == -1) {
1148 if (errno == ENOENT) {
1149 if (got_fileindex_entry_has_file_on_disk(ie))
1150 *status = GOT_STATUS_MISSING;
1152 *status = GOT_STATUS_DELETE;
1155 err = got_error_from_errno2("fstat", abspath);
1160 if (!S_ISREG(sb->st_mode)) {
1161 *status = GOT_STATUS_OBSTRUCTED;
1165 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1166 *status = GOT_STATUS_DELETE;
1168 } else if (!got_fileindex_entry_has_blob(ie) &&
1169 staged_status != GOT_STATUS_ADD) {
1170 *status = GOT_STATUS_ADD;
1174 if (!stat_info_differs(ie, sb))
1177 if (staged_status == GOT_STATUS_MODIFY ||
1178 staged_status == GOT_STATUS_ADD)
1179 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1181 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1183 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1188 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1190 err = got_error_from_errno2("openat", abspath);
1195 f = fdopen(fd, "r");
1197 err = got_error_from_errno2("fdopen", abspath);
1201 hdrlen = got_object_blob_get_hdrlen(blob);
1203 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1204 err = got_object_blob_read_block(&blen, blob);
1207 /* Skip length of blob object header first time around. */
1208 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1209 if (flen == 0 && ferror(f)) {
1210 err = got_error_from_errno("fread");
1215 *status = GOT_STATUS_MODIFY;
1217 } else if (flen == 0) {
1219 *status = GOT_STATUS_MODIFY;
1221 } else if (blen - hdrlen == flen) {
1222 /* Skip blob object header first time around. */
1223 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1224 *status = GOT_STATUS_MODIFY;
1228 *status = GOT_STATUS_MODIFY;
1234 if (*status == GOT_STATUS_MODIFY) {
1236 err = get_modified_file_content_status(status, f);
1237 } else if (xbit_differs(ie, sb->st_mode))
1238 *status = GOT_STATUS_MODE_CHANGE;
1241 got_object_blob_close(blob);
1242 if (f != NULL && fclose(f) == EOF && err == NULL)
1243 err = got_error_from_errno2("fclose", abspath);
1244 if (fd != -1 && close(fd) == -1 && err == NULL)
1245 err = got_error_from_errno2("close", abspath);
1250 * Update timestamps in the file index if a file is unmodified and
1251 * we had to run a full content comparison to find out.
1253 static const struct got_error *
1254 sync_timestamps(char *ondisk_path, unsigned char status,
1255 struct got_fileindex_entry *ie, struct stat *sb)
1257 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1258 return got_fileindex_entry_update(ie, ondisk_path,
1259 ie->blob_sha1, ie->commit_sha1, 1);
1264 static const struct got_error *
1265 update_blob(struct got_worktree *worktree,
1266 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1267 struct got_tree_entry *te, const char *path,
1268 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1271 const struct got_error *err = NULL;
1272 struct got_blob_object *blob = NULL;
1274 unsigned char status = GOT_STATUS_NO_CHANGE;
1277 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1278 return got_error_from_errno("asprintf");
1281 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1282 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1285 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1289 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1290 sb.st_mode = got_fileindex_perms_to_st(ie);
1292 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1294 if (status == GOT_STATUS_OBSTRUCTED) {
1295 err = (*progress_cb)(progress_arg, status, path);
1299 if (ie && status != GOT_STATUS_MISSING &&
1300 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1301 if (got_fileindex_entry_has_commit(ie) &&
1302 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1303 SHA1_DIGEST_LENGTH) == 0) {
1304 err = sync_timestamps(ondisk_path, status, ie, &sb);
1307 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1311 if (got_fileindex_entry_has_blob(ie) &&
1312 memcmp(ie->blob_sha1, te->id.sha1,
1313 SHA1_DIGEST_LENGTH) == 0) {
1314 err = sync_timestamps(ondisk_path, status, ie, &sb);
1319 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1323 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1324 int update_timestamps;
1325 struct got_blob_object *blob2 = NULL;
1326 char *label_orig = NULL;
1327 if (got_fileindex_entry_has_blob(ie)) {
1328 struct got_object_id id2;
1329 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1330 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1334 if (got_fileindex_entry_has_commit(ie)) {
1335 char id_str[SHA1_DIGEST_STRING_LENGTH];
1336 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1337 sizeof(id_str)) == NULL) {
1338 err = got_error_path(id_str,
1339 GOT_ERR_BAD_OBJ_ID_STR);
1342 if (asprintf(&label_orig, "%s: commit %s",
1343 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1344 err = got_error_from_errno("asprintf");
1348 err = merge_blob(&update_timestamps, worktree, blob2,
1349 ondisk_path, path, sb.st_mode, label_orig, blob,
1350 worktree->base_commit_id, repo,
1351 progress_cb, progress_arg);
1354 got_object_blob_close(blob2);
1358 * Do not update timestamps of files with local changes.
1359 * Otherwise, a future status walk would treat them as
1360 * unmodified files again.
1362 err = got_fileindex_entry_update(ie, ondisk_path,
1363 blob->id.sha1, worktree->base_commit_id->sha1,
1365 } else if (status == GOT_STATUS_MODE_CHANGE) {
1366 err = got_fileindex_entry_update(ie, ondisk_path,
1367 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1368 } else if (status == GOT_STATUS_DELETE) {
1369 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1372 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1373 ondisk_path, path, blob, 0);
1377 err = install_blob(worktree, ondisk_path, path, te->mode,
1378 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1379 repo, progress_cb, progress_arg);
1382 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1383 ondisk_path, path, blob, 1);
1387 got_object_blob_close(blob);
1393 static const struct got_error *
1394 remove_ondisk_file(const char *root_path, const char *path)
1396 const struct got_error *err = NULL;
1397 char *ondisk_path = NULL;
1399 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1400 return got_error_from_errno("asprintf");
1402 if (unlink(ondisk_path) == -1) {
1403 if (errno != ENOENT)
1404 err = got_error_from_errno2("unlink", ondisk_path);
1406 char *parent = dirname(ondisk_path);
1407 while (parent && strcmp(parent, root_path) != 0) {
1408 if (rmdir(parent) == -1) {
1409 if (errno != ENOTEMPTY)
1410 err = got_error_from_errno2("rmdir",
1414 parent = dirname(parent);
1421 static const struct got_error *
1422 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1423 struct got_fileindex_entry *ie, struct got_repository *repo,
1424 got_worktree_checkout_cb progress_cb, void *progress_arg)
1426 const struct got_error *err = NULL;
1427 unsigned char status;
1431 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1432 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1434 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1436 return got_error_from_errno("asprintf");
1438 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1442 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1443 status == GOT_STATUS_ADD) {
1444 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1448 * Preserve the working file and change the deleted blob's
1449 * entry into a schedule-add entry.
1451 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1459 if (status == GOT_STATUS_NO_CHANGE) {
1460 err = remove_ondisk_file(worktree->root_path, ie->path);
1464 got_fileindex_entry_remove(fileindex, ie);
1470 struct diff_cb_arg {
1471 struct got_fileindex *fileindex;
1472 struct got_worktree *worktree;
1473 struct got_repository *repo;
1474 got_worktree_checkout_cb progress_cb;
1476 got_cancel_cb cancel_cb;
1480 static const struct got_error *
1481 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1482 struct got_tree_entry *te, const char *parent_path)
1484 struct diff_cb_arg *a = arg;
1486 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1487 return got_error(GOT_ERR_CANCELLED);
1489 return update_blob(a->worktree, a->fileindex, ie, te,
1490 ie->path, a->repo, a->progress_cb, a->progress_arg);
1493 static const struct got_error *
1494 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1496 struct diff_cb_arg *a = arg;
1498 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1499 return got_error(GOT_ERR_CANCELLED);
1501 return delete_blob(a->worktree, a->fileindex, ie,
1502 a->repo, a->progress_cb, a->progress_arg);
1505 static const struct got_error *
1506 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1508 struct diff_cb_arg *a = arg;
1509 const struct got_error *err;
1512 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1513 return got_error(GOT_ERR_CANCELLED);
1515 if (got_object_tree_entry_is_submodule(te))
1518 if (asprintf(&path, "%s%s%s", parent_path,
1519 parent_path[0] ? "/" : "", te->name)
1521 return got_error_from_errno("asprintf");
1523 if (S_ISDIR(te->mode))
1524 err = add_dir_on_disk(a->worktree, path);
1526 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1527 a->repo, a->progress_cb, a->progress_arg);
1533 static const struct got_error *
1534 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1536 const struct got_error *err = NULL;
1537 char *uuidstr = NULL;
1538 uint32_t uuid_status;
1542 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1543 if (uuid_status != uuid_s_ok)
1544 return got_error_uuid(uuid_status, "uuid_to_string");
1546 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1548 err = got_error_from_errno("asprintf");
1555 const struct got_error *
1556 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1558 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1561 static const struct got_error *
1562 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1564 return get_ref_name(refname, worktree,
1565 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1568 static const struct got_error *
1569 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1571 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1574 static const struct got_error *
1575 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1577 return get_ref_name(refname, worktree,
1578 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1581 static const struct got_error *
1582 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1584 return get_ref_name(refname, worktree,
1585 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1588 static const struct got_error *
1589 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1591 return get_ref_name(refname, worktree,
1592 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1595 static const struct got_error *
1596 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1598 return get_ref_name(refname, worktree,
1599 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1602 static const struct got_error *
1603 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1605 return get_ref_name(refname, worktree,
1606 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1609 static const struct got_error *
1610 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1612 return get_ref_name(refname, worktree,
1613 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1616 const struct got_error *
1617 got_worktree_get_histedit_script_path(char **path,
1618 struct got_worktree *worktree)
1620 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1621 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1623 return got_error_from_errno("asprintf");
1629 * Prevent Git's garbage collector from deleting our base commit by
1630 * setting a reference to our base commit's ID.
1632 static const struct got_error *
1633 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1635 const struct got_error *err = NULL;
1636 struct got_reference *ref = NULL;
1639 err = got_worktree_get_base_ref_name(&refname, worktree);
1643 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1647 err = got_ref_write(ref, repo);
1655 static const struct got_error *
1656 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1658 const struct got_error *err = NULL;
1660 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1661 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1662 err = got_error_from_errno("asprintf");
1663 *fileindex_path = NULL;
1669 static const struct got_error *
1670 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1671 struct got_worktree *worktree)
1673 const struct got_error *err = NULL;
1676 *fileindex_path = NULL;
1677 *fileindex = got_fileindex_alloc();
1678 if (*fileindex == NULL)
1679 return got_error_from_errno("got_fileindex_alloc");
1681 err = get_fileindex_path(fileindex_path, worktree);
1685 index = fopen(*fileindex_path, "rb");
1686 if (index == NULL) {
1687 if (errno != ENOENT)
1688 err = got_error_from_errno2("fopen", *fileindex_path);
1690 err = got_fileindex_read(*fileindex, index);
1691 if (fclose(index) != 0 && err == NULL)
1692 err = got_error_from_errno("fclose");
1696 free(*fileindex_path);
1697 *fileindex_path = NULL;
1698 got_fileindex_free(*fileindex);
1704 struct bump_base_commit_id_arg {
1705 struct got_object_id *base_commit_id;
1708 const char *entry_name;
1709 got_worktree_checkout_cb progress_cb;
1713 /* Bump base commit ID of all files within an updated part of the work tree. */
1714 static const struct got_error *
1715 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1717 const struct got_error *err;
1718 struct bump_base_commit_id_arg *a = arg;
1720 if (a->entry_name) {
1721 if (strcmp(ie->path, a->path) != 0)
1723 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1726 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1727 SHA1_DIGEST_LENGTH) == 0)
1730 if (a->progress_cb) {
1731 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1736 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1740 static const struct got_error *
1741 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1743 const struct got_error *err = NULL;
1744 char *new_fileindex_path = NULL;
1745 FILE *new_index = NULL;
1746 struct timespec timeout;
1748 err = got_opentemp_named(&new_fileindex_path, &new_index,
1753 err = got_fileindex_write(fileindex, new_index);
1757 if (rename(new_fileindex_path, fileindex_path) != 0) {
1758 err = got_error_from_errno3("rename", new_fileindex_path,
1760 unlink(new_fileindex_path);
1764 * Sleep for a short amount of time to ensure that files modified after
1765 * this program exits have a different time stamp from the one which
1766 * was recorded in the file index.
1769 timeout.tv_nsec = 1;
1770 nanosleep(&timeout, NULL);
1774 free(new_fileindex_path);
1778 static const struct got_error *
1779 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1780 struct got_object_id **tree_id, const char *wt_relpath,
1781 struct got_worktree *worktree, struct got_repository *repo)
1783 const struct got_error *err = NULL;
1784 struct got_object_id *id = NULL;
1785 char *in_repo_path = NULL;
1786 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1788 *entry_type = GOT_OBJ_TYPE_ANY;
1789 *tree_relpath = NULL;
1792 if (wt_relpath[0] == '\0') {
1793 /* Check out all files within the work tree. */
1794 *entry_type = GOT_OBJ_TYPE_TREE;
1795 *tree_relpath = strdup("");
1796 if (*tree_relpath == NULL) {
1797 err = got_error_from_errno("strdup");
1800 err = got_object_id_by_path(tree_id, repo,
1801 worktree->base_commit_id, worktree->path_prefix);
1807 /* Check out a subset of files in the work tree. */
1809 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1810 is_root_wt ? "" : "/", wt_relpath) == -1) {
1811 err = got_error_from_errno("asprintf");
1815 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1821 in_repo_path = NULL;
1823 err = got_object_get_type(entry_type, repo, id);
1827 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1828 /* Check out a single file. */
1829 if (strchr(wt_relpath, '/') == NULL) {
1830 /* Check out a single file in work tree's root dir. */
1831 in_repo_path = strdup(worktree->path_prefix);
1832 if (in_repo_path == NULL) {
1833 err = got_error_from_errno("strdup");
1836 *tree_relpath = strdup("");
1837 if (*tree_relpath == NULL) {
1838 err = got_error_from_errno("strdup");
1842 /* Check out a single file in a subdirectory. */
1843 err = got_path_dirname(tree_relpath, wt_relpath);
1846 if (asprintf(&in_repo_path, "%s%s%s",
1847 worktree->path_prefix, is_root_wt ? "" : "/",
1848 *tree_relpath) == -1) {
1849 err = got_error_from_errno("asprintf");
1853 err = got_object_id_by_path(tree_id, repo,
1854 worktree->base_commit_id, in_repo_path);
1856 /* Check out all files within a subdirectory. */
1857 *tree_id = got_object_id_dup(id);
1858 if (*tree_id == NULL) {
1859 err = got_error_from_errno("got_object_id_dup");
1862 *tree_relpath = strdup(wt_relpath);
1863 if (*tree_relpath == NULL) {
1864 err = got_error_from_errno("strdup");
1872 *entry_type = GOT_OBJ_TYPE_ANY;
1873 free(*tree_relpath);
1874 *tree_relpath = NULL;
1881 static const struct got_error *
1882 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1883 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1884 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1885 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1887 const struct got_error *err = NULL;
1888 struct got_commit_object *commit = NULL;
1889 struct got_tree_object *tree = NULL;
1890 struct got_fileindex_diff_tree_cb diff_cb;
1891 struct diff_cb_arg arg;
1893 err = ref_base_commit(worktree, repo);
1895 if (!(err->code == GOT_ERR_ERRNO &&
1896 (errno == EACCES || errno == EROFS)))
1898 err = (*progress_cb)(progress_arg,
1899 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
1904 err = got_object_open_as_commit(&commit, repo,
1905 worktree->base_commit_id);
1909 err = got_object_open_as_tree(&tree, repo, tree_id);
1914 got_object_tree_find_entry(tree, entry_name) == NULL) {
1915 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1919 diff_cb.diff_old_new = diff_old_new;
1920 diff_cb.diff_old = diff_old;
1921 diff_cb.diff_new = diff_new;
1922 arg.fileindex = fileindex;
1923 arg.worktree = worktree;
1925 arg.progress_cb = progress_cb;
1926 arg.progress_arg = progress_arg;
1927 arg.cancel_cb = cancel_cb;
1928 arg.cancel_arg = cancel_arg;
1929 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1930 entry_name, repo, &diff_cb, &arg);
1933 got_object_tree_close(tree);
1935 got_object_commit_close(commit);
1939 const struct got_error *
1940 got_worktree_checkout_files(struct got_worktree *worktree,
1941 struct got_pathlist_head *paths, struct got_repository *repo,
1942 got_worktree_checkout_cb progress_cb, void *progress_arg,
1943 got_cancel_cb cancel_cb, void *cancel_arg)
1945 const struct got_error *err = NULL, *sync_err, *unlockerr;
1946 struct got_commit_object *commit = NULL;
1947 struct got_tree_object *tree = NULL;
1948 struct got_fileindex *fileindex = NULL;
1949 char *fileindex_path = NULL;
1950 struct got_pathlist_entry *pe;
1951 struct tree_path_data {
1952 SIMPLEQ_ENTRY(tree_path_data) entry;
1953 struct got_object_id *tree_id;
1958 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1960 SIMPLEQ_INIT(&tree_paths);
1962 err = lock_worktree(worktree, LOCK_EX);
1966 /* Map all specified paths to in-repository trees. */
1967 TAILQ_FOREACH(pe, paths, entry) {
1968 tpd = malloc(sizeof(*tpd));
1970 err = got_error_from_errno("malloc");
1974 err = find_tree_entry_for_checkout(&tpd->entry_type,
1975 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1981 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1982 err = got_path_basename(&tpd->entry_name, pe->path);
1990 tpd->entry_name = NULL;
1992 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1996 * Read the file index.
1997 * Checking out files is supposed to be an idempotent operation.
1998 * If the on-disk file index is incomplete we will try to complete it.
2000 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2004 tpd = SIMPLEQ_FIRST(&tree_paths);
2005 TAILQ_FOREACH(pe, paths, entry) {
2006 struct bump_base_commit_id_arg bbc_arg;
2008 err = checkout_files(worktree, fileindex, tpd->relpath,
2009 tpd->tree_id, tpd->entry_name, repo,
2010 progress_cb, progress_arg, cancel_cb, cancel_arg);
2014 bbc_arg.base_commit_id = worktree->base_commit_id;
2015 bbc_arg.entry_name = tpd->entry_name;
2016 bbc_arg.path = pe->path;
2017 bbc_arg.path_len = pe->path_len;
2018 bbc_arg.progress_cb = progress_cb;
2019 bbc_arg.progress_arg = progress_arg;
2020 err = got_fileindex_for_each_entry_safe(fileindex,
2021 bump_base_commit_id, &bbc_arg);
2025 tpd = SIMPLEQ_NEXT(tpd, entry);
2027 sync_err = sync_fileindex(fileindex, fileindex_path);
2028 if (sync_err && err == NULL)
2031 free(fileindex_path);
2033 got_object_tree_close(tree);
2035 got_object_commit_close(commit);
2037 got_fileindex_free(fileindex);
2038 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2039 tpd = SIMPLEQ_FIRST(&tree_paths);
2040 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2045 unlockerr = lock_worktree(worktree, LOCK_SH);
2046 if (unlockerr && err == NULL)
2051 struct merge_file_cb_arg {
2052 struct got_worktree *worktree;
2053 struct got_fileindex *fileindex;
2054 got_worktree_checkout_cb progress_cb;
2056 got_cancel_cb cancel_cb;
2058 const char *label_orig;
2059 struct got_object_id *commit_id2;
2062 static const struct got_error *
2063 merge_file_cb(void *arg, struct got_blob_object *blob1,
2064 struct got_blob_object *blob2, struct got_object_id *id1,
2065 struct got_object_id *id2, const char *path1, const char *path2,
2066 mode_t mode1, mode_t mode2, struct got_repository *repo)
2068 static const struct got_error *err = NULL;
2069 struct merge_file_cb_arg *a = arg;
2070 struct got_fileindex_entry *ie;
2071 char *ondisk_path = NULL;
2073 unsigned char status;
2074 int local_changes_subsumed;
2076 if (blob1 && blob2) {
2077 ie = got_fileindex_entry_get(a->fileindex, path2,
2080 return (*a->progress_cb)(a->progress_arg,
2081 GOT_STATUS_MISSING, path2);
2083 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2085 return got_error_from_errno("asprintf");
2087 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2092 if (status == GOT_STATUS_DELETE) {
2093 err = (*a->progress_cb)(a->progress_arg,
2094 GOT_STATUS_MERGE, path2);
2097 if (status != GOT_STATUS_NO_CHANGE &&
2098 status != GOT_STATUS_MODIFY &&
2099 status != GOT_STATUS_CONFLICT &&
2100 status != GOT_STATUS_ADD) {
2101 err = (*a->progress_cb)(a->progress_arg, status, path2);
2105 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2106 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2107 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2109 ie = got_fileindex_entry_get(a->fileindex, path1,
2112 return (*a->progress_cb)(a->progress_arg,
2113 GOT_STATUS_MISSING, path1);
2115 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2117 return got_error_from_errno("asprintf");
2119 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2125 case GOT_STATUS_NO_CHANGE:
2126 err = (*a->progress_cb)(a->progress_arg,
2127 GOT_STATUS_DELETE, path1);
2130 err = remove_ondisk_file(a->worktree->root_path, path1);
2134 got_fileindex_entry_mark_deleted_from_disk(ie);
2136 case GOT_STATUS_DELETE:
2137 case GOT_STATUS_MISSING:
2138 err = (*a->progress_cb)(a->progress_arg,
2139 GOT_STATUS_DELETE, path1);
2143 got_fileindex_entry_mark_deleted_from_disk(ie);
2145 case GOT_STATUS_ADD:
2146 case GOT_STATUS_MODIFY:
2147 case GOT_STATUS_CONFLICT:
2148 err = (*a->progress_cb)(a->progress_arg,
2149 GOT_STATUS_CANNOT_DELETE, path1);
2153 case GOT_STATUS_OBSTRUCTED:
2154 err = (*a->progress_cb)(a->progress_arg, status, path1);
2162 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2164 return got_error_from_errno("asprintf");
2165 ie = got_fileindex_entry_get(a->fileindex, path2,
2168 err = get_file_status(&status, &sb, ie, ondisk_path,
2172 if (status != GOT_STATUS_NO_CHANGE &&
2173 status != GOT_STATUS_MODIFY &&
2174 status != GOT_STATUS_CONFLICT &&
2175 status != GOT_STATUS_ADD) {
2176 err = (*a->progress_cb)(a->progress_arg,
2180 err = merge_blob(&local_changes_subsumed, a->worktree,
2181 NULL, ondisk_path, path2, sb.st_mode,
2182 a->label_orig, blob2, a->commit_id2, repo,
2185 if (status == GOT_STATUS_DELETE) {
2186 err = update_blob_fileindex_entry(a->worktree,
2187 a->fileindex, ie, ondisk_path, ie->path,
2193 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2194 err = install_blob(a->worktree, ondisk_path, path2,
2195 /* XXX get this from parent tree! */
2196 GOT_DEFAULT_FILE_MODE,
2197 sb.st_mode, blob2, 0, 0, repo,
2198 a->progress_cb, a->progress_arg);
2201 err = got_fileindex_entry_alloc(&ie,
2202 ondisk_path, path2, NULL, NULL);
2205 err = got_fileindex_entry_add(a->fileindex, ie);
2207 got_fileindex_entry_free(ie);
2217 struct check_merge_ok_arg {
2218 struct got_worktree *worktree;
2219 struct got_repository *repo;
2222 static const struct got_error *
2223 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2225 const struct got_error *err = NULL;
2226 struct check_merge_ok_arg *a = arg;
2227 unsigned char status;
2231 /* Reject merges into a work tree with mixed base commits. */
2232 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2233 SHA1_DIGEST_LENGTH))
2234 return got_error(GOT_ERR_MIXED_COMMITS);
2236 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2238 return got_error_from_errno("asprintf");
2240 /* Reject merges into a work tree with conflicted files. */
2241 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2244 if (status == GOT_STATUS_CONFLICT)
2245 return got_error(GOT_ERR_CONFLICTS);
2250 static const struct got_error *
2251 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2252 const char *fileindex_path, struct got_object_id *commit_id1,
2253 struct got_object_id *commit_id2, struct got_repository *repo,
2254 got_worktree_checkout_cb progress_cb, void *progress_arg,
2255 got_cancel_cb cancel_cb, void *cancel_arg)
2257 const struct got_error *err = NULL, *sync_err;
2258 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2259 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2260 struct merge_file_cb_arg arg;
2261 char *label_orig = NULL;
2266 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2267 worktree->path_prefix);
2271 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2275 err = got_object_id_str(&id_str, commit_id1);
2279 if (asprintf(&label_orig, "%s: commit %s",
2280 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2281 err = got_error_from_errno("asprintf");
2288 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2289 worktree->path_prefix);
2293 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2297 arg.worktree = worktree;
2298 arg.fileindex = fileindex;
2299 arg.progress_cb = progress_cb;
2300 arg.progress_arg = progress_arg;
2301 arg.cancel_cb = cancel_cb;
2302 arg.cancel_arg = cancel_arg;
2303 arg.label_orig = label_orig;
2304 arg.commit_id2 = commit_id2;
2305 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2306 sync_err = sync_fileindex(fileindex, fileindex_path);
2307 if (sync_err && err == NULL)
2311 got_object_tree_close(tree1);
2313 got_object_tree_close(tree2);
2318 const struct got_error *
2319 got_worktree_merge_files(struct got_worktree *worktree,
2320 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2321 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2322 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2324 const struct got_error *err, *unlockerr;
2325 char *fileindex_path = NULL;
2326 struct got_fileindex *fileindex = NULL;
2327 struct check_merge_ok_arg mok_arg;
2329 err = lock_worktree(worktree, LOCK_EX);
2333 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2337 mok_arg.worktree = worktree;
2338 mok_arg.repo = repo;
2339 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2344 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2345 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2348 got_fileindex_free(fileindex);
2349 free(fileindex_path);
2350 unlockerr = lock_worktree(worktree, LOCK_SH);
2351 if (unlockerr && err == NULL)
2356 struct diff_dir_cb_arg {
2357 struct got_fileindex *fileindex;
2358 struct got_worktree *worktree;
2359 const char *status_path;
2360 size_t status_path_len;
2361 struct got_repository *repo;
2362 got_worktree_status_cb status_cb;
2364 got_cancel_cb cancel_cb;
2366 /* A pathlist containing per-directory pathlists of ignore patterns. */
2367 struct got_pathlist_head ignores;
2368 int report_unchanged;
2371 static const struct got_error *
2372 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2373 int dirfd, const char *de_name,
2374 got_worktree_status_cb status_cb, void *status_arg,
2375 struct got_repository *repo, int report_unchanged)
2377 const struct got_error *err = NULL;
2378 unsigned char status = GOT_STATUS_NO_CHANGE;
2379 unsigned char staged_status = get_staged_status(ie);
2381 struct got_object_id blob_id, commit_id, staged_blob_id;
2382 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2383 struct got_object_id *staged_blob_idp = NULL;
2385 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2389 if (status == GOT_STATUS_NO_CHANGE &&
2390 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2393 if (got_fileindex_entry_has_blob(ie)) {
2394 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2395 blob_idp = &blob_id;
2397 if (got_fileindex_entry_has_commit(ie)) {
2398 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2399 commit_idp = &commit_id;
2401 if (staged_status == GOT_STATUS_ADD ||
2402 staged_status == GOT_STATUS_MODIFY) {
2403 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2404 SHA1_DIGEST_LENGTH);
2405 staged_blob_idp = &staged_blob_id;
2408 return (*status_cb)(status_arg, status, staged_status,
2409 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2412 static const struct got_error *
2413 status_old_new(void *arg, struct got_fileindex_entry *ie,
2414 struct dirent *de, const char *parent_path, int dirfd)
2416 const struct got_error *err = NULL;
2417 struct diff_dir_cb_arg *a = arg;
2420 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2421 return got_error(GOT_ERR_CANCELLED);
2423 if (got_path_cmp(parent_path, a->status_path,
2424 strlen(parent_path), a->status_path_len) != 0 &&
2425 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2428 if (parent_path[0]) {
2429 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2430 parent_path, de->d_name) == -1)
2431 return got_error_from_errno("asprintf");
2433 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2435 return got_error_from_errno("asprintf");
2438 err = report_file_status(ie, abspath, dirfd, de->d_name,
2439 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2444 static const struct got_error *
2445 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2447 struct diff_dir_cb_arg *a = arg;
2448 struct got_object_id blob_id, commit_id;
2449 unsigned char status;
2451 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2452 return got_error(GOT_ERR_CANCELLED);
2454 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2457 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2458 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2459 if (got_fileindex_entry_has_file_on_disk(ie))
2460 status = GOT_STATUS_MISSING;
2462 status = GOT_STATUS_DELETE;
2463 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2464 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2468 free_ignorelist(struct got_pathlist_head *ignorelist)
2470 struct got_pathlist_entry *pe;
2472 TAILQ_FOREACH(pe, ignorelist, entry)
2473 free((char *)pe->path);
2474 got_pathlist_free(ignorelist);
2478 free_ignores(struct got_pathlist_head *ignores)
2480 struct got_pathlist_entry *pe;
2482 TAILQ_FOREACH(pe, ignores, entry) {
2483 struct got_pathlist_head *ignorelist = pe->data;
2484 free_ignorelist(ignorelist);
2485 free((char *)pe->path);
2487 got_pathlist_free(ignores);
2490 static const struct got_error *
2491 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2493 const struct got_error *err = NULL;
2494 struct got_pathlist_entry *pe = NULL;
2495 struct got_pathlist_head *ignorelist;
2496 char *line = NULL, *pattern, *dirpath = NULL;
2497 size_t linesize = 0;
2500 ignorelist = calloc(1, sizeof(*ignorelist));
2501 if (ignorelist == NULL)
2502 return got_error_from_errno("calloc");
2503 TAILQ_INIT(ignorelist);
2505 while ((linelen = getline(&line, &linesize, f)) != -1) {
2506 if (linelen > 0 && line[linelen - 1] == '\n')
2507 line[linelen - 1] = '\0';
2509 /* Git's ignores may contain comments. */
2513 /* Git's negated patterns are not (yet?) supported. */
2517 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2519 err = got_error_from_errno("asprintf");
2522 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2527 err = got_error_from_errno("getline");
2531 dirpath = strdup(path);
2532 if (dirpath == NULL) {
2533 err = got_error_from_errno("strdup");
2536 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2539 if (err || pe == NULL) {
2541 free_ignorelist(ignorelist);
2547 match_ignores(struct got_pathlist_head *ignores, const char *path)
2549 struct got_pathlist_entry *pe;
2551 /* Handle patterns which match in all directories. */
2552 TAILQ_FOREACH(pe, ignores, entry) {
2553 struct got_pathlist_head *ignorelist = pe->data;
2554 struct got_pathlist_entry *pi;
2556 TAILQ_FOREACH(pi, ignorelist, entry) {
2557 const char *p, *pattern = pi->path;
2559 if (strncmp(pattern, "**/", 3) != 0)
2564 if (fnmatch(pattern, p,
2565 FNM_PATHNAME | FNM_LEADING_DIR)) {
2566 /* Retry in next directory. */
2567 while (*p && *p != '/')
2579 * The ignores pathlist contains ignore lists from children before
2580 * parents, so we can find the most specific ignorelist by walking
2581 * ignores backwards.
2583 pe = TAILQ_LAST(ignores, got_pathlist_head);
2585 if (got_path_is_child(path, pe->path, pe->path_len)) {
2586 struct got_pathlist_head *ignorelist = pe->data;
2587 struct got_pathlist_entry *pi;
2588 TAILQ_FOREACH(pi, ignorelist, entry) {
2589 const char *pattern = pi->path;
2590 int flags = FNM_LEADING_DIR;
2591 if (strstr(pattern, "/**/") == NULL)
2592 flags |= FNM_PATHNAME;
2593 if (fnmatch(pattern, path, flags))
2598 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2604 static const struct got_error *
2605 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2606 const char *path, int dirfd, const char *ignores_filename)
2608 const struct got_error *err = NULL;
2611 FILE *ignoresfile = NULL;
2613 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2614 path[0] ? "/" : "", ignores_filename) == -1)
2615 return got_error_from_errno("asprintf");
2618 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2620 if (errno != ENOENT && errno != EACCES)
2621 err = got_error_from_errno2("openat",
2624 ignoresfile = fdopen(fd, "r");
2625 if (ignoresfile == NULL)
2626 err = got_error_from_errno2("fdopen",
2630 err = read_ignores(ignores, path, ignoresfile);
2634 ignoresfile = fopen(ignorespath, "r");
2635 if (ignoresfile == NULL) {
2636 if (errno != ENOENT && errno != EACCES)
2637 err = got_error_from_errno2("fopen",
2640 err = read_ignores(ignores, path, ignoresfile);
2643 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2644 err = got_error_from_errno2("fclose", path);
2645 if (fd != -1 && close(fd) == -1 && err == NULL)
2646 err = got_error_from_errno2("close", path);
2651 static const struct got_error *
2652 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2654 const struct got_error *err = NULL;
2655 struct diff_dir_cb_arg *a = arg;
2658 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2659 return got_error(GOT_ERR_CANCELLED);
2661 /* XXX ignore symlinks for now */
2662 if (de->d_type == DT_LNK)
2665 if (parent_path[0]) {
2666 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2667 return got_error_from_errno("asprintf");
2672 if (de->d_type == DT_DIR) {
2673 int subdirfd = openat(dirfd, de->d_name,
2674 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2675 if (subdirfd == -1) {
2676 if (errno != ENOENT && errno != EACCES)
2677 err = got_error_from_errno2("openat", path);
2679 err = add_ignores(&a->ignores, a->worktree->root_path,
2680 path, subdirfd, ".cvsignore");
2682 err = add_ignores(&a->ignores,
2683 a->worktree->root_path, path,
2684 subdirfd, ".gitignore");
2685 if (close(subdirfd) == -1 && err == NULL)
2686 err = got_error_from_errno2("close", path);
2688 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2689 && !match_ignores(&a->ignores, path))
2690 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2691 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2697 static const struct got_error *
2698 report_single_file_status(const char *path, const char *ondisk_path,
2699 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2700 void *status_arg, struct got_repository *repo, int report_unchanged)
2702 struct got_fileindex_entry *ie;
2705 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2707 return report_file_status(ie, ondisk_path, -1, NULL,
2708 status_cb, status_arg, repo, report_unchanged);
2710 if (lstat(ondisk_path, &sb) == -1) {
2711 if (errno != ENOENT)
2712 return got_error_from_errno2("lstat", ondisk_path);
2713 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2714 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2718 if (S_ISREG(sb.st_mode))
2719 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2720 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2725 static const struct got_error *
2726 worktree_status(struct got_worktree *worktree, const char *path,
2727 struct got_fileindex *fileindex, struct got_repository *repo,
2728 got_worktree_status_cb status_cb, void *status_arg,
2729 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2730 int report_unchanged)
2732 const struct got_error *err = NULL;
2734 struct got_fileindex_diff_dir_cb fdiff_cb;
2735 struct diff_dir_cb_arg arg;
2736 char *ondisk_path = NULL;
2738 if (asprintf(&ondisk_path, "%s%s%s",
2739 worktree->root_path, path[0] ? "/" : "", path) == -1)
2740 return got_error_from_errno("asprintf");
2742 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2744 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2745 err = got_error_from_errno2("open", ondisk_path);
2747 err = report_single_file_status(path, ondisk_path,
2748 fileindex, status_cb, status_arg, repo,
2751 fdiff_cb.diff_old_new = status_old_new;
2752 fdiff_cb.diff_old = status_old;
2753 fdiff_cb.diff_new = status_new;
2754 arg.fileindex = fileindex;
2755 arg.worktree = worktree;
2756 arg.status_path = path;
2757 arg.status_path_len = strlen(path);
2759 arg.status_cb = status_cb;
2760 arg.status_arg = status_arg;
2761 arg.cancel_cb = cancel_cb;
2762 arg.cancel_arg = cancel_arg;
2763 arg.report_unchanged = report_unchanged;
2764 TAILQ_INIT(&arg.ignores);
2766 err = add_ignores(&arg.ignores, worktree->root_path,
2767 path, fd, ".cvsignore");
2769 err = add_ignores(&arg.ignores,
2770 worktree->root_path, path, fd,
2774 err = got_fileindex_diff_dir(fileindex, fd,
2775 worktree->root_path, path, repo, &fdiff_cb, &arg);
2776 free_ignores(&arg.ignores);
2779 if (fd != -1 && close(fd) != 0 && err == NULL)
2780 err = got_error_from_errno("close");
2785 const struct got_error *
2786 got_worktree_status(struct got_worktree *worktree,
2787 struct got_pathlist_head *paths, struct got_repository *repo,
2788 got_worktree_status_cb status_cb, void *status_arg,
2789 got_cancel_cb cancel_cb, void *cancel_arg)
2791 const struct got_error *err = NULL;
2792 char *fileindex_path = NULL;
2793 struct got_fileindex *fileindex = NULL;
2794 struct got_pathlist_entry *pe;
2796 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2800 TAILQ_FOREACH(pe, paths, entry) {
2801 err = worktree_status(worktree, pe->path, fileindex, repo,
2802 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2806 free(fileindex_path);
2807 got_fileindex_free(fileindex);
2811 const struct got_error *
2812 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2815 const struct got_error *err = NULL;
2816 char *resolved, *cwd = NULL, *path = NULL;
2821 resolved = realpath(arg, NULL);
2822 if (resolved == NULL) {
2823 if (errno != ENOENT)
2824 return got_error_from_errno2("realpath", arg);
2825 cwd = getcwd(NULL, 0);
2827 return got_error_from_errno("getcwd");
2828 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2829 err = got_error_from_errno("asprintf");
2834 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2835 strlen(got_worktree_get_root_path(worktree)))) {
2836 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
2840 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2841 err = got_path_skip_common_ancestor(&path,
2842 got_worktree_get_root_path(worktree), resolved);
2848 err = got_error_from_errno("strdup");
2853 /* XXX status walk can't deal with trailing slash! */
2855 while (len > 0 && path[len - 1] == '/') {
2856 path[len - 1] = '\0';
2869 struct schedule_addition_args {
2870 struct got_worktree *worktree;
2871 struct got_fileindex *fileindex;
2872 got_worktree_checkout_cb progress_cb;
2874 struct got_repository *repo;
2877 static const struct got_error *
2878 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2879 const char *relpath, struct got_object_id *blob_id,
2880 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2881 int dirfd, const char *de_name)
2883 struct schedule_addition_args *a = arg;
2884 const struct got_error *err = NULL;
2885 struct got_fileindex_entry *ie;
2889 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2891 return got_error_from_errno("asprintf");
2893 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2895 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2899 /* Re-adding an existing entry is a no-op. */
2900 if (status == GOT_STATUS_ADD)
2902 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2907 if (status != GOT_STATUS_UNVERSIONED) {
2908 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2912 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2916 err = got_fileindex_entry_add(a->fileindex, ie);
2918 got_fileindex_entry_free(ie);
2925 if (status == GOT_STATUS_ADD)
2927 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2930 const struct got_error *
2931 got_worktree_schedule_add(struct got_worktree *worktree,
2932 struct got_pathlist_head *paths,
2933 got_worktree_checkout_cb progress_cb, void *progress_arg,
2934 struct got_repository *repo, int no_ignores)
2936 struct got_fileindex *fileindex = NULL;
2937 char *fileindex_path = NULL;
2938 const struct got_error *err = NULL, *sync_err, *unlockerr;
2939 struct got_pathlist_entry *pe;
2940 struct schedule_addition_args saa;
2942 err = lock_worktree(worktree, LOCK_EX);
2946 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2950 saa.worktree = worktree;
2951 saa.fileindex = fileindex;
2952 saa.progress_cb = progress_cb;
2953 saa.progress_arg = progress_arg;
2956 TAILQ_FOREACH(pe, paths, entry) {
2957 err = worktree_status(worktree, pe->path, fileindex, repo,
2958 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2962 sync_err = sync_fileindex(fileindex, fileindex_path);
2963 if (sync_err && err == NULL)
2966 free(fileindex_path);
2968 got_fileindex_free(fileindex);
2969 unlockerr = lock_worktree(worktree, LOCK_SH);
2970 if (unlockerr && err == NULL)
2975 struct schedule_deletion_args {
2976 struct got_worktree *worktree;
2977 struct got_fileindex *fileindex;
2978 got_worktree_delete_cb progress_cb;
2980 struct got_repository *repo;
2981 int delete_local_mods;
2985 static const struct got_error *
2986 schedule_for_deletion(void *arg, unsigned char status,
2987 unsigned char staged_status, const char *relpath,
2988 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2989 struct got_object_id *commit_id, int dirfd, const char *de_name)
2991 struct schedule_deletion_args *a = arg;
2992 const struct got_error *err = NULL;
2993 struct got_fileindex_entry *ie = NULL;
2995 char *ondisk_path, *parent = NULL;
2997 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2999 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3001 staged_status = get_staged_status(ie);
3002 if (staged_status != GOT_STATUS_NO_CHANGE) {
3003 if (staged_status == GOT_STATUS_DELETE)
3005 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3008 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3010 return got_error_from_errno("asprintf");
3012 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3017 if (status != GOT_STATUS_NO_CHANGE) {
3018 if (status == GOT_STATUS_DELETE)
3020 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3021 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3024 if (status != GOT_STATUS_MODIFY &&
3025 status != GOT_STATUS_MISSING) {
3026 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3031 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3033 if (unlinkat(dirfd, de_name, 0) != 0) {
3034 err = got_error_from_errno2("unlinkat",
3038 } else if (unlink(ondisk_path) != 0) {
3039 err = got_error_from_errno2("unlink", ondisk_path);
3043 parent = dirname(ondisk_path);
3045 if (parent == NULL) {
3046 err = got_error_from_errno2("dirname", ondisk_path);
3049 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3050 if (rmdir(parent) == -1) {
3051 if (errno != ENOTEMPTY)
3052 err = got_error_from_errno2("rmdir",
3056 parent = dirname(parent);
3057 if (parent == NULL) {
3058 err = got_error_from_errno2("dirname", parent);
3064 got_fileindex_entry_mark_deleted_from_disk(ie);
3069 if (status == GOT_STATUS_DELETE)
3071 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3072 staged_status, relpath);
3075 const struct got_error *
3076 got_worktree_schedule_delete(struct got_worktree *worktree,
3077 struct got_pathlist_head *paths, int delete_local_mods,
3078 got_worktree_delete_cb progress_cb, void *progress_arg,
3079 struct got_repository *repo, int keep_on_disk)
3081 struct got_fileindex *fileindex = NULL;
3082 char *fileindex_path = NULL;
3083 const struct got_error *err = NULL, *sync_err, *unlockerr;
3084 struct got_pathlist_entry *pe;
3085 struct schedule_deletion_args sda;
3087 err = lock_worktree(worktree, LOCK_EX);
3091 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3095 sda.worktree = worktree;
3096 sda.fileindex = fileindex;
3097 sda.progress_cb = progress_cb;
3098 sda.progress_arg = progress_arg;
3100 sda.delete_local_mods = delete_local_mods;
3101 sda.keep_on_disk = keep_on_disk;
3103 TAILQ_FOREACH(pe, paths, entry) {
3104 err = worktree_status(worktree, pe->path, fileindex, repo,
3105 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3109 sync_err = sync_fileindex(fileindex, fileindex_path);
3110 if (sync_err && err == NULL)
3113 free(fileindex_path);
3115 got_fileindex_free(fileindex);
3116 unlockerr = lock_worktree(worktree, LOCK_SH);
3117 if (unlockerr && err == NULL)
3122 static const struct got_error *
3123 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3125 const struct got_error *err = NULL;
3127 size_t linesize = 0, n;
3130 linelen = getline(&line, &linesize, infile);
3131 if (linelen == -1) {
3132 if (ferror(infile)) {
3133 err = got_error_from_errno("getline");
3139 n = fwrite(line, 1, linelen, outfile);
3141 err = got_ferror(outfile, GOT_ERR_IO);
3146 n = fwrite(line, 1, linelen, rejectfile);
3148 err = got_ferror(outfile, GOT_ERR_IO);
3155 static const struct got_error *
3156 skip_one_line(FILE *f)
3159 size_t linesize = 0;
3162 linelen = getline(&line, &linesize, f);
3163 if (linelen == -1) {
3165 return got_error_from_errno("getline");
3172 static const struct got_error *
3173 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3174 int start_old, int end_old, int start_new, int end_new,
3175 FILE *outfile, FILE *rejectfile)
3177 const struct got_error *err;
3179 /* Copy old file's lines leading up to patch. */
3180 while (!feof(f1) && *line_cur1 < start_old) {
3181 err = copy_one_line(f1, outfile, NULL);
3186 /* Skip new file's lines leading up to patch. */
3187 while (!feof(f2) && *line_cur2 < start_new) {
3189 err = copy_one_line(f2, NULL, rejectfile);
3191 err = skip_one_line(f2);
3196 /* Copy patched lines. */
3197 while (!feof(f2) && *line_cur2 <= end_new) {
3198 err = copy_one_line(f2, outfile, NULL);
3203 /* Skip over old file's replaced lines. */
3204 while (!feof(f1) && *line_cur1 <= end_old) {
3206 err = copy_one_line(f1, NULL, rejectfile);
3208 err = skip_one_line(f1);
3217 static const struct got_error *
3218 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3219 FILE *outfile, FILE *rejectfile)
3221 const struct got_error *err;
3224 /* Copy old file's lines until EOF. */
3226 err = copy_one_line(f1, outfile, NULL);
3233 /* Copy new file's lines until EOF. */
3235 err = copy_one_line(f2, NULL, rejectfile);
3245 static const struct got_error *
3246 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3247 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3248 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3249 int *line_cur2, FILE *outfile, FILE *rejectfile,
3250 got_worktree_patch_cb patch_cb, void *patch_arg)
3252 const struct got_error *err = NULL;
3253 int start_old = change->cv.a;
3254 int end_old = change->cv.b;
3255 int start_new = change->cv.c;
3256 int end_new = change->cv.d;
3260 *choice = GOT_PATCH_CHOICE_NONE;
3262 hunkfile = got_opentemp();
3263 if (hunkfile == NULL)
3264 return got_error_from_errno("got_opentemp");
3269 /* XXX TODO needs error checking */
3270 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3272 if (fseek(f1, pos1, SEEK_SET) == -1) {
3273 err = got_ferror(f1, GOT_ERR_IO);
3276 if (fseek(f2, pos2, SEEK_SET) == -1) {
3277 err = got_ferror(f1, GOT_ERR_IO);
3280 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3281 err = got_ferror(hunkfile, GOT_ERR_IO);
3285 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3286 hunkfile, n, nchanges);
3291 case GOT_PATCH_CHOICE_YES:
3292 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3293 end_old, start_new, end_new, outfile, rejectfile);
3295 case GOT_PATCH_CHOICE_NO:
3296 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3297 end_old, start_new, end_new, rejectfile, outfile);
3299 case GOT_PATCH_CHOICE_QUIT:
3302 err = got_error(GOT_ERR_PATCH_CHOICE);
3306 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3307 err = got_error_from_errno("fclose");
3311 struct revert_file_args {
3312 struct got_worktree *worktree;
3313 struct got_fileindex *fileindex;
3314 got_worktree_checkout_cb progress_cb;
3316 got_worktree_patch_cb patch_cb;
3318 struct got_repository *repo;
3321 static const struct got_error *
3322 create_patched_content(char **path_outfile, int reverse_patch,
3323 struct got_object_id *blob_id, const char *path2,
3324 int dirfd2, const char *de_name2,
3325 const char *relpath, struct got_repository *repo,
3326 got_worktree_patch_cb patch_cb, void *patch_arg)
3328 const struct got_error *err;
3329 struct got_blob_object *blob = NULL;
3330 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3332 char *path1 = NULL, *id_str = NULL;
3333 struct stat sb1, sb2;
3334 struct got_diff_changes *changes = NULL;
3335 struct got_diff_state *ds = NULL;
3336 struct got_diff_args *args = NULL;
3337 struct got_diff_change *change;
3338 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3341 *path_outfile = NULL;
3343 err = got_object_id_str(&id_str, blob_id);
3348 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3350 err = got_error_from_errno2("openat", path2);
3354 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3356 err = got_error_from_errno2("open", path2);
3360 if (fstat(fd2, &sb2) == -1) {
3361 err = got_error_from_errno2("fstat", path2);
3365 f2 = fdopen(fd2, "r");
3367 err = got_error_from_errno2("fdopen", path2);
3372 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3376 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3380 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3384 if (stat(path1, &sb1) == -1) {
3385 err = got_error_from_errno2("stat", path1);
3389 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3390 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3394 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3398 if (fseek(f1, 0L, SEEK_SET) == -1)
3399 return got_ferror(f1, GOT_ERR_IO);
3400 if (fseek(f2, 0L, SEEK_SET) == -1)
3401 return got_ferror(f2, GOT_ERR_IO);
3402 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3404 err = apply_or_reject_change(&choice, change, ++n,
3405 changes->nchanges, ds, args, diff_flags, relpath,
3406 f1, f2, &line_cur1, &line_cur2,
3407 reverse_patch ? NULL : outfile,
3408 reverse_patch ? outfile : NULL,
3409 patch_cb, patch_arg);
3412 if (choice == GOT_PATCH_CHOICE_YES)
3414 else if (choice == GOT_PATCH_CHOICE_QUIT)
3418 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3419 reverse_patch ? NULL : outfile,
3420 reverse_patch ? outfile : NULL);
3424 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3425 err = got_error_from_errno2("chmod", path2);
3432 got_object_blob_close(blob);
3433 if (f1 && fclose(f1) == EOF && err == NULL)
3434 err = got_error_from_errno2("fclose", path1);
3435 if (f2 && fclose(f2) == EOF && err == NULL)
3436 err = got_error_from_errno2("fclose", path2);
3437 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3438 err = got_error_from_errno2("close", path2);
3439 if (outfile && fclose(outfile) == EOF && err == NULL)
3440 err = got_error_from_errno2("fclose", *path_outfile);
3441 if (path1 && unlink(path1) == -1 && err == NULL)
3442 err = got_error_from_errno2("unlink", path1);
3443 if (err || !have_content) {
3444 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3445 err = got_error_from_errno2("unlink", *path_outfile);
3446 free(*path_outfile);
3447 *path_outfile = NULL;
3451 got_diff_state_free(ds);
3455 got_diff_free_changes(changes);
3460 static const struct got_error *
3461 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3462 const char *relpath, struct got_object_id *blob_id,
3463 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3464 int dirfd, const char *de_name)
3466 struct revert_file_args *a = arg;
3467 const struct got_error *err = NULL;
3468 char *parent_path = NULL;
3469 struct got_fileindex_entry *ie;
3470 struct got_tree_object *tree = NULL;
3471 struct got_object_id *tree_id = NULL;
3472 const struct got_tree_entry *te = NULL;
3473 char *tree_path = NULL, *te_name;
3474 char *ondisk_path = NULL, *path_content = NULL;
3475 struct got_blob_object *blob = NULL;
3477 /* Reverting a staged deletion is a no-op. */
3478 if (status == GOT_STATUS_DELETE &&
3479 staged_status != GOT_STATUS_NO_CHANGE)
3482 if (status == GOT_STATUS_UNVERSIONED)
3483 return (*a->progress_cb)(a->progress_arg,
3484 GOT_STATUS_UNVERSIONED, relpath);
3486 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3488 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3490 /* Construct in-repository path of tree which contains this blob. */
3491 err = got_path_dirname(&parent_path, ie->path);
3493 if (err->code != GOT_ERR_BAD_PATH)
3495 parent_path = strdup("/");
3496 if (parent_path == NULL) {
3497 err = got_error_from_errno("strdup");
3501 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3502 tree_path = strdup(parent_path);
3503 if (tree_path == NULL) {
3504 err = got_error_from_errno("strdup");
3508 if (got_path_is_root_dir(parent_path)) {
3509 tree_path = strdup(a->worktree->path_prefix);
3510 if (tree_path == NULL) {
3511 err = got_error_from_errno("strdup");
3515 if (asprintf(&tree_path, "%s/%s",
3516 a->worktree->path_prefix, parent_path) == -1) {
3517 err = got_error_from_errno("asprintf");
3523 err = got_object_id_by_path(&tree_id, a->repo,
3524 a->worktree->base_commit_id, tree_path);
3526 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3527 (status == GOT_STATUS_ADD ||
3528 staged_status == GOT_STATUS_ADD)))
3531 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3535 te_name = basename(ie->path);
3536 if (te_name == NULL) {
3537 err = got_error_from_errno2("basename", ie->path);
3541 te = got_object_tree_find_entry(tree, te_name);
3542 if (te == NULL && status != GOT_STATUS_ADD &&
3543 staged_status != GOT_STATUS_ADD) {
3544 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3550 case GOT_STATUS_ADD:
3552 int choice = GOT_PATCH_CHOICE_NONE;
3553 err = (*a->patch_cb)(&choice, a->patch_arg,
3554 status, ie->path, NULL, 1, 1);
3557 if (choice != GOT_PATCH_CHOICE_YES)
3560 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3564 got_fileindex_entry_remove(a->fileindex, ie);
3566 case GOT_STATUS_DELETE:
3568 int choice = GOT_PATCH_CHOICE_NONE;
3569 err = (*a->patch_cb)(&choice, a->patch_arg,
3570 status, ie->path, NULL, 1, 1);
3573 if (choice != GOT_PATCH_CHOICE_YES)
3577 case GOT_STATUS_MODIFY:
3578 case GOT_STATUS_MODE_CHANGE:
3579 case GOT_STATUS_CONFLICT:
3580 case GOT_STATUS_MISSING: {
3581 struct got_object_id id;
3582 if (staged_status == GOT_STATUS_ADD ||
3583 staged_status == GOT_STATUS_MODIFY) {
3584 memcpy(id.sha1, ie->staged_blob_sha1,
3585 SHA1_DIGEST_LENGTH);
3587 memcpy(id.sha1, ie->blob_sha1,
3588 SHA1_DIGEST_LENGTH);
3589 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3593 if (asprintf(&ondisk_path, "%s/%s",
3594 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3595 err = got_error_from_errno("asprintf");
3599 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3600 status == GOT_STATUS_CONFLICT)) {
3601 err = create_patched_content(&path_content, 1, &id,
3602 ondisk_path, dirfd, de_name, ie->path, a->repo,
3603 a->patch_cb, a->patch_arg);
3604 if (err || path_content == NULL)
3606 if (rename(path_content, ondisk_path) == -1) {
3607 err = got_error_from_errno3("rename",
3608 path_content, ondisk_path);
3612 err = install_blob(a->worktree, ondisk_path, ie->path,
3613 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3614 got_fileindex_perms_to_st(ie), blob, 0, 1,
3615 a->repo, a->progress_cb, a->progress_arg);
3618 if (status == GOT_STATUS_DELETE ||
3619 status == GOT_STATUS_MODE_CHANGE) {
3620 err = update_blob_fileindex_entry(a->worktree,
3621 a->fileindex, ie, ondisk_path, ie->path,
3638 got_object_blob_close(blob);
3640 got_object_tree_close(tree);
3645 const struct got_error *
3646 got_worktree_revert(struct got_worktree *worktree,
3647 struct got_pathlist_head *paths,
3648 got_worktree_checkout_cb progress_cb, void *progress_arg,
3649 got_worktree_patch_cb patch_cb, void *patch_arg,
3650 struct got_repository *repo)
3652 struct got_fileindex *fileindex = NULL;
3653 char *fileindex_path = NULL;
3654 const struct got_error *err = NULL, *unlockerr = NULL;
3655 const struct got_error *sync_err = NULL;
3656 struct got_pathlist_entry *pe;
3657 struct revert_file_args rfa;
3659 err = lock_worktree(worktree, LOCK_EX);
3663 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3667 rfa.worktree = worktree;
3668 rfa.fileindex = fileindex;
3669 rfa.progress_cb = progress_cb;
3670 rfa.progress_arg = progress_arg;
3671 rfa.patch_cb = patch_cb;
3672 rfa.patch_arg = patch_arg;
3674 TAILQ_FOREACH(pe, paths, entry) {
3675 err = worktree_status(worktree, pe->path, fileindex, repo,
3676 revert_file, &rfa, NULL, NULL, 0, 0);
3680 sync_err = sync_fileindex(fileindex, fileindex_path);
3681 if (sync_err && err == NULL)
3684 free(fileindex_path);
3686 got_fileindex_free(fileindex);
3687 unlockerr = lock_worktree(worktree, LOCK_SH);
3688 if (unlockerr && err == NULL)
3694 free_commitable(struct got_commitable *ct)
3697 free(ct->in_repo_path);
3698 free(ct->ondisk_path);
3700 free(ct->base_blob_id);
3701 free(ct->staged_blob_id);
3702 free(ct->base_commit_id);
3706 struct collect_commitables_arg {
3707 struct got_pathlist_head *commitable_paths;
3708 struct got_repository *repo;
3709 struct got_worktree *worktree;
3710 int have_staged_files;
3713 static const struct got_error *
3714 collect_commitables(void *arg, unsigned char status,
3715 unsigned char staged_status, const char *relpath,
3716 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3717 struct got_object_id *commit_id, int dirfd, const char *de_name)
3719 struct collect_commitables_arg *a = arg;
3720 const struct got_error *err = NULL;
3721 struct got_commitable *ct = NULL;
3722 struct got_pathlist_entry *new = NULL;
3723 char *parent_path = NULL, *path = NULL;
3726 if (a->have_staged_files) {
3727 if (staged_status != GOT_STATUS_MODIFY &&
3728 staged_status != GOT_STATUS_ADD &&
3729 staged_status != GOT_STATUS_DELETE)
3732 if (status == GOT_STATUS_CONFLICT)
3733 return got_error(GOT_ERR_COMMIT_CONFLICT);
3735 if (status != GOT_STATUS_MODIFY &&
3736 status != GOT_STATUS_MODE_CHANGE &&
3737 status != GOT_STATUS_ADD &&
3738 status != GOT_STATUS_DELETE)
3742 if (asprintf(&path, "/%s", relpath) == -1) {
3743 err = got_error_from_errno("asprintf");
3746 if (strcmp(path, "/") == 0) {
3747 parent_path = strdup("");
3748 if (parent_path == NULL)
3749 return got_error_from_errno("strdup");
3751 err = got_path_dirname(&parent_path, path);
3756 ct = calloc(1, sizeof(*ct));
3758 err = got_error_from_errno("calloc");
3762 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3764 err = got_error_from_errno("asprintf");
3767 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3768 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3771 if (fstatat(dirfd, de_name, &sb,
3772 AT_SYMLINK_NOFOLLOW) == -1) {
3773 err = got_error_from_errno2("fstatat",
3777 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3778 err = got_error_from_errno2("lstat", ct->ondisk_path);
3781 ct->mode = sb.st_mode;
3784 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3785 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3787 err = got_error_from_errno("asprintf");
3791 ct->status = status;
3792 ct->staged_status = staged_status;
3793 ct->blob_id = NULL; /* will be filled in when blob gets created */
3794 if (ct->status != GOT_STATUS_ADD &&
3795 ct->staged_status != GOT_STATUS_ADD) {
3796 ct->base_blob_id = got_object_id_dup(blob_id);
3797 if (ct->base_blob_id == NULL) {
3798 err = got_error_from_errno("got_object_id_dup");
3801 ct->base_commit_id = got_object_id_dup(commit_id);
3802 if (ct->base_commit_id == NULL) {
3803 err = got_error_from_errno("got_object_id_dup");
3807 if (ct->staged_status == GOT_STATUS_ADD ||
3808 ct->staged_status == GOT_STATUS_MODIFY) {
3809 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3810 if (ct->staged_blob_id == NULL) {
3811 err = got_error_from_errno("got_object_id_dup");
3815 ct->path = strdup(path);
3816 if (ct->path == NULL) {
3817 err = got_error_from_errno("strdup");
3820 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3822 if (ct && (err || new == NULL))
3823 free_commitable(ct);
3829 static const struct got_error *write_tree(struct got_object_id **,
3830 struct got_tree_object *, const char *, struct got_pathlist_head *,
3831 got_worktree_status_cb status_cb, void *status_arg,
3832 struct got_repository *);
3834 static const struct got_error *
3835 write_subtree(struct got_object_id **new_subtree_id,
3836 struct got_tree_entry *te, const char *parent_path,
3837 struct got_pathlist_head *commitable_paths,
3838 got_worktree_status_cb status_cb, void *status_arg,
3839 struct got_repository *repo)
3841 const struct got_error *err = NULL;
3842 struct got_tree_object *subtree;
3845 if (asprintf(&subpath, "%s%s%s", parent_path,
3846 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3847 return got_error_from_errno("asprintf");
3849 err = got_object_open_as_tree(&subtree, repo, &te->id);
3853 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3854 status_cb, status_arg, repo);
3855 got_object_tree_close(subtree);
3860 static const struct got_error *
3861 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3863 const struct got_error *err = NULL;
3864 char *ct_parent_path = NULL;
3868 if (strchr(ct->in_repo_path, '/') == NULL) {
3869 *match = got_path_is_root_dir(path);
3873 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3876 *match = (strcmp(path, ct_parent_path) == 0);
3877 free(ct_parent_path);
3882 get_ct_file_mode(struct got_commitable *ct)
3884 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3887 static const struct got_error *
3888 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3889 struct got_tree_entry *te, struct got_commitable *ct)
3891 const struct got_error *err = NULL;
3895 err = got_object_tree_entry_dup(new_te, te);
3899 (*new_te)->mode = get_ct_file_mode(ct);
3901 if (ct->staged_status == GOT_STATUS_MODIFY)
3902 memcpy(&(*new_te)->id, ct->staged_blob_id,
3903 sizeof((*new_te)->id));
3905 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3907 if (err && *new_te) {
3914 static const struct got_error *
3915 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3916 struct got_commitable *ct)
3918 const struct got_error *err = NULL;
3923 *new_te = calloc(1, sizeof(**new_te));
3924 if (*new_te == NULL)
3925 return got_error_from_errno("calloc");
3927 ct_name = basename(ct->path);
3928 if (ct_name == NULL) {
3929 err = got_error_from_errno2("basename", ct->path);
3932 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3933 sizeof((*new_te)->name)) {
3934 err = got_error(GOT_ERR_NO_SPACE);
3938 (*new_te)->mode = get_ct_file_mode(ct);
3940 if (ct->staged_status == GOT_STATUS_ADD)
3941 memcpy(&(*new_te)->id, ct->staged_blob_id,
3942 sizeof((*new_te)->id));
3944 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3946 if (err && *new_te) {
3953 static const struct got_error *
3954 insert_tree_entry(struct got_tree_entry *new_te,
3955 struct got_pathlist_head *paths)
3957 const struct got_error *err = NULL;
3958 struct got_pathlist_entry *new_pe;
3960 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3964 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3968 static const struct got_error *
3969 report_ct_status(struct got_commitable *ct,
3970 got_worktree_status_cb status_cb, void *status_arg)
3972 const char *ct_path = ct->path;
3973 unsigned char status;
3975 while (ct_path[0] == '/')
3978 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3979 status = ct->staged_status;
3981 status = ct->status;
3983 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3984 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3987 static const struct got_error *
3988 match_modified_subtree(int *modified, struct got_tree_entry *te,
3989 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3991 const struct got_error *err = NULL;
3992 struct got_pathlist_entry *pe;
3997 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3998 got_path_is_root_dir(base_tree_path) ? "" : "/",
4000 return got_error_from_errno("asprintf");
4002 TAILQ_FOREACH(pe, commitable_paths, entry) {
4003 struct got_commitable *ct = pe->data;
4004 *modified = got_path_is_child(ct->in_repo_path, te_path,
4014 static const struct got_error *
4015 match_deleted_or_modified_ct(struct got_commitable **ctp,
4016 struct got_tree_entry *te, const char *base_tree_path,
4017 struct got_pathlist_head *commitable_paths)
4019 const struct got_error *err = NULL;
4020 struct got_pathlist_entry *pe;
4024 TAILQ_FOREACH(pe, commitable_paths, entry) {
4025 struct got_commitable *ct = pe->data;
4026 char *ct_name = NULL;
4029 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4030 if (ct->status != GOT_STATUS_MODIFY &&
4031 ct->status != GOT_STATUS_MODE_CHANGE &&
4032 ct->status != GOT_STATUS_DELETE)
4035 if (ct->staged_status != GOT_STATUS_MODIFY &&
4036 ct->staged_status != GOT_STATUS_DELETE)
4040 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4043 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4049 ct_name = basename(pe->path);
4050 if (ct_name == NULL)
4051 return got_error_from_errno2("basename", pe->path);
4053 if (strcmp(te->name, ct_name) != 0)
4063 static const struct got_error *
4064 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4065 const char *child_path, const char *path_base_tree,
4066 struct got_pathlist_head *commitable_paths,
4067 got_worktree_status_cb status_cb, void *status_arg,
4068 struct got_repository *repo)
4070 const struct got_error *err = NULL;
4071 struct got_tree_entry *new_te;
4073 struct got_object_id *id = NULL;
4077 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4078 got_path_is_root_dir(path_base_tree) ? "" : "/",
4080 return got_error_from_errno("asprintf");
4082 new_te = calloc(1, sizeof(*new_te));
4084 return got_error_from_errno("calloc");
4085 new_te->mode = S_IFDIR;
4087 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4088 sizeof(new_te->name)) {
4089 err = got_error(GOT_ERR_NO_SPACE);
4092 err = write_tree(&id, NULL, subtree_path,
4093 commitable_paths, status_cb, status_arg, repo);
4098 memcpy(&new_te->id, id, sizeof(new_te->id));
4107 static const struct got_error *
4108 write_tree(struct got_object_id **new_tree_id,
4109 struct got_tree_object *base_tree, const char *path_base_tree,
4110 struct got_pathlist_head *commitable_paths,
4111 got_worktree_status_cb status_cb, void *status_arg,
4112 struct got_repository *repo)
4114 const struct got_error *err = NULL;
4115 struct got_pathlist_head paths;
4116 struct got_tree_entry *te, *new_te = NULL;
4117 struct got_pathlist_entry *pe;
4122 /* Insert, and recurse into, newly added entries first. */
4123 TAILQ_FOREACH(pe, commitable_paths, entry) {
4124 struct got_commitable *ct = pe->data;
4125 char *child_path = NULL, *slash;
4127 if ((ct->status != GOT_STATUS_ADD &&
4128 ct->staged_status != GOT_STATUS_ADD) ||
4129 (ct->flags & GOT_COMMITABLE_ADDED))
4132 if (!got_path_is_child(pe->path, path_base_tree,
4133 strlen(path_base_tree)))
4136 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4141 slash = strchr(child_path, '/');
4142 if (slash == NULL) {
4143 err = alloc_added_blob_tree_entry(&new_te, ct);
4146 err = report_ct_status(ct, status_cb, status_arg);
4149 ct->flags |= GOT_COMMITABLE_ADDED;
4150 err = insert_tree_entry(new_te, &paths);
4155 *slash = '\0'; /* trim trailing path components */
4156 if (base_tree == NULL ||
4157 got_object_tree_find_entry(base_tree, child_path)
4159 err = make_subtree_for_added_blob(&new_te,
4160 child_path, path_base_tree,
4161 commitable_paths, status_cb, status_arg,
4165 err = insert_tree_entry(new_te, &paths);
4174 int i, nbase_entries;
4175 /* Handle modified and deleted entries. */
4176 nbase_entries = got_object_tree_get_nentries(base_tree);
4177 for (i = 0; i < nbase_entries; i++) {
4178 struct got_commitable *ct = NULL;
4180 te = got_object_tree_get_entry(base_tree, i);
4181 if (got_object_tree_entry_is_submodule(te)) {
4182 /* Entry is a submodule; just copy it. */
4183 err = got_object_tree_entry_dup(&new_te, te);
4186 err = insert_tree_entry(new_te, &paths);
4193 if (S_ISDIR(te->mode)) {
4195 err = got_object_tree_entry_dup(&new_te, te);
4198 err = match_modified_subtree(&modified, te,
4199 path_base_tree, commitable_paths);
4202 /* Avoid recursion into unmodified subtrees. */
4204 struct got_object_id *new_id;
4205 err = write_subtree(&new_id, te,
4206 path_base_tree, commitable_paths,
4207 status_cb, status_arg, repo);
4210 memcpy(&new_te->id, new_id,
4211 sizeof(new_te->id));
4214 err = insert_tree_entry(new_te, &paths);
4221 err = match_deleted_or_modified_ct(&ct, te,
4222 path_base_tree, commitable_paths);
4226 /* NB: Deleted entries get dropped here. */
4227 if (ct->status == GOT_STATUS_MODIFY ||
4228 ct->status == GOT_STATUS_MODE_CHANGE ||
4229 ct->staged_status == GOT_STATUS_MODIFY) {
4230 err = alloc_modified_blob_tree_entry(
4234 err = insert_tree_entry(new_te, &paths);
4239 err = report_ct_status(ct, status_cb,
4244 /* Entry is unchanged; just copy it. */
4245 err = got_object_tree_entry_dup(&new_te, te);
4248 err = insert_tree_entry(new_te, &paths);
4256 /* Write new list of entries; deleted entries have been dropped. */
4257 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4259 got_pathlist_free(&paths);
4263 static const struct got_error *
4264 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4265 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4266 int have_staged_files)
4268 const struct got_error *err = NULL;
4269 struct got_pathlist_entry *pe;
4271 TAILQ_FOREACH(pe, commitable_paths, entry) {
4272 struct got_fileindex_entry *ie;
4273 struct got_commitable *ct = pe->data;
4275 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4277 if (ct->status == GOT_STATUS_DELETE ||
4278 ct->staged_status == GOT_STATUS_DELETE) {
4279 got_fileindex_entry_remove(fileindex, ie);
4280 } else if (ct->staged_status == GOT_STATUS_ADD ||
4281 ct->staged_status == GOT_STATUS_MODIFY) {
4282 got_fileindex_entry_stage_set(ie,
4283 GOT_FILEIDX_STAGE_NONE);
4284 err = got_fileindex_entry_update(ie,
4285 ct->ondisk_path, ct->staged_blob_id->sha1,
4286 new_base_commit_id->sha1,
4287 !have_staged_files);
4289 err = got_fileindex_entry_update(ie,
4290 ct->ondisk_path, ct->blob_id->sha1,
4291 new_base_commit_id->sha1,
4292 !have_staged_files);
4294 err = got_fileindex_entry_alloc(&ie,
4295 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4296 new_base_commit_id->sha1);
4299 err = got_fileindex_entry_add(fileindex, ie);
4308 static const struct got_error *
4309 check_out_of_date(const char *in_repo_path, unsigned char status,
4310 unsigned char staged_status, struct got_object_id *base_blob_id,
4311 struct got_object_id *base_commit_id,
4312 struct got_object_id *head_commit_id, struct got_repository *repo,
4315 const struct got_error *err = NULL;
4316 struct got_object_id *id = NULL;
4318 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4319 /* Trivial case: base commit == head commit */
4320 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4323 * Ensure file content which local changes were based
4324 * on matches file content in the branch head.
4326 err = got_object_id_by_path(&id, repo, head_commit_id,
4329 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4330 err = got_error(ood_errcode);
4332 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4333 err = got_error(ood_errcode);
4335 /* Require that added files don't exist in the branch head. */
4336 err = got_object_id_by_path(&id, repo, head_commit_id,
4338 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4340 err = id ? got_error(ood_errcode) : NULL;
4347 const struct got_error *
4348 commit_worktree(struct got_object_id **new_commit_id,
4349 struct got_pathlist_head *commitable_paths,
4350 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4351 const char *author, const char *committer,
4352 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4353 got_worktree_status_cb status_cb, void *status_arg,
4354 struct got_repository *repo)
4356 const struct got_error *err = NULL, *unlockerr = NULL;
4357 struct got_pathlist_entry *pe;
4358 const char *head_ref_name = NULL;
4359 struct got_commit_object *head_commit = NULL;
4360 struct got_reference *head_ref2 = NULL;
4361 struct got_object_id *head_commit_id2 = NULL;
4362 struct got_tree_object *head_tree = NULL;
4363 struct got_object_id *new_tree_id = NULL;
4364 struct got_object_id_queue parent_ids;
4365 struct got_object_qid *pid = NULL;
4366 char *logmsg = NULL;
4368 *new_commit_id = NULL;
4370 SIMPLEQ_INIT(&parent_ids);
4372 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4376 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4380 if (commit_msg_cb != NULL) {
4381 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4386 if (logmsg == NULL || strlen(logmsg) == 0) {
4387 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4391 /* Create blobs from added and modified files and record their IDs. */
4392 TAILQ_FOREACH(pe, commitable_paths, entry) {
4393 struct got_commitable *ct = pe->data;
4396 /* Blobs for staged files already exist. */
4397 if (ct->staged_status == GOT_STATUS_ADD ||
4398 ct->staged_status == GOT_STATUS_MODIFY)
4401 if (ct->status != GOT_STATUS_ADD &&
4402 ct->status != GOT_STATUS_MODIFY &&
4403 ct->status != GOT_STATUS_MODE_CHANGE)
4406 if (asprintf(&ondisk_path, "%s/%s",
4407 worktree->root_path, pe->path) == -1) {
4408 err = got_error_from_errno("asprintf");
4411 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4417 /* Recursively write new tree objects. */
4418 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4419 status_cb, status_arg, repo);
4423 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4426 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4427 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4428 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4429 got_object_qid_free(pid);
4435 /* Check if a concurrent commit to our branch has occurred. */
4436 head_ref_name = got_worktree_get_head_ref_name(worktree);
4437 if (head_ref_name == NULL) {
4438 err = got_error_from_errno("got_worktree_get_head_ref_name");
4441 /* Lock the reference here to prevent concurrent modification. */
4442 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4445 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4448 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4449 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4452 /* Update branch head in repository. */
4453 err = got_ref_change_ref(head_ref2, *new_commit_id);
4456 err = got_ref_write(head_ref2, repo);
4460 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4464 err = ref_base_commit(worktree, repo);
4469 got_object_tree_close(head_tree);
4471 got_object_commit_close(head_commit);
4472 free(head_commit_id2);
4474 unlockerr = got_ref_unlock(head_ref2);
4475 if (unlockerr && err == NULL)
4477 got_ref_close(head_ref2);
4482 static const struct got_error *
4483 check_path_is_commitable(const char *path,
4484 struct got_pathlist_head *commitable_paths)
4486 struct got_pathlist_entry *cpe = NULL;
4487 size_t path_len = strlen(path);
4489 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4490 struct got_commitable *ct = cpe->data;
4491 const char *ct_path = ct->path;
4493 while (ct_path[0] == '/')
4496 if (strcmp(path, ct_path) == 0 ||
4497 got_path_is_child(ct_path, path, path_len))
4502 return got_error_path(path, GOT_ERR_BAD_PATH);
4507 static const struct got_error *
4508 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4510 int *have_staged_files = arg;
4512 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4513 *have_staged_files = 1;
4514 return got_error(GOT_ERR_CANCELLED);
4520 static const struct got_error *
4521 check_non_staged_files(struct got_fileindex *fileindex,
4522 struct got_pathlist_head *paths)
4524 struct got_pathlist_entry *pe;
4525 struct got_fileindex_entry *ie;
4527 TAILQ_FOREACH(pe, paths, entry) {
4528 if (pe->path[0] == '\0')
4530 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4532 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4533 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4534 return got_error_path(pe->path,
4535 GOT_ERR_FILE_NOT_STAGED);
4541 const struct got_error *
4542 got_worktree_commit(struct got_object_id **new_commit_id,
4543 struct got_worktree *worktree, struct got_pathlist_head *paths,
4544 const char *author, const char *committer,
4545 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4546 got_worktree_status_cb status_cb, void *status_arg,
4547 struct got_repository *repo)
4549 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4550 struct got_fileindex *fileindex = NULL;
4551 char *fileindex_path = NULL;
4552 struct got_pathlist_head commitable_paths;
4553 struct collect_commitables_arg cc_arg;
4554 struct got_pathlist_entry *pe;
4555 struct got_reference *head_ref = NULL;
4556 struct got_object_id *head_commit_id = NULL;
4557 int have_staged_files = 0;
4559 *new_commit_id = NULL;
4561 TAILQ_INIT(&commitable_paths);
4563 err = lock_worktree(worktree, LOCK_EX);
4567 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4571 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4575 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4579 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4580 &have_staged_files);
4581 if (err && err->code != GOT_ERR_CANCELLED)
4583 if (have_staged_files) {
4584 err = check_non_staged_files(fileindex, paths);
4589 cc_arg.commitable_paths = &commitable_paths;
4590 cc_arg.worktree = worktree;
4592 cc_arg.have_staged_files = have_staged_files;
4593 TAILQ_FOREACH(pe, paths, entry) {
4594 err = worktree_status(worktree, pe->path, fileindex, repo,
4595 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4600 if (TAILQ_EMPTY(&commitable_paths)) {
4601 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4605 TAILQ_FOREACH(pe, paths, entry) {
4606 err = check_path_is_commitable(pe->path, &commitable_paths);
4611 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4612 struct got_commitable *ct = pe->data;
4613 const char *ct_path = ct->in_repo_path;
4615 while (ct_path[0] == '/')
4617 err = check_out_of_date(ct_path, ct->status,
4618 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4619 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4625 err = commit_worktree(new_commit_id, &commitable_paths,
4626 head_commit_id, worktree, author, committer,
4627 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4631 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4632 fileindex, have_staged_files);
4633 sync_err = sync_fileindex(fileindex, fileindex_path);
4634 if (sync_err && err == NULL)
4638 got_fileindex_free(fileindex);
4639 free(fileindex_path);
4640 unlockerr = lock_worktree(worktree, LOCK_SH);
4641 if (unlockerr && err == NULL)
4643 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4644 struct got_commitable *ct = pe->data;
4645 free_commitable(ct);
4647 got_pathlist_free(&commitable_paths);
4652 got_commitable_get_path(struct got_commitable *ct)
4658 got_commitable_get_status(struct got_commitable *ct)
4663 struct check_rebase_ok_arg {
4664 struct got_worktree *worktree;
4665 struct got_repository *repo;
4668 static const struct got_error *
4669 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4671 const struct got_error *err = NULL;
4672 struct check_rebase_ok_arg *a = arg;
4673 unsigned char status;
4677 /* Reject rebase of a work tree with mixed base commits. */
4678 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4679 SHA1_DIGEST_LENGTH))
4680 return got_error(GOT_ERR_MIXED_COMMITS);
4682 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4684 return got_error_from_errno("asprintf");
4686 /* Reject rebase of a work tree with modified or staged files. */
4687 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4692 if (status != GOT_STATUS_NO_CHANGE)
4693 return got_error(GOT_ERR_MODIFIED);
4694 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4695 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4700 const struct got_error *
4701 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4702 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4703 struct got_worktree *worktree, struct got_reference *branch,
4704 struct got_repository *repo)
4706 const struct got_error *err = NULL;
4707 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4708 char *branch_ref_name = NULL;
4709 char *fileindex_path = NULL;
4710 struct check_rebase_ok_arg ok_arg;
4711 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4712 struct got_object_id *wt_branch_tip = NULL;
4714 *new_base_branch_ref = NULL;
4718 err = lock_worktree(worktree, LOCK_EX);
4722 err = open_fileindex(fileindex, &fileindex_path, worktree);
4726 ok_arg.worktree = worktree;
4728 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4733 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4737 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4741 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4745 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4750 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
4753 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
4754 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
4758 err = got_ref_alloc_symref(new_base_branch_ref,
4759 new_base_branch_ref_name, wt_branch);
4762 err = got_ref_write(*new_base_branch_ref, repo);
4766 /* TODO Lock original branch's ref while rebasing? */
4768 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4772 err = got_ref_write(branch_ref, repo);
4776 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4777 worktree->base_commit_id);
4780 err = got_ref_write(*tmp_branch, repo);
4784 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4788 free(fileindex_path);
4789 free(tmp_branch_name);
4790 free(new_base_branch_ref_name);
4791 free(branch_ref_name);
4793 got_ref_close(branch_ref);
4795 got_ref_close(wt_branch);
4796 free(wt_branch_tip);
4798 if (*new_base_branch_ref) {
4799 got_ref_close(*new_base_branch_ref);
4800 *new_base_branch_ref = NULL;
4803 got_ref_close(*tmp_branch);
4807 got_fileindex_free(*fileindex);
4810 lock_worktree(worktree, LOCK_SH);
4815 const struct got_error *
4816 got_worktree_rebase_continue(struct got_object_id **commit_id,
4817 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4818 struct got_reference **branch, struct got_fileindex **fileindex,
4819 struct got_worktree *worktree, struct got_repository *repo)
4821 const struct got_error *err;
4822 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4823 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4824 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4825 char *fileindex_path = NULL;
4826 int have_staged_files = 0;
4829 *new_base_branch = NULL;
4834 err = lock_worktree(worktree, LOCK_EX);
4838 err = open_fileindex(fileindex, &fileindex_path, worktree);
4842 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4843 &have_staged_files);
4844 if (err && err->code != GOT_ERR_CANCELLED)
4846 if (have_staged_files) {
4847 err = got_error(GOT_ERR_STAGED_PATHS);
4851 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4855 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4859 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4863 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4867 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4871 err = got_ref_open(branch, repo,
4872 got_ref_get_symref_target(branch_ref), 0);
4876 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4880 err = got_ref_resolve(commit_id, repo, commit_ref);
4884 err = got_ref_open(new_base_branch, repo,
4885 new_base_branch_ref_name, 0);
4889 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4893 free(commit_ref_name);
4894 free(branch_ref_name);
4895 free(fileindex_path);
4897 got_ref_close(commit_ref);
4899 got_ref_close(branch_ref);
4904 got_ref_close(*tmp_branch);
4907 if (*new_base_branch) {
4908 got_ref_close(*new_base_branch);
4909 *new_base_branch = NULL;
4912 got_ref_close(*branch);
4916 got_fileindex_free(*fileindex);
4919 lock_worktree(worktree, LOCK_SH);
4924 const struct got_error *
4925 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4927 const struct got_error *err;
4928 char *tmp_branch_name = NULL;
4930 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4934 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4935 free(tmp_branch_name);
4939 static const struct got_error *
4940 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4941 char **logmsg, void *arg)
4947 static const struct got_error *
4948 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4949 const char *path, struct got_object_id *blob_id,
4950 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4951 int dirfd, const char *de_name)
4956 struct collect_merged_paths_arg {
4957 got_worktree_checkout_cb progress_cb;
4959 struct got_pathlist_head *merged_paths;
4962 static const struct got_error *
4963 collect_merged_paths(void *arg, unsigned char status, const char *path)
4965 const struct got_error *err;
4966 struct collect_merged_paths_arg *a = arg;
4968 struct got_pathlist_entry *new;
4970 err = (*a->progress_cb)(a->progress_arg, status, path);
4974 if (status != GOT_STATUS_MERGE &&
4975 status != GOT_STATUS_ADD &&
4976 status != GOT_STATUS_DELETE &&
4977 status != GOT_STATUS_CONFLICT)
4982 return got_error_from_errno("strdup");
4984 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4985 if (err || new == NULL)
4991 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4993 struct got_pathlist_entry *pe;
4995 TAILQ_FOREACH(pe, merged_paths, entry)
4996 free((char *)pe->path);
4998 got_pathlist_free(merged_paths);
5001 static const struct got_error *
5002 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5003 int is_rebase, struct got_repository *repo)
5005 const struct got_error *err;
5006 struct got_reference *commit_ref = NULL;
5008 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5010 if (err->code != GOT_ERR_NOT_REF)
5012 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5015 err = got_ref_write(commit_ref, repo);
5018 } else if (is_rebase) {
5019 struct got_object_id *stored_id;
5022 err = got_ref_resolve(&stored_id, repo, commit_ref);
5025 cmp = got_object_id_cmp(commit_id, stored_id);
5028 err = got_error(GOT_ERR_REBASE_COMMITID);
5034 got_ref_close(commit_ref);
5038 static const struct got_error *
5039 rebase_merge_files(struct got_pathlist_head *merged_paths,
5040 const char *commit_ref_name, struct got_worktree *worktree,
5041 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5042 struct got_object_id *commit_id, struct got_repository *repo,
5043 got_worktree_checkout_cb progress_cb, void *progress_arg,
5044 got_cancel_cb cancel_cb, void *cancel_arg)
5046 const struct got_error *err;
5047 struct got_reference *commit_ref = NULL;
5048 struct collect_merged_paths_arg cmp_arg;
5049 char *fileindex_path;
5051 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5053 err = get_fileindex_path(&fileindex_path, worktree);
5057 cmp_arg.progress_cb = progress_cb;
5058 cmp_arg.progress_arg = progress_arg;
5059 cmp_arg.merged_paths = merged_paths;
5060 err = merge_files(worktree, fileindex, fileindex_path,
5061 parent_commit_id, commit_id, repo, collect_merged_paths,
5062 &cmp_arg, cancel_cb, cancel_arg);
5064 got_ref_close(commit_ref);
5068 const struct got_error *
5069 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5070 struct got_worktree *worktree, struct got_fileindex *fileindex,
5071 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5072 struct got_repository *repo,
5073 got_worktree_checkout_cb progress_cb, void *progress_arg,
5074 got_cancel_cb cancel_cb, void *cancel_arg)
5076 const struct got_error *err;
5077 char *commit_ref_name;
5079 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5083 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5087 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5088 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5089 progress_arg, cancel_cb, cancel_arg);
5091 free(commit_ref_name);
5095 const struct got_error *
5096 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5097 struct got_worktree *worktree, struct got_fileindex *fileindex,
5098 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5099 struct got_repository *repo,
5100 got_worktree_checkout_cb progress_cb, void *progress_arg,
5101 got_cancel_cb cancel_cb, void *cancel_arg)
5103 const struct got_error *err;
5104 char *commit_ref_name;
5106 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5110 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5114 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5115 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5116 progress_arg, cancel_cb, cancel_arg);
5118 free(commit_ref_name);
5122 static const struct got_error *
5123 rebase_commit(struct got_object_id **new_commit_id,
5124 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5125 struct got_worktree *worktree, struct got_fileindex *fileindex,
5126 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5127 const char *new_logmsg, struct got_repository *repo)
5129 const struct got_error *err, *sync_err;
5130 struct got_pathlist_head commitable_paths;
5131 struct collect_commitables_arg cc_arg;
5132 char *fileindex_path = NULL;
5133 struct got_reference *head_ref = NULL;
5134 struct got_object_id *head_commit_id = NULL;
5135 char *logmsg = NULL;
5137 TAILQ_INIT(&commitable_paths);
5138 *new_commit_id = NULL;
5140 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5142 err = get_fileindex_path(&fileindex_path, worktree);
5146 cc_arg.commitable_paths = &commitable_paths;
5147 cc_arg.worktree = worktree;
5149 cc_arg.have_staged_files = 0;
5151 * If possible get the status of individual files directly to
5152 * avoid crawling the entire work tree once per rebased commit.
5153 * TODO: Ideally, merged_paths would contain a list of commitables
5154 * we could use so we could skip worktree_status() entirely.
5157 struct got_pathlist_entry *pe;
5158 if (TAILQ_EMPTY(merged_paths)) {
5159 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5162 TAILQ_FOREACH(pe, merged_paths, entry) {
5163 err = worktree_status(worktree, pe->path, fileindex,
5164 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5170 err = worktree_status(worktree, "", fileindex, repo,
5171 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5176 if (TAILQ_EMPTY(&commitable_paths)) {
5177 /* No-op change; commit will be elided. */
5178 err = got_ref_delete(commit_ref, repo);
5181 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5185 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5189 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5194 logmsg = strdup(new_logmsg);
5195 if (logmsg == NULL) {
5196 err = got_error_from_errno("strdup");
5200 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5205 /* NB: commit_worktree will call free(logmsg) */
5206 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5207 worktree, got_object_commit_get_author(orig_commit),
5208 got_object_commit_get_committer(orig_commit),
5209 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5213 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5217 err = got_ref_delete(commit_ref, repo);
5221 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5223 sync_err = sync_fileindex(fileindex, fileindex_path);
5224 if (sync_err && err == NULL)
5227 free(fileindex_path);
5228 free(head_commit_id);
5230 got_ref_close(head_ref);
5232 free(*new_commit_id);
5233 *new_commit_id = NULL;
5238 const struct got_error *
5239 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5240 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5241 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5242 struct got_commit_object *orig_commit,
5243 struct got_object_id *orig_commit_id, struct got_repository *repo)
5245 const struct got_error *err;
5246 char *commit_ref_name;
5247 struct got_reference *commit_ref = NULL;
5248 struct got_object_id *commit_id = NULL;
5250 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5254 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5257 err = got_ref_resolve(&commit_id, repo, commit_ref);
5260 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5261 err = got_error(GOT_ERR_REBASE_COMMITID);
5265 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5266 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5269 got_ref_close(commit_ref);
5270 free(commit_ref_name);
5275 const struct got_error *
5276 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5277 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5278 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5279 struct got_commit_object *orig_commit,
5280 struct got_object_id *orig_commit_id, const char *new_logmsg,
5281 struct got_repository *repo)
5283 const struct got_error *err;
5284 char *commit_ref_name;
5285 struct got_reference *commit_ref = NULL;
5287 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5291 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5295 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5296 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5299 got_ref_close(commit_ref);
5300 free(commit_ref_name);
5304 const struct got_error *
5305 got_worktree_rebase_postpone(struct got_worktree *worktree,
5306 struct got_fileindex *fileindex)
5309 got_fileindex_free(fileindex);
5310 return lock_worktree(worktree, LOCK_SH);
5313 static const struct got_error *
5314 delete_ref(const char *name, struct got_repository *repo)
5316 const struct got_error *err;
5317 struct got_reference *ref;
5319 err = got_ref_open(&ref, repo, name, 0);
5321 if (err->code == GOT_ERR_NOT_REF)
5326 err = got_ref_delete(ref, repo);
5331 static const struct got_error *
5332 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5334 const struct got_error *err;
5335 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5336 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5338 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5341 err = delete_ref(tmp_branch_name, repo);
5345 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5348 err = delete_ref(new_base_branch_ref_name, repo);
5352 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5355 err = delete_ref(branch_ref_name, repo);
5359 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5362 err = delete_ref(commit_ref_name, repo);
5367 free(tmp_branch_name);
5368 free(new_base_branch_ref_name);
5369 free(branch_ref_name);
5370 free(commit_ref_name);
5374 const struct got_error *
5375 got_worktree_rebase_complete(struct got_worktree *worktree,
5376 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5377 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5378 struct got_repository *repo)
5380 const struct got_error *err, *unlockerr;
5381 struct got_object_id *new_head_commit_id = NULL;
5383 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5387 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5391 err = got_ref_write(rebased_branch, repo);
5395 err = got_worktree_set_head_ref(worktree, rebased_branch);
5399 err = delete_rebase_refs(worktree, repo);
5402 got_fileindex_free(fileindex);
5403 free(new_head_commit_id);
5404 unlockerr = lock_worktree(worktree, LOCK_SH);
5405 if (unlockerr && err == NULL)
5410 const struct got_error *
5411 got_worktree_rebase_abort(struct got_worktree *worktree,
5412 struct got_fileindex *fileindex, struct got_repository *repo,
5413 struct got_reference *new_base_branch,
5414 got_worktree_checkout_cb progress_cb, void *progress_arg)
5416 const struct got_error *err, *unlockerr, *sync_err;
5417 struct got_reference *resolved = NULL;
5418 struct got_object_id *commit_id = NULL;
5419 char *fileindex_path = NULL;
5420 struct revert_file_args rfa;
5421 struct got_object_id *tree_id = NULL;
5423 err = lock_worktree(worktree, LOCK_EX);
5427 err = got_ref_open(&resolved, repo,
5428 got_ref_get_symref_target(new_base_branch), 0);
5432 err = got_worktree_set_head_ref(worktree, resolved);
5437 * XXX commits to the base branch could have happened while
5438 * we were busy rebasing; should we store the original commit ID
5439 * when rebase begins and read it back here?
5441 err = got_ref_resolve(&commit_id, repo, resolved);
5445 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5449 err = got_object_id_by_path(&tree_id, repo,
5450 worktree->base_commit_id, worktree->path_prefix);
5454 err = delete_rebase_refs(worktree, repo);
5458 err = get_fileindex_path(&fileindex_path, worktree);
5462 rfa.worktree = worktree;
5463 rfa.fileindex = fileindex;
5464 rfa.progress_cb = progress_cb;
5465 rfa.progress_arg = progress_arg;
5466 rfa.patch_cb = NULL;
5467 rfa.patch_arg = NULL;
5469 err = worktree_status(worktree, "", fileindex, repo,
5470 revert_file, &rfa, NULL, NULL, 0, 0);
5474 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5475 repo, progress_cb, progress_arg, NULL, NULL);
5477 sync_err = sync_fileindex(fileindex, fileindex_path);
5478 if (sync_err && err == NULL)
5481 got_ref_close(resolved);
5485 got_fileindex_free(fileindex);
5486 free(fileindex_path);
5488 unlockerr = lock_worktree(worktree, LOCK_SH);
5489 if (unlockerr && err == NULL)
5494 const struct got_error *
5495 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5496 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5497 struct got_fileindex **fileindex, struct got_worktree *worktree,
5498 struct got_repository *repo)
5500 const struct got_error *err = NULL;
5501 char *tmp_branch_name = NULL;
5502 char *branch_ref_name = NULL;
5503 char *base_commit_ref_name = NULL;
5504 char *fileindex_path = NULL;
5505 struct check_rebase_ok_arg ok_arg;
5506 struct got_reference *wt_branch = NULL;
5507 struct got_reference *base_commit_ref = NULL;
5511 *base_commit_id = NULL;
5514 err = lock_worktree(worktree, LOCK_EX);
5518 err = open_fileindex(fileindex, &fileindex_path, worktree);
5522 ok_arg.worktree = worktree;
5524 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5529 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5533 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5537 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5542 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5547 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5551 err = got_ref_write(*branch_ref, repo);
5555 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5556 worktree->base_commit_id);
5559 err = got_ref_write(base_commit_ref, repo);
5562 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5563 if (*base_commit_id == NULL) {
5564 err = got_error_from_errno("got_object_id_dup");
5568 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5569 worktree->base_commit_id);
5572 err = got_ref_write(*tmp_branch, repo);
5576 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5580 free(fileindex_path);
5581 free(tmp_branch_name);
5582 free(branch_ref_name);
5583 free(base_commit_ref_name);
5585 got_ref_close(wt_branch);
5588 got_ref_close(*branch_ref);
5592 got_ref_close(*tmp_branch);
5595 free(*base_commit_id);
5597 got_fileindex_free(*fileindex);
5600 lock_worktree(worktree, LOCK_SH);
5605 const struct got_error *
5606 got_worktree_histedit_postpone(struct got_worktree *worktree,
5607 struct got_fileindex *fileindex)
5610 got_fileindex_free(fileindex);
5611 return lock_worktree(worktree, LOCK_SH);
5614 const struct got_error *
5615 got_worktree_histedit_in_progress(int *in_progress,
5616 struct got_worktree *worktree)
5618 const struct got_error *err;
5619 char *tmp_branch_name = NULL;
5621 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5625 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5626 free(tmp_branch_name);
5630 const struct got_error *
5631 got_worktree_histedit_continue(struct got_object_id **commit_id,
5632 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5633 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5634 struct got_worktree *worktree, struct got_repository *repo)
5636 const struct got_error *err;
5637 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5638 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5639 struct got_reference *commit_ref = NULL;
5640 struct got_reference *base_commit_ref = NULL;
5641 char *fileindex_path = NULL;
5642 int have_staged_files = 0;
5646 *base_commit_id = NULL;
5649 err = lock_worktree(worktree, LOCK_EX);
5653 err = open_fileindex(fileindex, &fileindex_path, worktree);
5657 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5658 &have_staged_files);
5659 if (err && err->code != GOT_ERR_CANCELLED)
5661 if (have_staged_files) {
5662 err = got_error(GOT_ERR_STAGED_PATHS);
5666 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5670 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5674 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5678 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5683 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5687 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5690 err = got_ref_resolve(commit_id, repo, commit_ref);
5694 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5697 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5701 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5705 free(commit_ref_name);
5706 free(branch_ref_name);
5707 free(fileindex_path);
5709 got_ref_close(commit_ref);
5710 if (base_commit_ref)
5711 got_ref_close(base_commit_ref);
5715 free(*base_commit_id);
5716 *base_commit_id = NULL;
5718 got_ref_close(*tmp_branch);
5722 got_fileindex_free(*fileindex);
5725 lock_worktree(worktree, LOCK_EX);
5730 static const struct got_error *
5731 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5733 const struct got_error *err;
5734 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5735 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5737 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5740 err = delete_ref(tmp_branch_name, repo);
5744 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5748 err = delete_ref(base_commit_ref_name, repo);
5752 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5755 err = delete_ref(branch_ref_name, repo);
5759 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5762 err = delete_ref(commit_ref_name, repo);
5766 free(tmp_branch_name);
5767 free(base_commit_ref_name);
5768 free(branch_ref_name);
5769 free(commit_ref_name);
5773 const struct got_error *
5774 got_worktree_histedit_abort(struct got_worktree *worktree,
5775 struct got_fileindex *fileindex, struct got_repository *repo,
5776 struct got_reference *branch, struct got_object_id *base_commit_id,
5777 got_worktree_checkout_cb progress_cb, void *progress_arg)
5779 const struct got_error *err, *unlockerr, *sync_err;
5780 struct got_reference *resolved = NULL;
5781 char *fileindex_path = NULL;
5782 struct got_object_id *tree_id = NULL;
5783 struct revert_file_args rfa;
5785 err = lock_worktree(worktree, LOCK_EX);
5789 err = got_ref_open(&resolved, repo,
5790 got_ref_get_symref_target(branch), 0);
5794 err = got_worktree_set_head_ref(worktree, resolved);
5798 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5802 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5803 worktree->path_prefix);
5807 err = delete_histedit_refs(worktree, repo);
5811 err = get_fileindex_path(&fileindex_path, worktree);
5815 rfa.worktree = worktree;
5816 rfa.fileindex = fileindex;
5817 rfa.progress_cb = progress_cb;
5818 rfa.progress_arg = progress_arg;
5819 rfa.patch_cb = NULL;
5820 rfa.patch_arg = NULL;
5822 err = worktree_status(worktree, "", fileindex, repo,
5823 revert_file, &rfa, NULL, NULL, 0, 0);
5827 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5828 repo, progress_cb, progress_arg, NULL, NULL);
5830 sync_err = sync_fileindex(fileindex, fileindex_path);
5831 if (sync_err && err == NULL)
5834 got_ref_close(resolved);
5836 free(fileindex_path);
5838 unlockerr = lock_worktree(worktree, LOCK_SH);
5839 if (unlockerr && err == NULL)
5844 const struct got_error *
5845 got_worktree_histedit_complete(struct got_worktree *worktree,
5846 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5847 struct got_reference *edited_branch, struct got_repository *repo)
5849 const struct got_error *err, *unlockerr;
5850 struct got_object_id *new_head_commit_id = NULL;
5851 struct got_reference *resolved = NULL;
5853 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5857 err = got_ref_open(&resolved, repo,
5858 got_ref_get_symref_target(edited_branch), 0);
5862 err = got_ref_change_ref(resolved, new_head_commit_id);
5866 err = got_ref_write(resolved, repo);
5870 err = got_worktree_set_head_ref(worktree, resolved);
5874 err = delete_histedit_refs(worktree, repo);
5877 got_fileindex_free(fileindex);
5878 free(new_head_commit_id);
5879 unlockerr = lock_worktree(worktree, LOCK_SH);
5880 if (unlockerr && err == NULL)
5885 const struct got_error *
5886 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5887 struct got_object_id *commit_id, struct got_repository *repo)
5889 const struct got_error *err;
5890 char *commit_ref_name;
5892 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5896 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5900 err = delete_ref(commit_ref_name, repo);
5902 free(commit_ref_name);
5906 const struct got_error *
5907 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5908 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5909 struct got_worktree *worktree, const char *refname,
5910 struct got_repository *repo)
5912 const struct got_error *err = NULL;
5913 char *fileindex_path = NULL;
5914 struct check_rebase_ok_arg ok_arg;
5918 *base_branch_ref = NULL;
5920 err = lock_worktree(worktree, LOCK_EX);
5924 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5925 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5926 "cannot integrate a branch into itself; "
5927 "update -b or different branch name required");
5931 err = open_fileindex(fileindex, &fileindex_path, worktree);
5935 /* Preconditions are the same as for rebase. */
5936 ok_arg.worktree = worktree;
5938 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5943 err = got_ref_open(branch_ref, repo, refname, 1);
5947 err = got_ref_open(base_branch_ref, repo,
5948 got_worktree_get_head_ref_name(worktree), 1);
5952 got_ref_close(*branch_ref);
5955 if (*base_branch_ref) {
5956 got_ref_close(*base_branch_ref);
5957 *base_branch_ref = NULL;
5960 got_fileindex_free(*fileindex);
5963 lock_worktree(worktree, LOCK_SH);
5968 const struct got_error *
5969 got_worktree_integrate_continue(struct got_worktree *worktree,
5970 struct got_fileindex *fileindex, struct got_repository *repo,
5971 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5972 got_worktree_checkout_cb progress_cb, void *progress_arg,
5973 got_cancel_cb cancel_cb, void *cancel_arg)
5975 const struct got_error *err = NULL, *sync_err, *unlockerr;
5976 char *fileindex_path = NULL;
5977 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5979 err = get_fileindex_path(&fileindex_path, worktree);
5983 err = got_ref_resolve(&commit_id, repo, branch_ref);
5987 err = got_object_id_by_path(&tree_id, repo, commit_id,
5988 worktree->path_prefix);
5992 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5996 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5997 progress_cb, progress_arg, cancel_cb, cancel_arg);
6001 err = got_ref_change_ref(base_branch_ref, commit_id);
6005 err = got_ref_write(base_branch_ref, repo);
6007 sync_err = sync_fileindex(fileindex, fileindex_path);
6008 if (sync_err && err == NULL)
6012 unlockerr = got_ref_unlock(branch_ref);
6013 if (unlockerr && err == NULL)
6015 got_ref_close(branch_ref);
6017 unlockerr = got_ref_unlock(base_branch_ref);
6018 if (unlockerr && err == NULL)
6020 got_ref_close(base_branch_ref);
6022 got_fileindex_free(fileindex);
6023 free(fileindex_path);
6026 unlockerr = lock_worktree(worktree, LOCK_SH);
6027 if (unlockerr && err == NULL)
6032 const struct got_error *
6033 got_worktree_integrate_abort(struct got_worktree *worktree,
6034 struct got_fileindex *fileindex, struct got_repository *repo,
6035 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6037 const struct got_error *err = NULL, *unlockerr = NULL;
6039 got_fileindex_free(fileindex);
6041 err = lock_worktree(worktree, LOCK_SH);
6043 unlockerr = got_ref_unlock(branch_ref);
6044 if (unlockerr && err == NULL)
6046 got_ref_close(branch_ref);
6048 unlockerr = got_ref_unlock(base_branch_ref);
6049 if (unlockerr && err == NULL)
6051 got_ref_close(base_branch_ref);
6056 struct check_stage_ok_arg {
6057 struct got_object_id *head_commit_id;
6058 struct got_worktree *worktree;
6059 struct got_fileindex *fileindex;
6060 struct got_repository *repo;
6064 const struct got_error *
6065 check_stage_ok(void *arg, unsigned char status,
6066 unsigned char staged_status, const char *relpath,
6067 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6068 struct got_object_id *commit_id, int dirfd, const char *de_name)
6070 struct check_stage_ok_arg *a = arg;
6071 const struct got_error *err = NULL;
6072 struct got_fileindex_entry *ie;
6073 struct got_object_id base_commit_id;
6074 struct got_object_id *base_commit_idp = NULL;
6075 char *in_repo_path = NULL, *p;
6077 if (status == GOT_STATUS_UNVERSIONED ||
6078 status == GOT_STATUS_NO_CHANGE)
6080 if (status == GOT_STATUS_NONEXISTENT)
6081 return got_error_set_errno(ENOENT, relpath);
6083 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6085 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6087 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6088 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6090 return got_error_from_errno("asprintf");
6092 if (got_fileindex_entry_has_commit(ie)) {
6093 memcpy(base_commit_id.sha1, ie->commit_sha1,
6094 SHA1_DIGEST_LENGTH);
6095 base_commit_idp = &base_commit_id;
6098 if (status == GOT_STATUS_CONFLICT) {
6099 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6101 } else if (status != GOT_STATUS_ADD &&
6102 status != GOT_STATUS_MODIFY &&
6103 status != GOT_STATUS_DELETE) {
6104 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6108 a->have_changes = 1;
6113 err = check_out_of_date(p, status, staged_status,
6114 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6115 GOT_ERR_STAGE_OUT_OF_DATE);
6121 struct stage_path_arg {
6122 struct got_worktree *worktree;
6123 struct got_fileindex *fileindex;
6124 struct got_repository *repo;
6125 got_worktree_status_cb status_cb;
6127 got_worktree_patch_cb patch_cb;
6129 int staged_something;
6132 static const struct got_error *
6133 stage_path(void *arg, unsigned char status,
6134 unsigned char staged_status, const char *relpath,
6135 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6136 struct got_object_id *commit_id, int dirfd, const char *de_name)
6138 struct stage_path_arg *a = arg;
6139 const struct got_error *err = NULL;
6140 struct got_fileindex_entry *ie;
6141 char *ondisk_path = NULL, *path_content = NULL;
6143 struct got_object_id *new_staged_blob_id = NULL;
6145 if (status == GOT_STATUS_UNVERSIONED)
6148 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6150 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6152 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6154 return got_error_from_errno("asprintf");
6157 case GOT_STATUS_ADD:
6158 case GOT_STATUS_MODIFY:
6160 if (status == GOT_STATUS_ADD) {
6161 int choice = GOT_PATCH_CHOICE_NONE;
6162 err = (*a->patch_cb)(&choice, a->patch_arg,
6163 status, ie->path, NULL, 1, 1);
6166 if (choice != GOT_PATCH_CHOICE_YES)
6169 err = create_patched_content(&path_content, 0,
6170 staged_blob_id ? staged_blob_id : blob_id,
6171 ondisk_path, dirfd, de_name, ie->path,
6172 a->repo, a->patch_cb, a->patch_arg);
6173 if (err || path_content == NULL)
6177 err = got_object_blob_create(&new_staged_blob_id,
6178 path_content ? path_content : ondisk_path, a->repo);
6181 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6182 SHA1_DIGEST_LENGTH);
6183 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6184 stage = GOT_FILEIDX_STAGE_ADD;
6186 stage = GOT_FILEIDX_STAGE_MODIFY;
6187 got_fileindex_entry_stage_set(ie, stage);
6188 a->staged_something = 1;
6189 if (a->status_cb == NULL)
6191 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6192 get_staged_status(ie), relpath, blob_id,
6193 new_staged_blob_id, NULL, dirfd, de_name);
6195 case GOT_STATUS_DELETE:
6196 if (staged_status == GOT_STATUS_DELETE)
6199 int choice = GOT_PATCH_CHOICE_NONE;
6200 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6201 ie->path, NULL, 1, 1);
6204 if (choice == GOT_PATCH_CHOICE_NO)
6206 if (choice != GOT_PATCH_CHOICE_YES) {
6207 err = got_error(GOT_ERR_PATCH_CHOICE);
6211 stage = GOT_FILEIDX_STAGE_DELETE;
6212 got_fileindex_entry_stage_set(ie, stage);
6213 a->staged_something = 1;
6214 if (a->status_cb == NULL)
6216 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6217 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6220 case GOT_STATUS_NO_CHANGE:
6222 case GOT_STATUS_CONFLICT:
6223 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6225 case GOT_STATUS_NONEXISTENT:
6226 err = got_error_set_errno(ENOENT, relpath);
6229 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6233 if (path_content && unlink(path_content) == -1 && err == NULL)
6234 err = got_error_from_errno2("unlink", path_content);
6237 free(new_staged_blob_id);
6241 const struct got_error *
6242 got_worktree_stage(struct got_worktree *worktree,
6243 struct got_pathlist_head *paths,
6244 got_worktree_status_cb status_cb, void *status_arg,
6245 got_worktree_patch_cb patch_cb, void *patch_arg,
6246 struct got_repository *repo)
6248 const struct got_error *err = NULL, *sync_err, *unlockerr;
6249 struct got_pathlist_entry *pe;
6250 struct got_fileindex *fileindex = NULL;
6251 char *fileindex_path = NULL;
6252 struct got_reference *head_ref = NULL;
6253 struct got_object_id *head_commit_id = NULL;
6254 struct check_stage_ok_arg oka;
6255 struct stage_path_arg spa;
6257 err = lock_worktree(worktree, LOCK_EX);
6261 err = got_ref_open(&head_ref, repo,
6262 got_worktree_get_head_ref_name(worktree), 0);
6265 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6268 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6272 /* Check pre-conditions before staging anything. */
6273 oka.head_commit_id = head_commit_id;
6274 oka.worktree = worktree;
6275 oka.fileindex = fileindex;
6277 oka.have_changes = 0;
6278 TAILQ_FOREACH(pe, paths, entry) {
6279 err = worktree_status(worktree, pe->path, fileindex, repo,
6280 check_stage_ok, &oka, NULL, NULL, 0, 0);
6284 if (!oka.have_changes) {
6285 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6289 spa.worktree = worktree;
6290 spa.fileindex = fileindex;
6292 spa.patch_cb = patch_cb;
6293 spa.patch_arg = patch_arg;
6294 spa.status_cb = status_cb;
6295 spa.status_arg = status_arg;
6296 spa.staged_something = 0;
6297 TAILQ_FOREACH(pe, paths, entry) {
6298 err = worktree_status(worktree, pe->path, fileindex, repo,
6299 stage_path, &spa, NULL, NULL, 0, 0);
6303 if (!spa.staged_something) {
6304 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6308 sync_err = sync_fileindex(fileindex, fileindex_path);
6309 if (sync_err && err == NULL)
6313 got_ref_close(head_ref);
6314 free(head_commit_id);
6315 free(fileindex_path);
6317 got_fileindex_free(fileindex);
6318 unlockerr = lock_worktree(worktree, LOCK_SH);
6319 if (unlockerr && err == NULL)
6324 struct unstage_path_arg {
6325 struct got_worktree *worktree;
6326 struct got_fileindex *fileindex;
6327 struct got_repository *repo;
6328 got_worktree_checkout_cb progress_cb;
6330 got_worktree_patch_cb patch_cb;
6334 static const struct got_error *
6335 create_unstaged_content(char **path_unstaged_content,
6336 char **path_new_staged_content, struct got_object_id *blob_id,
6337 struct got_object_id *staged_blob_id, const char *relpath,
6338 struct got_repository *repo,
6339 got_worktree_patch_cb patch_cb, void *patch_arg)
6341 const struct got_error *err;
6342 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6343 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6344 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6345 struct stat sb1, sb2;
6346 struct got_diff_changes *changes = NULL;
6347 struct got_diff_state *ds = NULL;
6348 struct got_diff_args *args = NULL;
6349 struct got_diff_change *change;
6350 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6351 int have_content = 0, have_rejected_content = 0;
6353 *path_unstaged_content = NULL;
6354 *path_new_staged_content = NULL;
6356 err = got_object_id_str(&label1, blob_id);
6359 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6363 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6367 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6371 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6375 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6379 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6383 if (stat(path1, &sb1) == -1) {
6384 err = got_error_from_errno2("stat", path1);
6388 if (stat(path2, &sb2) == -1) {
6389 err = got_error_from_errno2("stat", path2);
6393 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6394 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6398 err = got_opentemp_named(path_unstaged_content, &outfile,
6399 "got-unstaged-content");
6402 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6403 "got-new-staged-content");
6407 if (fseek(f1, 0L, SEEK_SET) == -1) {
6408 err = got_ferror(f1, GOT_ERR_IO);
6411 if (fseek(f2, 0L, SEEK_SET) == -1) {
6412 err = got_ferror(f2, GOT_ERR_IO);
6415 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6417 err = apply_or_reject_change(&choice, change, ++n,
6418 changes->nchanges, ds, args, diff_flags, relpath,
6419 f1, f2, &line_cur1, &line_cur2,
6420 outfile, rejectfile, patch_cb, patch_arg);
6423 if (choice == GOT_PATCH_CHOICE_YES)
6426 have_rejected_content = 1;
6427 if (choice == GOT_PATCH_CHOICE_QUIT)
6430 if (have_content || have_rejected_content)
6431 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6432 outfile, rejectfile);
6436 got_object_blob_close(blob);
6438 got_object_blob_close(staged_blob);
6439 if (f1 && fclose(f1) == EOF && err == NULL)
6440 err = got_error_from_errno2("fclose", path1);
6441 if (f2 && fclose(f2) == EOF && err == NULL)
6442 err = got_error_from_errno2("fclose", path2);
6443 if (outfile && fclose(outfile) == EOF && err == NULL)
6444 err = got_error_from_errno2("fclose", *path_unstaged_content);
6445 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6446 err = got_error_from_errno2("fclose", *path_new_staged_content);
6447 if (path1 && unlink(path1) == -1 && err == NULL)
6448 err = got_error_from_errno2("unlink", path1);
6449 if (path2 && unlink(path2) == -1 && err == NULL)
6450 err = got_error_from_errno2("unlink", path2);
6451 if (err || !have_content) {
6452 if (*path_unstaged_content &&
6453 unlink(*path_unstaged_content) == -1 && err == NULL)
6454 err = got_error_from_errno2("unlink",
6455 *path_unstaged_content);
6456 free(*path_unstaged_content);
6457 *path_unstaged_content = NULL;
6459 if (err || !have_rejected_content) {
6460 if (*path_new_staged_content &&
6461 unlink(*path_new_staged_content) == -1 && err == NULL)
6462 err = got_error_from_errno2("unlink",
6463 *path_new_staged_content);
6464 free(*path_new_staged_content);
6465 *path_new_staged_content = NULL;
6469 got_diff_state_free(ds);
6473 got_diff_free_changes(changes);
6479 static const struct got_error *
6480 unstage_path(void *arg, unsigned char status,
6481 unsigned char staged_status, const char *relpath,
6482 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6483 struct got_object_id *commit_id, int dirfd, const char *de_name)
6485 const struct got_error *err = NULL;
6486 struct unstage_path_arg *a = arg;
6487 struct got_fileindex_entry *ie;
6488 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6489 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6490 char *path_new_staged_content = NULL;
6491 char *id_str = NULL, *label_orig = NULL;
6492 int local_changes_subsumed;
6495 if (staged_status != GOT_STATUS_ADD &&
6496 staged_status != GOT_STATUS_MODIFY &&
6497 staged_status != GOT_STATUS_DELETE)
6500 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6502 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6504 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6506 return got_error_from_errno("asprintf");
6508 err = got_object_id_str(&id_str,
6509 commit_id ? commit_id : a->worktree->base_commit_id);
6512 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6514 err = got_error_from_errno("asprintf");
6518 switch (staged_status) {
6519 case GOT_STATUS_MODIFY:
6520 err = got_object_open_as_blob(&blob_base, a->repo,
6525 case GOT_STATUS_ADD:
6527 if (staged_status == GOT_STATUS_ADD) {
6528 int choice = GOT_PATCH_CHOICE_NONE;
6529 err = (*a->patch_cb)(&choice, a->patch_arg,
6530 staged_status, ie->path, NULL, 1, 1);
6533 if (choice != GOT_PATCH_CHOICE_YES)
6536 err = create_unstaged_content(
6537 &path_unstaged_content,
6538 &path_new_staged_content, blob_id,
6539 staged_blob_id, ie->path, a->repo,
6540 a->patch_cb, a->patch_arg);
6541 if (err || path_unstaged_content == NULL)
6543 if (path_new_staged_content) {
6544 err = got_object_blob_create(
6546 path_new_staged_content,
6550 memcpy(ie->staged_blob_sha1,
6551 staged_blob_id->sha1,
6552 SHA1_DIGEST_LENGTH);
6554 err = merge_file(&local_changes_subsumed,
6555 a->worktree, blob_base, ondisk_path,
6556 relpath, got_fileindex_perms_to_st(ie),
6557 path_unstaged_content, label_orig,
6558 "unstaged", a->repo, a->progress_cb,
6561 path_new_staged_content == NULL)
6562 got_fileindex_entry_stage_set(ie,
6563 GOT_FILEIDX_STAGE_NONE);
6564 break; /* Done with this file. */
6567 err = got_object_open_as_blob(&blob_staged, a->repo,
6568 staged_blob_id, 8192);
6571 err = merge_blob(&local_changes_subsumed, a->worktree,
6572 blob_base, ondisk_path, relpath,
6573 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6574 commit_id ? commit_id : a->worktree->base_commit_id,
6575 a->repo, a->progress_cb, a->progress_arg);
6577 got_fileindex_entry_stage_set(ie,
6578 GOT_FILEIDX_STAGE_NONE);
6580 case GOT_STATUS_DELETE:
6582 int choice = GOT_PATCH_CHOICE_NONE;
6583 err = (*a->patch_cb)(&choice, a->patch_arg,
6584 staged_status, ie->path, NULL, 1, 1);
6587 if (choice == GOT_PATCH_CHOICE_NO)
6589 if (choice != GOT_PATCH_CHOICE_YES) {
6590 err = got_error(GOT_ERR_PATCH_CHOICE);
6594 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6595 err = get_file_status(&status, &sb, ie, ondisk_path,
6596 dirfd, de_name, a->repo);
6599 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6604 if (path_unstaged_content &&
6605 unlink(path_unstaged_content) == -1 && err == NULL)
6606 err = got_error_from_errno2("unlink", path_unstaged_content);
6607 if (path_new_staged_content &&
6608 unlink(path_new_staged_content) == -1 && err == NULL)
6609 err = got_error_from_errno2("unlink", path_new_staged_content);
6610 free(path_unstaged_content);
6611 free(path_new_staged_content);
6613 got_object_blob_close(blob_base);
6615 got_object_blob_close(blob_staged);
6621 const struct got_error *
6622 got_worktree_unstage(struct got_worktree *worktree,
6623 struct got_pathlist_head *paths,
6624 got_worktree_checkout_cb progress_cb, void *progress_arg,
6625 got_worktree_patch_cb patch_cb, void *patch_arg,
6626 struct got_repository *repo)
6628 const struct got_error *err = NULL, *sync_err, *unlockerr;
6629 struct got_pathlist_entry *pe;
6630 struct got_fileindex *fileindex = NULL;
6631 char *fileindex_path = NULL;
6632 struct unstage_path_arg upa;
6634 err = lock_worktree(worktree, LOCK_EX);
6638 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6642 upa.worktree = worktree;
6643 upa.fileindex = fileindex;
6645 upa.progress_cb = progress_cb;
6646 upa.progress_arg = progress_arg;
6647 upa.patch_cb = patch_cb;
6648 upa.patch_arg = patch_arg;
6649 TAILQ_FOREACH(pe, paths, entry) {
6650 err = worktree_status(worktree, pe->path, fileindex, repo,
6651 unstage_path, &upa, NULL, NULL, 0, 0);
6656 sync_err = sync_fileindex(fileindex, fileindex_path);
6657 if (sync_err && err == NULL)
6660 free(fileindex_path);
6662 got_fileindex_free(fileindex);
6663 unlockerr = lock_worktree(worktree, LOCK_SH);
6664 if (unlockerr && err == NULL)