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 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1131 err = got_error_from_errno2("fstatat", abspath);
1135 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1136 if (fd == -1 && errno != ENOENT)
1137 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);
1151 if (!S_ISREG(sb->st_mode)) {
1152 *status = GOT_STATUS_OBSTRUCTED;
1156 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1157 *status = GOT_STATUS_DELETE;
1159 } else if (!got_fileindex_entry_has_blob(ie) &&
1160 staged_status != GOT_STATUS_ADD) {
1161 *status = GOT_STATUS_ADD;
1165 if (!stat_info_differs(ie, sb))
1168 if (staged_status == GOT_STATUS_MODIFY ||
1169 staged_status == GOT_STATUS_ADD)
1170 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1172 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1174 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1179 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1180 if (fd == -1 && errno != ENOENT)
1181 return got_error_from_errno2("openat", abspath);
1184 f = fdopen(fd, "r");
1186 err = got_error_from_errno2("fopen", abspath);
1190 hdrlen = got_object_blob_get_hdrlen(blob);
1192 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1193 err = got_object_blob_read_block(&blen, blob);
1196 /* Skip length of blob object header first time around. */
1197 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1198 if (flen == 0 && ferror(f)) {
1199 err = got_error_from_errno("fread");
1204 *status = GOT_STATUS_MODIFY;
1206 } else if (flen == 0) {
1208 *status = GOT_STATUS_MODIFY;
1210 } else if (blen - hdrlen == flen) {
1211 /* Skip blob object header first time around. */
1212 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1213 *status = GOT_STATUS_MODIFY;
1217 *status = GOT_STATUS_MODIFY;
1223 if (*status == GOT_STATUS_MODIFY) {
1225 err = get_modified_file_content_status(status, f);
1226 } else if (xbit_differs(ie, sb->st_mode))
1227 *status = GOT_STATUS_MODE_CHANGE;
1230 got_object_blob_close(blob);
1231 if (f != NULL && fclose(f) == EOF && err == NULL)
1232 err = got_error_from_errno2("fclose", abspath);
1233 if (fd != -1 && close(fd) == -1 && err == NULL)
1234 err = got_error_from_errno2("close", abspath);
1239 * Update timestamps in the file index if a file is unmodified and
1240 * we had to run a full content comparison to find out.
1242 static const struct got_error *
1243 sync_timestamps(char *ondisk_path, unsigned char status,
1244 struct got_fileindex_entry *ie, struct stat *sb)
1246 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1247 return got_fileindex_entry_update(ie, ondisk_path,
1248 ie->blob_sha1, ie->commit_sha1, 1);
1253 static const struct got_error *
1254 update_blob(struct got_worktree *worktree,
1255 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1256 struct got_tree_entry *te, const char *path,
1257 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1260 const struct got_error *err = NULL;
1261 struct got_blob_object *blob = NULL;
1263 unsigned char status = GOT_STATUS_NO_CHANGE;
1266 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1267 return got_error_from_errno("asprintf");
1270 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1271 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1274 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1278 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1279 sb.st_mode = got_fileindex_perms_to_st(ie);
1281 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1283 if (status == GOT_STATUS_OBSTRUCTED) {
1284 err = (*progress_cb)(progress_arg, status, path);
1288 if (ie && status != GOT_STATUS_MISSING &&
1289 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1290 if (got_fileindex_entry_has_commit(ie) &&
1291 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1292 SHA1_DIGEST_LENGTH) == 0) {
1293 err = sync_timestamps(ondisk_path, status, ie, &sb);
1296 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1300 if (got_fileindex_entry_has_blob(ie) &&
1301 memcmp(ie->blob_sha1, te->id.sha1,
1302 SHA1_DIGEST_LENGTH) == 0) {
1303 err = sync_timestamps(ondisk_path, status, ie, &sb);
1308 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1312 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1313 int update_timestamps;
1314 struct got_blob_object *blob2 = NULL;
1315 char *label_orig = NULL;
1316 if (got_fileindex_entry_has_blob(ie)) {
1317 struct got_object_id id2;
1318 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1319 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1323 if (got_fileindex_entry_has_commit(ie)) {
1324 char id_str[SHA1_DIGEST_STRING_LENGTH];
1325 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1326 sizeof(id_str)) == NULL) {
1327 err = got_error_path(id_str,
1328 GOT_ERR_BAD_OBJ_ID_STR);
1331 if (asprintf(&label_orig, "%s: commit %s",
1332 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1333 err = got_error_from_errno("asprintf");
1337 err = merge_blob(&update_timestamps, worktree, blob2,
1338 ondisk_path, path, sb.st_mode, label_orig, blob,
1339 worktree->base_commit_id, repo,
1340 progress_cb, progress_arg);
1343 got_object_blob_close(blob2);
1347 * Do not update timestamps of files with local changes.
1348 * Otherwise, a future status walk would treat them as
1349 * unmodified files again.
1351 err = got_fileindex_entry_update(ie, ondisk_path,
1352 blob->id.sha1, worktree->base_commit_id->sha1,
1354 } else if (status == GOT_STATUS_MODE_CHANGE) {
1355 err = got_fileindex_entry_update(ie, ondisk_path,
1356 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1357 } else if (status == GOT_STATUS_DELETE) {
1358 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1361 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1362 ondisk_path, path, blob, 0);
1366 err = install_blob(worktree, ondisk_path, path, te->mode,
1367 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1368 repo, progress_cb, progress_arg);
1371 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1372 ondisk_path, path, blob, 1);
1376 got_object_blob_close(blob);
1382 static const struct got_error *
1383 remove_ondisk_file(const char *root_path, const char *path)
1385 const struct got_error *err = NULL;
1386 char *ondisk_path = NULL;
1388 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1389 return got_error_from_errno("asprintf");
1391 if (unlink(ondisk_path) == -1) {
1392 if (errno != ENOENT)
1393 err = got_error_from_errno2("unlink", ondisk_path);
1395 char *parent = dirname(ondisk_path);
1396 while (parent && strcmp(parent, root_path) != 0) {
1397 if (rmdir(parent) == -1) {
1398 if (errno != ENOTEMPTY)
1399 err = got_error_from_errno2("rmdir",
1403 parent = dirname(parent);
1410 static const struct got_error *
1411 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1412 struct got_fileindex_entry *ie, struct got_repository *repo,
1413 got_worktree_checkout_cb progress_cb, void *progress_arg)
1415 const struct got_error *err = NULL;
1416 unsigned char status;
1420 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1421 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1423 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1425 return got_error_from_errno("asprintf");
1427 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1431 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1432 status == GOT_STATUS_ADD) {
1433 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1437 * Preserve the working file and change the deleted blob's
1438 * entry into a schedule-add entry.
1440 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1445 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1448 if (status == GOT_STATUS_NO_CHANGE) {
1449 err = remove_ondisk_file(worktree->root_path, ie->path);
1453 got_fileindex_entry_remove(fileindex, ie);
1459 struct diff_cb_arg {
1460 struct got_fileindex *fileindex;
1461 struct got_worktree *worktree;
1462 struct got_repository *repo;
1463 got_worktree_checkout_cb progress_cb;
1465 got_cancel_cb cancel_cb;
1469 static const struct got_error *
1470 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1471 struct got_tree_entry *te, const char *parent_path)
1473 struct diff_cb_arg *a = arg;
1475 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1476 return got_error(GOT_ERR_CANCELLED);
1478 return update_blob(a->worktree, a->fileindex, ie, te,
1479 ie->path, a->repo, a->progress_cb, a->progress_arg);
1482 static const struct got_error *
1483 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1485 struct diff_cb_arg *a = arg;
1487 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1488 return got_error(GOT_ERR_CANCELLED);
1490 return delete_blob(a->worktree, a->fileindex, ie,
1491 a->repo, a->progress_cb, a->progress_arg);
1494 static const struct got_error *
1495 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1497 struct diff_cb_arg *a = arg;
1498 const struct got_error *err;
1501 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1502 return got_error(GOT_ERR_CANCELLED);
1504 if (got_object_tree_entry_is_submodule(te))
1507 if (asprintf(&path, "%s%s%s", parent_path,
1508 parent_path[0] ? "/" : "", te->name)
1510 return got_error_from_errno("asprintf");
1512 if (S_ISDIR(te->mode))
1513 err = add_dir_on_disk(a->worktree, path);
1515 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1516 a->repo, a->progress_cb, a->progress_arg);
1522 static const struct got_error *
1523 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1525 const struct got_error *err = NULL;
1526 char *uuidstr = NULL;
1527 uint32_t uuid_status;
1531 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1532 if (uuid_status != uuid_s_ok)
1533 return got_error_uuid(uuid_status, "uuid_to_string");
1535 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1537 err = got_error_from_errno("asprintf");
1544 const struct got_error *
1545 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1547 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1550 static const struct got_error *
1551 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1553 return get_ref_name(refname, worktree,
1554 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1557 static const struct got_error *
1558 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1560 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1563 static const struct got_error *
1564 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1566 return get_ref_name(refname, worktree,
1567 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1570 static const struct got_error *
1571 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1573 return get_ref_name(refname, worktree,
1574 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1577 static const struct got_error *
1578 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1580 return get_ref_name(refname, worktree,
1581 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1584 static const struct got_error *
1585 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1587 return get_ref_name(refname, worktree,
1588 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1591 static const struct got_error *
1592 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1594 return get_ref_name(refname, worktree,
1595 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1598 static const struct got_error *
1599 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1601 return get_ref_name(refname, worktree,
1602 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1605 const struct got_error *
1606 got_worktree_get_histedit_script_path(char **path,
1607 struct got_worktree *worktree)
1609 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1610 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1612 return got_error_from_errno("asprintf");
1618 * Prevent Git's garbage collector from deleting our base commit by
1619 * setting a reference to our base commit's ID.
1621 static const struct got_error *
1622 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1624 const struct got_error *err = NULL;
1625 struct got_reference *ref = NULL;
1628 err = got_worktree_get_base_ref_name(&refname, worktree);
1632 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1636 err = got_ref_write(ref, repo);
1644 static const struct got_error *
1645 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1647 const struct got_error *err = NULL;
1649 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1650 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1651 err = got_error_from_errno("asprintf");
1652 *fileindex_path = NULL;
1658 static const struct got_error *
1659 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1660 struct got_worktree *worktree)
1662 const struct got_error *err = NULL;
1665 *fileindex_path = NULL;
1666 *fileindex = got_fileindex_alloc();
1667 if (*fileindex == NULL)
1668 return got_error_from_errno("got_fileindex_alloc");
1670 err = get_fileindex_path(fileindex_path, worktree);
1674 index = fopen(*fileindex_path, "rb");
1675 if (index == NULL) {
1676 if (errno != ENOENT)
1677 err = got_error_from_errno2("fopen", *fileindex_path);
1679 err = got_fileindex_read(*fileindex, index);
1680 if (fclose(index) != 0 && err == NULL)
1681 err = got_error_from_errno("fclose");
1685 free(*fileindex_path);
1686 *fileindex_path = NULL;
1687 got_fileindex_free(*fileindex);
1693 struct bump_base_commit_id_arg {
1694 struct got_object_id *base_commit_id;
1697 const char *entry_name;
1698 got_worktree_checkout_cb progress_cb;
1702 /* Bump base commit ID of all files within an updated part of the work tree. */
1703 static const struct got_error *
1704 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1706 const struct got_error *err;
1707 struct bump_base_commit_id_arg *a = arg;
1709 if (a->entry_name) {
1710 if (strcmp(ie->path, a->path) != 0)
1712 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1715 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1716 SHA1_DIGEST_LENGTH) == 0)
1719 if (a->progress_cb) {
1720 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1725 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1729 static const struct got_error *
1730 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1732 const struct got_error *err = NULL;
1733 char *new_fileindex_path = NULL;
1734 FILE *new_index = NULL;
1736 err = got_opentemp_named(&new_fileindex_path, &new_index,
1741 err = got_fileindex_write(fileindex, new_index);
1745 if (rename(new_fileindex_path, fileindex_path) != 0) {
1746 err = got_error_from_errno3("rename", new_fileindex_path,
1748 unlink(new_fileindex_path);
1753 free(new_fileindex_path);
1757 static const struct got_error *
1758 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1759 struct got_object_id **tree_id, const char *wt_relpath,
1760 struct got_worktree *worktree, struct got_repository *repo)
1762 const struct got_error *err = NULL;
1763 struct got_object_id *id = NULL;
1764 char *in_repo_path = NULL;
1765 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1767 *entry_type = GOT_OBJ_TYPE_ANY;
1768 *tree_relpath = NULL;
1771 if (wt_relpath[0] == '\0') {
1772 /* Check out all files within the work tree. */
1773 *entry_type = GOT_OBJ_TYPE_TREE;
1774 *tree_relpath = strdup("");
1775 if (*tree_relpath == NULL) {
1776 err = got_error_from_errno("strdup");
1779 err = got_object_id_by_path(tree_id, repo,
1780 worktree->base_commit_id, worktree->path_prefix);
1786 /* Check out a subset of files in the work tree. */
1788 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1789 is_root_wt ? "" : "/", wt_relpath) == -1) {
1790 err = got_error_from_errno("asprintf");
1794 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1800 in_repo_path = NULL;
1802 err = got_object_get_type(entry_type, repo, id);
1806 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1807 /* Check out a single file. */
1808 if (strchr(wt_relpath, '/') == NULL) {
1809 /* Check out a single file in work tree's root dir. */
1810 in_repo_path = strdup(worktree->path_prefix);
1811 if (in_repo_path == NULL) {
1812 err = got_error_from_errno("strdup");
1815 *tree_relpath = strdup("");
1816 if (*tree_relpath == NULL) {
1817 err = got_error_from_errno("strdup");
1821 /* Check out a single file in a subdirectory. */
1822 err = got_path_dirname(tree_relpath, wt_relpath);
1825 if (asprintf(&in_repo_path, "%s%s%s",
1826 worktree->path_prefix, is_root_wt ? "" : "/",
1827 *tree_relpath) == -1) {
1828 err = got_error_from_errno("asprintf");
1832 err = got_object_id_by_path(tree_id, repo,
1833 worktree->base_commit_id, in_repo_path);
1835 /* Check out all files within a subdirectory. */
1836 *tree_id = got_object_id_dup(id);
1837 if (*tree_id == NULL) {
1838 err = got_error_from_errno("got_object_id_dup");
1841 *tree_relpath = strdup(wt_relpath);
1842 if (*tree_relpath == NULL) {
1843 err = got_error_from_errno("strdup");
1851 *entry_type = GOT_OBJ_TYPE_ANY;
1852 free(*tree_relpath);
1853 *tree_relpath = NULL;
1860 static const struct got_error *
1861 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1862 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1863 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1864 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1866 const struct got_error *err = NULL;
1867 struct got_commit_object *commit = NULL;
1868 struct got_tree_object *tree = NULL;
1869 struct got_fileindex_diff_tree_cb diff_cb;
1870 struct diff_cb_arg arg;
1872 err = ref_base_commit(worktree, repo);
1876 err = got_object_open_as_commit(&commit, repo,
1877 worktree->base_commit_id);
1881 err = got_object_open_as_tree(&tree, repo, tree_id);
1886 got_object_tree_find_entry(tree, entry_name) == NULL) {
1887 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1891 diff_cb.diff_old_new = diff_old_new;
1892 diff_cb.diff_old = diff_old;
1893 diff_cb.diff_new = diff_new;
1894 arg.fileindex = fileindex;
1895 arg.worktree = worktree;
1897 arg.progress_cb = progress_cb;
1898 arg.progress_arg = progress_arg;
1899 arg.cancel_cb = cancel_cb;
1900 arg.cancel_arg = cancel_arg;
1901 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1902 entry_name, repo, &diff_cb, &arg);
1905 got_object_tree_close(tree);
1907 got_object_commit_close(commit);
1911 const struct got_error *
1912 got_worktree_checkout_files(struct got_worktree *worktree,
1913 struct got_pathlist_head *paths, struct got_repository *repo,
1914 got_worktree_checkout_cb progress_cb, void *progress_arg,
1915 got_cancel_cb cancel_cb, void *cancel_arg)
1917 const struct got_error *err = NULL, *sync_err, *unlockerr;
1918 struct got_commit_object *commit = NULL;
1919 struct got_tree_object *tree = NULL;
1920 struct got_fileindex *fileindex = NULL;
1921 char *fileindex_path = NULL;
1922 struct got_pathlist_entry *pe;
1923 struct tree_path_data {
1924 SIMPLEQ_ENTRY(tree_path_data) entry;
1925 struct got_object_id *tree_id;
1930 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1932 SIMPLEQ_INIT(&tree_paths);
1934 err = lock_worktree(worktree, LOCK_EX);
1938 /* Map all specified paths to in-repository trees. */
1939 TAILQ_FOREACH(pe, paths, entry) {
1940 tpd = malloc(sizeof(*tpd));
1942 err = got_error_from_errno("malloc");
1946 err = find_tree_entry_for_checkout(&tpd->entry_type,
1947 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1953 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1954 err = got_path_basename(&tpd->entry_name, pe->path);
1962 tpd->entry_name = NULL;
1964 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1968 * Read the file index.
1969 * Checking out files is supposed to be an idempotent operation.
1970 * If the on-disk file index is incomplete we will try to complete it.
1972 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1976 tpd = SIMPLEQ_FIRST(&tree_paths);
1977 TAILQ_FOREACH(pe, paths, entry) {
1978 struct bump_base_commit_id_arg bbc_arg;
1980 err = checkout_files(worktree, fileindex, tpd->relpath,
1981 tpd->tree_id, tpd->entry_name, repo,
1982 progress_cb, progress_arg, cancel_cb, cancel_arg);
1986 bbc_arg.base_commit_id = worktree->base_commit_id;
1987 bbc_arg.entry_name = tpd->entry_name;
1988 bbc_arg.path = pe->path;
1989 bbc_arg.path_len = pe->path_len;
1990 bbc_arg.progress_cb = progress_cb;
1991 bbc_arg.progress_arg = progress_arg;
1992 err = got_fileindex_for_each_entry_safe(fileindex,
1993 bump_base_commit_id, &bbc_arg);
1997 tpd = SIMPLEQ_NEXT(tpd, entry);
1999 sync_err = sync_fileindex(fileindex, fileindex_path);
2000 if (sync_err && err == NULL)
2003 free(fileindex_path);
2005 got_object_tree_close(tree);
2007 got_object_commit_close(commit);
2009 got_fileindex_free(fileindex);
2010 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2011 tpd = SIMPLEQ_FIRST(&tree_paths);
2012 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2017 unlockerr = lock_worktree(worktree, LOCK_SH);
2018 if (unlockerr && err == NULL)
2023 struct merge_file_cb_arg {
2024 struct got_worktree *worktree;
2025 struct got_fileindex *fileindex;
2026 got_worktree_checkout_cb progress_cb;
2028 got_cancel_cb cancel_cb;
2030 const char *label_orig;
2031 struct got_object_id *commit_id2;
2034 static const struct got_error *
2035 merge_file_cb(void *arg, struct got_blob_object *blob1,
2036 struct got_blob_object *blob2, struct got_object_id *id1,
2037 struct got_object_id *id2, const char *path1, const char *path2,
2038 mode_t mode1, mode_t mode2, struct got_repository *repo)
2040 static const struct got_error *err = NULL;
2041 struct merge_file_cb_arg *a = arg;
2042 struct got_fileindex_entry *ie;
2043 char *ondisk_path = NULL;
2045 unsigned char status;
2046 int local_changes_subsumed;
2048 if (blob1 && blob2) {
2049 ie = got_fileindex_entry_get(a->fileindex, path2,
2052 return (*a->progress_cb)(a->progress_arg,
2053 GOT_STATUS_MISSING, path2);
2055 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2057 return got_error_from_errno("asprintf");
2059 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2064 if (status == GOT_STATUS_DELETE) {
2065 err = (*a->progress_cb)(a->progress_arg,
2066 GOT_STATUS_MERGE, path2);
2069 if (status != GOT_STATUS_NO_CHANGE &&
2070 status != GOT_STATUS_MODIFY &&
2071 status != GOT_STATUS_CONFLICT &&
2072 status != GOT_STATUS_ADD) {
2073 err = (*a->progress_cb)(a->progress_arg, status, path2);
2077 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2078 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2079 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2081 ie = got_fileindex_entry_get(a->fileindex, path1,
2084 return (*a->progress_cb)(a->progress_arg,
2085 GOT_STATUS_MISSING, path2);
2087 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2089 return got_error_from_errno("asprintf");
2091 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2097 case GOT_STATUS_NO_CHANGE:
2098 err = (*a->progress_cb)(a->progress_arg,
2099 GOT_STATUS_DELETE, path1);
2102 err = remove_ondisk_file(a->worktree->root_path, path1);
2106 got_fileindex_entry_mark_deleted_from_disk(ie);
2108 case GOT_STATUS_DELETE:
2109 case GOT_STATUS_MISSING:
2110 err = (*a->progress_cb)(a->progress_arg,
2111 GOT_STATUS_DELETE, path1);
2115 got_fileindex_entry_mark_deleted_from_disk(ie);
2117 case GOT_STATUS_ADD:
2118 case GOT_STATUS_MODIFY:
2119 case GOT_STATUS_CONFLICT:
2120 err = (*a->progress_cb)(a->progress_arg,
2121 GOT_STATUS_CANNOT_DELETE, path1);
2125 case GOT_STATUS_OBSTRUCTED:
2126 err = (*a->progress_cb)(a->progress_arg, status, path1);
2134 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2136 return got_error_from_errno("asprintf");
2137 ie = got_fileindex_entry_get(a->fileindex, path2,
2140 err = get_file_status(&status, &sb, ie, ondisk_path,
2144 if (status != GOT_STATUS_NO_CHANGE &&
2145 status != GOT_STATUS_MODIFY &&
2146 status != GOT_STATUS_CONFLICT &&
2147 status != GOT_STATUS_ADD) {
2148 err = (*a->progress_cb)(a->progress_arg,
2152 err = merge_blob(&local_changes_subsumed, a->worktree,
2153 NULL, ondisk_path, path2, sb.st_mode,
2154 a->label_orig, blob2, a->commit_id2, repo,
2157 if (status == GOT_STATUS_DELETE) {
2158 err = update_blob_fileindex_entry(a->worktree,
2159 a->fileindex, ie, ondisk_path, ie->path,
2165 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2166 err = install_blob(a->worktree, ondisk_path, path2,
2167 /* XXX get this from parent tree! */
2168 GOT_DEFAULT_FILE_MODE,
2169 sb.st_mode, blob2, 0, 0, repo,
2170 a->progress_cb, a->progress_arg);
2173 err = got_fileindex_entry_alloc(&ie,
2174 ondisk_path, path2, NULL, NULL);
2177 err = got_fileindex_entry_add(a->fileindex, ie);
2179 got_fileindex_entry_free(ie);
2189 struct check_merge_ok_arg {
2190 struct got_worktree *worktree;
2191 struct got_repository *repo;
2194 static const struct got_error *
2195 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2197 const struct got_error *err = NULL;
2198 struct check_merge_ok_arg *a = arg;
2199 unsigned char status;
2203 /* Reject merges into a work tree with mixed base commits. */
2204 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2205 SHA1_DIGEST_LENGTH))
2206 return got_error(GOT_ERR_MIXED_COMMITS);
2208 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2210 return got_error_from_errno("asprintf");
2212 /* Reject merges into a work tree with conflicted files. */
2213 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2216 if (status == GOT_STATUS_CONFLICT)
2217 return got_error(GOT_ERR_CONFLICTS);
2222 static const struct got_error *
2223 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2224 const char *fileindex_path, struct got_object_id *commit_id1,
2225 struct got_object_id *commit_id2, struct got_repository *repo,
2226 got_worktree_checkout_cb progress_cb, void *progress_arg,
2227 got_cancel_cb cancel_cb, void *cancel_arg)
2229 const struct got_error *err = NULL, *sync_err;
2230 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2231 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2232 struct merge_file_cb_arg arg;
2233 char *label_orig = NULL;
2238 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2239 worktree->path_prefix);
2243 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2247 err = got_object_id_str(&id_str, commit_id1);
2251 if (asprintf(&label_orig, "%s: commit %s",
2252 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2253 err = got_error_from_errno("asprintf");
2260 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2261 worktree->path_prefix);
2265 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2269 arg.worktree = worktree;
2270 arg.fileindex = fileindex;
2271 arg.progress_cb = progress_cb;
2272 arg.progress_arg = progress_arg;
2273 arg.cancel_cb = cancel_cb;
2274 arg.cancel_arg = cancel_arg;
2275 arg.label_orig = label_orig;
2276 arg.commit_id2 = commit_id2;
2277 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2278 sync_err = sync_fileindex(fileindex, fileindex_path);
2279 if (sync_err && err == NULL)
2283 got_object_tree_close(tree1);
2285 got_object_tree_close(tree2);
2290 const struct got_error *
2291 got_worktree_merge_files(struct got_worktree *worktree,
2292 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2293 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2294 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2296 const struct got_error *err, *unlockerr;
2297 char *fileindex_path = NULL;
2298 struct got_fileindex *fileindex = NULL;
2299 struct check_merge_ok_arg mok_arg;
2301 err = lock_worktree(worktree, LOCK_EX);
2305 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2309 mok_arg.worktree = worktree;
2310 mok_arg.repo = repo;
2311 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2316 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2317 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2320 got_fileindex_free(fileindex);
2321 free(fileindex_path);
2322 unlockerr = lock_worktree(worktree, LOCK_SH);
2323 if (unlockerr && err == NULL)
2328 struct diff_dir_cb_arg {
2329 struct got_fileindex *fileindex;
2330 struct got_worktree *worktree;
2331 const char *status_path;
2332 size_t status_path_len;
2333 struct got_repository *repo;
2334 got_worktree_status_cb status_cb;
2336 got_cancel_cb cancel_cb;
2338 /* A pathlist containing per-directory pathlists of ignore patterns. */
2339 struct got_pathlist_head ignores;
2340 int report_unchanged;
2343 static const struct got_error *
2344 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2345 int dirfd, const char *de_name,
2346 got_worktree_status_cb status_cb, void *status_arg,
2347 struct got_repository *repo, int report_unchanged)
2349 const struct got_error *err = NULL;
2350 unsigned char status = GOT_STATUS_NO_CHANGE;
2351 unsigned char staged_status = get_staged_status(ie);
2353 struct got_object_id blob_id, commit_id, staged_blob_id;
2354 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2355 struct got_object_id *staged_blob_idp = NULL;
2357 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2361 if (status == GOT_STATUS_NO_CHANGE &&
2362 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2365 if (got_fileindex_entry_has_blob(ie)) {
2366 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2367 blob_idp = &blob_id;
2369 if (got_fileindex_entry_has_commit(ie)) {
2370 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2371 commit_idp = &commit_id;
2373 if (staged_status == GOT_STATUS_ADD ||
2374 staged_status == GOT_STATUS_MODIFY) {
2375 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2376 SHA1_DIGEST_LENGTH);
2377 staged_blob_idp = &staged_blob_id;
2380 return (*status_cb)(status_arg, status, staged_status,
2381 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2384 static const struct got_error *
2385 status_old_new(void *arg, struct got_fileindex_entry *ie,
2386 struct dirent *de, const char *parent_path, int dirfd)
2388 const struct got_error *err = NULL;
2389 struct diff_dir_cb_arg *a = arg;
2392 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2393 return got_error(GOT_ERR_CANCELLED);
2395 if (got_path_cmp(parent_path, a->status_path,
2396 strlen(parent_path), a->status_path_len) != 0 &&
2397 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2400 if (parent_path[0]) {
2401 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2402 parent_path, de->d_name) == -1)
2403 return got_error_from_errno("asprintf");
2405 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2407 return got_error_from_errno("asprintf");
2410 err = report_file_status(ie, abspath, dirfd, de->d_name,
2411 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2416 static const struct got_error *
2417 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2419 struct diff_dir_cb_arg *a = arg;
2420 struct got_object_id blob_id, commit_id;
2421 unsigned char status;
2423 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2424 return got_error(GOT_ERR_CANCELLED);
2426 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2429 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2430 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2431 if (got_fileindex_entry_has_file_on_disk(ie))
2432 status = GOT_STATUS_MISSING;
2434 status = GOT_STATUS_DELETE;
2435 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2436 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2440 free_ignorelist(struct got_pathlist_head *ignorelist)
2442 struct got_pathlist_entry *pe;
2444 TAILQ_FOREACH(pe, ignorelist, entry)
2445 free((char *)pe->path);
2446 got_pathlist_free(ignorelist);
2450 free_ignores(struct got_pathlist_head *ignores)
2452 struct got_pathlist_entry *pe;
2454 TAILQ_FOREACH(pe, ignores, entry) {
2455 struct got_pathlist_head *ignorelist = pe->data;
2456 free_ignorelist(ignorelist);
2457 free((char *)pe->path);
2459 got_pathlist_free(ignores);
2462 static const struct got_error *
2463 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2465 const struct got_error *err = NULL;
2466 struct got_pathlist_entry *pe = NULL;
2467 struct got_pathlist_head *ignorelist;
2468 char *line = NULL, *pattern, *dirpath = NULL;
2469 size_t linesize = 0;
2472 ignorelist = calloc(1, sizeof(*ignorelist));
2473 if (ignorelist == NULL)
2474 return got_error_from_errno("calloc");
2475 TAILQ_INIT(ignorelist);
2477 while ((linelen = getline(&line, &linesize, f)) != -1) {
2478 if (linelen > 0 && line[linelen - 1] == '\n')
2479 line[linelen - 1] = '\0';
2481 /* Git's ignores may contain comments. */
2485 /* Git's negated patterns are not (yet?) supported. */
2489 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2491 err = got_error_from_errno("asprintf");
2494 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2499 err = got_error_from_errno("getline");
2503 dirpath = strdup(path);
2504 if (dirpath == NULL) {
2505 err = got_error_from_errno("strdup");
2508 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2511 if (err || pe == NULL) {
2513 free_ignorelist(ignorelist);
2519 match_ignores(struct got_pathlist_head *ignores, const char *path)
2521 struct got_pathlist_entry *pe;
2523 /* Handle patterns which match in all directories. */
2524 TAILQ_FOREACH(pe, ignores, entry) {
2525 struct got_pathlist_head *ignorelist = pe->data;
2526 struct got_pathlist_entry *pi;
2528 TAILQ_FOREACH(pi, ignorelist, entry) {
2529 const char *p, *pattern = pi->path;
2531 if (strncmp(pattern, "**/", 3) != 0)
2536 if (fnmatch(pattern, p,
2537 FNM_PATHNAME | FNM_LEADING_DIR)) {
2538 /* Retry in next directory. */
2539 while (*p && *p != '/')
2551 * The ignores pathlist contains ignore lists from children before
2552 * parents, so we can find the most specific ignorelist by walking
2553 * ignores backwards.
2555 pe = TAILQ_LAST(ignores, got_pathlist_head);
2557 if (got_path_is_child(path, pe->path, pe->path_len)) {
2558 struct got_pathlist_head *ignorelist = pe->data;
2559 struct got_pathlist_entry *pi;
2560 TAILQ_FOREACH(pi, ignorelist, entry) {
2561 const char *pattern = pi->path;
2562 int flags = FNM_LEADING_DIR;
2563 if (strstr(pattern, "/**/") == NULL)
2564 flags |= FNM_PATHNAME;
2565 if (fnmatch(pattern, path, flags))
2570 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2576 static const struct got_error *
2577 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2578 const char *path, const char *ignores_filename)
2580 const struct got_error *err = NULL;
2582 FILE *ignoresfile = NULL;
2584 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2585 path[0] ? "/" : "", ignores_filename) == -1)
2586 return got_error_from_errno("asprintf");
2588 ignoresfile = fopen(ignorespath, "r");
2589 if (ignoresfile == NULL) {
2590 if (errno != ENOENT && errno != EACCES)
2591 err = got_error_from_errno2("fopen",
2594 err = read_ignores(ignores, path, ignoresfile);
2596 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2597 err = got_error_from_errno2("fclose", path);
2602 static const struct got_error *
2603 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2605 const struct got_error *err = NULL;
2606 struct diff_dir_cb_arg *a = arg;
2609 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2610 return got_error(GOT_ERR_CANCELLED);
2612 /* XXX ignore symlinks for now */
2613 if (de->d_type == DT_LNK)
2616 if (parent_path[0]) {
2617 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2618 return got_error_from_errno("asprintf");
2623 if (de->d_type == DT_DIR) {
2624 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2627 err = add_ignores(&a->ignores, a->worktree->root_path,
2628 path, ".gitignore");
2630 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2631 && !match_ignores(&a->ignores, path))
2632 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2633 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2639 static const struct got_error *
2640 report_single_file_status(const char *path, const char *ondisk_path,
2641 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2642 void *status_arg, struct got_repository *repo, int report_unchanged)
2644 struct got_fileindex_entry *ie;
2647 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2649 return report_file_status(ie, ondisk_path, -1, NULL,
2650 status_cb, status_arg, repo, report_unchanged);
2652 if (lstat(ondisk_path, &sb) == -1) {
2653 if (errno != ENOENT)
2654 return got_error_from_errno2("lstat", ondisk_path);
2655 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2656 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2660 if (S_ISREG(sb.st_mode))
2661 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2662 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2667 static const struct got_error *
2668 worktree_status(struct got_worktree *worktree, const char *path,
2669 struct got_fileindex *fileindex, struct got_repository *repo,
2670 got_worktree_status_cb status_cb, void *status_arg,
2671 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2672 int report_unchanged)
2674 const struct got_error *err = NULL;
2676 struct got_fileindex_diff_dir_cb fdiff_cb;
2677 struct diff_dir_cb_arg arg;
2678 char *ondisk_path = NULL;
2680 if (asprintf(&ondisk_path, "%s%s%s",
2681 worktree->root_path, path[0] ? "/" : "", path) == -1)
2682 return got_error_from_errno("asprintf");
2684 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2686 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2687 err = got_error_from_errno2("open", ondisk_path);
2689 err = report_single_file_status(path, ondisk_path,
2690 fileindex, status_cb, status_arg, repo,
2693 fdiff_cb.diff_old_new = status_old_new;
2694 fdiff_cb.diff_old = status_old;
2695 fdiff_cb.diff_new = status_new;
2696 arg.fileindex = fileindex;
2697 arg.worktree = worktree;
2698 arg.status_path = path;
2699 arg.status_path_len = strlen(path);
2701 arg.status_cb = status_cb;
2702 arg.status_arg = status_arg;
2703 arg.cancel_cb = cancel_cb;
2704 arg.cancel_arg = cancel_arg;
2705 arg.report_unchanged = report_unchanged;
2706 TAILQ_INIT(&arg.ignores);
2708 err = add_ignores(&arg.ignores, worktree->root_path,
2709 path, ".cvsignore");
2711 err = add_ignores(&arg.ignores,
2712 worktree->root_path, path, ".gitignore");
2715 err = got_fileindex_diff_dir(fileindex, fd,
2716 worktree->root_path, path, repo, &fdiff_cb, &arg);
2717 free_ignores(&arg.ignores);
2720 if (fd != -1 && close(fd) != 0 && err == NULL)
2721 err = got_error_from_errno("close");
2726 const struct got_error *
2727 got_worktree_status(struct got_worktree *worktree,
2728 struct got_pathlist_head *paths, struct got_repository *repo,
2729 got_worktree_status_cb status_cb, void *status_arg,
2730 got_cancel_cb cancel_cb, void *cancel_arg)
2732 const struct got_error *err = NULL;
2733 char *fileindex_path = NULL;
2734 struct got_fileindex *fileindex = NULL;
2735 struct got_pathlist_entry *pe;
2737 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2741 TAILQ_FOREACH(pe, paths, entry) {
2742 err = worktree_status(worktree, pe->path, fileindex, repo,
2743 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2747 free(fileindex_path);
2748 got_fileindex_free(fileindex);
2752 const struct got_error *
2753 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2756 const struct got_error *err = NULL;
2757 char *resolved, *cwd = NULL, *path = NULL;
2762 resolved = realpath(arg, NULL);
2763 if (resolved == NULL) {
2764 if (errno != ENOENT)
2765 return got_error_from_errno2("realpath", arg);
2766 cwd = getcwd(NULL, 0);
2768 return got_error_from_errno("getcwd");
2769 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2770 err = got_error_from_errno("asprintf");
2775 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2776 strlen(got_worktree_get_root_path(worktree)))) {
2777 err = got_error(GOT_ERR_BAD_PATH);
2781 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2782 err = got_path_skip_common_ancestor(&path,
2783 got_worktree_get_root_path(worktree), resolved);
2789 err = got_error_from_errno("strdup");
2794 /* XXX status walk can't deal with trailing slash! */
2796 while (len > 0 && path[len - 1] == '/') {
2797 path[len - 1] = '\0';
2810 struct schedule_addition_args {
2811 struct got_worktree *worktree;
2812 struct got_fileindex *fileindex;
2813 got_worktree_checkout_cb progress_cb;
2815 struct got_repository *repo;
2818 static const struct got_error *
2819 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2820 const char *relpath, struct got_object_id *blob_id,
2821 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2822 int dirfd, const char *de_name)
2824 struct schedule_addition_args *a = arg;
2825 const struct got_error *err = NULL;
2826 struct got_fileindex_entry *ie;
2830 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2832 return got_error_from_errno("asprintf");
2834 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2836 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2840 /* Re-adding an existing entry is a no-op. */
2841 if (status == GOT_STATUS_ADD)
2843 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2848 if (status != GOT_STATUS_UNVERSIONED) {
2849 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2853 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2857 err = got_fileindex_entry_add(a->fileindex, ie);
2859 got_fileindex_entry_free(ie);
2866 if (status == GOT_STATUS_ADD)
2868 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2871 const struct got_error *
2872 got_worktree_schedule_add(struct got_worktree *worktree,
2873 struct got_pathlist_head *paths,
2874 got_worktree_checkout_cb progress_cb, void *progress_arg,
2875 struct got_repository *repo, int no_ignores)
2877 struct got_fileindex *fileindex = NULL;
2878 char *fileindex_path = NULL;
2879 const struct got_error *err = NULL, *sync_err, *unlockerr;
2880 struct got_pathlist_entry *pe;
2881 struct schedule_addition_args saa;
2883 err = lock_worktree(worktree, LOCK_EX);
2887 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2891 saa.worktree = worktree;
2892 saa.fileindex = fileindex;
2893 saa.progress_cb = progress_cb;
2894 saa.progress_arg = progress_arg;
2897 TAILQ_FOREACH(pe, paths, entry) {
2898 err = worktree_status(worktree, pe->path, fileindex, repo,
2899 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2903 sync_err = sync_fileindex(fileindex, fileindex_path);
2904 if (sync_err && err == NULL)
2907 free(fileindex_path);
2909 got_fileindex_free(fileindex);
2910 unlockerr = lock_worktree(worktree, LOCK_SH);
2911 if (unlockerr && err == NULL)
2916 struct schedule_deletion_args {
2917 struct got_worktree *worktree;
2918 struct got_fileindex *fileindex;
2919 got_worktree_delete_cb progress_cb;
2921 struct got_repository *repo;
2922 int delete_local_mods;
2925 static const struct got_error *
2926 schedule_for_deletion(void *arg, unsigned char status,
2927 unsigned char staged_status, const char *relpath,
2928 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2929 struct got_object_id *commit_id, int dirfd, const char *de_name)
2931 struct schedule_deletion_args *a = arg;
2932 const struct got_error *err = NULL;
2933 struct got_fileindex_entry *ie = NULL;
2937 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2939 return got_error(GOT_ERR_BAD_PATH);
2941 staged_status = get_staged_status(ie);
2942 if (staged_status != GOT_STATUS_NO_CHANGE) {
2943 if (staged_status == GOT_STATUS_DELETE)
2945 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2948 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2950 return got_error_from_errno("asprintf");
2952 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
2957 if (status != GOT_STATUS_NO_CHANGE) {
2958 if (status == GOT_STATUS_DELETE)
2960 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
2961 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2964 if (status != GOT_STATUS_MODIFY &&
2965 status != GOT_STATUS_MISSING) {
2966 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2971 if (status != GOT_STATUS_MISSING) {
2973 if (unlinkat(dirfd, de_name, 0) != 0) {
2974 err = got_error_from_errno2("unlinkat",
2978 } else if (unlink(ondisk_path) != 0) {
2979 err = got_error_from_errno2("unlink", ondisk_path);
2984 got_fileindex_entry_mark_deleted_from_disk(ie);
2989 if (status == GOT_STATUS_DELETE)
2991 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
2992 staged_status, relpath);
2995 const struct got_error *
2996 got_worktree_schedule_delete(struct got_worktree *worktree,
2997 struct got_pathlist_head *paths, int delete_local_mods,
2998 got_worktree_delete_cb progress_cb, void *progress_arg,
2999 struct got_repository *repo)
3001 struct got_fileindex *fileindex = NULL;
3002 char *fileindex_path = NULL;
3003 const struct got_error *err = NULL, *sync_err, *unlockerr;
3004 struct got_pathlist_entry *pe;
3005 struct schedule_deletion_args sda;
3007 err = lock_worktree(worktree, LOCK_EX);
3011 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3015 sda.worktree = worktree;
3016 sda.fileindex = fileindex;
3017 sda.progress_cb = progress_cb;
3018 sda.progress_arg = progress_arg;
3020 sda.delete_local_mods = delete_local_mods;
3022 TAILQ_FOREACH(pe, paths, entry) {
3023 err = worktree_status(worktree, pe->path, fileindex, repo,
3024 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3028 sync_err = sync_fileindex(fileindex, fileindex_path);
3029 if (sync_err && err == NULL)
3032 free(fileindex_path);
3034 got_fileindex_free(fileindex);
3035 unlockerr = lock_worktree(worktree, LOCK_SH);
3036 if (unlockerr && err == NULL)
3041 static const struct got_error *
3042 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3044 const struct got_error *err = NULL;
3046 size_t linesize = 0, n;
3049 linelen = getline(&line, &linesize, infile);
3050 if (linelen == -1) {
3051 if (ferror(infile)) {
3052 err = got_error_from_errno("getline");
3058 n = fwrite(line, 1, linelen, outfile);
3060 err = got_ferror(outfile, GOT_ERR_IO);
3065 n = fwrite(line, 1, linelen, rejectfile);
3067 err = got_ferror(outfile, GOT_ERR_IO);
3074 static const struct got_error *
3075 skip_one_line(FILE *f)
3078 size_t linesize = 0;
3081 linelen = getline(&line, &linesize, f);
3082 if (linelen == -1) {
3084 return got_error_from_errno("getline");
3091 static const struct got_error *
3092 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3093 int start_old, int end_old, int start_new, int end_new,
3094 FILE *outfile, FILE *rejectfile)
3096 const struct got_error *err;
3098 /* Copy old file's lines leading up to patch. */
3099 while (!feof(f1) && *line_cur1 < start_old) {
3100 err = copy_one_line(f1, outfile, NULL);
3105 /* Skip new file's lines leading up to patch. */
3106 while (!feof(f2) && *line_cur2 < start_new) {
3108 err = copy_one_line(f2, NULL, rejectfile);
3110 err = skip_one_line(f2);
3115 /* Copy patched lines. */
3116 while (!feof(f2) && *line_cur2 <= end_new) {
3117 err = copy_one_line(f2, outfile, NULL);
3122 /* Skip over old file's replaced lines. */
3123 while (!feof(f1) && *line_cur1 <= end_old) {
3125 err = copy_one_line(f1, NULL, rejectfile);
3127 err = skip_one_line(f1);
3136 static const struct got_error *
3137 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3138 FILE *outfile, FILE *rejectfile)
3140 const struct got_error *err;
3143 /* Copy old file's lines until EOF. */
3145 err = copy_one_line(f1, outfile, NULL);
3152 /* Copy new file's lines until EOF. */
3154 err = copy_one_line(f2, NULL, rejectfile);
3164 static const struct got_error *
3165 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3166 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3167 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3168 int *line_cur2, FILE *outfile, FILE *rejectfile,
3169 got_worktree_patch_cb patch_cb, void *patch_arg)
3171 const struct got_error *err = NULL;
3172 int start_old = change->cv.a;
3173 int end_old = change->cv.b;
3174 int start_new = change->cv.c;
3175 int end_new = change->cv.d;
3179 *choice = GOT_PATCH_CHOICE_NONE;
3181 hunkfile = got_opentemp();
3182 if (hunkfile == NULL)
3183 return got_error_from_errno("got_opentemp");
3188 /* XXX TODO needs error checking */
3189 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3191 if (fseek(f1, pos1, SEEK_SET) == -1) {
3192 err = got_ferror(f1, GOT_ERR_IO);
3195 if (fseek(f2, pos2, SEEK_SET) == -1) {
3196 err = got_ferror(f1, GOT_ERR_IO);
3199 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3200 err = got_ferror(hunkfile, GOT_ERR_IO);
3204 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3205 hunkfile, n, nchanges);
3210 case GOT_PATCH_CHOICE_YES:
3211 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3212 end_old, start_new, end_new, outfile, rejectfile);
3214 case GOT_PATCH_CHOICE_NO:
3215 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3216 end_old, start_new, end_new, rejectfile, outfile);
3218 case GOT_PATCH_CHOICE_QUIT:
3221 err = got_error(GOT_ERR_PATCH_CHOICE);
3225 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3226 err = got_error_from_errno("fclose");
3230 struct revert_file_args {
3231 struct got_worktree *worktree;
3232 struct got_fileindex *fileindex;
3233 got_worktree_checkout_cb progress_cb;
3235 got_worktree_patch_cb patch_cb;
3237 struct got_repository *repo;
3240 static const struct got_error *
3241 create_patched_content(char **path_outfile, int reverse_patch,
3242 struct got_object_id *blob_id, const char *path2,
3243 int dirfd2, const char *de_name2,
3244 const char *relpath, struct got_repository *repo,
3245 got_worktree_patch_cb patch_cb, void *patch_arg)
3247 const struct got_error *err;
3248 struct got_blob_object *blob = NULL;
3249 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3251 char *path1 = NULL, *id_str = NULL;
3252 struct stat sb1, sb2;
3253 struct got_diff_changes *changes = NULL;
3254 struct got_diff_state *ds = NULL;
3255 struct got_diff_args *args = NULL;
3256 struct got_diff_change *change;
3257 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3260 *path_outfile = NULL;
3262 err = got_object_id_str(&id_str, blob_id);
3267 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3269 err = got_error_from_errno2("openat", path2);
3273 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3275 err = got_error_from_errno2("open", path2);
3279 if (fstat(fd2, &sb2) == -1) {
3280 err = got_error_from_errno2("fstat", path2);
3284 f2 = fdopen(fd2, "r");
3286 err = got_error_from_errno2("fopen", path2);
3291 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3295 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3299 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3303 if (stat(path1, &sb1) == -1) {
3304 err = got_error_from_errno2("stat", path1);
3308 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3309 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3313 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3317 if (fseek(f1, 0L, SEEK_SET) == -1)
3318 return got_ferror(f1, GOT_ERR_IO);
3319 if (fseek(f2, 0L, SEEK_SET) == -1)
3320 return got_ferror(f2, GOT_ERR_IO);
3321 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3323 err = apply_or_reject_change(&choice, change, ++n,
3324 changes->nchanges, ds, args, diff_flags, relpath,
3325 f1, f2, &line_cur1, &line_cur2,
3326 reverse_patch ? NULL : outfile,
3327 reverse_patch ? outfile : NULL,
3328 patch_cb, patch_arg);
3331 if (choice == GOT_PATCH_CHOICE_YES)
3333 else if (choice == GOT_PATCH_CHOICE_QUIT)
3337 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3338 reverse_patch ? NULL : outfile,
3339 reverse_patch ? outfile : NULL);
3343 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3344 err = got_error_from_errno2("chmod", path2);
3351 got_object_blob_close(blob);
3352 if (f1 && fclose(f1) == EOF && err == NULL)
3353 err = got_error_from_errno2("fclose", path1);
3354 if (f2 && fclose(f2) == EOF && err == NULL)
3355 err = got_error_from_errno2("fclose", path2);
3356 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3357 err = got_error_from_errno2("close", path2);
3358 if (outfile && fclose(outfile) == EOF && err == NULL)
3359 err = got_error_from_errno2("fclose", *path_outfile);
3360 if (path1 && unlink(path1) == -1 && err == NULL)
3361 err = got_error_from_errno2("unlink", path1);
3362 if (err || !have_content) {
3363 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3364 err = got_error_from_errno2("unlink", *path_outfile);
3365 free(*path_outfile);
3366 *path_outfile = NULL;
3370 got_diff_state_free(ds);
3374 got_diff_free_changes(changes);
3379 static const struct got_error *
3380 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3381 const char *relpath, struct got_object_id *blob_id,
3382 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3383 int dirfd, const char *de_name)
3385 struct revert_file_args *a = arg;
3386 const struct got_error *err = NULL;
3387 char *parent_path = NULL;
3388 struct got_fileindex_entry *ie;
3389 struct got_tree_object *tree = NULL;
3390 struct got_object_id *tree_id = NULL;
3391 const struct got_tree_entry *te = NULL;
3392 char *tree_path = NULL, *te_name;
3393 char *ondisk_path = NULL, *path_content = NULL;
3394 struct got_blob_object *blob = NULL;
3396 /* Reverting a staged deletion is a no-op. */
3397 if (status == GOT_STATUS_DELETE &&
3398 staged_status != GOT_STATUS_NO_CHANGE)
3401 if (status == GOT_STATUS_UNVERSIONED)
3402 return (*a->progress_cb)(a->progress_arg,
3403 GOT_STATUS_UNVERSIONED, relpath);
3405 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3407 return got_error(GOT_ERR_BAD_PATH);
3409 /* Construct in-repository path of tree which contains this blob. */
3410 err = got_path_dirname(&parent_path, ie->path);
3412 if (err->code != GOT_ERR_BAD_PATH)
3414 parent_path = strdup("/");
3415 if (parent_path == NULL) {
3416 err = got_error_from_errno("strdup");
3420 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3421 tree_path = strdup(parent_path);
3422 if (tree_path == NULL) {
3423 err = got_error_from_errno("strdup");
3427 if (got_path_is_root_dir(parent_path)) {
3428 tree_path = strdup(a->worktree->path_prefix);
3429 if (tree_path == NULL) {
3430 err = got_error_from_errno("strdup");
3434 if (asprintf(&tree_path, "%s/%s",
3435 a->worktree->path_prefix, parent_path) == -1) {
3436 err = got_error_from_errno("asprintf");
3442 err = got_object_id_by_path(&tree_id, a->repo,
3443 a->worktree->base_commit_id, tree_path);
3445 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3446 (status == GOT_STATUS_ADD ||
3447 staged_status == GOT_STATUS_ADD)))
3450 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3454 te_name = basename(ie->path);
3455 if (te_name == NULL) {
3456 err = got_error_from_errno2("basename", ie->path);
3460 te = got_object_tree_find_entry(tree, te_name);
3461 if (te == NULL && status != GOT_STATUS_ADD &&
3462 staged_status != GOT_STATUS_ADD) {
3463 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3469 case GOT_STATUS_ADD:
3471 int choice = GOT_PATCH_CHOICE_NONE;
3472 err = (*a->patch_cb)(&choice, a->patch_arg,
3473 status, ie->path, NULL, 1, 1);
3476 if (choice != GOT_PATCH_CHOICE_YES)
3479 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3483 got_fileindex_entry_remove(a->fileindex, ie);
3485 case GOT_STATUS_DELETE:
3487 int choice = GOT_PATCH_CHOICE_NONE;
3488 err = (*a->patch_cb)(&choice, a->patch_arg,
3489 status, ie->path, NULL, 1, 1);
3492 if (choice != GOT_PATCH_CHOICE_YES)
3496 case GOT_STATUS_MODIFY:
3497 case GOT_STATUS_MODE_CHANGE:
3498 case GOT_STATUS_CONFLICT:
3499 case GOT_STATUS_MISSING: {
3500 struct got_object_id id;
3501 if (staged_status == GOT_STATUS_ADD ||
3502 staged_status == GOT_STATUS_MODIFY) {
3503 memcpy(id.sha1, ie->staged_blob_sha1,
3504 SHA1_DIGEST_LENGTH);
3506 memcpy(id.sha1, ie->blob_sha1,
3507 SHA1_DIGEST_LENGTH);
3508 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3512 if (asprintf(&ondisk_path, "%s/%s",
3513 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3514 err = got_error_from_errno("asprintf");
3518 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3519 status == GOT_STATUS_CONFLICT)) {
3520 err = create_patched_content(&path_content, 1, &id,
3521 ondisk_path, dirfd, de_name, ie->path, a->repo,
3522 a->patch_cb, a->patch_arg);
3523 if (err || path_content == NULL)
3525 if (rename(path_content, ondisk_path) == -1) {
3526 err = got_error_from_errno3("rename",
3527 path_content, ondisk_path);
3531 err = install_blob(a->worktree, ondisk_path, ie->path,
3532 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3533 got_fileindex_perms_to_st(ie), blob, 0, 1,
3534 a->repo, a->progress_cb, a->progress_arg);
3537 if (status == GOT_STATUS_DELETE ||
3538 status == GOT_STATUS_MODE_CHANGE) {
3539 err = update_blob_fileindex_entry(a->worktree,
3540 a->fileindex, ie, ondisk_path, ie->path,
3557 got_object_blob_close(blob);
3559 got_object_tree_close(tree);
3564 const struct got_error *
3565 got_worktree_revert(struct got_worktree *worktree,
3566 struct got_pathlist_head *paths,
3567 got_worktree_checkout_cb progress_cb, void *progress_arg,
3568 got_worktree_patch_cb patch_cb, void *patch_arg,
3569 struct got_repository *repo)
3571 struct got_fileindex *fileindex = NULL;
3572 char *fileindex_path = NULL;
3573 const struct got_error *err = NULL, *unlockerr = NULL;
3574 const struct got_error *sync_err = NULL;
3575 struct got_pathlist_entry *pe;
3576 struct revert_file_args rfa;
3578 err = lock_worktree(worktree, LOCK_EX);
3582 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3586 rfa.worktree = worktree;
3587 rfa.fileindex = fileindex;
3588 rfa.progress_cb = progress_cb;
3589 rfa.progress_arg = progress_arg;
3590 rfa.patch_cb = patch_cb;
3591 rfa.patch_arg = patch_arg;
3593 TAILQ_FOREACH(pe, paths, entry) {
3594 err = worktree_status(worktree, pe->path, fileindex, repo,
3595 revert_file, &rfa, NULL, NULL, 0, 0);
3599 sync_err = sync_fileindex(fileindex, fileindex_path);
3600 if (sync_err && err == NULL)
3603 free(fileindex_path);
3605 got_fileindex_free(fileindex);
3606 unlockerr = lock_worktree(worktree, LOCK_SH);
3607 if (unlockerr && err == NULL)
3613 free_commitable(struct got_commitable *ct)
3616 free(ct->in_repo_path);
3617 free(ct->ondisk_path);
3619 free(ct->base_blob_id);
3620 free(ct->staged_blob_id);
3621 free(ct->base_commit_id);
3625 struct collect_commitables_arg {
3626 struct got_pathlist_head *commitable_paths;
3627 struct got_repository *repo;
3628 struct got_worktree *worktree;
3629 int have_staged_files;
3632 static const struct got_error *
3633 collect_commitables(void *arg, unsigned char status,
3634 unsigned char staged_status, const char *relpath,
3635 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3636 struct got_object_id *commit_id, int dirfd, const char *de_name)
3638 struct collect_commitables_arg *a = arg;
3639 const struct got_error *err = NULL;
3640 struct got_commitable *ct = NULL;
3641 struct got_pathlist_entry *new = NULL;
3642 char *parent_path = NULL, *path = NULL;
3645 if (a->have_staged_files) {
3646 if (staged_status != GOT_STATUS_MODIFY &&
3647 staged_status != GOT_STATUS_ADD &&
3648 staged_status != GOT_STATUS_DELETE)
3651 if (status == GOT_STATUS_CONFLICT)
3652 return got_error(GOT_ERR_COMMIT_CONFLICT);
3654 if (status != GOT_STATUS_MODIFY &&
3655 status != GOT_STATUS_MODE_CHANGE &&
3656 status != GOT_STATUS_ADD &&
3657 status != GOT_STATUS_DELETE)
3661 if (asprintf(&path, "/%s", relpath) == -1) {
3662 err = got_error_from_errno("asprintf");
3665 if (strcmp(path, "/") == 0) {
3666 parent_path = strdup("");
3667 if (parent_path == NULL)
3668 return got_error_from_errno("strdup");
3670 err = got_path_dirname(&parent_path, path);
3675 ct = calloc(1, sizeof(*ct));
3677 err = got_error_from_errno("calloc");
3681 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3683 err = got_error_from_errno("asprintf");
3686 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3687 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3690 if (fstatat(dirfd, de_name, &sb,
3691 AT_SYMLINK_NOFOLLOW) == -1) {
3692 err = got_error_from_errno2("fstatat",
3696 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3697 err = got_error_from_errno2("lstat", ct->ondisk_path);
3700 ct->mode = sb.st_mode;
3703 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3704 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3706 err = got_error_from_errno("asprintf");
3710 ct->status = status;
3711 ct->staged_status = staged_status;
3712 ct->blob_id = NULL; /* will be filled in when blob gets created */
3713 if (ct->status != GOT_STATUS_ADD &&
3714 ct->staged_status != GOT_STATUS_ADD) {
3715 ct->base_blob_id = got_object_id_dup(blob_id);
3716 if (ct->base_blob_id == NULL) {
3717 err = got_error_from_errno("got_object_id_dup");
3720 ct->base_commit_id = got_object_id_dup(commit_id);
3721 if (ct->base_commit_id == NULL) {
3722 err = got_error_from_errno("got_object_id_dup");
3726 if (ct->staged_status == GOT_STATUS_ADD ||
3727 ct->staged_status == GOT_STATUS_MODIFY) {
3728 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3729 if (ct->staged_blob_id == NULL) {
3730 err = got_error_from_errno("got_object_id_dup");
3734 ct->path = strdup(path);
3735 if (ct->path == NULL) {
3736 err = got_error_from_errno("strdup");
3739 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3741 if (ct && (err || new == NULL))
3742 free_commitable(ct);
3748 static const struct got_error *write_tree(struct got_object_id **,
3749 struct got_tree_object *, const char *, struct got_pathlist_head *,
3750 got_worktree_status_cb status_cb, void *status_arg,
3751 struct got_repository *);
3753 static const struct got_error *
3754 write_subtree(struct got_object_id **new_subtree_id,
3755 struct got_tree_entry *te, const char *parent_path,
3756 struct got_pathlist_head *commitable_paths,
3757 got_worktree_status_cb status_cb, void *status_arg,
3758 struct got_repository *repo)
3760 const struct got_error *err = NULL;
3761 struct got_tree_object *subtree;
3764 if (asprintf(&subpath, "%s%s%s", parent_path,
3765 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3766 return got_error_from_errno("asprintf");
3768 err = got_object_open_as_tree(&subtree, repo, &te->id);
3772 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3773 status_cb, status_arg, repo);
3774 got_object_tree_close(subtree);
3779 static const struct got_error *
3780 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3782 const struct got_error *err = NULL;
3783 char *ct_parent_path = NULL;
3787 if (strchr(ct->in_repo_path, '/') == NULL) {
3788 *match = got_path_is_root_dir(path);
3792 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3795 *match = (strcmp(path, ct_parent_path) == 0);
3796 free(ct_parent_path);
3801 get_ct_file_mode(struct got_commitable *ct)
3803 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3806 static const struct got_error *
3807 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3808 struct got_tree_entry *te, struct got_commitable *ct)
3810 const struct got_error *err = NULL;
3814 err = got_object_tree_entry_dup(new_te, te);
3818 (*new_te)->mode = get_ct_file_mode(ct);
3820 if (ct->staged_status == GOT_STATUS_MODIFY)
3821 memcpy(&(*new_te)->id, ct->staged_blob_id,
3822 sizeof((*new_te)->id));
3824 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3826 if (err && *new_te) {
3833 static const struct got_error *
3834 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3835 struct got_commitable *ct)
3837 const struct got_error *err = NULL;
3842 *new_te = calloc(1, sizeof(**new_te));
3843 if (*new_te == NULL)
3844 return got_error_from_errno("calloc");
3846 ct_name = basename(ct->path);
3847 if (ct_name == NULL) {
3848 err = got_error_from_errno2("basename", ct->path);
3851 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3852 sizeof((*new_te)->name)) {
3853 err = got_error(GOT_ERR_NO_SPACE);
3857 (*new_te)->mode = get_ct_file_mode(ct);
3859 if (ct->staged_status == GOT_STATUS_ADD)
3860 memcpy(&(*new_te)->id, ct->staged_blob_id,
3861 sizeof((*new_te)->id));
3863 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3865 if (err && *new_te) {
3872 static const struct got_error *
3873 insert_tree_entry(struct got_tree_entry *new_te,
3874 struct got_pathlist_head *paths)
3876 const struct got_error *err = NULL;
3877 struct got_pathlist_entry *new_pe;
3879 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3883 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3887 static const struct got_error *
3888 report_ct_status(struct got_commitable *ct,
3889 got_worktree_status_cb status_cb, void *status_arg)
3891 const char *ct_path = ct->path;
3892 unsigned char status;
3894 while (ct_path[0] == '/')
3897 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3898 status = ct->staged_status;
3900 status = ct->status;
3902 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3903 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3906 static const struct got_error *
3907 match_modified_subtree(int *modified, struct got_tree_entry *te,
3908 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3910 const struct got_error *err = NULL;
3911 struct got_pathlist_entry *pe;
3916 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3917 got_path_is_root_dir(base_tree_path) ? "" : "/",
3919 return got_error_from_errno("asprintf");
3921 TAILQ_FOREACH(pe, commitable_paths, entry) {
3922 struct got_commitable *ct = pe->data;
3923 *modified = got_path_is_child(ct->in_repo_path, te_path,
3933 static const struct got_error *
3934 match_deleted_or_modified_ct(struct got_commitable **ctp,
3935 struct got_tree_entry *te, const char *base_tree_path,
3936 struct got_pathlist_head *commitable_paths)
3938 const struct got_error *err = NULL;
3939 struct got_pathlist_entry *pe;
3943 TAILQ_FOREACH(pe, commitable_paths, entry) {
3944 struct got_commitable *ct = pe->data;
3945 char *ct_name = NULL;
3948 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3949 if (ct->status != GOT_STATUS_MODIFY &&
3950 ct->status != GOT_STATUS_MODE_CHANGE &&
3951 ct->status != GOT_STATUS_DELETE)
3954 if (ct->staged_status != GOT_STATUS_MODIFY &&
3955 ct->staged_status != GOT_STATUS_DELETE)
3959 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
3962 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3968 ct_name = basename(pe->path);
3969 if (ct_name == NULL)
3970 return got_error_from_errno2("basename", pe->path);
3972 if (strcmp(te->name, ct_name) != 0)
3982 static const struct got_error *
3983 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3984 const char *child_path, const char *path_base_tree,
3985 struct got_pathlist_head *commitable_paths,
3986 got_worktree_status_cb status_cb, void *status_arg,
3987 struct got_repository *repo)
3989 const struct got_error *err = NULL;
3990 struct got_tree_entry *new_te;
3992 struct got_object_id *id = NULL;
3996 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3997 got_path_is_root_dir(path_base_tree) ? "" : "/",
3999 return got_error_from_errno("asprintf");
4001 new_te = calloc(1, sizeof(*new_te));
4003 return got_error_from_errno("calloc");
4004 new_te->mode = S_IFDIR;
4006 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4007 sizeof(new_te->name)) {
4008 err = got_error(GOT_ERR_NO_SPACE);
4011 err = write_tree(&id, NULL, subtree_path,
4012 commitable_paths, status_cb, status_arg, repo);
4017 memcpy(&new_te->id, id, sizeof(new_te->id));
4026 static const struct got_error *
4027 write_tree(struct got_object_id **new_tree_id,
4028 struct got_tree_object *base_tree, const char *path_base_tree,
4029 struct got_pathlist_head *commitable_paths,
4030 got_worktree_status_cb status_cb, void *status_arg,
4031 struct got_repository *repo)
4033 const struct got_error *err = NULL;
4034 struct got_pathlist_head paths;
4035 struct got_tree_entry *te, *new_te = NULL;
4036 struct got_pathlist_entry *pe;
4041 /* Insert, and recurse into, newly added entries first. */
4042 TAILQ_FOREACH(pe, commitable_paths, entry) {
4043 struct got_commitable *ct = pe->data;
4044 char *child_path = NULL, *slash;
4046 if ((ct->status != GOT_STATUS_ADD &&
4047 ct->staged_status != GOT_STATUS_ADD) ||
4048 (ct->flags & GOT_COMMITABLE_ADDED))
4051 if (!got_path_is_child(pe->path, path_base_tree,
4052 strlen(path_base_tree)))
4055 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4060 slash = strchr(child_path, '/');
4061 if (slash == NULL) {
4062 err = alloc_added_blob_tree_entry(&new_te, ct);
4065 err = report_ct_status(ct, status_cb, status_arg);
4068 ct->flags |= GOT_COMMITABLE_ADDED;
4069 err = insert_tree_entry(new_te, &paths);
4074 *slash = '\0'; /* trim trailing path components */
4075 if (base_tree == NULL ||
4076 got_object_tree_find_entry(base_tree, child_path)
4078 err = make_subtree_for_added_blob(&new_te,
4079 child_path, path_base_tree,
4080 commitable_paths, status_cb, status_arg,
4084 err = insert_tree_entry(new_te, &paths);
4093 int i, nbase_entries;
4094 /* Handle modified and deleted entries. */
4095 nbase_entries = got_object_tree_get_nentries(base_tree);
4096 for (i = 0; i < nbase_entries; i++) {
4097 struct got_commitable *ct = NULL;
4099 te = got_object_tree_get_entry(base_tree, i);
4100 if (got_object_tree_entry_is_submodule(te)) {
4101 /* Entry is a submodule; just copy it. */
4102 err = got_object_tree_entry_dup(&new_te, te);
4105 err = insert_tree_entry(new_te, &paths);
4112 if (S_ISDIR(te->mode)) {
4114 err = got_object_tree_entry_dup(&new_te, te);
4117 err = match_modified_subtree(&modified, te,
4118 path_base_tree, commitable_paths);
4121 /* Avoid recursion into unmodified subtrees. */
4123 struct got_object_id *new_id;
4124 err = write_subtree(&new_id, te,
4125 path_base_tree, commitable_paths,
4126 status_cb, status_arg, repo);
4129 memcpy(&new_te->id, new_id,
4130 sizeof(new_te->id));
4133 err = insert_tree_entry(new_te, &paths);
4140 err = match_deleted_or_modified_ct(&ct, te,
4141 path_base_tree, commitable_paths);
4145 /* NB: Deleted entries get dropped here. */
4146 if (ct->status == GOT_STATUS_MODIFY ||
4147 ct->status == GOT_STATUS_MODE_CHANGE ||
4148 ct->staged_status == GOT_STATUS_MODIFY) {
4149 err = alloc_modified_blob_tree_entry(
4153 err = insert_tree_entry(new_te, &paths);
4158 err = report_ct_status(ct, status_cb,
4163 /* Entry is unchanged; just copy it. */
4164 err = got_object_tree_entry_dup(&new_te, te);
4167 err = insert_tree_entry(new_te, &paths);
4175 /* Write new list of entries; deleted entries have been dropped. */
4176 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4178 got_pathlist_free(&paths);
4182 static const struct got_error *
4183 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4184 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4185 int have_staged_files)
4187 const struct got_error *err = NULL;
4188 struct got_pathlist_entry *pe;
4190 TAILQ_FOREACH(pe, commitable_paths, entry) {
4191 struct got_fileindex_entry *ie;
4192 struct got_commitable *ct = pe->data;
4194 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4196 if (ct->status == GOT_STATUS_DELETE ||
4197 ct->staged_status == GOT_STATUS_DELETE) {
4198 got_fileindex_entry_remove(fileindex, ie);
4199 got_fileindex_entry_free(ie);
4200 } else if (ct->staged_status == GOT_STATUS_ADD ||
4201 ct->staged_status == GOT_STATUS_MODIFY) {
4202 got_fileindex_entry_stage_set(ie,
4203 GOT_FILEIDX_STAGE_NONE);
4204 err = got_fileindex_entry_update(ie,
4205 ct->ondisk_path, ct->staged_blob_id->sha1,
4206 new_base_commit_id->sha1,
4207 !have_staged_files);
4209 err = got_fileindex_entry_update(ie,
4210 ct->ondisk_path, ct->blob_id->sha1,
4211 new_base_commit_id->sha1,
4212 !have_staged_files);
4214 err = got_fileindex_entry_alloc(&ie,
4215 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4216 new_base_commit_id->sha1);
4219 err = got_fileindex_entry_add(fileindex, ie);
4228 static const struct got_error *
4229 check_out_of_date(const char *in_repo_path, unsigned char status,
4230 unsigned char staged_status, struct got_object_id *base_blob_id,
4231 struct got_object_id *base_commit_id,
4232 struct got_object_id *head_commit_id, struct got_repository *repo,
4235 const struct got_error *err = NULL;
4236 struct got_object_id *id = NULL;
4238 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4239 /* Trivial case: base commit == head commit */
4240 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4243 * Ensure file content which local changes were based
4244 * on matches file content in the branch head.
4246 err = got_object_id_by_path(&id, repo, head_commit_id,
4249 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4250 err = got_error(ood_errcode);
4252 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4253 err = got_error(ood_errcode);
4255 /* Require that added files don't exist in the branch head. */
4256 err = got_object_id_by_path(&id, repo, head_commit_id,
4258 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4260 err = id ? got_error(ood_errcode) : NULL;
4267 const struct got_error *
4268 commit_worktree(struct got_object_id **new_commit_id,
4269 struct got_pathlist_head *commitable_paths,
4270 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4271 const char *author, const char *committer,
4272 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4273 got_worktree_status_cb status_cb, void *status_arg,
4274 struct got_repository *repo)
4276 const struct got_error *err = NULL, *unlockerr = NULL;
4277 struct got_pathlist_entry *pe;
4278 const char *head_ref_name = NULL;
4279 struct got_commit_object *head_commit = NULL;
4280 struct got_reference *head_ref2 = NULL;
4281 struct got_object_id *head_commit_id2 = NULL;
4282 struct got_tree_object *head_tree = NULL;
4283 struct got_object_id *new_tree_id = NULL;
4284 struct got_object_id_queue parent_ids;
4285 struct got_object_qid *pid = NULL;
4286 char *logmsg = NULL;
4288 *new_commit_id = NULL;
4290 SIMPLEQ_INIT(&parent_ids);
4292 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4296 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4300 if (commit_msg_cb != NULL) {
4301 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4306 if (logmsg == NULL || strlen(logmsg) == 0) {
4307 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4311 /* Create blobs from added and modified files and record their IDs. */
4312 TAILQ_FOREACH(pe, commitable_paths, entry) {
4313 struct got_commitable *ct = pe->data;
4316 /* Blobs for staged files already exist. */
4317 if (ct->staged_status == GOT_STATUS_ADD ||
4318 ct->staged_status == GOT_STATUS_MODIFY)
4321 if (ct->status != GOT_STATUS_ADD &&
4322 ct->status != GOT_STATUS_MODIFY &&
4323 ct->status != GOT_STATUS_MODE_CHANGE)
4326 if (asprintf(&ondisk_path, "%s/%s",
4327 worktree->root_path, pe->path) == -1) {
4328 err = got_error_from_errno("asprintf");
4331 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4337 /* Recursively write new tree objects. */
4338 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4339 status_cb, status_arg, repo);
4343 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4346 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4347 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4348 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4349 got_object_qid_free(pid);
4355 /* Check if a concurrent commit to our branch has occurred. */
4356 head_ref_name = got_worktree_get_head_ref_name(worktree);
4357 if (head_ref_name == NULL) {
4358 err = got_error_from_errno("got_worktree_get_head_ref_name");
4361 /* Lock the reference here to prevent concurrent modification. */
4362 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4365 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4368 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4369 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4372 /* Update branch head in repository. */
4373 err = got_ref_change_ref(head_ref2, *new_commit_id);
4376 err = got_ref_write(head_ref2, repo);
4380 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4384 err = ref_base_commit(worktree, repo);
4389 got_object_tree_close(head_tree);
4391 got_object_commit_close(head_commit);
4392 free(head_commit_id2);
4394 unlockerr = got_ref_unlock(head_ref2);
4395 if (unlockerr && err == NULL)
4397 got_ref_close(head_ref2);
4402 static const struct got_error *
4403 check_path_is_commitable(const char *path,
4404 struct got_pathlist_head *commitable_paths)
4406 struct got_pathlist_entry *cpe = NULL;
4407 size_t path_len = strlen(path);
4409 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4410 struct got_commitable *ct = cpe->data;
4411 const char *ct_path = ct->path;
4413 while (ct_path[0] == '/')
4416 if (strcmp(path, ct_path) == 0 ||
4417 got_path_is_child(ct_path, path, path_len))
4422 return got_error_path(path, GOT_ERR_BAD_PATH);
4427 static const struct got_error *
4428 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4430 int *have_staged_files = arg;
4432 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4433 *have_staged_files = 1;
4434 return got_error(GOT_ERR_CANCELLED);
4440 static const struct got_error *
4441 check_non_staged_files(struct got_fileindex *fileindex,
4442 struct got_pathlist_head *paths)
4444 struct got_pathlist_entry *pe;
4445 struct got_fileindex_entry *ie;
4447 TAILQ_FOREACH(pe, paths, entry) {
4448 if (pe->path[0] == '\0')
4450 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4452 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4453 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4454 return got_error_path(pe->path,
4455 GOT_ERR_FILE_NOT_STAGED);
4461 const struct got_error *
4462 got_worktree_commit(struct got_object_id **new_commit_id,
4463 struct got_worktree *worktree, struct got_pathlist_head *paths,
4464 const char *author, const char *committer,
4465 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4466 got_worktree_status_cb status_cb, void *status_arg,
4467 struct got_repository *repo)
4469 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4470 struct got_fileindex *fileindex = NULL;
4471 char *fileindex_path = NULL;
4472 struct got_pathlist_head commitable_paths;
4473 struct collect_commitables_arg cc_arg;
4474 struct got_pathlist_entry *pe;
4475 struct got_reference *head_ref = NULL;
4476 struct got_object_id *head_commit_id = NULL;
4477 int have_staged_files = 0;
4479 *new_commit_id = NULL;
4481 TAILQ_INIT(&commitable_paths);
4483 err = lock_worktree(worktree, LOCK_EX);
4487 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4491 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4495 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4499 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4500 &have_staged_files);
4501 if (err && err->code != GOT_ERR_CANCELLED)
4503 if (have_staged_files) {
4504 err = check_non_staged_files(fileindex, paths);
4509 cc_arg.commitable_paths = &commitable_paths;
4510 cc_arg.worktree = worktree;
4512 cc_arg.have_staged_files = have_staged_files;
4513 TAILQ_FOREACH(pe, paths, entry) {
4514 err = worktree_status(worktree, pe->path, fileindex, repo,
4515 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4520 if (TAILQ_EMPTY(&commitable_paths)) {
4521 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4525 TAILQ_FOREACH(pe, paths, entry) {
4526 err = check_path_is_commitable(pe->path, &commitable_paths);
4531 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4532 struct got_commitable *ct = pe->data;
4533 const char *ct_path = ct->in_repo_path;
4535 while (ct_path[0] == '/')
4537 err = check_out_of_date(ct_path, ct->status,
4538 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4539 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4545 err = commit_worktree(new_commit_id, &commitable_paths,
4546 head_commit_id, worktree, author, committer,
4547 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4551 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4552 fileindex, have_staged_files);
4553 sync_err = sync_fileindex(fileindex, fileindex_path);
4554 if (sync_err && err == NULL)
4558 got_fileindex_free(fileindex);
4559 free(fileindex_path);
4560 unlockerr = lock_worktree(worktree, LOCK_SH);
4561 if (unlockerr && err == NULL)
4563 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4564 struct got_commitable *ct = pe->data;
4565 free_commitable(ct);
4567 got_pathlist_free(&commitable_paths);
4572 got_commitable_get_path(struct got_commitable *ct)
4578 got_commitable_get_status(struct got_commitable *ct)
4583 struct check_rebase_ok_arg {
4584 struct got_worktree *worktree;
4585 struct got_repository *repo;
4588 static const struct got_error *
4589 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4591 const struct got_error *err = NULL;
4592 struct check_rebase_ok_arg *a = arg;
4593 unsigned char status;
4597 /* Reject rebase of a work tree with mixed base commits. */
4598 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4599 SHA1_DIGEST_LENGTH))
4600 return got_error(GOT_ERR_MIXED_COMMITS);
4602 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4604 return got_error_from_errno("asprintf");
4606 /* Reject rebase of a work tree with modified or staged files. */
4607 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4612 if (status != GOT_STATUS_NO_CHANGE)
4613 return got_error(GOT_ERR_MODIFIED);
4614 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4615 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4620 const struct got_error *
4621 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4622 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4623 struct got_worktree *worktree, struct got_reference *branch,
4624 struct got_repository *repo)
4626 const struct got_error *err = NULL;
4627 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4628 char *branch_ref_name = NULL;
4629 char *fileindex_path = NULL;
4630 struct check_rebase_ok_arg ok_arg;
4631 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4633 *new_base_branch_ref = NULL;
4637 err = lock_worktree(worktree, LOCK_EX);
4641 err = open_fileindex(fileindex, &fileindex_path, worktree);
4645 ok_arg.worktree = worktree;
4647 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4652 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4656 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4660 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4664 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4669 err = got_ref_alloc_symref(new_base_branch_ref,
4670 new_base_branch_ref_name, wt_branch);
4673 err = got_ref_write(*new_base_branch_ref, repo);
4677 /* TODO Lock original branch's ref while rebasing? */
4679 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4683 err = got_ref_write(branch_ref, repo);
4687 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4688 worktree->base_commit_id);
4691 err = got_ref_write(*tmp_branch, repo);
4695 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4699 free(fileindex_path);
4700 free(tmp_branch_name);
4701 free(new_base_branch_ref_name);
4702 free(branch_ref_name);
4704 got_ref_close(branch_ref);
4706 got_ref_close(wt_branch);
4708 if (*new_base_branch_ref) {
4709 got_ref_close(*new_base_branch_ref);
4710 *new_base_branch_ref = NULL;
4713 got_ref_close(*tmp_branch);
4717 got_fileindex_free(*fileindex);
4720 lock_worktree(worktree, LOCK_SH);
4725 const struct got_error *
4726 got_worktree_rebase_continue(struct got_object_id **commit_id,
4727 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4728 struct got_reference **branch, struct got_fileindex **fileindex,
4729 struct got_worktree *worktree, struct got_repository *repo)
4731 const struct got_error *err;
4732 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4733 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4734 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4735 char *fileindex_path = NULL;
4736 int have_staged_files = 0;
4739 *new_base_branch = NULL;
4744 err = lock_worktree(worktree, LOCK_EX);
4748 err = open_fileindex(fileindex, &fileindex_path, worktree);
4752 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4753 &have_staged_files);
4754 if (err && err->code != GOT_ERR_CANCELLED)
4756 if (have_staged_files) {
4757 err = got_error(GOT_ERR_STAGED_PATHS);
4761 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4765 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4769 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4773 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4777 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4781 err = got_ref_open(branch, repo,
4782 got_ref_get_symref_target(branch_ref), 0);
4786 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4790 err = got_ref_resolve(commit_id, repo, commit_ref);
4794 err = got_ref_open(new_base_branch, repo,
4795 new_base_branch_ref_name, 0);
4799 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4803 free(commit_ref_name);
4804 free(branch_ref_name);
4805 free(fileindex_path);
4807 got_ref_close(commit_ref);
4809 got_ref_close(branch_ref);
4814 got_ref_close(*tmp_branch);
4817 if (*new_base_branch) {
4818 got_ref_close(*new_base_branch);
4819 *new_base_branch = NULL;
4822 got_ref_close(*branch);
4826 got_fileindex_free(*fileindex);
4829 lock_worktree(worktree, LOCK_SH);
4834 const struct got_error *
4835 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4837 const struct got_error *err;
4838 char *tmp_branch_name = NULL;
4840 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4844 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4845 free(tmp_branch_name);
4849 static const struct got_error *
4850 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4851 char **logmsg, void *arg)
4857 static const struct got_error *
4858 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4859 const char *path, struct got_object_id *blob_id,
4860 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4861 int dirfd, const char *de_name)
4866 struct collect_merged_paths_arg {
4867 got_worktree_checkout_cb progress_cb;
4869 struct got_pathlist_head *merged_paths;
4872 static const struct got_error *
4873 collect_merged_paths(void *arg, unsigned char status, const char *path)
4875 const struct got_error *err;
4876 struct collect_merged_paths_arg *a = arg;
4878 struct got_pathlist_entry *new;
4880 err = (*a->progress_cb)(a->progress_arg, status, path);
4884 if (status != GOT_STATUS_MERGE &&
4885 status != GOT_STATUS_ADD &&
4886 status != GOT_STATUS_DELETE &&
4887 status != GOT_STATUS_CONFLICT)
4892 return got_error_from_errno("strdup");
4894 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4895 if (err || new == NULL)
4901 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4903 struct got_pathlist_entry *pe;
4905 TAILQ_FOREACH(pe, merged_paths, entry)
4906 free((char *)pe->path);
4908 got_pathlist_free(merged_paths);
4911 static const struct got_error *
4912 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4913 struct got_repository *repo)
4915 const struct got_error *err;
4916 struct got_reference *commit_ref = NULL;
4918 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4920 if (err->code != GOT_ERR_NOT_REF)
4922 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4925 err = got_ref_write(commit_ref, repo);
4929 struct got_object_id *stored_id;
4932 err = got_ref_resolve(&stored_id, repo, commit_ref);
4935 cmp = got_object_id_cmp(commit_id, stored_id);
4938 err = got_error(GOT_ERR_REBASE_COMMITID);
4944 got_ref_close(commit_ref);
4948 static const struct got_error *
4949 rebase_merge_files(struct got_pathlist_head *merged_paths,
4950 const char *commit_ref_name, struct got_worktree *worktree,
4951 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4952 struct got_object_id *commit_id, struct got_repository *repo,
4953 got_worktree_checkout_cb progress_cb, void *progress_arg,
4954 got_cancel_cb cancel_cb, void *cancel_arg)
4956 const struct got_error *err;
4957 struct got_reference *commit_ref = NULL;
4958 struct collect_merged_paths_arg cmp_arg;
4959 char *fileindex_path;
4961 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4963 err = get_fileindex_path(&fileindex_path, worktree);
4967 cmp_arg.progress_cb = progress_cb;
4968 cmp_arg.progress_arg = progress_arg;
4969 cmp_arg.merged_paths = merged_paths;
4970 err = merge_files(worktree, fileindex, fileindex_path,
4971 parent_commit_id, commit_id, repo, collect_merged_paths,
4972 &cmp_arg, cancel_cb, cancel_arg);
4974 got_ref_close(commit_ref);
4978 const struct got_error *
4979 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4980 struct got_worktree *worktree, struct got_fileindex *fileindex,
4981 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4982 struct got_repository *repo,
4983 got_worktree_checkout_cb progress_cb, void *progress_arg,
4984 got_cancel_cb cancel_cb, void *cancel_arg)
4986 const struct got_error *err;
4987 char *commit_ref_name;
4989 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4993 err = store_commit_id(commit_ref_name, commit_id, repo);
4997 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4998 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4999 progress_arg, cancel_cb, cancel_arg);
5001 free(commit_ref_name);
5005 const struct got_error *
5006 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5007 struct got_worktree *worktree, struct got_fileindex *fileindex,
5008 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5009 struct got_repository *repo,
5010 got_worktree_checkout_cb progress_cb, void *progress_arg,
5011 got_cancel_cb cancel_cb, void *cancel_arg)
5013 const struct got_error *err;
5014 char *commit_ref_name;
5016 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5020 err = store_commit_id(commit_ref_name, commit_id, repo);
5024 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5025 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5026 progress_arg, cancel_cb, cancel_arg);
5028 free(commit_ref_name);
5032 static const struct got_error *
5033 rebase_commit(struct got_object_id **new_commit_id,
5034 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5035 struct got_worktree *worktree, struct got_fileindex *fileindex,
5036 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5037 const char *new_logmsg, struct got_repository *repo)
5039 const struct got_error *err, *sync_err;
5040 struct got_pathlist_head commitable_paths;
5041 struct collect_commitables_arg cc_arg;
5042 char *fileindex_path = NULL;
5043 struct got_reference *head_ref = NULL;
5044 struct got_object_id *head_commit_id = NULL;
5045 char *logmsg = NULL;
5047 TAILQ_INIT(&commitable_paths);
5048 *new_commit_id = NULL;
5050 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5052 err = get_fileindex_path(&fileindex_path, worktree);
5056 cc_arg.commitable_paths = &commitable_paths;
5057 cc_arg.worktree = worktree;
5059 cc_arg.have_staged_files = 0;
5061 * If possible get the status of individual files directly to
5062 * avoid crawling the entire work tree once per rebased commit.
5063 * TODO: Ideally, merged_paths would contain a list of commitables
5064 * we could use so we could skip worktree_status() entirely.
5067 struct got_pathlist_entry *pe;
5068 if (TAILQ_EMPTY(merged_paths)) {
5069 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5072 TAILQ_FOREACH(pe, merged_paths, entry) {
5073 err = worktree_status(worktree, pe->path, fileindex,
5074 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5080 err = worktree_status(worktree, "", fileindex, repo,
5081 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5086 if (TAILQ_EMPTY(&commitable_paths)) {
5087 /* No-op change; commit will be elided. */
5088 err = got_ref_delete(commit_ref, repo);
5091 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5095 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5099 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5104 logmsg = strdup(new_logmsg);
5105 if (logmsg == NULL) {
5106 err = got_error_from_errno("strdup");
5110 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5115 /* NB: commit_worktree will call free(logmsg) */
5116 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5117 worktree, got_object_commit_get_author(orig_commit),
5118 got_object_commit_get_committer(orig_commit),
5119 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5123 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5127 err = got_ref_delete(commit_ref, repo);
5131 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5133 sync_err = sync_fileindex(fileindex, fileindex_path);
5134 if (sync_err && err == NULL)
5137 free(fileindex_path);
5138 free(head_commit_id);
5140 got_ref_close(head_ref);
5142 free(*new_commit_id);
5143 *new_commit_id = NULL;
5148 const struct got_error *
5149 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5150 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5151 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5152 struct got_commit_object *orig_commit,
5153 struct got_object_id *orig_commit_id, struct got_repository *repo)
5155 const struct got_error *err;
5156 char *commit_ref_name;
5157 struct got_reference *commit_ref = NULL;
5158 struct got_object_id *commit_id = NULL;
5160 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5164 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5167 err = got_ref_resolve(&commit_id, repo, commit_ref);
5170 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5171 err = got_error(GOT_ERR_REBASE_COMMITID);
5175 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5176 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5179 got_ref_close(commit_ref);
5180 free(commit_ref_name);
5185 const struct got_error *
5186 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5187 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5188 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5189 struct got_commit_object *orig_commit,
5190 struct got_object_id *orig_commit_id, const char *new_logmsg,
5191 struct got_repository *repo)
5193 const struct got_error *err;
5194 char *commit_ref_name;
5195 struct got_reference *commit_ref = NULL;
5196 struct got_object_id *commit_id = NULL;
5198 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5202 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5205 err = got_ref_resolve(&commit_id, repo, commit_ref);
5208 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5209 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5213 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5214 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5217 got_ref_close(commit_ref);
5218 free(commit_ref_name);
5223 const struct got_error *
5224 got_worktree_rebase_postpone(struct got_worktree *worktree,
5225 struct got_fileindex *fileindex)
5228 got_fileindex_free(fileindex);
5229 return lock_worktree(worktree, LOCK_SH);
5232 static const struct got_error *
5233 delete_ref(const char *name, struct got_repository *repo)
5235 const struct got_error *err;
5236 struct got_reference *ref;
5238 err = got_ref_open(&ref, repo, name, 0);
5240 if (err->code == GOT_ERR_NOT_REF)
5245 err = got_ref_delete(ref, repo);
5250 static const struct got_error *
5251 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5253 const struct got_error *err;
5254 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5255 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5257 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5260 err = delete_ref(tmp_branch_name, repo);
5264 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5267 err = delete_ref(new_base_branch_ref_name, repo);
5271 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5274 err = delete_ref(branch_ref_name, repo);
5278 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5281 err = delete_ref(commit_ref_name, repo);
5286 free(tmp_branch_name);
5287 free(new_base_branch_ref_name);
5288 free(branch_ref_name);
5289 free(commit_ref_name);
5293 const struct got_error *
5294 got_worktree_rebase_complete(struct got_worktree *worktree,
5295 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5296 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5297 struct got_repository *repo)
5299 const struct got_error *err, *unlockerr;
5300 struct got_object_id *new_head_commit_id = NULL;
5302 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5306 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5310 err = got_ref_write(rebased_branch, repo);
5314 err = got_worktree_set_head_ref(worktree, rebased_branch);
5318 err = delete_rebase_refs(worktree, repo);
5321 got_fileindex_free(fileindex);
5322 free(new_head_commit_id);
5323 unlockerr = lock_worktree(worktree, LOCK_SH);
5324 if (unlockerr && err == NULL)
5329 const struct got_error *
5330 got_worktree_rebase_abort(struct got_worktree *worktree,
5331 struct got_fileindex *fileindex, struct got_repository *repo,
5332 struct got_reference *new_base_branch,
5333 got_worktree_checkout_cb progress_cb, void *progress_arg)
5335 const struct got_error *err, *unlockerr, *sync_err;
5336 struct got_reference *resolved = NULL;
5337 struct got_object_id *commit_id = NULL;
5338 char *fileindex_path = NULL;
5339 struct revert_file_args rfa;
5340 struct got_object_id *tree_id = NULL;
5342 err = lock_worktree(worktree, LOCK_EX);
5346 err = got_ref_open(&resolved, repo,
5347 got_ref_get_symref_target(new_base_branch), 0);
5351 err = got_worktree_set_head_ref(worktree, resolved);
5356 * XXX commits to the base branch could have happened while
5357 * we were busy rebasing; should we store the original commit ID
5358 * when rebase begins and read it back here?
5360 err = got_ref_resolve(&commit_id, repo, resolved);
5364 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5368 err = got_object_id_by_path(&tree_id, repo,
5369 worktree->base_commit_id, worktree->path_prefix);
5373 err = delete_rebase_refs(worktree, repo);
5377 err = get_fileindex_path(&fileindex_path, worktree);
5381 rfa.worktree = worktree;
5382 rfa.fileindex = fileindex;
5383 rfa.progress_cb = progress_cb;
5384 rfa.progress_arg = progress_arg;
5385 rfa.patch_cb = NULL;
5386 rfa.patch_arg = NULL;
5388 err = worktree_status(worktree, "", fileindex, repo,
5389 revert_file, &rfa, NULL, NULL, 0, 0);
5393 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5394 repo, progress_cb, progress_arg, NULL, NULL);
5396 sync_err = sync_fileindex(fileindex, fileindex_path);
5397 if (sync_err && err == NULL)
5400 got_ref_close(resolved);
5404 got_fileindex_free(fileindex);
5405 free(fileindex_path);
5407 unlockerr = lock_worktree(worktree, LOCK_SH);
5408 if (unlockerr && err == NULL)
5413 const struct got_error *
5414 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5415 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5416 struct got_fileindex **fileindex, struct got_worktree *worktree,
5417 struct got_repository *repo)
5419 const struct got_error *err = NULL;
5420 char *tmp_branch_name = NULL;
5421 char *branch_ref_name = NULL;
5422 char *base_commit_ref_name = NULL;
5423 char *fileindex_path = NULL;
5424 struct check_rebase_ok_arg ok_arg;
5425 struct got_reference *wt_branch = NULL;
5426 struct got_reference *base_commit_ref = NULL;
5430 *base_commit_id = NULL;
5433 err = lock_worktree(worktree, LOCK_EX);
5437 err = open_fileindex(fileindex, &fileindex_path, worktree);
5441 ok_arg.worktree = worktree;
5443 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5448 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5452 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5456 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5461 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5466 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5470 err = got_ref_write(*branch_ref, repo);
5474 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5475 worktree->base_commit_id);
5478 err = got_ref_write(base_commit_ref, repo);
5481 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5482 if (*base_commit_id == NULL) {
5483 err = got_error_from_errno("got_object_id_dup");
5487 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5488 worktree->base_commit_id);
5491 err = got_ref_write(*tmp_branch, repo);
5495 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5499 free(fileindex_path);
5500 free(tmp_branch_name);
5501 free(branch_ref_name);
5502 free(base_commit_ref_name);
5504 got_ref_close(wt_branch);
5507 got_ref_close(*branch_ref);
5511 got_ref_close(*tmp_branch);
5514 free(*base_commit_id);
5516 got_fileindex_free(*fileindex);
5519 lock_worktree(worktree, LOCK_SH);
5524 const struct got_error *
5525 got_worktree_histedit_postpone(struct got_worktree *worktree,
5526 struct got_fileindex *fileindex)
5529 got_fileindex_free(fileindex);
5530 return lock_worktree(worktree, LOCK_SH);
5533 const struct got_error *
5534 got_worktree_histedit_in_progress(int *in_progress,
5535 struct got_worktree *worktree)
5537 const struct got_error *err;
5538 char *tmp_branch_name = NULL;
5540 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5544 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5545 free(tmp_branch_name);
5549 const struct got_error *
5550 got_worktree_histedit_continue(struct got_object_id **commit_id,
5551 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5552 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5553 struct got_worktree *worktree, struct got_repository *repo)
5555 const struct got_error *err;
5556 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5557 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5558 struct got_reference *commit_ref = NULL;
5559 struct got_reference *base_commit_ref = NULL;
5560 char *fileindex_path = NULL;
5561 int have_staged_files = 0;
5565 *base_commit_id = NULL;
5568 err = lock_worktree(worktree, LOCK_EX);
5572 err = open_fileindex(fileindex, &fileindex_path, worktree);
5576 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5577 &have_staged_files);
5578 if (err && err->code != GOT_ERR_CANCELLED)
5580 if (have_staged_files) {
5581 err = got_error(GOT_ERR_STAGED_PATHS);
5585 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5589 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5593 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5597 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5602 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5606 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5609 err = got_ref_resolve(commit_id, repo, commit_ref);
5613 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5616 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5620 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5624 free(commit_ref_name);
5625 free(branch_ref_name);
5626 free(fileindex_path);
5628 got_ref_close(commit_ref);
5629 if (base_commit_ref)
5630 got_ref_close(base_commit_ref);
5634 free(*base_commit_id);
5635 *base_commit_id = NULL;
5637 got_ref_close(*tmp_branch);
5641 got_fileindex_free(*fileindex);
5644 lock_worktree(worktree, LOCK_EX);
5649 static const struct got_error *
5650 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5652 const struct got_error *err;
5653 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5654 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5656 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5659 err = delete_ref(tmp_branch_name, repo);
5663 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5667 err = delete_ref(base_commit_ref_name, repo);
5671 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5674 err = delete_ref(branch_ref_name, repo);
5678 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5681 err = delete_ref(commit_ref_name, repo);
5685 free(tmp_branch_name);
5686 free(base_commit_ref_name);
5687 free(branch_ref_name);
5688 free(commit_ref_name);
5692 const struct got_error *
5693 got_worktree_histedit_abort(struct got_worktree *worktree,
5694 struct got_fileindex *fileindex, struct got_repository *repo,
5695 struct got_reference *branch, struct got_object_id *base_commit_id,
5696 got_worktree_checkout_cb progress_cb, void *progress_arg)
5698 const struct got_error *err, *unlockerr, *sync_err;
5699 struct got_reference *resolved = NULL;
5700 char *fileindex_path = NULL;
5701 struct got_object_id *tree_id = NULL;
5702 struct revert_file_args rfa;
5704 err = lock_worktree(worktree, LOCK_EX);
5708 err = got_ref_open(&resolved, repo,
5709 got_ref_get_symref_target(branch), 0);
5713 err = got_worktree_set_head_ref(worktree, resolved);
5717 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5721 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5722 worktree->path_prefix);
5726 err = delete_histedit_refs(worktree, repo);
5730 err = get_fileindex_path(&fileindex_path, worktree);
5734 rfa.worktree = worktree;
5735 rfa.fileindex = fileindex;
5736 rfa.progress_cb = progress_cb;
5737 rfa.progress_arg = progress_arg;
5738 rfa.patch_cb = NULL;
5739 rfa.patch_arg = NULL;
5741 err = worktree_status(worktree, "", fileindex, repo,
5742 revert_file, &rfa, NULL, NULL, 0, 0);
5746 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5747 repo, progress_cb, progress_arg, NULL, NULL);
5749 sync_err = sync_fileindex(fileindex, fileindex_path);
5750 if (sync_err && err == NULL)
5753 got_ref_close(resolved);
5755 free(fileindex_path);
5757 unlockerr = lock_worktree(worktree, LOCK_SH);
5758 if (unlockerr && err == NULL)
5763 const struct got_error *
5764 got_worktree_histedit_complete(struct got_worktree *worktree,
5765 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5766 struct got_reference *edited_branch, struct got_repository *repo)
5768 const struct got_error *err, *unlockerr;
5769 struct got_object_id *new_head_commit_id = NULL;
5770 struct got_reference *resolved = NULL;
5772 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5776 err = got_ref_open(&resolved, repo,
5777 got_ref_get_symref_target(edited_branch), 0);
5781 err = got_ref_change_ref(resolved, new_head_commit_id);
5785 err = got_ref_write(resolved, repo);
5789 err = got_worktree_set_head_ref(worktree, resolved);
5793 err = delete_histedit_refs(worktree, repo);
5796 got_fileindex_free(fileindex);
5797 free(new_head_commit_id);
5798 unlockerr = lock_worktree(worktree, LOCK_SH);
5799 if (unlockerr && err == NULL)
5804 const struct got_error *
5805 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5806 struct got_object_id *commit_id, struct got_repository *repo)
5808 const struct got_error *err;
5809 char *commit_ref_name;
5811 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5815 err = store_commit_id(commit_ref_name, commit_id, repo);
5819 err = delete_ref(commit_ref_name, repo);
5821 free(commit_ref_name);
5825 const struct got_error *
5826 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5827 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5828 struct got_worktree *worktree, const char *refname,
5829 struct got_repository *repo)
5831 const struct got_error *err = NULL;
5832 char *fileindex_path = NULL;
5833 struct check_rebase_ok_arg ok_arg;
5837 *base_branch_ref = NULL;
5839 err = lock_worktree(worktree, LOCK_EX);
5843 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5844 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5845 "cannot integrate a branch into itself; "
5846 "update -b or different branch name required");
5850 err = open_fileindex(fileindex, &fileindex_path, worktree);
5854 /* Preconditions are the same as for rebase. */
5855 ok_arg.worktree = worktree;
5857 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5862 err = got_ref_open(branch_ref, repo, refname, 1);
5866 err = got_ref_open(base_branch_ref, repo,
5867 got_worktree_get_head_ref_name(worktree), 1);
5871 got_ref_close(*branch_ref);
5874 if (*base_branch_ref) {
5875 got_ref_close(*base_branch_ref);
5876 *base_branch_ref = NULL;
5879 got_fileindex_free(*fileindex);
5882 lock_worktree(worktree, LOCK_SH);
5887 const struct got_error *
5888 got_worktree_integrate_continue(struct got_worktree *worktree,
5889 struct got_fileindex *fileindex, struct got_repository *repo,
5890 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5891 got_worktree_checkout_cb progress_cb, void *progress_arg,
5892 got_cancel_cb cancel_cb, void *cancel_arg)
5894 const struct got_error *err = NULL, *sync_err, *unlockerr;
5895 char *fileindex_path = NULL;
5896 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5898 err = get_fileindex_path(&fileindex_path, worktree);
5902 err = got_ref_resolve(&commit_id, repo, branch_ref);
5906 err = got_object_id_by_path(&tree_id, repo, commit_id,
5907 worktree->path_prefix);
5911 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5915 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5916 progress_cb, progress_arg, cancel_cb, cancel_arg);
5920 err = got_ref_change_ref(base_branch_ref, commit_id);
5924 err = got_ref_write(base_branch_ref, repo);
5926 sync_err = sync_fileindex(fileindex, fileindex_path);
5927 if (sync_err && err == NULL)
5931 unlockerr = got_ref_unlock(branch_ref);
5932 if (unlockerr && err == NULL)
5934 got_ref_close(branch_ref);
5936 unlockerr = got_ref_unlock(base_branch_ref);
5937 if (unlockerr && err == NULL)
5939 got_ref_close(base_branch_ref);
5941 got_fileindex_free(fileindex);
5942 free(fileindex_path);
5945 unlockerr = lock_worktree(worktree, LOCK_SH);
5946 if (unlockerr && err == NULL)
5951 const struct got_error *
5952 got_worktree_integrate_abort(struct got_worktree *worktree,
5953 struct got_fileindex *fileindex, struct got_repository *repo,
5954 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5956 const struct got_error *err = NULL, *unlockerr = NULL;
5958 got_fileindex_free(fileindex);
5960 err = lock_worktree(worktree, LOCK_SH);
5962 unlockerr = got_ref_unlock(branch_ref);
5963 if (unlockerr && err == NULL)
5965 got_ref_close(branch_ref);
5967 unlockerr = got_ref_unlock(base_branch_ref);
5968 if (unlockerr && err == NULL)
5970 got_ref_close(base_branch_ref);
5975 struct check_stage_ok_arg {
5976 struct got_object_id *head_commit_id;
5977 struct got_worktree *worktree;
5978 struct got_fileindex *fileindex;
5979 struct got_repository *repo;
5983 const struct got_error *
5984 check_stage_ok(void *arg, unsigned char status,
5985 unsigned char staged_status, const char *relpath,
5986 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5987 struct got_object_id *commit_id, int dirfd, const char *de_name)
5989 struct check_stage_ok_arg *a = arg;
5990 const struct got_error *err = NULL;
5991 struct got_fileindex_entry *ie;
5992 struct got_object_id base_commit_id;
5993 struct got_object_id *base_commit_idp = NULL;
5994 char *in_repo_path = NULL, *p;
5996 if (status == GOT_STATUS_UNVERSIONED ||
5997 status == GOT_STATUS_NO_CHANGE)
5999 if (status == GOT_STATUS_NONEXISTENT)
6000 return got_error_set_errno(ENOENT, relpath);
6002 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6004 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6006 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6007 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6009 return got_error_from_errno("asprintf");
6011 if (got_fileindex_entry_has_commit(ie)) {
6012 memcpy(base_commit_id.sha1, ie->commit_sha1,
6013 SHA1_DIGEST_LENGTH);
6014 base_commit_idp = &base_commit_id;
6017 if (status == GOT_STATUS_CONFLICT) {
6018 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6020 } else if (status != GOT_STATUS_ADD &&
6021 status != GOT_STATUS_MODIFY &&
6022 status != GOT_STATUS_DELETE) {
6023 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6027 a->have_changes = 1;
6032 err = check_out_of_date(p, status, staged_status,
6033 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6034 GOT_ERR_STAGE_OUT_OF_DATE);
6040 struct stage_path_arg {
6041 struct got_worktree *worktree;
6042 struct got_fileindex *fileindex;
6043 struct got_repository *repo;
6044 got_worktree_status_cb status_cb;
6046 got_worktree_patch_cb patch_cb;
6048 int staged_something;
6051 static const struct got_error *
6052 stage_path(void *arg, unsigned char status,
6053 unsigned char staged_status, const char *relpath,
6054 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6055 struct got_object_id *commit_id, int dirfd, const char *de_name)
6057 struct stage_path_arg *a = arg;
6058 const struct got_error *err = NULL;
6059 struct got_fileindex_entry *ie;
6060 char *ondisk_path = NULL, *path_content = NULL;
6062 struct got_object_id *new_staged_blob_id = NULL;
6064 if (status == GOT_STATUS_UNVERSIONED)
6067 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6069 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6071 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6073 return got_error_from_errno("asprintf");
6076 case GOT_STATUS_ADD:
6077 case GOT_STATUS_MODIFY:
6079 if (status == GOT_STATUS_ADD) {
6080 int choice = GOT_PATCH_CHOICE_NONE;
6081 err = (*a->patch_cb)(&choice, a->patch_arg,
6082 status, ie->path, NULL, 1, 1);
6085 if (choice != GOT_PATCH_CHOICE_YES)
6088 err = create_patched_content(&path_content, 0,
6089 staged_blob_id ? staged_blob_id : blob_id,
6090 ondisk_path, dirfd, de_name, ie->path,
6091 a->repo, a->patch_cb, a->patch_arg);
6092 if (err || path_content == NULL)
6096 err = got_object_blob_create(&new_staged_blob_id,
6097 path_content ? path_content : ondisk_path, a->repo);
6100 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6101 SHA1_DIGEST_LENGTH);
6102 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6103 stage = GOT_FILEIDX_STAGE_ADD;
6105 stage = GOT_FILEIDX_STAGE_MODIFY;
6106 got_fileindex_entry_stage_set(ie, stage);
6107 a->staged_something = 1;
6108 if (a->status_cb == NULL)
6110 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6111 get_staged_status(ie), relpath, blob_id,
6112 new_staged_blob_id, NULL, dirfd, de_name);
6114 case GOT_STATUS_DELETE:
6115 if (staged_status == GOT_STATUS_DELETE)
6118 int choice = GOT_PATCH_CHOICE_NONE;
6119 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6120 ie->path, NULL, 1, 1);
6123 if (choice == GOT_PATCH_CHOICE_NO)
6125 if (choice != GOT_PATCH_CHOICE_YES) {
6126 err = got_error(GOT_ERR_PATCH_CHOICE);
6130 stage = GOT_FILEIDX_STAGE_DELETE;
6131 got_fileindex_entry_stage_set(ie, stage);
6132 a->staged_something = 1;
6133 if (a->status_cb == NULL)
6135 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6136 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6139 case GOT_STATUS_NO_CHANGE:
6141 case GOT_STATUS_CONFLICT:
6142 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6144 case GOT_STATUS_NONEXISTENT:
6145 err = got_error_set_errno(ENOENT, relpath);
6148 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6152 if (path_content && unlink(path_content) == -1 && err == NULL)
6153 err = got_error_from_errno2("unlink", path_content);
6156 free(new_staged_blob_id);
6160 const struct got_error *
6161 got_worktree_stage(struct got_worktree *worktree,
6162 struct got_pathlist_head *paths,
6163 got_worktree_status_cb status_cb, void *status_arg,
6164 got_worktree_patch_cb patch_cb, void *patch_arg,
6165 struct got_repository *repo)
6167 const struct got_error *err = NULL, *sync_err, *unlockerr;
6168 struct got_pathlist_entry *pe;
6169 struct got_fileindex *fileindex = NULL;
6170 char *fileindex_path = NULL;
6171 struct got_reference *head_ref = NULL;
6172 struct got_object_id *head_commit_id = NULL;
6173 struct check_stage_ok_arg oka;
6174 struct stage_path_arg spa;
6176 err = lock_worktree(worktree, LOCK_EX);
6180 err = got_ref_open(&head_ref, repo,
6181 got_worktree_get_head_ref_name(worktree), 0);
6184 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6187 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6191 /* Check pre-conditions before staging anything. */
6192 oka.head_commit_id = head_commit_id;
6193 oka.worktree = worktree;
6194 oka.fileindex = fileindex;
6196 oka.have_changes = 0;
6197 TAILQ_FOREACH(pe, paths, entry) {
6198 err = worktree_status(worktree, pe->path, fileindex, repo,
6199 check_stage_ok, &oka, NULL, NULL, 0, 0);
6203 if (!oka.have_changes) {
6204 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6208 spa.worktree = worktree;
6209 spa.fileindex = fileindex;
6211 spa.patch_cb = patch_cb;
6212 spa.patch_arg = patch_arg;
6213 spa.status_cb = status_cb;
6214 spa.status_arg = status_arg;
6215 spa.staged_something = 0;
6216 TAILQ_FOREACH(pe, paths, entry) {
6217 err = worktree_status(worktree, pe->path, fileindex, repo,
6218 stage_path, &spa, NULL, NULL, 0, 0);
6222 if (!spa.staged_something) {
6223 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6227 sync_err = sync_fileindex(fileindex, fileindex_path);
6228 if (sync_err && err == NULL)
6232 got_ref_close(head_ref);
6233 free(head_commit_id);
6234 free(fileindex_path);
6236 got_fileindex_free(fileindex);
6237 unlockerr = lock_worktree(worktree, LOCK_SH);
6238 if (unlockerr && err == NULL)
6243 struct unstage_path_arg {
6244 struct got_worktree *worktree;
6245 struct got_fileindex *fileindex;
6246 struct got_repository *repo;
6247 got_worktree_checkout_cb progress_cb;
6249 got_worktree_patch_cb patch_cb;
6253 static const struct got_error *
6254 create_unstaged_content(char **path_unstaged_content,
6255 char **path_new_staged_content, struct got_object_id *blob_id,
6256 struct got_object_id *staged_blob_id, const char *relpath,
6257 struct got_repository *repo,
6258 got_worktree_patch_cb patch_cb, void *patch_arg)
6260 const struct got_error *err;
6261 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6262 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6263 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6264 struct stat sb1, sb2;
6265 struct got_diff_changes *changes = NULL;
6266 struct got_diff_state *ds = NULL;
6267 struct got_diff_args *args = NULL;
6268 struct got_diff_change *change;
6269 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6270 int have_content = 0, have_rejected_content = 0;
6272 *path_unstaged_content = NULL;
6273 *path_new_staged_content = NULL;
6275 err = got_object_id_str(&label1, blob_id);
6278 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6282 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6286 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6290 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6294 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6298 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6302 if (stat(path1, &sb1) == -1) {
6303 err = got_error_from_errno2("stat", path1);
6307 if (stat(path2, &sb2) == -1) {
6308 err = got_error_from_errno2("stat", path2);
6312 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6313 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6317 err = got_opentemp_named(path_unstaged_content, &outfile,
6318 "got-unstaged-content");
6321 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6322 "got-new-staged-content");
6326 if (fseek(f1, 0L, SEEK_SET) == -1) {
6327 err = got_ferror(f1, GOT_ERR_IO);
6330 if (fseek(f2, 0L, SEEK_SET) == -1) {
6331 err = got_ferror(f2, GOT_ERR_IO);
6334 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6336 err = apply_or_reject_change(&choice, change, ++n,
6337 changes->nchanges, ds, args, diff_flags, relpath,
6338 f1, f2, &line_cur1, &line_cur2,
6339 outfile, rejectfile, patch_cb, patch_arg);
6342 if (choice == GOT_PATCH_CHOICE_YES)
6345 have_rejected_content = 1;
6346 if (choice == GOT_PATCH_CHOICE_QUIT)
6349 if (have_content || have_rejected_content)
6350 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6351 outfile, rejectfile);
6355 got_object_blob_close(blob);
6357 got_object_blob_close(staged_blob);
6358 if (f1 && fclose(f1) == EOF && err == NULL)
6359 err = got_error_from_errno2("fclose", path1);
6360 if (f2 && fclose(f2) == EOF && err == NULL)
6361 err = got_error_from_errno2("fclose", path2);
6362 if (outfile && fclose(outfile) == EOF && err == NULL)
6363 err = got_error_from_errno2("fclose", *path_unstaged_content);
6364 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6365 err = got_error_from_errno2("fclose", *path_new_staged_content);
6366 if (path1 && unlink(path1) == -1 && err == NULL)
6367 err = got_error_from_errno2("unlink", path1);
6368 if (path2 && unlink(path2) == -1 && err == NULL)
6369 err = got_error_from_errno2("unlink", path2);
6370 if (err || !have_content) {
6371 if (*path_unstaged_content &&
6372 unlink(*path_unstaged_content) == -1 && err == NULL)
6373 err = got_error_from_errno2("unlink",
6374 *path_unstaged_content);
6375 free(*path_unstaged_content);
6376 *path_unstaged_content = NULL;
6378 if (err || !have_rejected_content) {
6379 if (*path_new_staged_content &&
6380 unlink(*path_new_staged_content) == -1 && err == NULL)
6381 err = got_error_from_errno2("unlink",
6382 *path_new_staged_content);
6383 free(*path_new_staged_content);
6384 *path_new_staged_content = NULL;
6388 got_diff_state_free(ds);
6392 got_diff_free_changes(changes);
6398 static const struct got_error *
6399 unstage_path(void *arg, unsigned char status,
6400 unsigned char staged_status, const char *relpath,
6401 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6402 struct got_object_id *commit_id, int dirfd, const char *de_name)
6404 const struct got_error *err = NULL;
6405 struct unstage_path_arg *a = arg;
6406 struct got_fileindex_entry *ie;
6407 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6408 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6409 char *path_new_staged_content = NULL;
6410 char *id_str = NULL, *label_orig = NULL;
6411 int local_changes_subsumed;
6414 if (staged_status != GOT_STATUS_ADD &&
6415 staged_status != GOT_STATUS_MODIFY &&
6416 staged_status != GOT_STATUS_DELETE)
6419 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6421 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6423 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6425 return got_error_from_errno("asprintf");
6427 err = got_object_id_str(&id_str,
6428 commit_id ? commit_id : a->worktree->base_commit_id);
6431 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6433 err = got_error_from_errno("asprintf");
6437 switch (staged_status) {
6438 case GOT_STATUS_MODIFY:
6439 err = got_object_open_as_blob(&blob_base, a->repo,
6444 case GOT_STATUS_ADD:
6446 if (staged_status == GOT_STATUS_ADD) {
6447 int choice = GOT_PATCH_CHOICE_NONE;
6448 err = (*a->patch_cb)(&choice, a->patch_arg,
6449 staged_status, ie->path, NULL, 1, 1);
6452 if (choice != GOT_PATCH_CHOICE_YES)
6455 err = create_unstaged_content(
6456 &path_unstaged_content,
6457 &path_new_staged_content, blob_id,
6458 staged_blob_id, ie->path, a->repo,
6459 a->patch_cb, a->patch_arg);
6460 if (err || path_unstaged_content == NULL)
6462 if (path_new_staged_content) {
6463 err = got_object_blob_create(
6465 path_new_staged_content,
6469 memcpy(ie->staged_blob_sha1,
6470 staged_blob_id->sha1,
6471 SHA1_DIGEST_LENGTH);
6473 err = merge_file(&local_changes_subsumed,
6474 a->worktree, blob_base, ondisk_path,
6475 relpath, got_fileindex_perms_to_st(ie),
6476 path_unstaged_content, label_orig,
6477 "unstaged", a->repo, a->progress_cb,
6480 path_new_staged_content == NULL)
6481 got_fileindex_entry_stage_set(ie,
6482 GOT_FILEIDX_STAGE_NONE);
6483 break; /* Done with this file. */
6486 err = got_object_open_as_blob(&blob_staged, a->repo,
6487 staged_blob_id, 8192);
6490 err = merge_blob(&local_changes_subsumed, a->worktree,
6491 blob_base, ondisk_path, relpath,
6492 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6493 commit_id ? commit_id : a->worktree->base_commit_id,
6494 a->repo, a->progress_cb, a->progress_arg);
6496 got_fileindex_entry_stage_set(ie,
6497 GOT_FILEIDX_STAGE_NONE);
6499 case GOT_STATUS_DELETE:
6501 int choice = GOT_PATCH_CHOICE_NONE;
6502 err = (*a->patch_cb)(&choice, a->patch_arg,
6503 staged_status, ie->path, NULL, 1, 1);
6506 if (choice == GOT_PATCH_CHOICE_NO)
6508 if (choice != GOT_PATCH_CHOICE_YES) {
6509 err = got_error(GOT_ERR_PATCH_CHOICE);
6513 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6514 err = get_file_status(&status, &sb, ie, ondisk_path,
6515 dirfd, de_name, a->repo);
6518 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6523 if (path_unstaged_content &&
6524 unlink(path_unstaged_content) == -1 && err == NULL)
6525 err = got_error_from_errno2("unlink", path_unstaged_content);
6526 if (path_new_staged_content &&
6527 unlink(path_new_staged_content) == -1 && err == NULL)
6528 err = got_error_from_errno2("unlink", path_new_staged_content);
6529 free(path_unstaged_content);
6530 free(path_new_staged_content);
6532 got_object_blob_close(blob_base);
6534 got_object_blob_close(blob_staged);
6540 const struct got_error *
6541 got_worktree_unstage(struct got_worktree *worktree,
6542 struct got_pathlist_head *paths,
6543 got_worktree_checkout_cb progress_cb, void *progress_arg,
6544 got_worktree_patch_cb patch_cb, void *patch_arg,
6545 struct got_repository *repo)
6547 const struct got_error *err = NULL, *sync_err, *unlockerr;
6548 struct got_pathlist_entry *pe;
6549 struct got_fileindex *fileindex = NULL;
6550 char *fileindex_path = NULL;
6551 struct unstage_path_arg upa;
6553 err = lock_worktree(worktree, LOCK_EX);
6557 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6561 upa.worktree = worktree;
6562 upa.fileindex = fileindex;
6564 upa.progress_cb = progress_cb;
6565 upa.progress_arg = progress_arg;
6566 upa.patch_cb = patch_cb;
6567 upa.patch_arg = patch_arg;
6568 TAILQ_FOREACH(pe, paths, entry) {
6569 err = worktree_status(worktree, pe->path, fileindex, repo,
6570 unstage_path, &upa, NULL, NULL, 0, 0);
6575 sync_err = sync_fileindex(fileindex, fileindex_path);
6576 if (sync_err && err == NULL)
6579 free(fileindex_path);
6581 got_fileindex_free(fileindex);
6582 unlockerr = lock_worktree(worktree, LOCK_SH);
6583 if (unlockerr && err == NULL)