2 * Copyright (c) 2018, 2019 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("open", f1_path);
709 f2 = fopen(f2_path, "r");
711 err = got_error_from_errno2("open", 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 (chmod(merged_path, st_mode) != 0) {
806 err = got_error_from_errno2("chmod", merged_path);
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
818 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
819 err = got_error_from_errno("close");
820 if (f_orig && fclose(f_orig) != 0 && err == NULL)
821 err = got_error_from_errno("fclose");
824 if (blob_orig_path) {
825 unlink(blob_orig_path);
826 free(blob_orig_path);
832 * Perform a 3-way merge where blob_orig acts as the common ancestor,
833 * blob_deriv acts as the first derived version, and the file on disk
834 * acts as the second derived version.
836 static const struct got_error *
837 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
838 struct got_blob_object *blob_orig, const char *ondisk_path,
839 const char *path, uint16_t st_mode, const char *label_orig,
840 struct got_blob_object *blob_deriv,
841 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg)
844 const struct got_error *err = NULL;
845 FILE *f_deriv = NULL;
846 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
847 char *label_deriv = NULL, *parent;
849 *local_changes_subsumed = 0;
851 parent = dirname(ondisk_path);
853 return got_error_from_errno2("dirname", ondisk_path);
856 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
857 err = got_error_from_errno("asprintf");
862 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
865 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
870 err = got_object_id_str(&id_str, deriv_base_commit_id);
873 if (asprintf(&label_deriv, "%s: commit %s",
874 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
875 err = got_error_from_errno("asprintf");
879 err = merge_file(local_changes_subsumed, worktree, blob_orig,
880 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
881 label_deriv, repo, progress_cb, progress_arg);
883 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
884 err = got_error_from_errno("fclose");
886 if (blob_deriv_path) {
887 unlink(blob_deriv_path);
888 free(blob_deriv_path);
895 static const struct got_error *
896 update_blob_fileindex_entry(struct got_worktree *worktree,
897 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
898 const char *ondisk_path, const char *path, struct got_blob_object *blob,
899 int update_timestamps)
901 const struct got_error *err = NULL;
904 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
906 err = got_fileindex_entry_update(ie, ondisk_path,
907 blob->id.sha1, worktree->base_commit_id->sha1,
910 struct got_fileindex_entry *new_ie;
911 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
912 path, blob->id.sha1, worktree->base_commit_id->sha1);
914 err = got_fileindex_entry_add(fileindex, new_ie);
920 get_ondisk_perms(int executable, mode_t st_mode)
922 mode_t xbits = S_IXUSR;
925 /* Map read bits to execute bits. */
926 if (st_mode & S_IRGRP)
928 if (st_mode & S_IROTH)
930 return st_mode | xbits;
933 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
936 static const struct got_error *
937 install_blob(struct got_worktree *worktree, const char *ondisk_path,
938 const char *path, uint16_t te_mode, uint16_t st_mode,
939 struct got_blob_object *blob, int restoring_missing_file,
940 int reverting_versioned_file, struct got_repository *repo,
941 got_worktree_checkout_cb progress_cb, void *progress_arg)
943 const struct got_error *err = NULL;
947 char *tmppath = NULL;
949 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
950 GOT_DEFAULT_FILE_MODE);
952 if (errno == ENOENT) {
953 char *parent = dirname(path);
955 return got_error_from_errno2("dirname", path);
956 err = add_dir_on_disk(worktree, parent);
959 fd = open(ondisk_path,
960 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
961 GOT_DEFAULT_FILE_MODE);
963 return got_error_from_errno2("open",
965 } else if (errno == EEXIST) {
966 if (!S_ISREG(st_mode)) {
967 /* TODO file is obstructed; do something */
968 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
971 err = got_opentemp_named_fd(&tmppath, &fd,
978 return got_error_from_errno2("open", ondisk_path);
981 if (restoring_missing_file)
982 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
983 else if (reverting_versioned_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
986 err = (*progress_cb)(progress_arg,
987 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
991 hdrlen = got_object_blob_get_hdrlen(blob);
993 const uint8_t *buf = got_object_blob_get_read_buf(blob);
994 err = got_object_blob_read_block(&len, blob);
998 /* Skip blob object header first time around. */
999 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1001 err = got_error_from_errno("write");
1003 } else if (outlen != len - hdrlen) {
1004 err = got_error(GOT_ERR_IO);
1011 if (fsync(fd) != 0) {
1012 err = got_error_from_errno("fsync");
1017 if (rename(tmppath, ondisk_path) != 0) {
1018 err = got_error_from_errno3("rename", tmppath,
1025 if (chmod(ondisk_path,
1026 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1027 err = got_error_from_errno2("chmod", ondisk_path);
1032 if (fd != -1 && close(fd) != 0 && err == NULL)
1033 err = got_error_from_errno("close");
1038 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1039 static const struct got_error *
1040 get_modified_file_content_status(unsigned char *status, FILE *f)
1042 const struct got_error *err = NULL;
1043 const char *markers[3] = {
1044 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1045 GOT_DIFF_CONFLICT_MARKER_SEP,
1046 GOT_DIFF_CONFLICT_MARKER_END
1051 const char delim[3] = {'\0', '\0', '\0'};
1053 while (*status == GOT_STATUS_MODIFY) {
1054 line = fparseln(f, &len, NULL, delim, 0);
1058 err = got_ferror(f, GOT_ERR_IO);
1062 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1063 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1065 *status = GOT_STATUS_CONFLICT;
1075 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1077 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1078 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1082 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1084 return !(ie->ctime_sec == sb->st_ctime &&
1085 ie->ctime_nsec == sb->st_ctimensec &&
1086 ie->mtime_sec == sb->st_mtime &&
1087 ie->mtime_nsec == sb->st_mtimensec &&
1088 ie->size == (sb->st_size & 0xffffffff) &&
1089 !xbit_differs(ie, sb->st_mode));
1092 static unsigned char
1093 get_staged_status(struct got_fileindex_entry *ie)
1095 switch (got_fileindex_entry_stage_get(ie)) {
1096 case GOT_FILEIDX_STAGE_ADD:
1097 return GOT_STATUS_ADD;
1098 case GOT_FILEIDX_STAGE_DELETE:
1099 return GOT_STATUS_DELETE;
1100 case GOT_FILEIDX_STAGE_MODIFY:
1101 return GOT_STATUS_MODIFY;
1103 return GOT_STATUS_NO_CHANGE;
1107 static const struct got_error *
1108 get_file_status(unsigned char *status, struct stat *sb,
1109 struct got_fileindex_entry *ie, const char *abspath,
1110 int dirfd, const char *de_name, struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_object_id id;
1118 struct got_blob_object *blob = NULL;
1120 unsigned char staged_status = get_staged_status(ie);
1122 *status = GOT_STATUS_NO_CHANGE;
1125 * Whenever the caller provides a directory descriptor and a
1126 * directory entry name for the file, use them! This prevents
1127 * race conditions if filesystem paths change beneath our feet.
1130 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1131 if (fd == -1 && errno != ENOENT)
1132 return got_error_from_errno2("openat", abspath);
1134 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1135 if (fd == -1 && errno != ENOENT)
1136 return got_error_from_errno2("open", abspath);
1138 if (fd == -1 || fstat(fd, sb) == -1) {
1139 if (errno == ENOENT) {
1140 if (got_fileindex_entry_has_file_on_disk(ie))
1141 *status = GOT_STATUS_MISSING;
1143 *status = GOT_STATUS_DELETE;
1146 err = got_error_from_errno2("fstat", abspath);
1150 if (!S_ISREG(sb->st_mode)) {
1151 *status = GOT_STATUS_OBSTRUCTED;
1155 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1156 *status = GOT_STATUS_DELETE;
1158 } else if (!got_fileindex_entry_has_blob(ie) &&
1159 staged_status != GOT_STATUS_ADD) {
1160 *status = GOT_STATUS_ADD;
1164 if (!stat_info_differs(ie, sb))
1167 if (staged_status == GOT_STATUS_MODIFY ||
1168 staged_status == GOT_STATUS_ADD)
1169 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1171 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1173 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1177 f = fdopen(fd, "r");
1179 err = got_error_from_errno2("fopen", abspath);
1183 hdrlen = got_object_blob_get_hdrlen(blob);
1185 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1186 err = got_object_blob_read_block(&blen, blob);
1189 /* Skip length of blob object header first time around. */
1190 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1191 if (flen == 0 && ferror(f)) {
1192 err = got_error_from_errno("fread");
1197 *status = GOT_STATUS_MODIFY;
1199 } else if (flen == 0) {
1201 *status = GOT_STATUS_MODIFY;
1203 } else if (blen - hdrlen == flen) {
1204 /* Skip blob object header first time around. */
1205 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1206 *status = GOT_STATUS_MODIFY;
1210 *status = GOT_STATUS_MODIFY;
1216 if (*status == GOT_STATUS_MODIFY) {
1218 err = get_modified_file_content_status(status, f);
1219 } else if (xbit_differs(ie, sb->st_mode))
1220 *status = GOT_STATUS_MODE_CHANGE;
1223 got_object_blob_close(blob);
1224 if (f != NULL && fclose(f) == EOF && err == NULL)
1225 err = got_error_from_errno2("fclose", abspath);
1226 if (fd != -1 && close(fd) == -1 && err == NULL)
1227 err = got_error_from_errno2("close", abspath);
1232 * Update timestamps in the file index if a file is unmodified and
1233 * we had to run a full content comparison to find out.
1235 static const struct got_error *
1236 sync_timestamps(char *ondisk_path, unsigned char status,
1237 struct got_fileindex_entry *ie, struct stat *sb)
1239 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1240 return got_fileindex_entry_update(ie, ondisk_path,
1241 ie->blob_sha1, ie->commit_sha1, 1);
1246 static const struct got_error *
1247 update_blob(struct got_worktree *worktree,
1248 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1249 struct got_tree_entry *te, const char *path,
1250 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1253 const struct got_error *err = NULL;
1254 struct got_blob_object *blob = NULL;
1256 unsigned char status = GOT_STATUS_NO_CHANGE;
1259 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1260 return got_error_from_errno("asprintf");
1263 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1264 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1267 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1271 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1272 sb.st_mode = got_fileindex_perms_to_st(ie);
1274 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1276 if (status == GOT_STATUS_OBSTRUCTED) {
1277 err = (*progress_cb)(progress_arg, status, path);
1281 if (ie && status != GOT_STATUS_MISSING &&
1282 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1283 if (got_fileindex_entry_has_commit(ie) &&
1284 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1285 SHA1_DIGEST_LENGTH) == 0) {
1286 err = sync_timestamps(ondisk_path, status, ie, &sb);
1289 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1293 if (got_fileindex_entry_has_blob(ie) &&
1294 memcmp(ie->blob_sha1, te->id.sha1,
1295 SHA1_DIGEST_LENGTH) == 0) {
1296 err = sync_timestamps(ondisk_path, status, ie, &sb);
1301 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1305 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1306 int update_timestamps;
1307 struct got_blob_object *blob2 = NULL;
1308 char *label_orig = NULL;
1309 if (got_fileindex_entry_has_blob(ie)) {
1310 struct got_object_id id2;
1311 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1312 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1316 if (got_fileindex_entry_has_commit(ie)) {
1317 char id_str[SHA1_DIGEST_STRING_LENGTH];
1318 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1319 sizeof(id_str)) == NULL) {
1320 err = got_error_path(id_str,
1321 GOT_ERR_BAD_OBJ_ID_STR);
1324 if (asprintf(&label_orig, "%s: commit %s",
1325 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1326 err = got_error_from_errno("asprintf");
1330 err = merge_blob(&update_timestamps, worktree, blob2,
1331 ondisk_path, path, sb.st_mode, label_orig, blob,
1332 worktree->base_commit_id, repo,
1333 progress_cb, progress_arg);
1336 got_object_blob_close(blob2);
1340 * Do not update timestamps of files with local changes.
1341 * Otherwise, a future status walk would treat them as
1342 * unmodified files again.
1344 err = got_fileindex_entry_update(ie, ondisk_path,
1345 blob->id.sha1, worktree->base_commit_id->sha1,
1347 } else if (status == GOT_STATUS_MODE_CHANGE) {
1348 err = got_fileindex_entry_update(ie, ondisk_path,
1349 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1350 } else if (status == GOT_STATUS_DELETE) {
1351 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1354 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1355 ondisk_path, path, blob, 0);
1359 err = install_blob(worktree, ondisk_path, path, te->mode,
1360 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1361 repo, progress_cb, progress_arg);
1364 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1365 ondisk_path, path, blob, 1);
1369 got_object_blob_close(blob);
1375 static const struct got_error *
1376 remove_ondisk_file(const char *root_path, const char *path)
1378 const struct got_error *err = NULL;
1379 char *ondisk_path = NULL;
1381 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1382 return got_error_from_errno("asprintf");
1384 if (unlink(ondisk_path) == -1) {
1385 if (errno != ENOENT)
1386 err = got_error_from_errno2("unlink", ondisk_path);
1388 char *parent = dirname(ondisk_path);
1389 while (parent && strcmp(parent, root_path) != 0) {
1390 if (rmdir(parent) == -1) {
1391 if (errno != ENOTEMPTY)
1392 err = got_error_from_errno2("rmdir",
1396 parent = dirname(parent);
1403 static const struct got_error *
1404 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1405 struct got_fileindex_entry *ie, struct got_repository *repo,
1406 got_worktree_checkout_cb progress_cb, void *progress_arg)
1408 const struct got_error *err = NULL;
1409 unsigned char status;
1413 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1414 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1416 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1418 return got_error_from_errno("asprintf");
1420 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1424 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1425 status == GOT_STATUS_ADD) {
1426 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1430 * Preserve the working file and change the deleted blob's
1431 * entry into a schedule-add entry.
1433 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1438 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1441 if (status == GOT_STATUS_NO_CHANGE) {
1442 err = remove_ondisk_file(worktree->root_path, ie->path);
1446 got_fileindex_entry_remove(fileindex, ie);
1452 struct diff_cb_arg {
1453 struct got_fileindex *fileindex;
1454 struct got_worktree *worktree;
1455 struct got_repository *repo;
1456 got_worktree_checkout_cb progress_cb;
1458 got_cancel_cb cancel_cb;
1462 static const struct got_error *
1463 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1464 struct got_tree_entry *te, const char *parent_path)
1466 struct diff_cb_arg *a = arg;
1468 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1469 return got_error(GOT_ERR_CANCELLED);
1471 return update_blob(a->worktree, a->fileindex, ie, te,
1472 ie->path, a->repo, a->progress_cb, a->progress_arg);
1475 static const struct got_error *
1476 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1478 struct diff_cb_arg *a = arg;
1480 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1481 return got_error(GOT_ERR_CANCELLED);
1483 return delete_blob(a->worktree, a->fileindex, ie,
1484 a->repo, a->progress_cb, a->progress_arg);
1487 static const struct got_error *
1488 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1490 struct diff_cb_arg *a = arg;
1491 const struct got_error *err;
1494 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1495 return got_error(GOT_ERR_CANCELLED);
1497 if (got_object_tree_entry_is_submodule(te))
1500 if (asprintf(&path, "%s%s%s", parent_path,
1501 parent_path[0] ? "/" : "", te->name)
1503 return got_error_from_errno("asprintf");
1505 if (S_ISDIR(te->mode))
1506 err = add_dir_on_disk(a->worktree, path);
1508 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1509 a->repo, a->progress_cb, a->progress_arg);
1515 static const struct got_error *
1516 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1518 const struct got_error *err = NULL;
1519 char *uuidstr = NULL;
1520 uint32_t uuid_status;
1524 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1525 if (uuid_status != uuid_s_ok)
1526 return got_error_uuid(uuid_status, "uuid_to_string");
1528 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1530 err = got_error_from_errno("asprintf");
1537 const struct got_error *
1538 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1540 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1543 static const struct got_error *
1544 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1546 return get_ref_name(refname, worktree,
1547 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1550 static const struct got_error *
1551 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1553 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1556 static const struct got_error *
1557 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1559 return get_ref_name(refname, worktree,
1560 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1563 static const struct got_error *
1564 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1566 return get_ref_name(refname, worktree,
1567 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1570 static const struct got_error *
1571 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1573 return get_ref_name(refname, worktree,
1574 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1577 static const struct got_error *
1578 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1580 return get_ref_name(refname, worktree,
1581 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1584 static const struct got_error *
1585 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1587 return get_ref_name(refname, worktree,
1588 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1591 static const struct got_error *
1592 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1594 return get_ref_name(refname, worktree,
1595 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1598 const struct got_error *
1599 got_worktree_get_histedit_script_path(char **path,
1600 struct got_worktree *worktree)
1602 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1603 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1605 return got_error_from_errno("asprintf");
1611 * Prevent Git's garbage collector from deleting our base commit by
1612 * setting a reference to our base commit's ID.
1614 static const struct got_error *
1615 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1617 const struct got_error *err = NULL;
1618 struct got_reference *ref = NULL;
1621 err = got_worktree_get_base_ref_name(&refname, worktree);
1625 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1629 err = got_ref_write(ref, repo);
1637 static const struct got_error *
1638 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1640 const struct got_error *err = NULL;
1642 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1643 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1644 err = got_error_from_errno("asprintf");
1645 *fileindex_path = NULL;
1651 static const struct got_error *
1652 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1653 struct got_worktree *worktree)
1655 const struct got_error *err = NULL;
1658 *fileindex_path = NULL;
1659 *fileindex = got_fileindex_alloc();
1660 if (*fileindex == NULL)
1661 return got_error_from_errno("got_fileindex_alloc");
1663 err = get_fileindex_path(fileindex_path, worktree);
1667 index = fopen(*fileindex_path, "rb");
1668 if (index == NULL) {
1669 if (errno != ENOENT)
1670 err = got_error_from_errno2("fopen", *fileindex_path);
1672 err = got_fileindex_read(*fileindex, index);
1673 if (fclose(index) != 0 && err == NULL)
1674 err = got_error_from_errno("fclose");
1678 free(*fileindex_path);
1679 *fileindex_path = NULL;
1680 got_fileindex_free(*fileindex);
1686 struct bump_base_commit_id_arg {
1687 struct got_object_id *base_commit_id;
1690 const char *entry_name;
1691 got_worktree_checkout_cb progress_cb;
1695 /* Bump base commit ID of all files within an updated part of the work tree. */
1696 static const struct got_error *
1697 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1699 const struct got_error *err;
1700 struct bump_base_commit_id_arg *a = arg;
1702 if (a->entry_name) {
1703 if (strcmp(ie->path, a->path) != 0)
1705 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1708 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1709 SHA1_DIGEST_LENGTH) == 0)
1712 if (a->progress_cb) {
1713 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1718 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1722 static const struct got_error *
1723 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1725 const struct got_error *err = NULL;
1726 char *new_fileindex_path = NULL;
1727 FILE *new_index = NULL;
1729 err = got_opentemp_named(&new_fileindex_path, &new_index,
1734 err = got_fileindex_write(fileindex, new_index);
1738 if (rename(new_fileindex_path, fileindex_path) != 0) {
1739 err = got_error_from_errno3("rename", new_fileindex_path,
1741 unlink(new_fileindex_path);
1746 free(new_fileindex_path);
1750 static const struct got_error *
1751 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1752 struct got_object_id **tree_id, const char *wt_relpath,
1753 struct got_worktree *worktree, struct got_repository *repo)
1755 const struct got_error *err = NULL;
1756 struct got_object_id *id = NULL;
1757 char *in_repo_path = NULL;
1758 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1760 *entry_type = GOT_OBJ_TYPE_ANY;
1761 *tree_relpath = NULL;
1764 if (wt_relpath[0] == '\0') {
1765 /* Check out all files within the work tree. */
1766 *entry_type = GOT_OBJ_TYPE_TREE;
1767 *tree_relpath = strdup("");
1768 if (*tree_relpath == NULL) {
1769 err = got_error_from_errno("strdup");
1772 err = got_object_id_by_path(tree_id, repo,
1773 worktree->base_commit_id, worktree->path_prefix);
1779 /* Check out a subset of files in the work tree. */
1781 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1782 is_root_wt ? "" : "/", wt_relpath) == -1) {
1783 err = got_error_from_errno("asprintf");
1787 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1793 in_repo_path = NULL;
1795 err = got_object_get_type(entry_type, repo, id);
1799 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1800 /* Check out a single file. */
1801 if (strchr(wt_relpath, '/') == NULL) {
1802 /* Check out a single file in work tree's root dir. */
1803 in_repo_path = strdup(worktree->path_prefix);
1804 if (in_repo_path == NULL) {
1805 err = got_error_from_errno("strdup");
1808 *tree_relpath = strdup("");
1809 if (*tree_relpath == NULL) {
1810 err = got_error_from_errno("strdup");
1814 /* Check out a single file in a subdirectory. */
1815 err = got_path_dirname(tree_relpath, wt_relpath);
1818 if (asprintf(&in_repo_path, "%s%s%s",
1819 worktree->path_prefix, is_root_wt ? "" : "/",
1820 *tree_relpath) == -1) {
1821 err = got_error_from_errno("asprintf");
1825 err = got_object_id_by_path(tree_id, repo,
1826 worktree->base_commit_id, in_repo_path);
1828 /* Check out all files within a subdirectory. */
1829 *tree_id = got_object_id_dup(id);
1830 if (*tree_id == NULL) {
1831 err = got_error_from_errno("got_object_id_dup");
1834 *tree_relpath = strdup(wt_relpath);
1835 if (*tree_relpath == NULL) {
1836 err = got_error_from_errno("strdup");
1844 *entry_type = GOT_OBJ_TYPE_ANY;
1845 free(*tree_relpath);
1846 *tree_relpath = NULL;
1853 static const struct got_error *
1854 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1855 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1856 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1857 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1859 const struct got_error *err = NULL;
1860 struct got_commit_object *commit = NULL;
1861 struct got_tree_object *tree = NULL;
1862 struct got_fileindex_diff_tree_cb diff_cb;
1863 struct diff_cb_arg arg;
1865 err = ref_base_commit(worktree, repo);
1869 err = got_object_open_as_commit(&commit, repo,
1870 worktree->base_commit_id);
1874 err = got_object_open_as_tree(&tree, repo, tree_id);
1879 got_object_tree_find_entry(tree, entry_name) == NULL) {
1880 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1884 diff_cb.diff_old_new = diff_old_new;
1885 diff_cb.diff_old = diff_old;
1886 diff_cb.diff_new = diff_new;
1887 arg.fileindex = fileindex;
1888 arg.worktree = worktree;
1890 arg.progress_cb = progress_cb;
1891 arg.progress_arg = progress_arg;
1892 arg.cancel_cb = cancel_cb;
1893 arg.cancel_arg = cancel_arg;
1894 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1895 entry_name, repo, &diff_cb, &arg);
1898 got_object_tree_close(tree);
1900 got_object_commit_close(commit);
1904 const struct got_error *
1905 got_worktree_checkout_files(struct got_worktree *worktree,
1906 struct got_pathlist_head *paths, struct got_repository *repo,
1907 got_worktree_checkout_cb progress_cb, void *progress_arg,
1908 got_cancel_cb cancel_cb, void *cancel_arg)
1910 const struct got_error *err = NULL, *sync_err, *unlockerr;
1911 struct got_commit_object *commit = NULL;
1912 struct got_tree_object *tree = NULL;
1913 struct got_fileindex *fileindex = NULL;
1914 char *fileindex_path = NULL;
1915 struct got_pathlist_entry *pe;
1916 struct tree_path_data {
1917 SIMPLEQ_ENTRY(tree_path_data) entry;
1918 struct got_object_id *tree_id;
1923 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1925 SIMPLEQ_INIT(&tree_paths);
1927 err = lock_worktree(worktree, LOCK_EX);
1931 /* Map all specified paths to in-repository trees. */
1932 TAILQ_FOREACH(pe, paths, entry) {
1933 tpd = malloc(sizeof(*tpd));
1935 err = got_error_from_errno("malloc");
1939 err = find_tree_entry_for_checkout(&tpd->entry_type,
1940 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1946 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1947 err = got_path_basename(&tpd->entry_name, pe->path);
1955 tpd->entry_name = NULL;
1957 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1961 * Read the file index.
1962 * Checking out files is supposed to be an idempotent operation.
1963 * If the on-disk file index is incomplete we will try to complete it.
1965 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1969 tpd = SIMPLEQ_FIRST(&tree_paths);
1970 TAILQ_FOREACH(pe, paths, entry) {
1971 struct bump_base_commit_id_arg bbc_arg;
1973 err = checkout_files(worktree, fileindex, tpd->relpath,
1974 tpd->tree_id, tpd->entry_name, repo,
1975 progress_cb, progress_arg, cancel_cb, cancel_arg);
1979 bbc_arg.base_commit_id = worktree->base_commit_id;
1980 bbc_arg.entry_name = tpd->entry_name;
1981 bbc_arg.path = pe->path;
1982 bbc_arg.path_len = pe->path_len;
1983 bbc_arg.progress_cb = progress_cb;
1984 bbc_arg.progress_arg = progress_arg;
1985 err = got_fileindex_for_each_entry_safe(fileindex,
1986 bump_base_commit_id, &bbc_arg);
1990 tpd = SIMPLEQ_NEXT(tpd, entry);
1992 sync_err = sync_fileindex(fileindex, fileindex_path);
1993 if (sync_err && err == NULL)
1996 free(fileindex_path);
1998 got_object_tree_close(tree);
2000 got_object_commit_close(commit);
2002 got_fileindex_free(fileindex);
2003 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2004 tpd = SIMPLEQ_FIRST(&tree_paths);
2005 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2010 unlockerr = lock_worktree(worktree, LOCK_SH);
2011 if (unlockerr && err == NULL)
2016 struct merge_file_cb_arg {
2017 struct got_worktree *worktree;
2018 struct got_fileindex *fileindex;
2019 got_worktree_checkout_cb progress_cb;
2021 got_cancel_cb cancel_cb;
2023 const char *label_orig;
2024 struct got_object_id *commit_id2;
2027 static const struct got_error *
2028 merge_file_cb(void *arg, struct got_blob_object *blob1,
2029 struct got_blob_object *blob2, struct got_object_id *id1,
2030 struct got_object_id *id2, const char *path1, const char *path2,
2031 mode_t mode1, mode_t mode2, struct got_repository *repo)
2033 static const struct got_error *err = NULL;
2034 struct merge_file_cb_arg *a = arg;
2035 struct got_fileindex_entry *ie;
2036 char *ondisk_path = NULL;
2038 unsigned char status;
2039 int local_changes_subsumed;
2041 if (blob1 && blob2) {
2042 ie = got_fileindex_entry_get(a->fileindex, path2,
2045 return (*a->progress_cb)(a->progress_arg,
2046 GOT_STATUS_MISSING, path2);
2048 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2050 return got_error_from_errno("asprintf");
2052 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2057 if (status == GOT_STATUS_DELETE) {
2058 err = (*a->progress_cb)(a->progress_arg,
2059 GOT_STATUS_MERGE, path2);
2062 if (status != GOT_STATUS_NO_CHANGE &&
2063 status != GOT_STATUS_MODIFY &&
2064 status != GOT_STATUS_CONFLICT &&
2065 status != GOT_STATUS_ADD) {
2066 err = (*a->progress_cb)(a->progress_arg, status, path2);
2070 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2071 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2072 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2074 ie = got_fileindex_entry_get(a->fileindex, path1,
2077 return (*a->progress_cb)(a->progress_arg,
2078 GOT_STATUS_MISSING, path2);
2080 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2082 return got_error_from_errno("asprintf");
2084 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2090 case GOT_STATUS_NO_CHANGE:
2091 err = (*a->progress_cb)(a->progress_arg,
2092 GOT_STATUS_DELETE, path1);
2095 err = remove_ondisk_file(a->worktree->root_path, path1);
2099 got_fileindex_entry_mark_deleted_from_disk(ie);
2101 case GOT_STATUS_DELETE:
2102 case GOT_STATUS_MISSING:
2103 err = (*a->progress_cb)(a->progress_arg,
2104 GOT_STATUS_DELETE, path1);
2108 got_fileindex_entry_mark_deleted_from_disk(ie);
2110 case GOT_STATUS_ADD:
2111 case GOT_STATUS_MODIFY:
2112 case GOT_STATUS_CONFLICT:
2113 err = (*a->progress_cb)(a->progress_arg,
2114 GOT_STATUS_CANNOT_DELETE, path1);
2118 case GOT_STATUS_OBSTRUCTED:
2119 err = (*a->progress_cb)(a->progress_arg, status, path1);
2127 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2129 return got_error_from_errno("asprintf");
2130 ie = got_fileindex_entry_get(a->fileindex, path2,
2133 err = get_file_status(&status, &sb, ie, ondisk_path,
2137 if (status != GOT_STATUS_NO_CHANGE &&
2138 status != GOT_STATUS_MODIFY &&
2139 status != GOT_STATUS_CONFLICT &&
2140 status != GOT_STATUS_ADD) {
2141 err = (*a->progress_cb)(a->progress_arg,
2145 err = merge_blob(&local_changes_subsumed, a->worktree,
2146 NULL, ondisk_path, path2, sb.st_mode,
2147 a->label_orig, blob2, a->commit_id2, repo,
2150 if (status == GOT_STATUS_DELETE) {
2151 err = update_blob_fileindex_entry(a->worktree,
2152 a->fileindex, ie, ondisk_path, ie->path,
2158 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2159 err = install_blob(a->worktree, ondisk_path, path2,
2160 /* XXX get this from parent tree! */
2161 GOT_DEFAULT_FILE_MODE,
2162 sb.st_mode, blob2, 0, 0, repo,
2163 a->progress_cb, a->progress_arg);
2166 err = got_fileindex_entry_alloc(&ie,
2167 ondisk_path, path2, NULL, NULL);
2170 err = got_fileindex_entry_add(a->fileindex, ie);
2172 got_fileindex_entry_free(ie);
2182 struct check_merge_ok_arg {
2183 struct got_worktree *worktree;
2184 struct got_repository *repo;
2187 static const struct got_error *
2188 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2190 const struct got_error *err = NULL;
2191 struct check_merge_ok_arg *a = arg;
2192 unsigned char status;
2196 /* Reject merges into a work tree with mixed base commits. */
2197 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2198 SHA1_DIGEST_LENGTH))
2199 return got_error(GOT_ERR_MIXED_COMMITS);
2201 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2203 return got_error_from_errno("asprintf");
2205 /* Reject merges into a work tree with conflicted files. */
2206 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2209 if (status == GOT_STATUS_CONFLICT)
2210 return got_error(GOT_ERR_CONFLICTS);
2215 static const struct got_error *
2216 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2217 const char *fileindex_path, struct got_object_id *commit_id1,
2218 struct got_object_id *commit_id2, struct got_repository *repo,
2219 got_worktree_checkout_cb progress_cb, void *progress_arg,
2220 got_cancel_cb cancel_cb, void *cancel_arg)
2222 const struct got_error *err = NULL, *sync_err;
2223 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2224 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2225 struct merge_file_cb_arg arg;
2226 char *label_orig = NULL;
2231 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2232 worktree->path_prefix);
2236 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2240 err = got_object_id_str(&id_str, commit_id1);
2244 if (asprintf(&label_orig, "%s: commit %s",
2245 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2246 err = got_error_from_errno("asprintf");
2253 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2254 worktree->path_prefix);
2258 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2262 arg.worktree = worktree;
2263 arg.fileindex = fileindex;
2264 arg.progress_cb = progress_cb;
2265 arg.progress_arg = progress_arg;
2266 arg.cancel_cb = cancel_cb;
2267 arg.cancel_arg = cancel_arg;
2268 arg.label_orig = label_orig;
2269 arg.commit_id2 = commit_id2;
2270 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2271 sync_err = sync_fileindex(fileindex, fileindex_path);
2272 if (sync_err && err == NULL)
2276 got_object_tree_close(tree1);
2278 got_object_tree_close(tree2);
2283 const struct got_error *
2284 got_worktree_merge_files(struct got_worktree *worktree,
2285 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2286 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2287 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2289 const struct got_error *err, *unlockerr;
2290 char *fileindex_path = NULL;
2291 struct got_fileindex *fileindex = NULL;
2292 struct check_merge_ok_arg mok_arg;
2294 err = lock_worktree(worktree, LOCK_EX);
2298 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2302 mok_arg.worktree = worktree;
2303 mok_arg.repo = repo;
2304 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2309 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2310 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2313 got_fileindex_free(fileindex);
2314 free(fileindex_path);
2315 unlockerr = lock_worktree(worktree, LOCK_SH);
2316 if (unlockerr && err == NULL)
2321 struct diff_dir_cb_arg {
2322 struct got_fileindex *fileindex;
2323 struct got_worktree *worktree;
2324 const char *status_path;
2325 size_t status_path_len;
2326 struct got_repository *repo;
2327 got_worktree_status_cb status_cb;
2329 got_cancel_cb cancel_cb;
2331 /* A pathlist containing per-directory pathlists of ignore patterns. */
2332 struct got_pathlist_head ignores;
2333 int report_unchanged;
2336 static const struct got_error *
2337 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2338 int dirfd, const char *de_name,
2339 got_worktree_status_cb status_cb, void *status_arg,
2340 struct got_repository *repo, int report_unchanged)
2342 const struct got_error *err = NULL;
2343 unsigned char status = GOT_STATUS_NO_CHANGE;
2344 unsigned char staged_status = get_staged_status(ie);
2346 struct got_object_id blob_id, commit_id, staged_blob_id;
2347 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2348 struct got_object_id *staged_blob_idp = NULL;
2350 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2354 if (status == GOT_STATUS_NO_CHANGE &&
2355 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2358 if (got_fileindex_entry_has_blob(ie)) {
2359 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2360 blob_idp = &blob_id;
2362 if (got_fileindex_entry_has_commit(ie)) {
2363 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2364 commit_idp = &commit_id;
2366 if (staged_status == GOT_STATUS_ADD ||
2367 staged_status == GOT_STATUS_MODIFY) {
2368 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2369 SHA1_DIGEST_LENGTH);
2370 staged_blob_idp = &staged_blob_id;
2373 return (*status_cb)(status_arg, status, staged_status,
2374 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2377 static const struct got_error *
2378 status_old_new(void *arg, struct got_fileindex_entry *ie,
2379 struct dirent *de, const char *parent_path, int dirfd)
2381 const struct got_error *err = NULL;
2382 struct diff_dir_cb_arg *a = arg;
2385 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2386 return got_error(GOT_ERR_CANCELLED);
2388 if (got_path_cmp(parent_path, a->status_path,
2389 strlen(parent_path), a->status_path_len) != 0 &&
2390 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2393 if (parent_path[0]) {
2394 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2395 parent_path, de->d_name) == -1)
2396 return got_error_from_errno("asprintf");
2398 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2400 return got_error_from_errno("asprintf");
2403 err = report_file_status(ie, abspath, dirfd, de->d_name,
2404 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2409 static const struct got_error *
2410 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2412 struct diff_dir_cb_arg *a = arg;
2413 struct got_object_id blob_id, commit_id;
2414 unsigned char status;
2416 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2417 return got_error(GOT_ERR_CANCELLED);
2419 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2422 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2423 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2424 if (got_fileindex_entry_has_file_on_disk(ie))
2425 status = GOT_STATUS_MISSING;
2427 status = GOT_STATUS_DELETE;
2428 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2429 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2433 free_ignorelist(struct got_pathlist_head *ignorelist)
2435 struct got_pathlist_entry *pe;
2437 TAILQ_FOREACH(pe, ignorelist, entry)
2438 free((char *)pe->path);
2439 got_pathlist_free(ignorelist);
2443 free_ignores(struct got_pathlist_head *ignores)
2445 struct got_pathlist_entry *pe;
2447 TAILQ_FOREACH(pe, ignores, entry) {
2448 struct got_pathlist_head *ignorelist = pe->data;
2449 free_ignorelist(ignorelist);
2450 free((char *)pe->path);
2452 got_pathlist_free(ignores);
2455 static const struct got_error *
2456 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2458 const struct got_error *err = NULL;
2459 struct got_pathlist_entry *pe = NULL;
2460 struct got_pathlist_head *ignorelist;
2461 char *line = NULL, *pattern, *dirpath = NULL;
2462 size_t linesize = 0;
2465 ignorelist = calloc(1, sizeof(*ignorelist));
2466 if (ignorelist == NULL)
2467 return got_error_from_errno("calloc");
2468 TAILQ_INIT(ignorelist);
2470 while ((linelen = getline(&line, &linesize, f)) != -1) {
2471 if (linelen > 0 && line[linelen - 1] == '\n')
2472 line[linelen - 1] = '\0';
2474 /* Git's ignores may contain comments. */
2478 /* Git's negated patterns are not (yet?) supported. */
2482 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2484 err = got_error_from_errno("asprintf");
2487 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2492 err = got_error_from_errno("getline");
2496 dirpath = strdup(path);
2497 if (dirpath == NULL) {
2498 err = got_error_from_errno("strdup");
2501 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2504 if (err || pe == NULL) {
2506 free_ignorelist(ignorelist);
2512 match_ignores(struct got_pathlist_head *ignores, const char *path)
2514 struct got_pathlist_entry *pe;
2516 /* Handle patterns which match in all directories. */
2517 TAILQ_FOREACH(pe, ignores, entry) {
2518 struct got_pathlist_head *ignorelist = pe->data;
2519 struct got_pathlist_entry *pi;
2521 TAILQ_FOREACH(pi, ignorelist, entry) {
2522 const char *p, *pattern = pi->path;
2524 if (strncmp(pattern, "**/", 3) != 0)
2529 if (fnmatch(pattern, p,
2530 FNM_PATHNAME | FNM_LEADING_DIR)) {
2531 /* Retry in next directory. */
2532 while (*p && *p != '/')
2544 * The ignores pathlist contains ignore lists from children before
2545 * parents, so we can find the most specific ignorelist by walking
2546 * ignores backwards.
2548 pe = TAILQ_LAST(ignores, got_pathlist_head);
2550 if (got_path_is_child(path, pe->path, pe->path_len)) {
2551 struct got_pathlist_head *ignorelist = pe->data;
2552 struct got_pathlist_entry *pi;
2553 TAILQ_FOREACH(pi, ignorelist, entry) {
2554 const char *pattern = pi->path;
2555 int flags = FNM_LEADING_DIR;
2556 if (strstr(pattern, "/**/") == NULL)
2557 flags |= FNM_PATHNAME;
2558 if (fnmatch(pattern, path, flags))
2563 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2569 static const struct got_error *
2570 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2571 const char *path, const char *ignores_filename)
2573 const struct got_error *err = NULL;
2575 FILE *ignoresfile = NULL;
2577 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2578 path[0] ? "/" : "", ignores_filename) == -1)
2579 return got_error_from_errno("asprintf");
2581 ignoresfile = fopen(ignorespath, "r");
2582 if (ignoresfile == NULL) {
2583 if (errno != ENOENT && errno != EACCES)
2584 err = got_error_from_errno2("fopen",
2587 err = read_ignores(ignores, path, ignoresfile);
2589 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2590 err = got_error_from_errno2("fclose", path);
2595 static const struct got_error *
2596 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2598 const struct got_error *err = NULL;
2599 struct diff_dir_cb_arg *a = arg;
2602 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2603 return got_error(GOT_ERR_CANCELLED);
2605 /* XXX ignore symlinks for now */
2606 if (de->d_type == DT_LNK)
2609 if (parent_path[0]) {
2610 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2611 return got_error_from_errno("asprintf");
2616 if (de->d_type == DT_DIR) {
2617 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2620 err = add_ignores(&a->ignores, a->worktree->root_path,
2621 path, ".gitignore");
2623 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2624 && !match_ignores(&a->ignores, path))
2625 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2626 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2632 static const struct got_error *
2633 report_single_file_status(const char *path, const char *ondisk_path,
2634 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2635 void *status_arg, struct got_repository *repo, int report_unchanged)
2637 struct got_fileindex_entry *ie;
2640 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2642 return report_file_status(ie, ondisk_path, -1, NULL,
2643 status_cb, status_arg, repo, report_unchanged);
2645 if (lstat(ondisk_path, &sb) == -1) {
2646 if (errno != ENOENT)
2647 return got_error_from_errno2("lstat", ondisk_path);
2648 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2649 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2653 if (S_ISREG(sb.st_mode))
2654 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2655 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2660 static const struct got_error *
2661 worktree_status(struct got_worktree *worktree, const char *path,
2662 struct got_fileindex *fileindex, struct got_repository *repo,
2663 got_worktree_status_cb status_cb, void *status_arg,
2664 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2665 int report_unchanged)
2667 const struct got_error *err = NULL;
2669 struct got_fileindex_diff_dir_cb fdiff_cb;
2670 struct diff_dir_cb_arg arg;
2671 char *ondisk_path = NULL;
2673 if (asprintf(&ondisk_path, "%s%s%s",
2674 worktree->root_path, path[0] ? "/" : "", path) == -1)
2675 return got_error_from_errno("asprintf");
2677 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2679 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2680 err = got_error_from_errno2("open", ondisk_path);
2682 err = report_single_file_status(path, ondisk_path,
2683 fileindex, status_cb, status_arg, repo,
2686 fdiff_cb.diff_old_new = status_old_new;
2687 fdiff_cb.diff_old = status_old;
2688 fdiff_cb.diff_new = status_new;
2689 arg.fileindex = fileindex;
2690 arg.worktree = worktree;
2691 arg.status_path = path;
2692 arg.status_path_len = strlen(path);
2694 arg.status_cb = status_cb;
2695 arg.status_arg = status_arg;
2696 arg.cancel_cb = cancel_cb;
2697 arg.cancel_arg = cancel_arg;
2698 arg.report_unchanged = report_unchanged;
2699 TAILQ_INIT(&arg.ignores);
2701 err = add_ignores(&arg.ignores, worktree->root_path,
2702 path, ".cvsignore");
2704 err = add_ignores(&arg.ignores,
2705 worktree->root_path, path, ".gitignore");
2708 err = got_fileindex_diff_dir(fileindex, fd,
2709 worktree->root_path, path, repo, &fdiff_cb, &arg);
2710 free_ignores(&arg.ignores);
2713 if (fd != -1 && close(fd) != 0 && err == NULL)
2714 err = got_error_from_errno("close");
2719 const struct got_error *
2720 got_worktree_status(struct got_worktree *worktree,
2721 struct got_pathlist_head *paths, struct got_repository *repo,
2722 got_worktree_status_cb status_cb, void *status_arg,
2723 got_cancel_cb cancel_cb, void *cancel_arg)
2725 const struct got_error *err = NULL;
2726 char *fileindex_path = NULL;
2727 struct got_fileindex *fileindex = NULL;
2728 struct got_pathlist_entry *pe;
2730 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2734 TAILQ_FOREACH(pe, paths, entry) {
2735 err = worktree_status(worktree, pe->path, fileindex, repo,
2736 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2740 free(fileindex_path);
2741 got_fileindex_free(fileindex);
2745 const struct got_error *
2746 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2749 const struct got_error *err = NULL;
2750 char *resolved, *cwd = NULL, *path = NULL;
2755 resolved = realpath(arg, NULL);
2756 if (resolved == NULL) {
2757 if (errno != ENOENT)
2758 return got_error_from_errno2("realpath", arg);
2759 cwd = getcwd(NULL, 0);
2761 return got_error_from_errno("getcwd");
2762 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2763 err = got_error_from_errno("asprintf");
2768 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2769 strlen(got_worktree_get_root_path(worktree)))) {
2770 err = got_error(GOT_ERR_BAD_PATH);
2774 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2775 err = got_path_skip_common_ancestor(&path,
2776 got_worktree_get_root_path(worktree), resolved);
2782 err = got_error_from_errno("strdup");
2787 /* XXX status walk can't deal with trailing slash! */
2789 while (len > 0 && path[len - 1] == '/') {
2790 path[len - 1] = '\0';
2803 struct schedule_addition_args {
2804 struct got_worktree *worktree;
2805 struct got_fileindex *fileindex;
2806 got_worktree_checkout_cb progress_cb;
2808 struct got_repository *repo;
2811 static const struct got_error *
2812 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2813 const char *relpath, struct got_object_id *blob_id,
2814 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2815 int dirfd, const char *de_name)
2817 struct schedule_addition_args *a = arg;
2818 const struct got_error *err = NULL;
2819 struct got_fileindex_entry *ie;
2823 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2825 return got_error_from_errno("asprintf");
2827 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2829 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2833 /* Re-adding an existing entry is a no-op. */
2834 if (status == GOT_STATUS_ADD)
2836 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2841 if (status != GOT_STATUS_UNVERSIONED) {
2842 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2846 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2850 err = got_fileindex_entry_add(a->fileindex, ie);
2852 got_fileindex_entry_free(ie);
2859 if (status == GOT_STATUS_ADD)
2861 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2864 const struct got_error *
2865 got_worktree_schedule_add(struct got_worktree *worktree,
2866 struct got_pathlist_head *paths,
2867 got_worktree_checkout_cb progress_cb, void *progress_arg,
2868 struct got_repository *repo, int no_ignores)
2870 struct got_fileindex *fileindex = NULL;
2871 char *fileindex_path = NULL;
2872 const struct got_error *err = NULL, *sync_err, *unlockerr;
2873 struct got_pathlist_entry *pe;
2874 struct schedule_addition_args saa;
2876 err = lock_worktree(worktree, LOCK_EX);
2880 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2884 saa.worktree = worktree;
2885 saa.fileindex = fileindex;
2886 saa.progress_cb = progress_cb;
2887 saa.progress_arg = progress_arg;
2890 TAILQ_FOREACH(pe, paths, entry) {
2891 err = worktree_status(worktree, pe->path, fileindex, repo,
2892 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2896 sync_err = sync_fileindex(fileindex, fileindex_path);
2897 if (sync_err && err == NULL)
2900 free(fileindex_path);
2902 got_fileindex_free(fileindex);
2903 unlockerr = lock_worktree(worktree, LOCK_SH);
2904 if (unlockerr && err == NULL)
2909 struct schedule_deletion_args {
2910 struct got_worktree *worktree;
2911 struct got_fileindex *fileindex;
2912 got_worktree_delete_cb progress_cb;
2914 struct got_repository *repo;
2915 int delete_local_mods;
2918 static const struct got_error *
2919 schedule_for_deletion(void *arg, unsigned char status,
2920 unsigned char staged_status, const char *relpath,
2921 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2922 struct got_object_id *commit_id, int dirfd, const char *de_name)
2924 struct schedule_deletion_args *a = arg;
2925 const struct got_error *err = NULL;
2926 struct got_fileindex_entry *ie = NULL;
2930 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2932 return got_error(GOT_ERR_BAD_PATH);
2934 staged_status = get_staged_status(ie);
2935 if (staged_status != GOT_STATUS_NO_CHANGE) {
2936 if (staged_status == GOT_STATUS_DELETE)
2938 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2941 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2943 return got_error_from_errno("asprintf");
2945 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
2950 if (status != GOT_STATUS_NO_CHANGE) {
2951 if (status == GOT_STATUS_DELETE)
2953 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
2954 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2957 if (status != GOT_STATUS_MODIFY &&
2958 status != GOT_STATUS_MISSING) {
2959 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2964 if (status != GOT_STATUS_MISSING) {
2966 if (unlinkat(dirfd, de_name, 0) != 0) {
2967 err = got_error_from_errno2("unlinkat",
2971 } else if (unlink(ondisk_path) != 0) {
2972 err = got_error_from_errno2("unlink", ondisk_path);
2977 got_fileindex_entry_mark_deleted_from_disk(ie);
2982 if (status == GOT_STATUS_DELETE)
2984 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
2985 staged_status, relpath);
2988 const struct got_error *
2989 got_worktree_schedule_delete(struct got_worktree *worktree,
2990 struct got_pathlist_head *paths, int delete_local_mods,
2991 got_worktree_delete_cb progress_cb, void *progress_arg,
2992 struct got_repository *repo)
2994 struct got_fileindex *fileindex = NULL;
2995 char *fileindex_path = NULL;
2996 const struct got_error *err = NULL, *sync_err, *unlockerr;
2997 struct got_pathlist_entry *pe;
2998 struct schedule_deletion_args sda;
3000 err = lock_worktree(worktree, LOCK_EX);
3004 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3008 sda.worktree = worktree;
3009 sda.fileindex = fileindex;
3010 sda.progress_cb = progress_cb;
3011 sda.progress_arg = progress_arg;
3013 sda.delete_local_mods = delete_local_mods;
3015 TAILQ_FOREACH(pe, paths, entry) {
3016 err = worktree_status(worktree, pe->path, fileindex, repo,
3017 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3021 sync_err = sync_fileindex(fileindex, fileindex_path);
3022 if (sync_err && err == NULL)
3025 free(fileindex_path);
3027 got_fileindex_free(fileindex);
3028 unlockerr = lock_worktree(worktree, LOCK_SH);
3029 if (unlockerr && err == NULL)
3034 static const struct got_error *
3035 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3037 const struct got_error *err = NULL;
3039 size_t linesize = 0, n;
3042 linelen = getline(&line, &linesize, infile);
3043 if (linelen == -1) {
3044 if (ferror(infile)) {
3045 err = got_error_from_errno("getline");
3051 n = fwrite(line, 1, linelen, outfile);
3053 err = got_ferror(outfile, GOT_ERR_IO);
3058 n = fwrite(line, 1, linelen, rejectfile);
3060 err = got_ferror(outfile, GOT_ERR_IO);
3067 static const struct got_error *
3068 skip_one_line(FILE *f)
3071 size_t linesize = 0;
3074 linelen = getline(&line, &linesize, f);
3075 if (linelen == -1) {
3077 return got_error_from_errno("getline");
3084 static const struct got_error *
3085 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3086 int start_old, int end_old, int start_new, int end_new,
3087 FILE *outfile, FILE *rejectfile)
3089 const struct got_error *err;
3091 /* Copy old file's lines leading up to patch. */
3092 while (!feof(f1) && *line_cur1 < start_old) {
3093 err = copy_one_line(f1, outfile, NULL);
3098 /* Skip new file's lines leading up to patch. */
3099 while (!feof(f2) && *line_cur2 < start_new) {
3101 err = copy_one_line(f2, NULL, rejectfile);
3103 err = skip_one_line(f2);
3108 /* Copy patched lines. */
3109 while (!feof(f2) && *line_cur2 <= end_new) {
3110 err = copy_one_line(f2, outfile, NULL);
3115 /* Skip over old file's replaced lines. */
3116 while (!feof(f1) && *line_cur1 <= end_old) {
3118 err = copy_one_line(f1, NULL, rejectfile);
3120 err = skip_one_line(f1);
3129 static const struct got_error *
3130 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3131 FILE *outfile, FILE *rejectfile)
3133 const struct got_error *err;
3136 /* Copy old file's lines until EOF. */
3138 err = copy_one_line(f1, outfile, NULL);
3145 /* Copy new file's lines until EOF. */
3147 err = copy_one_line(f2, NULL, rejectfile);
3157 static const struct got_error *
3158 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3159 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3160 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3161 int *line_cur2, FILE *outfile, FILE *rejectfile,
3162 got_worktree_patch_cb patch_cb, void *patch_arg)
3164 const struct got_error *err = NULL;
3165 int start_old = change->cv.a;
3166 int end_old = change->cv.b;
3167 int start_new = change->cv.c;
3168 int end_new = change->cv.d;
3172 *choice = GOT_PATCH_CHOICE_NONE;
3174 hunkfile = got_opentemp();
3175 if (hunkfile == NULL)
3176 return got_error_from_errno("got_opentemp");
3181 /* XXX TODO needs error checking */
3182 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3184 if (fseek(f1, pos1, SEEK_SET) == -1) {
3185 err = got_ferror(f1, GOT_ERR_IO);
3188 if (fseek(f2, pos2, SEEK_SET) == -1) {
3189 err = got_ferror(f1, GOT_ERR_IO);
3192 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3193 err = got_ferror(hunkfile, GOT_ERR_IO);
3197 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3198 hunkfile, n, nchanges);
3203 case GOT_PATCH_CHOICE_YES:
3204 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3205 end_old, start_new, end_new, outfile, rejectfile);
3207 case GOT_PATCH_CHOICE_NO:
3208 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3209 end_old, start_new, end_new, rejectfile, outfile);
3211 case GOT_PATCH_CHOICE_QUIT:
3214 err = got_error(GOT_ERR_PATCH_CHOICE);
3218 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3219 err = got_error_from_errno("fclose");
3223 struct revert_file_args {
3224 struct got_worktree *worktree;
3225 struct got_fileindex *fileindex;
3226 got_worktree_checkout_cb progress_cb;
3228 got_worktree_patch_cb patch_cb;
3230 struct got_repository *repo;
3233 static const struct got_error *
3234 create_patched_content(char **path_outfile, int reverse_patch,
3235 struct got_object_id *blob_id, const char *path2,
3236 int dirfd2, const char *de_name2,
3237 const char *relpath, struct got_repository *repo,
3238 got_worktree_patch_cb patch_cb, void *patch_arg)
3240 const struct got_error *err;
3241 struct got_blob_object *blob = NULL;
3242 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3244 char *path1 = NULL, *id_str = NULL;
3245 struct stat sb1, sb2;
3246 struct got_diff_changes *changes = NULL;
3247 struct got_diff_state *ds = NULL;
3248 struct got_diff_args *args = NULL;
3249 struct got_diff_change *change;
3250 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3253 *path_outfile = NULL;
3255 err = got_object_id_str(&id_str, blob_id);
3260 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3262 err = got_error_from_errno2("openat", path2);
3266 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3268 err = got_error_from_errno2("open", path2);
3272 if (fstat(fd2, &sb2) == -1) {
3273 err = got_error_from_errno2("fstat", path2);
3277 f2 = fdopen(fd2, "r");
3279 err = got_error_from_errno2("fopen", path2);
3284 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3288 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3292 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3296 if (stat(path1, &sb1) == -1) {
3297 err = got_error_from_errno2("stat", path1);
3301 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3302 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3306 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3310 if (fseek(f1, 0L, SEEK_SET) == -1)
3311 return got_ferror(f1, GOT_ERR_IO);
3312 if (fseek(f2, 0L, SEEK_SET) == -1)
3313 return got_ferror(f2, GOT_ERR_IO);
3314 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3316 err = apply_or_reject_change(&choice, change, ++n,
3317 changes->nchanges, ds, args, diff_flags, relpath,
3318 f1, f2, &line_cur1, &line_cur2,
3319 reverse_patch ? NULL : outfile,
3320 reverse_patch ? outfile : NULL,
3321 patch_cb, patch_arg);
3324 if (choice == GOT_PATCH_CHOICE_YES)
3326 else if (choice == GOT_PATCH_CHOICE_QUIT)
3330 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3331 reverse_patch ? NULL : outfile,
3332 reverse_patch ? outfile : NULL);
3336 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3337 err = got_error_from_errno2("chmod", path2);
3344 got_object_blob_close(blob);
3345 if (f1 && fclose(f1) == EOF && err == NULL)
3346 err = got_error_from_errno2("fclose", path1);
3347 if (f2 && fclose(f2) == EOF && err == NULL)
3348 err = got_error_from_errno2("fclose", path2);
3349 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3350 err = got_error_from_errno2("close", path2);
3351 if (outfile && fclose(outfile) == EOF && err == NULL)
3352 err = got_error_from_errno2("fclose", *path_outfile);
3353 if (path1 && unlink(path1) == -1 && err == NULL)
3354 err = got_error_from_errno2("unlink", path1);
3355 if (err || !have_content) {
3356 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3357 err = got_error_from_errno2("unlink", *path_outfile);
3358 free(*path_outfile);
3359 *path_outfile = NULL;
3363 got_diff_state_free(ds);
3367 got_diff_free_changes(changes);
3372 static const struct got_error *
3373 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3374 const char *relpath, struct got_object_id *blob_id,
3375 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3376 int dirfd, const char *de_name)
3378 struct revert_file_args *a = arg;
3379 const struct got_error *err = NULL;
3380 char *parent_path = NULL;
3381 struct got_fileindex_entry *ie;
3382 struct got_tree_object *tree = NULL;
3383 struct got_object_id *tree_id = NULL;
3384 const struct got_tree_entry *te = NULL;
3385 char *tree_path = NULL, *te_name;
3386 char *ondisk_path = NULL, *path_content = NULL;
3387 struct got_blob_object *blob = NULL;
3389 /* Reverting a staged deletion is a no-op. */
3390 if (status == GOT_STATUS_DELETE &&
3391 staged_status != GOT_STATUS_NO_CHANGE)
3394 if (status == GOT_STATUS_UNVERSIONED)
3395 return (*a->progress_cb)(a->progress_arg,
3396 GOT_STATUS_UNVERSIONED, relpath);
3398 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3400 return got_error(GOT_ERR_BAD_PATH);
3402 /* Construct in-repository path of tree which contains this blob. */
3403 err = got_path_dirname(&parent_path, ie->path);
3405 if (err->code != GOT_ERR_BAD_PATH)
3407 parent_path = strdup("/");
3408 if (parent_path == NULL) {
3409 err = got_error_from_errno("strdup");
3413 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3414 tree_path = strdup(parent_path);
3415 if (tree_path == NULL) {
3416 err = got_error_from_errno("strdup");
3420 if (got_path_is_root_dir(parent_path)) {
3421 tree_path = strdup(a->worktree->path_prefix);
3422 if (tree_path == NULL) {
3423 err = got_error_from_errno("strdup");
3427 if (asprintf(&tree_path, "%s/%s",
3428 a->worktree->path_prefix, parent_path) == -1) {
3429 err = got_error_from_errno("asprintf");
3435 err = got_object_id_by_path(&tree_id, a->repo,
3436 a->worktree->base_commit_id, tree_path);
3438 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3439 (status == GOT_STATUS_ADD ||
3440 staged_status == GOT_STATUS_ADD)))
3443 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3447 te_name = basename(ie->path);
3448 if (te_name == NULL) {
3449 err = got_error_from_errno2("basename", ie->path);
3453 te = got_object_tree_find_entry(tree, te_name);
3454 if (te == NULL && status != GOT_STATUS_ADD &&
3455 staged_status != GOT_STATUS_ADD) {
3456 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3462 case GOT_STATUS_ADD:
3464 int choice = GOT_PATCH_CHOICE_NONE;
3465 err = (*a->patch_cb)(&choice, a->patch_arg,
3466 status, ie->path, NULL, 1, 1);
3469 if (choice != GOT_PATCH_CHOICE_YES)
3472 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3476 got_fileindex_entry_remove(a->fileindex, ie);
3478 case GOT_STATUS_DELETE:
3480 int choice = GOT_PATCH_CHOICE_NONE;
3481 err = (*a->patch_cb)(&choice, a->patch_arg,
3482 status, ie->path, NULL, 1, 1);
3485 if (choice != GOT_PATCH_CHOICE_YES)
3489 case GOT_STATUS_MODIFY:
3490 case GOT_STATUS_MODE_CHANGE:
3491 case GOT_STATUS_CONFLICT:
3492 case GOT_STATUS_MISSING: {
3493 struct got_object_id id;
3494 if (staged_status == GOT_STATUS_ADD ||
3495 staged_status == GOT_STATUS_MODIFY) {
3496 memcpy(id.sha1, ie->staged_blob_sha1,
3497 SHA1_DIGEST_LENGTH);
3499 memcpy(id.sha1, ie->blob_sha1,
3500 SHA1_DIGEST_LENGTH);
3501 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3505 if (asprintf(&ondisk_path, "%s/%s",
3506 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3507 err = got_error_from_errno("asprintf");
3511 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3512 status == GOT_STATUS_CONFLICT)) {
3513 err = create_patched_content(&path_content, 1, &id,
3514 ondisk_path, dirfd, de_name, ie->path, a->repo,
3515 a->patch_cb, a->patch_arg);
3516 if (err || path_content == NULL)
3518 if (rename(path_content, ondisk_path) == -1) {
3519 err = got_error_from_errno3("rename",
3520 path_content, ondisk_path);
3524 err = install_blob(a->worktree, ondisk_path, ie->path,
3525 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3526 got_fileindex_perms_to_st(ie), blob, 0, 1,
3527 a->repo, a->progress_cb, a->progress_arg);
3530 if (status == GOT_STATUS_DELETE ||
3531 status == GOT_STATUS_MODE_CHANGE) {
3532 err = update_blob_fileindex_entry(a->worktree,
3533 a->fileindex, ie, ondisk_path, ie->path,
3550 got_object_blob_close(blob);
3552 got_object_tree_close(tree);
3557 const struct got_error *
3558 got_worktree_revert(struct got_worktree *worktree,
3559 struct got_pathlist_head *paths,
3560 got_worktree_checkout_cb progress_cb, void *progress_arg,
3561 got_worktree_patch_cb patch_cb, void *patch_arg,
3562 struct got_repository *repo)
3564 struct got_fileindex *fileindex = NULL;
3565 char *fileindex_path = NULL;
3566 const struct got_error *err = NULL, *unlockerr = NULL;
3567 const struct got_error *sync_err = NULL;
3568 struct got_pathlist_entry *pe;
3569 struct revert_file_args rfa;
3571 err = lock_worktree(worktree, LOCK_EX);
3575 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3579 rfa.worktree = worktree;
3580 rfa.fileindex = fileindex;
3581 rfa.progress_cb = progress_cb;
3582 rfa.progress_arg = progress_arg;
3583 rfa.patch_cb = patch_cb;
3584 rfa.patch_arg = patch_arg;
3586 TAILQ_FOREACH(pe, paths, entry) {
3587 err = worktree_status(worktree, pe->path, fileindex, repo,
3588 revert_file, &rfa, NULL, NULL, 0, 0);
3592 sync_err = sync_fileindex(fileindex, fileindex_path);
3593 if (sync_err && err == NULL)
3596 free(fileindex_path);
3598 got_fileindex_free(fileindex);
3599 unlockerr = lock_worktree(worktree, LOCK_SH);
3600 if (unlockerr && err == NULL)
3606 free_commitable(struct got_commitable *ct)
3609 free(ct->in_repo_path);
3610 free(ct->ondisk_path);
3612 free(ct->base_blob_id);
3613 free(ct->staged_blob_id);
3614 free(ct->base_commit_id);
3618 struct collect_commitables_arg {
3619 struct got_pathlist_head *commitable_paths;
3620 struct got_repository *repo;
3621 struct got_worktree *worktree;
3622 int have_staged_files;
3625 static const struct got_error *
3626 collect_commitables(void *arg, unsigned char status,
3627 unsigned char staged_status, const char *relpath,
3628 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3629 struct got_object_id *commit_id, int dirfd, const char *de_name)
3631 struct collect_commitables_arg *a = arg;
3632 const struct got_error *err = NULL;
3633 struct got_commitable *ct = NULL;
3634 struct got_pathlist_entry *new = NULL;
3635 char *parent_path = NULL, *path = NULL;
3638 if (a->have_staged_files) {
3639 if (staged_status != GOT_STATUS_MODIFY &&
3640 staged_status != GOT_STATUS_ADD &&
3641 staged_status != GOT_STATUS_DELETE)
3644 if (status == GOT_STATUS_CONFLICT)
3645 return got_error(GOT_ERR_COMMIT_CONFLICT);
3647 if (status != GOT_STATUS_MODIFY &&
3648 status != GOT_STATUS_MODE_CHANGE &&
3649 status != GOT_STATUS_ADD &&
3650 status != GOT_STATUS_DELETE)
3654 if (asprintf(&path, "/%s", relpath) == -1) {
3655 err = got_error_from_errno("asprintf");
3658 if (strcmp(path, "/") == 0) {
3659 parent_path = strdup("");
3660 if (parent_path == NULL)
3661 return got_error_from_errno("strdup");
3663 err = got_path_dirname(&parent_path, path);
3668 ct = calloc(1, sizeof(*ct));
3670 err = got_error_from_errno("calloc");
3674 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3676 err = got_error_from_errno("asprintf");
3679 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3680 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3683 if (fstatat(dirfd, de_name, &sb,
3684 AT_SYMLINK_NOFOLLOW) == -1) {
3685 err = got_error_from_errno2("lstat",
3689 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3690 err = got_error_from_errno2("lstat", ct->ondisk_path);
3693 ct->mode = sb.st_mode;
3696 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3697 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3699 err = got_error_from_errno("asprintf");
3703 ct->status = status;
3704 ct->staged_status = staged_status;
3705 ct->blob_id = NULL; /* will be filled in when blob gets created */
3706 if (ct->status != GOT_STATUS_ADD &&
3707 ct->staged_status != GOT_STATUS_ADD) {
3708 ct->base_blob_id = got_object_id_dup(blob_id);
3709 if (ct->base_blob_id == NULL) {
3710 err = got_error_from_errno("got_object_id_dup");
3713 ct->base_commit_id = got_object_id_dup(commit_id);
3714 if (ct->base_commit_id == NULL) {
3715 err = got_error_from_errno("got_object_id_dup");
3719 if (ct->staged_status == GOT_STATUS_ADD ||
3720 ct->staged_status == GOT_STATUS_MODIFY) {
3721 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3722 if (ct->staged_blob_id == NULL) {
3723 err = got_error_from_errno("got_object_id_dup");
3727 ct->path = strdup(path);
3728 if (ct->path == NULL) {
3729 err = got_error_from_errno("strdup");
3732 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3734 if (ct && (err || new == NULL))
3735 free_commitable(ct);
3741 static const struct got_error *write_tree(struct got_object_id **,
3742 struct got_tree_object *, const char *, struct got_pathlist_head *,
3743 got_worktree_status_cb status_cb, void *status_arg,
3744 struct got_repository *);
3746 static const struct got_error *
3747 write_subtree(struct got_object_id **new_subtree_id,
3748 struct got_tree_entry *te, const char *parent_path,
3749 struct got_pathlist_head *commitable_paths,
3750 got_worktree_status_cb status_cb, void *status_arg,
3751 struct got_repository *repo)
3753 const struct got_error *err = NULL;
3754 struct got_tree_object *subtree;
3757 if (asprintf(&subpath, "%s%s%s", parent_path,
3758 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3759 return got_error_from_errno("asprintf");
3761 err = got_object_open_as_tree(&subtree, repo, &te->id);
3765 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3766 status_cb, status_arg, repo);
3767 got_object_tree_close(subtree);
3772 static const struct got_error *
3773 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3775 const struct got_error *err = NULL;
3776 char *ct_parent_path = NULL;
3780 if (strchr(ct->in_repo_path, '/') == NULL) {
3781 *match = got_path_is_root_dir(path);
3785 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3788 *match = (strcmp(path, ct_parent_path) == 0);
3789 free(ct_parent_path);
3794 get_ct_file_mode(struct got_commitable *ct)
3796 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3799 static const struct got_error *
3800 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3801 struct got_tree_entry *te, struct got_commitable *ct)
3803 const struct got_error *err = NULL;
3807 err = got_object_tree_entry_dup(new_te, te);
3811 (*new_te)->mode = get_ct_file_mode(ct);
3813 if (ct->staged_status == GOT_STATUS_MODIFY)
3814 memcpy(&(*new_te)->id, ct->staged_blob_id,
3815 sizeof((*new_te)->id));
3817 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3819 if (err && *new_te) {
3826 static const struct got_error *
3827 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3828 struct got_commitable *ct)
3830 const struct got_error *err = NULL;
3835 *new_te = calloc(1, sizeof(**new_te));
3836 if (*new_te == NULL)
3837 return got_error_from_errno("calloc");
3839 ct_name = basename(ct->path);
3840 if (ct_name == NULL) {
3841 err = got_error_from_errno2("basename", ct->path);
3844 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3845 sizeof((*new_te)->name)) {
3846 err = got_error(GOT_ERR_NO_SPACE);
3850 (*new_te)->mode = get_ct_file_mode(ct);
3852 if (ct->staged_status == GOT_STATUS_ADD)
3853 memcpy(&(*new_te)->id, ct->staged_blob_id,
3854 sizeof((*new_te)->id));
3856 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3858 if (err && *new_te) {
3865 static const struct got_error *
3866 insert_tree_entry(struct got_tree_entry *new_te,
3867 struct got_pathlist_head *paths)
3869 const struct got_error *err = NULL;
3870 struct got_pathlist_entry *new_pe;
3872 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3876 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3880 static const struct got_error *
3881 report_ct_status(struct got_commitable *ct,
3882 got_worktree_status_cb status_cb, void *status_arg)
3884 const char *ct_path = ct->path;
3885 unsigned char status;
3887 while (ct_path[0] == '/')
3890 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3891 status = ct->staged_status;
3893 status = ct->status;
3895 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3896 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3899 static const struct got_error *
3900 match_modified_subtree(int *modified, struct got_tree_entry *te,
3901 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3903 const struct got_error *err = NULL;
3904 struct got_pathlist_entry *pe;
3909 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3910 got_path_is_root_dir(base_tree_path) ? "" : "/",
3912 return got_error_from_errno("asprintf");
3914 TAILQ_FOREACH(pe, commitable_paths, entry) {
3915 struct got_commitable *ct = pe->data;
3916 *modified = got_path_is_child(ct->in_repo_path, te_path,
3926 static const struct got_error *
3927 match_deleted_or_modified_ct(struct got_commitable **ctp,
3928 struct got_tree_entry *te, const char *base_tree_path,
3929 struct got_pathlist_head *commitable_paths)
3931 const struct got_error *err = NULL;
3932 struct got_pathlist_entry *pe;
3936 TAILQ_FOREACH(pe, commitable_paths, entry) {
3937 struct got_commitable *ct = pe->data;
3938 char *ct_name = NULL;
3941 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3942 if (ct->status != GOT_STATUS_MODIFY &&
3943 ct->status != GOT_STATUS_MODE_CHANGE &&
3944 ct->status != GOT_STATUS_DELETE)
3947 if (ct->staged_status != GOT_STATUS_MODIFY &&
3948 ct->staged_status != GOT_STATUS_DELETE)
3952 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
3955 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3961 ct_name = basename(pe->path);
3962 if (ct_name == NULL)
3963 return got_error_from_errno2("basename", pe->path);
3965 if (strcmp(te->name, ct_name) != 0)
3975 static const struct got_error *
3976 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3977 const char *child_path, const char *path_base_tree,
3978 struct got_pathlist_head *commitable_paths,
3979 got_worktree_status_cb status_cb, void *status_arg,
3980 struct got_repository *repo)
3982 const struct got_error *err = NULL;
3983 struct got_tree_entry *new_te;
3985 struct got_object_id *id = NULL;
3989 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3990 got_path_is_root_dir(path_base_tree) ? "" : "/",
3992 return got_error_from_errno("asprintf");
3994 new_te = calloc(1, sizeof(*new_te));
3996 return got_error_from_errno("calloc");
3997 new_te->mode = S_IFDIR;
3999 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4000 sizeof(new_te->name)) {
4001 err = got_error(GOT_ERR_NO_SPACE);
4004 err = write_tree(&id, NULL, subtree_path,
4005 commitable_paths, status_cb, status_arg, repo);
4010 memcpy(&new_te->id, id, sizeof(new_te->id));
4019 static const struct got_error *
4020 write_tree(struct got_object_id **new_tree_id,
4021 struct got_tree_object *base_tree, const char *path_base_tree,
4022 struct got_pathlist_head *commitable_paths,
4023 got_worktree_status_cb status_cb, void *status_arg,
4024 struct got_repository *repo)
4026 const struct got_error *err = NULL;
4027 struct got_pathlist_head paths;
4028 struct got_tree_entry *te, *new_te = NULL;
4029 struct got_pathlist_entry *pe;
4034 /* Insert, and recurse into, newly added entries first. */
4035 TAILQ_FOREACH(pe, commitable_paths, entry) {
4036 struct got_commitable *ct = pe->data;
4037 char *child_path = NULL, *slash;
4039 if ((ct->status != GOT_STATUS_ADD &&
4040 ct->staged_status != GOT_STATUS_ADD) ||
4041 (ct->flags & GOT_COMMITABLE_ADDED))
4044 if (!got_path_is_child(pe->path, path_base_tree,
4045 strlen(path_base_tree)))
4048 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4053 slash = strchr(child_path, '/');
4054 if (slash == NULL) {
4055 err = alloc_added_blob_tree_entry(&new_te, ct);
4058 err = report_ct_status(ct, status_cb, status_arg);
4061 ct->flags |= GOT_COMMITABLE_ADDED;
4062 err = insert_tree_entry(new_te, &paths);
4067 *slash = '\0'; /* trim trailing path components */
4068 if (base_tree == NULL ||
4069 got_object_tree_find_entry(base_tree, child_path)
4071 err = make_subtree_for_added_blob(&new_te,
4072 child_path, path_base_tree,
4073 commitable_paths, status_cb, status_arg,
4077 err = insert_tree_entry(new_te, &paths);
4086 int i, nbase_entries;
4087 /* Handle modified and deleted entries. */
4088 nbase_entries = got_object_tree_get_nentries(base_tree);
4089 for (i = 0; i < nbase_entries; i++) {
4090 struct got_commitable *ct = NULL;
4092 te = got_object_tree_get_entry(base_tree, i);
4093 if (got_object_tree_entry_is_submodule(te)) {
4094 /* Entry is a submodule; just copy it. */
4095 err = got_object_tree_entry_dup(&new_te, te);
4098 err = insert_tree_entry(new_te, &paths);
4105 if (S_ISDIR(te->mode)) {
4107 err = got_object_tree_entry_dup(&new_te, te);
4110 err = match_modified_subtree(&modified, te,
4111 path_base_tree, commitable_paths);
4114 /* Avoid recursion into unmodified subtrees. */
4116 struct got_object_id *new_id;
4117 err = write_subtree(&new_id, te,
4118 path_base_tree, commitable_paths,
4119 status_cb, status_arg, repo);
4122 memcpy(&new_te->id, new_id,
4123 sizeof(new_te->id));
4126 err = insert_tree_entry(new_te, &paths);
4133 err = match_deleted_or_modified_ct(&ct, te,
4134 path_base_tree, commitable_paths);
4138 /* NB: Deleted entries get dropped here. */
4139 if (ct->status == GOT_STATUS_MODIFY ||
4140 ct->status == GOT_STATUS_MODE_CHANGE ||
4141 ct->staged_status == GOT_STATUS_MODIFY) {
4142 err = alloc_modified_blob_tree_entry(
4146 err = insert_tree_entry(new_te, &paths);
4151 err = report_ct_status(ct, status_cb,
4156 /* Entry is unchanged; just copy it. */
4157 err = got_object_tree_entry_dup(&new_te, te);
4160 err = insert_tree_entry(new_te, &paths);
4168 /* Write new list of entries; deleted entries have been dropped. */
4169 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4171 got_pathlist_free(&paths);
4175 static const struct got_error *
4176 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4177 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4178 int have_staged_files)
4180 const struct got_error *err = NULL;
4181 struct got_pathlist_entry *pe;
4183 TAILQ_FOREACH(pe, commitable_paths, entry) {
4184 struct got_fileindex_entry *ie;
4185 struct got_commitable *ct = pe->data;
4187 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4189 if (ct->status == GOT_STATUS_DELETE ||
4190 ct->staged_status == GOT_STATUS_DELETE) {
4191 got_fileindex_entry_remove(fileindex, ie);
4192 got_fileindex_entry_free(ie);
4193 } else if (ct->staged_status == GOT_STATUS_ADD ||
4194 ct->staged_status == GOT_STATUS_MODIFY) {
4195 got_fileindex_entry_stage_set(ie,
4196 GOT_FILEIDX_STAGE_NONE);
4197 err = got_fileindex_entry_update(ie,
4198 ct->ondisk_path, ct->staged_blob_id->sha1,
4199 new_base_commit_id->sha1,
4200 !have_staged_files);
4202 err = got_fileindex_entry_update(ie,
4203 ct->ondisk_path, ct->blob_id->sha1,
4204 new_base_commit_id->sha1,
4205 !have_staged_files);
4207 err = got_fileindex_entry_alloc(&ie,
4208 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4209 new_base_commit_id->sha1);
4212 err = got_fileindex_entry_add(fileindex, ie);
4221 static const struct got_error *
4222 check_out_of_date(const char *in_repo_path, unsigned char status,
4223 unsigned char staged_status, struct got_object_id *base_blob_id,
4224 struct got_object_id *base_commit_id,
4225 struct got_object_id *head_commit_id, struct got_repository *repo,
4228 const struct got_error *err = NULL;
4229 struct got_object_id *id = NULL;
4231 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4232 /* Trivial case: base commit == head commit */
4233 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4236 * Ensure file content which local changes were based
4237 * on matches file content in the branch head.
4239 err = got_object_id_by_path(&id, repo, head_commit_id,
4242 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4243 err = got_error(ood_errcode);
4245 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4246 err = got_error(ood_errcode);
4248 /* Require that added files don't exist in the branch head. */
4249 err = got_object_id_by_path(&id, repo, head_commit_id,
4251 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4253 err = id ? got_error(ood_errcode) : NULL;
4260 const struct got_error *
4261 commit_worktree(struct got_object_id **new_commit_id,
4262 struct got_pathlist_head *commitable_paths,
4263 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4264 const char *author, const char *committer,
4265 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4266 got_worktree_status_cb status_cb, void *status_arg,
4267 struct got_repository *repo)
4269 const struct got_error *err = NULL, *unlockerr = NULL;
4270 struct got_pathlist_entry *pe;
4271 const char *head_ref_name = NULL;
4272 struct got_commit_object *head_commit = NULL;
4273 struct got_reference *head_ref2 = NULL;
4274 struct got_object_id *head_commit_id2 = NULL;
4275 struct got_tree_object *head_tree = NULL;
4276 struct got_object_id *new_tree_id = NULL;
4277 struct got_object_id_queue parent_ids;
4278 struct got_object_qid *pid = NULL;
4279 char *logmsg = NULL;
4281 *new_commit_id = NULL;
4283 SIMPLEQ_INIT(&parent_ids);
4285 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4289 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4293 if (commit_msg_cb != NULL) {
4294 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4299 if (logmsg == NULL || strlen(logmsg) == 0) {
4300 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4304 /* Create blobs from added and modified files and record their IDs. */
4305 TAILQ_FOREACH(pe, commitable_paths, entry) {
4306 struct got_commitable *ct = pe->data;
4309 /* Blobs for staged files already exist. */
4310 if (ct->staged_status == GOT_STATUS_ADD ||
4311 ct->staged_status == GOT_STATUS_MODIFY)
4314 if (ct->status != GOT_STATUS_ADD &&
4315 ct->status != GOT_STATUS_MODIFY &&
4316 ct->status != GOT_STATUS_MODE_CHANGE)
4319 if (asprintf(&ondisk_path, "%s/%s",
4320 worktree->root_path, pe->path) == -1) {
4321 err = got_error_from_errno("asprintf");
4324 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4330 /* Recursively write new tree objects. */
4331 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4332 status_cb, status_arg, repo);
4336 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4339 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4340 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4341 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4342 got_object_qid_free(pid);
4348 /* Check if a concurrent commit to our branch has occurred. */
4349 head_ref_name = got_worktree_get_head_ref_name(worktree);
4350 if (head_ref_name == NULL) {
4351 err = got_error_from_errno("got_worktree_get_head_ref_name");
4354 /* Lock the reference here to prevent concurrent modification. */
4355 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4358 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4361 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4362 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4365 /* Update branch head in repository. */
4366 err = got_ref_change_ref(head_ref2, *new_commit_id);
4369 err = got_ref_write(head_ref2, repo);
4373 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4377 err = ref_base_commit(worktree, repo);
4382 got_object_tree_close(head_tree);
4384 got_object_commit_close(head_commit);
4385 free(head_commit_id2);
4387 unlockerr = got_ref_unlock(head_ref2);
4388 if (unlockerr && err == NULL)
4390 got_ref_close(head_ref2);
4395 static const struct got_error *
4396 check_path_is_commitable(const char *path,
4397 struct got_pathlist_head *commitable_paths)
4399 struct got_pathlist_entry *cpe = NULL;
4400 size_t path_len = strlen(path);
4402 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4403 struct got_commitable *ct = cpe->data;
4404 const char *ct_path = ct->path;
4406 while (ct_path[0] == '/')
4409 if (strcmp(path, ct_path) == 0 ||
4410 got_path_is_child(ct_path, path, path_len))
4415 return got_error_path(path, GOT_ERR_BAD_PATH);
4420 static const struct got_error *
4421 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4423 int *have_staged_files = arg;
4425 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4426 *have_staged_files = 1;
4427 return got_error(GOT_ERR_CANCELLED);
4433 static const struct got_error *
4434 check_non_staged_files(struct got_fileindex *fileindex,
4435 struct got_pathlist_head *paths)
4437 struct got_pathlist_entry *pe;
4438 struct got_fileindex_entry *ie;
4440 TAILQ_FOREACH(pe, paths, entry) {
4441 if (pe->path[0] == '\0')
4443 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4445 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4446 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4447 return got_error_path(pe->path,
4448 GOT_ERR_FILE_NOT_STAGED);
4454 const struct got_error *
4455 got_worktree_commit(struct got_object_id **new_commit_id,
4456 struct got_worktree *worktree, struct got_pathlist_head *paths,
4457 const char *author, const char *committer,
4458 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4459 got_worktree_status_cb status_cb, void *status_arg,
4460 struct got_repository *repo)
4462 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4463 struct got_fileindex *fileindex = NULL;
4464 char *fileindex_path = NULL;
4465 struct got_pathlist_head commitable_paths;
4466 struct collect_commitables_arg cc_arg;
4467 struct got_pathlist_entry *pe;
4468 struct got_reference *head_ref = NULL;
4469 struct got_object_id *head_commit_id = NULL;
4470 int have_staged_files = 0;
4472 *new_commit_id = NULL;
4474 TAILQ_INIT(&commitable_paths);
4476 err = lock_worktree(worktree, LOCK_EX);
4480 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4484 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4488 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4492 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4493 &have_staged_files);
4494 if (err && err->code != GOT_ERR_CANCELLED)
4496 if (have_staged_files) {
4497 err = check_non_staged_files(fileindex, paths);
4502 cc_arg.commitable_paths = &commitable_paths;
4503 cc_arg.worktree = worktree;
4505 cc_arg.have_staged_files = have_staged_files;
4506 TAILQ_FOREACH(pe, paths, entry) {
4507 err = worktree_status(worktree, pe->path, fileindex, repo,
4508 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4513 if (TAILQ_EMPTY(&commitable_paths)) {
4514 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4518 TAILQ_FOREACH(pe, paths, entry) {
4519 err = check_path_is_commitable(pe->path, &commitable_paths);
4524 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4525 struct got_commitable *ct = pe->data;
4526 const char *ct_path = ct->in_repo_path;
4528 while (ct_path[0] == '/')
4530 err = check_out_of_date(ct_path, ct->status,
4531 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4532 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4538 err = commit_worktree(new_commit_id, &commitable_paths,
4539 head_commit_id, worktree, author, committer,
4540 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4544 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4545 fileindex, have_staged_files);
4546 sync_err = sync_fileindex(fileindex, fileindex_path);
4547 if (sync_err && err == NULL)
4551 got_fileindex_free(fileindex);
4552 free(fileindex_path);
4553 unlockerr = lock_worktree(worktree, LOCK_SH);
4554 if (unlockerr && err == NULL)
4556 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4557 struct got_commitable *ct = pe->data;
4558 free_commitable(ct);
4560 got_pathlist_free(&commitable_paths);
4565 got_commitable_get_path(struct got_commitable *ct)
4571 got_commitable_get_status(struct got_commitable *ct)
4576 struct check_rebase_ok_arg {
4577 struct got_worktree *worktree;
4578 struct got_repository *repo;
4581 static const struct got_error *
4582 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4584 const struct got_error *err = NULL;
4585 struct check_rebase_ok_arg *a = arg;
4586 unsigned char status;
4590 /* Reject rebase of a work tree with mixed base commits. */
4591 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4592 SHA1_DIGEST_LENGTH))
4593 return got_error(GOT_ERR_MIXED_COMMITS);
4595 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4597 return got_error_from_errno("asprintf");
4599 /* Reject rebase of a work tree with modified or staged files. */
4600 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4605 if (status != GOT_STATUS_NO_CHANGE)
4606 return got_error(GOT_ERR_MODIFIED);
4607 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4608 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4613 const struct got_error *
4614 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4615 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4616 struct got_worktree *worktree, struct got_reference *branch,
4617 struct got_repository *repo)
4619 const struct got_error *err = NULL;
4620 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4621 char *branch_ref_name = NULL;
4622 char *fileindex_path = NULL;
4623 struct check_rebase_ok_arg ok_arg;
4624 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4626 *new_base_branch_ref = NULL;
4630 err = lock_worktree(worktree, LOCK_EX);
4634 err = open_fileindex(fileindex, &fileindex_path, worktree);
4638 ok_arg.worktree = worktree;
4640 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4645 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4649 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4653 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4657 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4662 err = got_ref_alloc_symref(new_base_branch_ref,
4663 new_base_branch_ref_name, wt_branch);
4666 err = got_ref_write(*new_base_branch_ref, repo);
4670 /* TODO Lock original branch's ref while rebasing? */
4672 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4676 err = got_ref_write(branch_ref, repo);
4680 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4681 worktree->base_commit_id);
4684 err = got_ref_write(*tmp_branch, repo);
4688 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4692 free(fileindex_path);
4693 free(tmp_branch_name);
4694 free(new_base_branch_ref_name);
4695 free(branch_ref_name);
4697 got_ref_close(branch_ref);
4699 got_ref_close(wt_branch);
4701 if (*new_base_branch_ref) {
4702 got_ref_close(*new_base_branch_ref);
4703 *new_base_branch_ref = NULL;
4706 got_ref_close(*tmp_branch);
4710 got_fileindex_free(*fileindex);
4713 lock_worktree(worktree, LOCK_SH);
4718 const struct got_error *
4719 got_worktree_rebase_continue(struct got_object_id **commit_id,
4720 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4721 struct got_reference **branch, struct got_fileindex **fileindex,
4722 struct got_worktree *worktree, struct got_repository *repo)
4724 const struct got_error *err;
4725 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4726 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4727 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4728 char *fileindex_path = NULL;
4729 int have_staged_files = 0;
4732 *new_base_branch = NULL;
4737 err = lock_worktree(worktree, LOCK_EX);
4741 err = open_fileindex(fileindex, &fileindex_path, worktree);
4745 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4746 &have_staged_files);
4747 if (err && err->code != GOT_ERR_CANCELLED)
4749 if (have_staged_files) {
4750 err = got_error(GOT_ERR_STAGED_PATHS);
4754 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4758 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4762 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4766 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4770 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4774 err = got_ref_open(branch, repo,
4775 got_ref_get_symref_target(branch_ref), 0);
4779 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4783 err = got_ref_resolve(commit_id, repo, commit_ref);
4787 err = got_ref_open(new_base_branch, repo,
4788 new_base_branch_ref_name, 0);
4792 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4796 free(commit_ref_name);
4797 free(branch_ref_name);
4798 free(fileindex_path);
4800 got_ref_close(commit_ref);
4802 got_ref_close(branch_ref);
4807 got_ref_close(*tmp_branch);
4810 if (*new_base_branch) {
4811 got_ref_close(*new_base_branch);
4812 *new_base_branch = NULL;
4815 got_ref_close(*branch);
4819 got_fileindex_free(*fileindex);
4822 lock_worktree(worktree, LOCK_SH);
4827 const struct got_error *
4828 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4830 const struct got_error *err;
4831 char *tmp_branch_name = NULL;
4833 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4837 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4838 free(tmp_branch_name);
4842 static const struct got_error *
4843 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4844 char **logmsg, void *arg)
4850 static const struct got_error *
4851 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4852 const char *path, struct got_object_id *blob_id,
4853 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4854 int dirfd, const char *de_name)
4859 struct collect_merged_paths_arg {
4860 got_worktree_checkout_cb progress_cb;
4862 struct got_pathlist_head *merged_paths;
4865 static const struct got_error *
4866 collect_merged_paths(void *arg, unsigned char status, const char *path)
4868 const struct got_error *err;
4869 struct collect_merged_paths_arg *a = arg;
4871 struct got_pathlist_entry *new;
4873 err = (*a->progress_cb)(a->progress_arg, status, path);
4877 if (status != GOT_STATUS_MERGE &&
4878 status != GOT_STATUS_ADD &&
4879 status != GOT_STATUS_DELETE &&
4880 status != GOT_STATUS_CONFLICT)
4885 return got_error_from_errno("strdup");
4887 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4888 if (err || new == NULL)
4894 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4896 struct got_pathlist_entry *pe;
4898 TAILQ_FOREACH(pe, merged_paths, entry)
4899 free((char *)pe->path);
4901 got_pathlist_free(merged_paths);
4904 static const struct got_error *
4905 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4906 struct got_repository *repo)
4908 const struct got_error *err;
4909 struct got_reference *commit_ref = NULL;
4911 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4913 if (err->code != GOT_ERR_NOT_REF)
4915 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4918 err = got_ref_write(commit_ref, repo);
4922 struct got_object_id *stored_id;
4925 err = got_ref_resolve(&stored_id, repo, commit_ref);
4928 cmp = got_object_id_cmp(commit_id, stored_id);
4931 err = got_error(GOT_ERR_REBASE_COMMITID);
4937 got_ref_close(commit_ref);
4941 static const struct got_error *
4942 rebase_merge_files(struct got_pathlist_head *merged_paths,
4943 const char *commit_ref_name, struct got_worktree *worktree,
4944 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4945 struct got_object_id *commit_id, struct got_repository *repo,
4946 got_worktree_checkout_cb progress_cb, void *progress_arg,
4947 got_cancel_cb cancel_cb, void *cancel_arg)
4949 const struct got_error *err;
4950 struct got_reference *commit_ref = NULL;
4951 struct collect_merged_paths_arg cmp_arg;
4952 char *fileindex_path;
4954 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4956 err = get_fileindex_path(&fileindex_path, worktree);
4960 cmp_arg.progress_cb = progress_cb;
4961 cmp_arg.progress_arg = progress_arg;
4962 cmp_arg.merged_paths = merged_paths;
4963 err = merge_files(worktree, fileindex, fileindex_path,
4964 parent_commit_id, commit_id, repo, collect_merged_paths,
4965 &cmp_arg, cancel_cb, cancel_arg);
4967 got_ref_close(commit_ref);
4971 const struct got_error *
4972 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4973 struct got_worktree *worktree, struct got_fileindex *fileindex,
4974 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4975 struct got_repository *repo,
4976 got_worktree_checkout_cb progress_cb, void *progress_arg,
4977 got_cancel_cb cancel_cb, void *cancel_arg)
4979 const struct got_error *err;
4980 char *commit_ref_name;
4982 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4986 err = store_commit_id(commit_ref_name, commit_id, repo);
4990 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4991 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4992 progress_arg, cancel_cb, cancel_arg);
4994 free(commit_ref_name);
4998 const struct got_error *
4999 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5000 struct got_worktree *worktree, struct got_fileindex *fileindex,
5001 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5002 struct got_repository *repo,
5003 got_worktree_checkout_cb progress_cb, void *progress_arg,
5004 got_cancel_cb cancel_cb, void *cancel_arg)
5006 const struct got_error *err;
5007 char *commit_ref_name;
5009 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5013 err = store_commit_id(commit_ref_name, commit_id, repo);
5017 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5018 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5019 progress_arg, cancel_cb, cancel_arg);
5021 free(commit_ref_name);
5025 static const struct got_error *
5026 rebase_commit(struct got_object_id **new_commit_id,
5027 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5028 struct got_worktree *worktree, struct got_fileindex *fileindex,
5029 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5030 const char *new_logmsg, struct got_repository *repo)
5032 const struct got_error *err, *sync_err;
5033 struct got_pathlist_head commitable_paths;
5034 struct collect_commitables_arg cc_arg;
5035 char *fileindex_path = NULL;
5036 struct got_reference *head_ref = NULL;
5037 struct got_object_id *head_commit_id = NULL;
5038 char *logmsg = NULL;
5040 TAILQ_INIT(&commitable_paths);
5041 *new_commit_id = NULL;
5043 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5045 err = get_fileindex_path(&fileindex_path, worktree);
5049 cc_arg.commitable_paths = &commitable_paths;
5050 cc_arg.worktree = worktree;
5052 cc_arg.have_staged_files = 0;
5054 * If possible get the status of individual files directly to
5055 * avoid crawling the entire work tree once per rebased commit.
5056 * TODO: Ideally, merged_paths would contain a list of commitables
5057 * we could use so we could skip worktree_status() entirely.
5060 struct got_pathlist_entry *pe;
5061 if (TAILQ_EMPTY(merged_paths)) {
5062 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5065 TAILQ_FOREACH(pe, merged_paths, entry) {
5066 err = worktree_status(worktree, pe->path, fileindex,
5067 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5073 err = worktree_status(worktree, "", fileindex, repo,
5074 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5079 if (TAILQ_EMPTY(&commitable_paths)) {
5080 /* No-op change; commit will be elided. */
5081 err = got_ref_delete(commit_ref, repo);
5084 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5088 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5092 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5097 logmsg = strdup(new_logmsg);
5098 if (logmsg == NULL) {
5099 err = got_error_from_errno("strdup");
5103 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5108 /* NB: commit_worktree will call free(logmsg) */
5109 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5110 worktree, got_object_commit_get_author(orig_commit),
5111 got_object_commit_get_committer(orig_commit),
5112 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5116 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5120 err = got_ref_delete(commit_ref, repo);
5124 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5126 sync_err = sync_fileindex(fileindex, fileindex_path);
5127 if (sync_err && err == NULL)
5130 free(fileindex_path);
5131 free(head_commit_id);
5133 got_ref_close(head_ref);
5135 free(*new_commit_id);
5136 *new_commit_id = NULL;
5141 const struct got_error *
5142 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5143 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5144 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5145 struct got_commit_object *orig_commit,
5146 struct got_object_id *orig_commit_id, struct got_repository *repo)
5148 const struct got_error *err;
5149 char *commit_ref_name;
5150 struct got_reference *commit_ref = NULL;
5151 struct got_object_id *commit_id = NULL;
5153 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5157 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5160 err = got_ref_resolve(&commit_id, repo, commit_ref);
5163 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5164 err = got_error(GOT_ERR_REBASE_COMMITID);
5168 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5169 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5172 got_ref_close(commit_ref);
5173 free(commit_ref_name);
5178 const struct got_error *
5179 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5180 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5181 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5182 struct got_commit_object *orig_commit,
5183 struct got_object_id *orig_commit_id, const char *new_logmsg,
5184 struct got_repository *repo)
5186 const struct got_error *err;
5187 char *commit_ref_name;
5188 struct got_reference *commit_ref = NULL;
5189 struct got_object_id *commit_id = NULL;
5191 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5195 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5198 err = got_ref_resolve(&commit_id, repo, commit_ref);
5201 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5202 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5206 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5207 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5210 got_ref_close(commit_ref);
5211 free(commit_ref_name);
5216 const struct got_error *
5217 got_worktree_rebase_postpone(struct got_worktree *worktree,
5218 struct got_fileindex *fileindex)
5221 got_fileindex_free(fileindex);
5222 return lock_worktree(worktree, LOCK_SH);
5225 static const struct got_error *
5226 delete_ref(const char *name, struct got_repository *repo)
5228 const struct got_error *err;
5229 struct got_reference *ref;
5231 err = got_ref_open(&ref, repo, name, 0);
5233 if (err->code == GOT_ERR_NOT_REF)
5238 err = got_ref_delete(ref, repo);
5243 static const struct got_error *
5244 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5246 const struct got_error *err;
5247 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5248 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5250 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5253 err = delete_ref(tmp_branch_name, repo);
5257 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5260 err = delete_ref(new_base_branch_ref_name, repo);
5264 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5267 err = delete_ref(branch_ref_name, repo);
5271 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5274 err = delete_ref(commit_ref_name, repo);
5279 free(tmp_branch_name);
5280 free(new_base_branch_ref_name);
5281 free(branch_ref_name);
5282 free(commit_ref_name);
5286 const struct got_error *
5287 got_worktree_rebase_complete(struct got_worktree *worktree,
5288 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5289 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5290 struct got_repository *repo)
5292 const struct got_error *err, *unlockerr;
5293 struct got_object_id *new_head_commit_id = NULL;
5295 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5299 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5303 err = got_ref_write(rebased_branch, repo);
5307 err = got_worktree_set_head_ref(worktree, rebased_branch);
5311 err = delete_rebase_refs(worktree, repo);
5314 got_fileindex_free(fileindex);
5315 free(new_head_commit_id);
5316 unlockerr = lock_worktree(worktree, LOCK_SH);
5317 if (unlockerr && err == NULL)
5322 const struct got_error *
5323 got_worktree_rebase_abort(struct got_worktree *worktree,
5324 struct got_fileindex *fileindex, struct got_repository *repo,
5325 struct got_reference *new_base_branch,
5326 got_worktree_checkout_cb progress_cb, void *progress_arg)
5328 const struct got_error *err, *unlockerr, *sync_err;
5329 struct got_reference *resolved = NULL;
5330 struct got_object_id *commit_id = NULL;
5331 char *fileindex_path = NULL;
5332 struct revert_file_args rfa;
5333 struct got_object_id *tree_id = NULL;
5335 err = lock_worktree(worktree, LOCK_EX);
5339 err = got_ref_open(&resolved, repo,
5340 got_ref_get_symref_target(new_base_branch), 0);
5344 err = got_worktree_set_head_ref(worktree, resolved);
5349 * XXX commits to the base branch could have happened while
5350 * we were busy rebasing; should we store the original commit ID
5351 * when rebase begins and read it back here?
5353 err = got_ref_resolve(&commit_id, repo, resolved);
5357 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5361 err = got_object_id_by_path(&tree_id, repo,
5362 worktree->base_commit_id, worktree->path_prefix);
5366 err = delete_rebase_refs(worktree, repo);
5370 err = get_fileindex_path(&fileindex_path, worktree);
5374 rfa.worktree = worktree;
5375 rfa.fileindex = fileindex;
5376 rfa.progress_cb = progress_cb;
5377 rfa.progress_arg = progress_arg;
5378 rfa.patch_cb = NULL;
5379 rfa.patch_arg = NULL;
5381 err = worktree_status(worktree, "", fileindex, repo,
5382 revert_file, &rfa, NULL, NULL, 0, 0);
5386 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5387 repo, progress_cb, progress_arg, NULL, NULL);
5389 sync_err = sync_fileindex(fileindex, fileindex_path);
5390 if (sync_err && err == NULL)
5393 got_ref_close(resolved);
5397 got_fileindex_free(fileindex);
5398 free(fileindex_path);
5400 unlockerr = lock_worktree(worktree, LOCK_SH);
5401 if (unlockerr && err == NULL)
5406 const struct got_error *
5407 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5408 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5409 struct got_fileindex **fileindex, struct got_worktree *worktree,
5410 struct got_repository *repo)
5412 const struct got_error *err = NULL;
5413 char *tmp_branch_name = NULL;
5414 char *branch_ref_name = NULL;
5415 char *base_commit_ref_name = NULL;
5416 char *fileindex_path = NULL;
5417 struct check_rebase_ok_arg ok_arg;
5418 struct got_reference *wt_branch = NULL;
5419 struct got_reference *base_commit_ref = NULL;
5423 *base_commit_id = NULL;
5426 err = lock_worktree(worktree, LOCK_EX);
5430 err = open_fileindex(fileindex, &fileindex_path, worktree);
5434 ok_arg.worktree = worktree;
5436 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5441 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5445 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5449 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5454 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5459 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5463 err = got_ref_write(*branch_ref, repo);
5467 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5468 worktree->base_commit_id);
5471 err = got_ref_write(base_commit_ref, repo);
5474 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5475 if (*base_commit_id == NULL) {
5476 err = got_error_from_errno("got_object_id_dup");
5480 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5481 worktree->base_commit_id);
5484 err = got_ref_write(*tmp_branch, repo);
5488 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5492 free(fileindex_path);
5493 free(tmp_branch_name);
5494 free(branch_ref_name);
5495 free(base_commit_ref_name);
5497 got_ref_close(wt_branch);
5500 got_ref_close(*branch_ref);
5504 got_ref_close(*tmp_branch);
5507 free(*base_commit_id);
5509 got_fileindex_free(*fileindex);
5512 lock_worktree(worktree, LOCK_SH);
5517 const struct got_error *
5518 got_worktree_histedit_postpone(struct got_worktree *worktree,
5519 struct got_fileindex *fileindex)
5522 got_fileindex_free(fileindex);
5523 return lock_worktree(worktree, LOCK_SH);
5526 const struct got_error *
5527 got_worktree_histedit_in_progress(int *in_progress,
5528 struct got_worktree *worktree)
5530 const struct got_error *err;
5531 char *tmp_branch_name = NULL;
5533 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5537 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5538 free(tmp_branch_name);
5542 const struct got_error *
5543 got_worktree_histedit_continue(struct got_object_id **commit_id,
5544 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5545 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5546 struct got_worktree *worktree, struct got_repository *repo)
5548 const struct got_error *err;
5549 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5550 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5551 struct got_reference *commit_ref = NULL;
5552 struct got_reference *base_commit_ref = NULL;
5553 char *fileindex_path = NULL;
5554 int have_staged_files = 0;
5558 *base_commit_id = NULL;
5561 err = lock_worktree(worktree, LOCK_EX);
5565 err = open_fileindex(fileindex, &fileindex_path, worktree);
5569 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5570 &have_staged_files);
5571 if (err && err->code != GOT_ERR_CANCELLED)
5573 if (have_staged_files) {
5574 err = got_error(GOT_ERR_STAGED_PATHS);
5578 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5582 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5586 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5590 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5595 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5599 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5602 err = got_ref_resolve(commit_id, repo, commit_ref);
5606 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5609 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5613 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5617 free(commit_ref_name);
5618 free(branch_ref_name);
5619 free(fileindex_path);
5621 got_ref_close(commit_ref);
5622 if (base_commit_ref)
5623 got_ref_close(base_commit_ref);
5627 free(*base_commit_id);
5628 *base_commit_id = NULL;
5630 got_ref_close(*tmp_branch);
5634 got_fileindex_free(*fileindex);
5637 lock_worktree(worktree, LOCK_EX);
5642 static const struct got_error *
5643 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5645 const struct got_error *err;
5646 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5647 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5649 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5652 err = delete_ref(tmp_branch_name, repo);
5656 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5660 err = delete_ref(base_commit_ref_name, repo);
5664 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5667 err = delete_ref(branch_ref_name, repo);
5671 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5674 err = delete_ref(commit_ref_name, repo);
5678 free(tmp_branch_name);
5679 free(base_commit_ref_name);
5680 free(branch_ref_name);
5681 free(commit_ref_name);
5685 const struct got_error *
5686 got_worktree_histedit_abort(struct got_worktree *worktree,
5687 struct got_fileindex *fileindex, struct got_repository *repo,
5688 struct got_reference *branch, struct got_object_id *base_commit_id,
5689 got_worktree_checkout_cb progress_cb, void *progress_arg)
5691 const struct got_error *err, *unlockerr, *sync_err;
5692 struct got_reference *resolved = NULL;
5693 char *fileindex_path = NULL;
5694 struct got_object_id *tree_id = NULL;
5695 struct revert_file_args rfa;
5697 err = lock_worktree(worktree, LOCK_EX);
5701 err = got_ref_open(&resolved, repo,
5702 got_ref_get_symref_target(branch), 0);
5706 err = got_worktree_set_head_ref(worktree, resolved);
5710 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5714 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5715 worktree->path_prefix);
5719 err = delete_histedit_refs(worktree, repo);
5723 err = get_fileindex_path(&fileindex_path, worktree);
5727 rfa.worktree = worktree;
5728 rfa.fileindex = fileindex;
5729 rfa.progress_cb = progress_cb;
5730 rfa.progress_arg = progress_arg;
5731 rfa.patch_cb = NULL;
5732 rfa.patch_arg = NULL;
5734 err = worktree_status(worktree, "", fileindex, repo,
5735 revert_file, &rfa, NULL, NULL, 0, 0);
5739 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5740 repo, progress_cb, progress_arg, NULL, NULL);
5742 sync_err = sync_fileindex(fileindex, fileindex_path);
5743 if (sync_err && err == NULL)
5746 got_ref_close(resolved);
5748 free(fileindex_path);
5750 unlockerr = lock_worktree(worktree, LOCK_SH);
5751 if (unlockerr && err == NULL)
5756 const struct got_error *
5757 got_worktree_histedit_complete(struct got_worktree *worktree,
5758 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5759 struct got_reference *edited_branch, struct got_repository *repo)
5761 const struct got_error *err, *unlockerr;
5762 struct got_object_id *new_head_commit_id = NULL;
5763 struct got_reference *resolved = NULL;
5765 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5769 err = got_ref_open(&resolved, repo,
5770 got_ref_get_symref_target(edited_branch), 0);
5774 err = got_ref_change_ref(resolved, new_head_commit_id);
5778 err = got_ref_write(resolved, repo);
5782 err = got_worktree_set_head_ref(worktree, resolved);
5786 err = delete_histedit_refs(worktree, repo);
5789 got_fileindex_free(fileindex);
5790 free(new_head_commit_id);
5791 unlockerr = lock_worktree(worktree, LOCK_SH);
5792 if (unlockerr && err == NULL)
5797 const struct got_error *
5798 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5799 struct got_object_id *commit_id, struct got_repository *repo)
5801 const struct got_error *err;
5802 char *commit_ref_name;
5804 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5808 err = store_commit_id(commit_ref_name, commit_id, repo);
5812 err = delete_ref(commit_ref_name, repo);
5814 free(commit_ref_name);
5818 const struct got_error *
5819 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5820 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5821 struct got_worktree *worktree, const char *refname,
5822 struct got_repository *repo)
5824 const struct got_error *err = NULL;
5825 char *fileindex_path = NULL;
5826 struct check_rebase_ok_arg ok_arg;
5830 *base_branch_ref = NULL;
5832 err = lock_worktree(worktree, LOCK_EX);
5836 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5837 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5838 "cannot integrate a branch into itself; "
5839 "update -b or different branch name required");
5843 err = open_fileindex(fileindex, &fileindex_path, worktree);
5847 /* Preconditions are the same as for rebase. */
5848 ok_arg.worktree = worktree;
5850 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5855 err = got_ref_open(branch_ref, repo, refname, 1);
5859 err = got_ref_open(base_branch_ref, repo,
5860 got_worktree_get_head_ref_name(worktree), 1);
5864 got_ref_close(*branch_ref);
5867 if (*base_branch_ref) {
5868 got_ref_close(*base_branch_ref);
5869 *base_branch_ref = NULL;
5872 got_fileindex_free(*fileindex);
5875 lock_worktree(worktree, LOCK_SH);
5880 const struct got_error *
5881 got_worktree_integrate_continue(struct got_worktree *worktree,
5882 struct got_fileindex *fileindex, struct got_repository *repo,
5883 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5884 got_worktree_checkout_cb progress_cb, void *progress_arg,
5885 got_cancel_cb cancel_cb, void *cancel_arg)
5887 const struct got_error *err = NULL, *sync_err, *unlockerr;
5888 char *fileindex_path = NULL;
5889 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5891 err = get_fileindex_path(&fileindex_path, worktree);
5895 err = got_ref_resolve(&commit_id, repo, branch_ref);
5899 err = got_object_id_by_path(&tree_id, repo, commit_id,
5900 worktree->path_prefix);
5904 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5908 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5909 progress_cb, progress_arg, cancel_cb, cancel_arg);
5913 err = got_ref_change_ref(base_branch_ref, commit_id);
5917 err = got_ref_write(base_branch_ref, repo);
5919 sync_err = sync_fileindex(fileindex, fileindex_path);
5920 if (sync_err && err == NULL)
5924 unlockerr = got_ref_unlock(branch_ref);
5925 if (unlockerr && err == NULL)
5927 got_ref_close(branch_ref);
5929 unlockerr = got_ref_unlock(base_branch_ref);
5930 if (unlockerr && err == NULL)
5932 got_ref_close(base_branch_ref);
5934 got_fileindex_free(fileindex);
5935 free(fileindex_path);
5938 unlockerr = lock_worktree(worktree, LOCK_SH);
5939 if (unlockerr && err == NULL)
5944 const struct got_error *
5945 got_worktree_integrate_abort(struct got_worktree *worktree,
5946 struct got_fileindex *fileindex, struct got_repository *repo,
5947 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5949 const struct got_error *err = NULL, *unlockerr = NULL;
5951 got_fileindex_free(fileindex);
5953 err = lock_worktree(worktree, LOCK_SH);
5955 unlockerr = got_ref_unlock(branch_ref);
5956 if (unlockerr && err == NULL)
5958 got_ref_close(branch_ref);
5960 unlockerr = got_ref_unlock(base_branch_ref);
5961 if (unlockerr && err == NULL)
5963 got_ref_close(base_branch_ref);
5968 struct check_stage_ok_arg {
5969 struct got_object_id *head_commit_id;
5970 struct got_worktree *worktree;
5971 struct got_fileindex *fileindex;
5972 struct got_repository *repo;
5976 const struct got_error *
5977 check_stage_ok(void *arg, unsigned char status,
5978 unsigned char staged_status, const char *relpath,
5979 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5980 struct got_object_id *commit_id, int dirfd, const char *de_name)
5982 struct check_stage_ok_arg *a = arg;
5983 const struct got_error *err = NULL;
5984 struct got_fileindex_entry *ie;
5985 struct got_object_id base_commit_id;
5986 struct got_object_id *base_commit_idp = NULL;
5987 char *in_repo_path = NULL, *p;
5989 if (status == GOT_STATUS_UNVERSIONED ||
5990 status == GOT_STATUS_NO_CHANGE)
5992 if (status == GOT_STATUS_NONEXISTENT)
5993 return got_error_set_errno(ENOENT, relpath);
5995 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5997 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5999 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6000 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6002 return got_error_from_errno("asprintf");
6004 if (got_fileindex_entry_has_commit(ie)) {
6005 memcpy(base_commit_id.sha1, ie->commit_sha1,
6006 SHA1_DIGEST_LENGTH);
6007 base_commit_idp = &base_commit_id;
6010 if (status == GOT_STATUS_CONFLICT) {
6011 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6013 } else if (status != GOT_STATUS_ADD &&
6014 status != GOT_STATUS_MODIFY &&
6015 status != GOT_STATUS_DELETE) {
6016 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6020 a->have_changes = 1;
6025 err = check_out_of_date(p, status, staged_status,
6026 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6027 GOT_ERR_STAGE_OUT_OF_DATE);
6033 struct stage_path_arg {
6034 struct got_worktree *worktree;
6035 struct got_fileindex *fileindex;
6036 struct got_repository *repo;
6037 got_worktree_status_cb status_cb;
6039 got_worktree_patch_cb patch_cb;
6041 int staged_something;
6044 static const struct got_error *
6045 stage_path(void *arg, unsigned char status,
6046 unsigned char staged_status, const char *relpath,
6047 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6048 struct got_object_id *commit_id, int dirfd, const char *de_name)
6050 struct stage_path_arg *a = arg;
6051 const struct got_error *err = NULL;
6052 struct got_fileindex_entry *ie;
6053 char *ondisk_path = NULL, *path_content = NULL;
6055 struct got_object_id *new_staged_blob_id = NULL;
6057 if (status == GOT_STATUS_UNVERSIONED)
6060 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6062 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6066 return got_error_from_errno("asprintf");
6069 case GOT_STATUS_ADD:
6070 case GOT_STATUS_MODIFY:
6072 if (status == GOT_STATUS_ADD) {
6073 int choice = GOT_PATCH_CHOICE_NONE;
6074 err = (*a->patch_cb)(&choice, a->patch_arg,
6075 status, ie->path, NULL, 1, 1);
6078 if (choice != GOT_PATCH_CHOICE_YES)
6081 err = create_patched_content(&path_content, 0,
6082 staged_blob_id ? staged_blob_id : blob_id,
6083 ondisk_path, dirfd, de_name, ie->path,
6084 a->repo, a->patch_cb, a->patch_arg);
6085 if (err || path_content == NULL)
6089 err = got_object_blob_create(&new_staged_blob_id,
6090 path_content ? path_content : ondisk_path, a->repo);
6093 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6094 SHA1_DIGEST_LENGTH);
6095 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6096 stage = GOT_FILEIDX_STAGE_ADD;
6098 stage = GOT_FILEIDX_STAGE_MODIFY;
6099 got_fileindex_entry_stage_set(ie, stage);
6100 a->staged_something = 1;
6101 if (a->status_cb == NULL)
6103 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6104 get_staged_status(ie), relpath, blob_id,
6105 new_staged_blob_id, NULL, dirfd, de_name);
6107 case GOT_STATUS_DELETE:
6108 if (staged_status == GOT_STATUS_DELETE)
6111 int choice = GOT_PATCH_CHOICE_NONE;
6112 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6113 ie->path, NULL, 1, 1);
6116 if (choice == GOT_PATCH_CHOICE_NO)
6118 if (choice != GOT_PATCH_CHOICE_YES) {
6119 err = got_error(GOT_ERR_PATCH_CHOICE);
6123 stage = GOT_FILEIDX_STAGE_DELETE;
6124 got_fileindex_entry_stage_set(ie, stage);
6125 a->staged_something = 1;
6126 if (a->status_cb == NULL)
6128 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6129 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6132 case GOT_STATUS_NO_CHANGE:
6134 case GOT_STATUS_CONFLICT:
6135 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6137 case GOT_STATUS_NONEXISTENT:
6138 err = got_error_set_errno(ENOENT, relpath);
6141 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6145 if (path_content && unlink(path_content) == -1 && err == NULL)
6146 err = got_error_from_errno2("unlink", path_content);
6149 free(new_staged_blob_id);
6153 const struct got_error *
6154 got_worktree_stage(struct got_worktree *worktree,
6155 struct got_pathlist_head *paths,
6156 got_worktree_status_cb status_cb, void *status_arg,
6157 got_worktree_patch_cb patch_cb, void *patch_arg,
6158 struct got_repository *repo)
6160 const struct got_error *err = NULL, *sync_err, *unlockerr;
6161 struct got_pathlist_entry *pe;
6162 struct got_fileindex *fileindex = NULL;
6163 char *fileindex_path = NULL;
6164 struct got_reference *head_ref = NULL;
6165 struct got_object_id *head_commit_id = NULL;
6166 struct check_stage_ok_arg oka;
6167 struct stage_path_arg spa;
6169 err = lock_worktree(worktree, LOCK_EX);
6173 err = got_ref_open(&head_ref, repo,
6174 got_worktree_get_head_ref_name(worktree), 0);
6177 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6180 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6184 /* Check pre-conditions before staging anything. */
6185 oka.head_commit_id = head_commit_id;
6186 oka.worktree = worktree;
6187 oka.fileindex = fileindex;
6189 oka.have_changes = 0;
6190 TAILQ_FOREACH(pe, paths, entry) {
6191 err = worktree_status(worktree, pe->path, fileindex, repo,
6192 check_stage_ok, &oka, NULL, NULL, 0, 0);
6196 if (!oka.have_changes) {
6197 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6201 spa.worktree = worktree;
6202 spa.fileindex = fileindex;
6204 spa.patch_cb = patch_cb;
6205 spa.patch_arg = patch_arg;
6206 spa.status_cb = status_cb;
6207 spa.status_arg = status_arg;
6208 spa.staged_something = 0;
6209 TAILQ_FOREACH(pe, paths, entry) {
6210 err = worktree_status(worktree, pe->path, fileindex, repo,
6211 stage_path, &spa, NULL, NULL, 0, 0);
6215 if (!spa.staged_something) {
6216 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6220 sync_err = sync_fileindex(fileindex, fileindex_path);
6221 if (sync_err && err == NULL)
6225 got_ref_close(head_ref);
6226 free(head_commit_id);
6227 free(fileindex_path);
6229 got_fileindex_free(fileindex);
6230 unlockerr = lock_worktree(worktree, LOCK_SH);
6231 if (unlockerr && err == NULL)
6236 struct unstage_path_arg {
6237 struct got_worktree *worktree;
6238 struct got_fileindex *fileindex;
6239 struct got_repository *repo;
6240 got_worktree_checkout_cb progress_cb;
6242 got_worktree_patch_cb patch_cb;
6246 static const struct got_error *
6247 create_unstaged_content(char **path_unstaged_content,
6248 char **path_new_staged_content, struct got_object_id *blob_id,
6249 struct got_object_id *staged_blob_id, const char *relpath,
6250 struct got_repository *repo,
6251 got_worktree_patch_cb patch_cb, void *patch_arg)
6253 const struct got_error *err;
6254 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6255 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6256 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6257 struct stat sb1, sb2;
6258 struct got_diff_changes *changes = NULL;
6259 struct got_diff_state *ds = NULL;
6260 struct got_diff_args *args = NULL;
6261 struct got_diff_change *change;
6262 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6263 int have_content = 0, have_rejected_content = 0;
6265 *path_unstaged_content = NULL;
6266 *path_new_staged_content = NULL;
6268 err = got_object_id_str(&label1, blob_id);
6271 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6275 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6279 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6283 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6287 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6291 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6295 if (stat(path1, &sb1) == -1) {
6296 err = got_error_from_errno2("stat", path1);
6300 if (stat(path2, &sb2) == -1) {
6301 err = got_error_from_errno2("stat", path2);
6305 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6306 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6310 err = got_opentemp_named(path_unstaged_content, &outfile,
6311 "got-unstaged-content");
6314 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6315 "got-new-staged-content");
6319 if (fseek(f1, 0L, SEEK_SET) == -1) {
6320 err = got_ferror(f1, GOT_ERR_IO);
6323 if (fseek(f2, 0L, SEEK_SET) == -1) {
6324 err = got_ferror(f2, GOT_ERR_IO);
6327 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6329 err = apply_or_reject_change(&choice, change, ++n,
6330 changes->nchanges, ds, args, diff_flags, relpath,
6331 f1, f2, &line_cur1, &line_cur2,
6332 outfile, rejectfile, patch_cb, patch_arg);
6335 if (choice == GOT_PATCH_CHOICE_YES)
6338 have_rejected_content = 1;
6339 if (choice == GOT_PATCH_CHOICE_QUIT)
6342 if (have_content || have_rejected_content)
6343 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6344 outfile, rejectfile);
6348 got_object_blob_close(blob);
6350 got_object_blob_close(staged_blob);
6351 if (f1 && fclose(f1) == EOF && err == NULL)
6352 err = got_error_from_errno2("fclose", path1);
6353 if (f2 && fclose(f2) == EOF && err == NULL)
6354 err = got_error_from_errno2("fclose", path2);
6355 if (outfile && fclose(outfile) == EOF && err == NULL)
6356 err = got_error_from_errno2("fclose", *path_unstaged_content);
6357 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6358 err = got_error_from_errno2("fclose", *path_new_staged_content);
6359 if (path1 && unlink(path1) == -1 && err == NULL)
6360 err = got_error_from_errno2("unlink", path1);
6361 if (path2 && unlink(path2) == -1 && err == NULL)
6362 err = got_error_from_errno2("unlink", path2);
6363 if (err || !have_content) {
6364 if (*path_unstaged_content &&
6365 unlink(*path_unstaged_content) == -1 && err == NULL)
6366 err = got_error_from_errno2("unlink",
6367 *path_unstaged_content);
6368 free(*path_unstaged_content);
6369 *path_unstaged_content = NULL;
6371 if (err || !have_rejected_content) {
6372 if (*path_new_staged_content &&
6373 unlink(*path_new_staged_content) == -1 && err == NULL)
6374 err = got_error_from_errno2("unlink",
6375 *path_new_staged_content);
6376 free(*path_new_staged_content);
6377 *path_new_staged_content = NULL;
6381 got_diff_state_free(ds);
6385 got_diff_free_changes(changes);
6391 static const struct got_error *
6392 unstage_path(void *arg, unsigned char status,
6393 unsigned char staged_status, const char *relpath,
6394 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6395 struct got_object_id *commit_id, int dirfd, const char *de_name)
6397 const struct got_error *err = NULL;
6398 struct unstage_path_arg *a = arg;
6399 struct got_fileindex_entry *ie;
6400 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6401 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6402 char *path_new_staged_content = NULL;
6403 char *id_str = NULL, *label_orig = NULL;
6404 int local_changes_subsumed;
6407 if (staged_status != GOT_STATUS_ADD &&
6408 staged_status != GOT_STATUS_MODIFY &&
6409 staged_status != GOT_STATUS_DELETE)
6412 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6414 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6416 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6418 return got_error_from_errno("asprintf");
6420 err = got_object_id_str(&id_str,
6421 commit_id ? commit_id : a->worktree->base_commit_id);
6424 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6426 err = got_error_from_errno("asprintf");
6430 switch (staged_status) {
6431 case GOT_STATUS_MODIFY:
6432 err = got_object_open_as_blob(&blob_base, a->repo,
6437 case GOT_STATUS_ADD:
6439 if (staged_status == GOT_STATUS_ADD) {
6440 int choice = GOT_PATCH_CHOICE_NONE;
6441 err = (*a->patch_cb)(&choice, a->patch_arg,
6442 staged_status, ie->path, NULL, 1, 1);
6445 if (choice != GOT_PATCH_CHOICE_YES)
6448 err = create_unstaged_content(
6449 &path_unstaged_content,
6450 &path_new_staged_content, blob_id,
6451 staged_blob_id, ie->path, a->repo,
6452 a->patch_cb, a->patch_arg);
6453 if (err || path_unstaged_content == NULL)
6455 if (path_new_staged_content) {
6456 err = got_object_blob_create(
6458 path_new_staged_content,
6462 memcpy(ie->staged_blob_sha1,
6463 staged_blob_id->sha1,
6464 SHA1_DIGEST_LENGTH);
6466 err = merge_file(&local_changes_subsumed,
6467 a->worktree, blob_base, ondisk_path,
6468 relpath, got_fileindex_perms_to_st(ie),
6469 path_unstaged_content, label_orig,
6470 "unstaged", a->repo, a->progress_cb,
6473 path_new_staged_content == NULL)
6474 got_fileindex_entry_stage_set(ie,
6475 GOT_FILEIDX_STAGE_NONE);
6476 break; /* Done with this file. */
6479 err = got_object_open_as_blob(&blob_staged, a->repo,
6480 staged_blob_id, 8192);
6483 err = merge_blob(&local_changes_subsumed, a->worktree,
6484 blob_base, ondisk_path, relpath,
6485 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6486 commit_id ? commit_id : a->worktree->base_commit_id,
6487 a->repo, a->progress_cb, a->progress_arg);
6489 got_fileindex_entry_stage_set(ie,
6490 GOT_FILEIDX_STAGE_NONE);
6492 case GOT_STATUS_DELETE:
6494 int choice = GOT_PATCH_CHOICE_NONE;
6495 err = (*a->patch_cb)(&choice, a->patch_arg,
6496 staged_status, ie->path, NULL, 1, 1);
6499 if (choice == GOT_PATCH_CHOICE_NO)
6501 if (choice != GOT_PATCH_CHOICE_YES) {
6502 err = got_error(GOT_ERR_PATCH_CHOICE);
6506 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6507 err = get_file_status(&status, &sb, ie, ondisk_path,
6508 dirfd, de_name, a->repo);
6511 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6516 if (path_unstaged_content &&
6517 unlink(path_unstaged_content) == -1 && err == NULL)
6518 err = got_error_from_errno2("unlink", path_unstaged_content);
6519 if (path_new_staged_content &&
6520 unlink(path_new_staged_content) == -1 && err == NULL)
6521 err = got_error_from_errno2("unlink", path_new_staged_content);
6522 free(path_unstaged_content);
6523 free(path_new_staged_content);
6525 got_object_blob_close(blob_base);
6527 got_object_blob_close(blob_staged);
6533 const struct got_error *
6534 got_worktree_unstage(struct got_worktree *worktree,
6535 struct got_pathlist_head *paths,
6536 got_worktree_checkout_cb progress_cb, void *progress_arg,
6537 got_worktree_patch_cb patch_cb, void *patch_arg,
6538 struct got_repository *repo)
6540 const struct got_error *err = NULL, *sync_err, *unlockerr;
6541 struct got_pathlist_entry *pe;
6542 struct got_fileindex *fileindex = NULL;
6543 char *fileindex_path = NULL;
6544 struct unstage_path_arg upa;
6546 err = lock_worktree(worktree, LOCK_EX);
6550 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6554 upa.worktree = worktree;
6555 upa.fileindex = fileindex;
6557 upa.progress_cb = progress_cb;
6558 upa.progress_arg = progress_arg;
6559 upa.patch_cb = patch_cb;
6560 upa.patch_arg = patch_arg;
6561 TAILQ_FOREACH(pe, paths, entry) {
6562 err = worktree_status(worktree, pe->path, fileindex, repo,
6563 unstage_path, &upa, NULL, NULL, 0, 0);
6568 sync_err = sync_fileindex(fileindex, fileindex_path);
6569 if (sync_err && err == NULL)
6572 free(fileindex_path);
6574 got_fileindex_free(fileindex);
6575 unlockerr = lock_worktree(worktree, LOCK_SH);
6576 if (unlockerr && err == NULL)