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("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
711 err = got_error_from_errno2("fopen", f2_path);
715 err = check_file_contents_equal(same, f1, f2);
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
805 if (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", merged_path);
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
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 if (errno == ENOENT) {
1132 if (got_fileindex_entry_has_file_on_disk(ie))
1133 *status = GOT_STATUS_MISSING;
1135 *status = GOT_STATUS_DELETE;
1138 err = got_error_from_errno2("fstatat", abspath);
1142 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1143 if (fd == -1 && errno != ENOENT)
1144 return got_error_from_errno2("open", abspath);
1145 if (fd == -1 || fstat(fd, sb) == -1) {
1146 if (errno == ENOENT) {
1147 if (got_fileindex_entry_has_file_on_disk(ie))
1148 *status = GOT_STATUS_MISSING;
1150 *status = GOT_STATUS_DELETE;
1153 err = got_error_from_errno2("fstat", abspath);
1158 if (!S_ISREG(sb->st_mode)) {
1159 *status = GOT_STATUS_OBSTRUCTED;
1163 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1164 *status = GOT_STATUS_DELETE;
1166 } else if (!got_fileindex_entry_has_blob(ie) &&
1167 staged_status != GOT_STATUS_ADD) {
1168 *status = GOT_STATUS_ADD;
1172 if (!stat_info_differs(ie, sb))
1175 if (staged_status == GOT_STATUS_MODIFY ||
1176 staged_status == GOT_STATUS_ADD)
1177 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1179 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1181 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1186 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1188 err = got_error_from_errno2("openat", abspath);
1193 f = fdopen(fd, "r");
1195 err = got_error_from_errno2("fdopen", abspath);
1199 hdrlen = got_object_blob_get_hdrlen(blob);
1201 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1202 err = got_object_blob_read_block(&blen, blob);
1205 /* Skip length of blob object header first time around. */
1206 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1207 if (flen == 0 && ferror(f)) {
1208 err = got_error_from_errno("fread");
1213 *status = GOT_STATUS_MODIFY;
1215 } else if (flen == 0) {
1217 *status = GOT_STATUS_MODIFY;
1219 } else if (blen - hdrlen == flen) {
1220 /* Skip blob object header first time around. */
1221 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1222 *status = GOT_STATUS_MODIFY;
1226 *status = GOT_STATUS_MODIFY;
1232 if (*status == GOT_STATUS_MODIFY) {
1234 err = get_modified_file_content_status(status, f);
1235 } else if (xbit_differs(ie, sb->st_mode))
1236 *status = GOT_STATUS_MODE_CHANGE;
1239 got_object_blob_close(blob);
1240 if (f != NULL && fclose(f) == EOF && err == NULL)
1241 err = got_error_from_errno2("fclose", abspath);
1242 if (fd != -1 && close(fd) == -1 && err == NULL)
1243 err = got_error_from_errno2("close", abspath);
1248 * Update timestamps in the file index if a file is unmodified and
1249 * we had to run a full content comparison to find out.
1251 static const struct got_error *
1252 sync_timestamps(char *ondisk_path, unsigned char status,
1253 struct got_fileindex_entry *ie, struct stat *sb)
1255 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1256 return got_fileindex_entry_update(ie, ondisk_path,
1257 ie->blob_sha1, ie->commit_sha1, 1);
1262 static const struct got_error *
1263 update_blob(struct got_worktree *worktree,
1264 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1265 struct got_tree_entry *te, const char *path,
1266 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1269 const struct got_error *err = NULL;
1270 struct got_blob_object *blob = NULL;
1272 unsigned char status = GOT_STATUS_NO_CHANGE;
1275 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1276 return got_error_from_errno("asprintf");
1279 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1280 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1283 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1287 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1288 sb.st_mode = got_fileindex_perms_to_st(ie);
1290 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1292 if (status == GOT_STATUS_OBSTRUCTED) {
1293 err = (*progress_cb)(progress_arg, status, path);
1297 if (ie && status != GOT_STATUS_MISSING &&
1298 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1299 if (got_fileindex_entry_has_commit(ie) &&
1300 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1301 SHA1_DIGEST_LENGTH) == 0) {
1302 err = sync_timestamps(ondisk_path, status, ie, &sb);
1305 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1309 if (got_fileindex_entry_has_blob(ie) &&
1310 memcmp(ie->blob_sha1, te->id.sha1,
1311 SHA1_DIGEST_LENGTH) == 0) {
1312 err = sync_timestamps(ondisk_path, status, ie, &sb);
1317 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1321 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1322 int update_timestamps;
1323 struct got_blob_object *blob2 = NULL;
1324 char *label_orig = NULL;
1325 if (got_fileindex_entry_has_blob(ie)) {
1326 struct got_object_id id2;
1327 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1328 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1332 if (got_fileindex_entry_has_commit(ie)) {
1333 char id_str[SHA1_DIGEST_STRING_LENGTH];
1334 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1335 sizeof(id_str)) == NULL) {
1336 err = got_error_path(id_str,
1337 GOT_ERR_BAD_OBJ_ID_STR);
1340 if (asprintf(&label_orig, "%s: commit %s",
1341 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1342 err = got_error_from_errno("asprintf");
1346 err = merge_blob(&update_timestamps, worktree, blob2,
1347 ondisk_path, path, sb.st_mode, label_orig, blob,
1348 worktree->base_commit_id, repo,
1349 progress_cb, progress_arg);
1352 got_object_blob_close(blob2);
1356 * Do not update timestamps of files with local changes.
1357 * Otherwise, a future status walk would treat them as
1358 * unmodified files again.
1360 err = got_fileindex_entry_update(ie, ondisk_path,
1361 blob->id.sha1, worktree->base_commit_id->sha1,
1363 } else if (status == GOT_STATUS_MODE_CHANGE) {
1364 err = got_fileindex_entry_update(ie, ondisk_path,
1365 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1366 } else if (status == GOT_STATUS_DELETE) {
1367 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1370 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1371 ondisk_path, path, blob, 0);
1375 err = install_blob(worktree, ondisk_path, path, te->mode,
1376 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1377 repo, progress_cb, progress_arg);
1380 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1381 ondisk_path, path, blob, 1);
1385 got_object_blob_close(blob);
1391 static const struct got_error *
1392 remove_ondisk_file(const char *root_path, const char *path)
1394 const struct got_error *err = NULL;
1395 char *ondisk_path = NULL;
1397 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1398 return got_error_from_errno("asprintf");
1400 if (unlink(ondisk_path) == -1) {
1401 if (errno != ENOENT)
1402 err = got_error_from_errno2("unlink", ondisk_path);
1404 char *parent = dirname(ondisk_path);
1405 while (parent && strcmp(parent, root_path) != 0) {
1406 if (rmdir(parent) == -1) {
1407 if (errno != ENOTEMPTY)
1408 err = got_error_from_errno2("rmdir",
1412 parent = dirname(parent);
1419 static const struct got_error *
1420 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1421 struct got_fileindex_entry *ie, struct got_repository *repo,
1422 got_worktree_checkout_cb progress_cb, void *progress_arg)
1424 const struct got_error *err = NULL;
1425 unsigned char status;
1429 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1430 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1432 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1434 return got_error_from_errno("asprintf");
1436 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1440 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1441 status == GOT_STATUS_ADD) {
1442 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1446 * Preserve the working file and change the deleted blob's
1447 * entry into a schedule-add entry.
1449 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1454 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1457 if (status == GOT_STATUS_NO_CHANGE) {
1458 err = remove_ondisk_file(worktree->root_path, ie->path);
1462 got_fileindex_entry_remove(fileindex, ie);
1468 struct diff_cb_arg {
1469 struct got_fileindex *fileindex;
1470 struct got_worktree *worktree;
1471 struct got_repository *repo;
1472 got_worktree_checkout_cb progress_cb;
1474 got_cancel_cb cancel_cb;
1478 static const struct got_error *
1479 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1480 struct got_tree_entry *te, const char *parent_path)
1482 struct diff_cb_arg *a = arg;
1484 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1485 return got_error(GOT_ERR_CANCELLED);
1487 return update_blob(a->worktree, a->fileindex, ie, te,
1488 ie->path, a->repo, a->progress_cb, a->progress_arg);
1491 static const struct got_error *
1492 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1494 struct diff_cb_arg *a = arg;
1496 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1497 return got_error(GOT_ERR_CANCELLED);
1499 return delete_blob(a->worktree, a->fileindex, ie,
1500 a->repo, a->progress_cb, a->progress_arg);
1503 static const struct got_error *
1504 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1506 struct diff_cb_arg *a = arg;
1507 const struct got_error *err;
1510 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1511 return got_error(GOT_ERR_CANCELLED);
1513 if (got_object_tree_entry_is_submodule(te))
1516 if (asprintf(&path, "%s%s%s", parent_path,
1517 parent_path[0] ? "/" : "", te->name)
1519 return got_error_from_errno("asprintf");
1521 if (S_ISDIR(te->mode))
1522 err = add_dir_on_disk(a->worktree, path);
1524 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1525 a->repo, a->progress_cb, a->progress_arg);
1531 static const struct got_error *
1532 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1534 const struct got_error *err = NULL;
1535 char *uuidstr = NULL;
1536 uint32_t uuid_status;
1540 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1541 if (uuid_status != uuid_s_ok)
1542 return got_error_uuid(uuid_status, "uuid_to_string");
1544 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1546 err = got_error_from_errno("asprintf");
1553 const struct got_error *
1554 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1556 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1559 static const struct got_error *
1560 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1562 return get_ref_name(refname, worktree,
1563 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1566 static const struct got_error *
1567 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1569 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1572 static const struct got_error *
1573 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1575 return get_ref_name(refname, worktree,
1576 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1579 static const struct got_error *
1580 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1582 return get_ref_name(refname, worktree,
1583 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1586 static const struct got_error *
1587 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1589 return get_ref_name(refname, worktree,
1590 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1593 static const struct got_error *
1594 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1596 return get_ref_name(refname, worktree,
1597 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1600 static const struct got_error *
1601 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1603 return get_ref_name(refname, worktree,
1604 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1607 static const struct got_error *
1608 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1610 return get_ref_name(refname, worktree,
1611 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1614 const struct got_error *
1615 got_worktree_get_histedit_script_path(char **path,
1616 struct got_worktree *worktree)
1618 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1619 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1621 return got_error_from_errno("asprintf");
1627 * Prevent Git's garbage collector from deleting our base commit by
1628 * setting a reference to our base commit's ID.
1630 static const struct got_error *
1631 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1633 const struct got_error *err = NULL;
1634 struct got_reference *ref = NULL;
1637 err = got_worktree_get_base_ref_name(&refname, worktree);
1641 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1645 err = got_ref_write(ref, repo);
1653 static const struct got_error *
1654 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1656 const struct got_error *err = NULL;
1658 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1659 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1660 err = got_error_from_errno("asprintf");
1661 *fileindex_path = NULL;
1667 static const struct got_error *
1668 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1669 struct got_worktree *worktree)
1671 const struct got_error *err = NULL;
1674 *fileindex_path = NULL;
1675 *fileindex = got_fileindex_alloc();
1676 if (*fileindex == NULL)
1677 return got_error_from_errno("got_fileindex_alloc");
1679 err = get_fileindex_path(fileindex_path, worktree);
1683 index = fopen(*fileindex_path, "rb");
1684 if (index == NULL) {
1685 if (errno != ENOENT)
1686 err = got_error_from_errno2("fopen", *fileindex_path);
1688 err = got_fileindex_read(*fileindex, index);
1689 if (fclose(index) != 0 && err == NULL)
1690 err = got_error_from_errno("fclose");
1694 free(*fileindex_path);
1695 *fileindex_path = NULL;
1696 got_fileindex_free(*fileindex);
1702 struct bump_base_commit_id_arg {
1703 struct got_object_id *base_commit_id;
1706 const char *entry_name;
1707 got_worktree_checkout_cb progress_cb;
1711 /* Bump base commit ID of all files within an updated part of the work tree. */
1712 static const struct got_error *
1713 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1715 const struct got_error *err;
1716 struct bump_base_commit_id_arg *a = arg;
1718 if (a->entry_name) {
1719 if (strcmp(ie->path, a->path) != 0)
1721 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1724 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1725 SHA1_DIGEST_LENGTH) == 0)
1728 if (a->progress_cb) {
1729 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1734 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1738 static const struct got_error *
1739 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1741 const struct got_error *err = NULL;
1742 char *new_fileindex_path = NULL;
1743 FILE *new_index = NULL;
1745 err = got_opentemp_named(&new_fileindex_path, &new_index,
1750 err = got_fileindex_write(fileindex, new_index);
1754 if (rename(new_fileindex_path, fileindex_path) != 0) {
1755 err = got_error_from_errno3("rename", new_fileindex_path,
1757 unlink(new_fileindex_path);
1762 free(new_fileindex_path);
1766 static const struct got_error *
1767 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1768 struct got_object_id **tree_id, const char *wt_relpath,
1769 struct got_worktree *worktree, struct got_repository *repo)
1771 const struct got_error *err = NULL;
1772 struct got_object_id *id = NULL;
1773 char *in_repo_path = NULL;
1774 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1776 *entry_type = GOT_OBJ_TYPE_ANY;
1777 *tree_relpath = NULL;
1780 if (wt_relpath[0] == '\0') {
1781 /* Check out all files within the work tree. */
1782 *entry_type = GOT_OBJ_TYPE_TREE;
1783 *tree_relpath = strdup("");
1784 if (*tree_relpath == NULL) {
1785 err = got_error_from_errno("strdup");
1788 err = got_object_id_by_path(tree_id, repo,
1789 worktree->base_commit_id, worktree->path_prefix);
1795 /* Check out a subset of files in the work tree. */
1797 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1798 is_root_wt ? "" : "/", wt_relpath) == -1) {
1799 err = got_error_from_errno("asprintf");
1803 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1809 in_repo_path = NULL;
1811 err = got_object_get_type(entry_type, repo, id);
1815 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1816 /* Check out a single file. */
1817 if (strchr(wt_relpath, '/') == NULL) {
1818 /* Check out a single file in work tree's root dir. */
1819 in_repo_path = strdup(worktree->path_prefix);
1820 if (in_repo_path == NULL) {
1821 err = got_error_from_errno("strdup");
1824 *tree_relpath = strdup("");
1825 if (*tree_relpath == NULL) {
1826 err = got_error_from_errno("strdup");
1830 /* Check out a single file in a subdirectory. */
1831 err = got_path_dirname(tree_relpath, wt_relpath);
1834 if (asprintf(&in_repo_path, "%s%s%s",
1835 worktree->path_prefix, is_root_wt ? "" : "/",
1836 *tree_relpath) == -1) {
1837 err = got_error_from_errno("asprintf");
1841 err = got_object_id_by_path(tree_id, repo,
1842 worktree->base_commit_id, in_repo_path);
1844 /* Check out all files within a subdirectory. */
1845 *tree_id = got_object_id_dup(id);
1846 if (*tree_id == NULL) {
1847 err = got_error_from_errno("got_object_id_dup");
1850 *tree_relpath = strdup(wt_relpath);
1851 if (*tree_relpath == NULL) {
1852 err = got_error_from_errno("strdup");
1860 *entry_type = GOT_OBJ_TYPE_ANY;
1861 free(*tree_relpath);
1862 *tree_relpath = NULL;
1869 static const struct got_error *
1870 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1871 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1872 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1873 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1875 const struct got_error *err = NULL;
1876 struct got_commit_object *commit = NULL;
1877 struct got_tree_object *tree = NULL;
1878 struct got_fileindex_diff_tree_cb diff_cb;
1879 struct diff_cb_arg arg;
1881 err = ref_base_commit(worktree, repo);
1885 err = got_object_open_as_commit(&commit, repo,
1886 worktree->base_commit_id);
1890 err = got_object_open_as_tree(&tree, repo, tree_id);
1895 got_object_tree_find_entry(tree, entry_name) == NULL) {
1896 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1900 diff_cb.diff_old_new = diff_old_new;
1901 diff_cb.diff_old = diff_old;
1902 diff_cb.diff_new = diff_new;
1903 arg.fileindex = fileindex;
1904 arg.worktree = worktree;
1906 arg.progress_cb = progress_cb;
1907 arg.progress_arg = progress_arg;
1908 arg.cancel_cb = cancel_cb;
1909 arg.cancel_arg = cancel_arg;
1910 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1911 entry_name, repo, &diff_cb, &arg);
1914 got_object_tree_close(tree);
1916 got_object_commit_close(commit);
1920 const struct got_error *
1921 got_worktree_checkout_files(struct got_worktree *worktree,
1922 struct got_pathlist_head *paths, struct got_repository *repo,
1923 got_worktree_checkout_cb progress_cb, void *progress_arg,
1924 got_cancel_cb cancel_cb, void *cancel_arg)
1926 const struct got_error *err = NULL, *sync_err, *unlockerr;
1927 struct got_commit_object *commit = NULL;
1928 struct got_tree_object *tree = NULL;
1929 struct got_fileindex *fileindex = NULL;
1930 char *fileindex_path = NULL;
1931 struct got_pathlist_entry *pe;
1932 struct tree_path_data {
1933 SIMPLEQ_ENTRY(tree_path_data) entry;
1934 struct got_object_id *tree_id;
1939 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1941 SIMPLEQ_INIT(&tree_paths);
1943 err = lock_worktree(worktree, LOCK_EX);
1947 /* Map all specified paths to in-repository trees. */
1948 TAILQ_FOREACH(pe, paths, entry) {
1949 tpd = malloc(sizeof(*tpd));
1951 err = got_error_from_errno("malloc");
1955 err = find_tree_entry_for_checkout(&tpd->entry_type,
1956 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1962 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1963 err = got_path_basename(&tpd->entry_name, pe->path);
1971 tpd->entry_name = NULL;
1973 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1977 * Read the file index.
1978 * Checking out files is supposed to be an idempotent operation.
1979 * If the on-disk file index is incomplete we will try to complete it.
1981 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1985 tpd = SIMPLEQ_FIRST(&tree_paths);
1986 TAILQ_FOREACH(pe, paths, entry) {
1987 struct bump_base_commit_id_arg bbc_arg;
1989 err = checkout_files(worktree, fileindex, tpd->relpath,
1990 tpd->tree_id, tpd->entry_name, repo,
1991 progress_cb, progress_arg, cancel_cb, cancel_arg);
1995 bbc_arg.base_commit_id = worktree->base_commit_id;
1996 bbc_arg.entry_name = tpd->entry_name;
1997 bbc_arg.path = pe->path;
1998 bbc_arg.path_len = pe->path_len;
1999 bbc_arg.progress_cb = progress_cb;
2000 bbc_arg.progress_arg = progress_arg;
2001 err = got_fileindex_for_each_entry_safe(fileindex,
2002 bump_base_commit_id, &bbc_arg);
2006 tpd = SIMPLEQ_NEXT(tpd, entry);
2008 sync_err = sync_fileindex(fileindex, fileindex_path);
2009 if (sync_err && err == NULL)
2012 free(fileindex_path);
2014 got_object_tree_close(tree);
2016 got_object_commit_close(commit);
2018 got_fileindex_free(fileindex);
2019 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2020 tpd = SIMPLEQ_FIRST(&tree_paths);
2021 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2026 unlockerr = lock_worktree(worktree, LOCK_SH);
2027 if (unlockerr && err == NULL)
2032 struct merge_file_cb_arg {
2033 struct got_worktree *worktree;
2034 struct got_fileindex *fileindex;
2035 got_worktree_checkout_cb progress_cb;
2037 got_cancel_cb cancel_cb;
2039 const char *label_orig;
2040 struct got_object_id *commit_id2;
2043 static const struct got_error *
2044 merge_file_cb(void *arg, struct got_blob_object *blob1,
2045 struct got_blob_object *blob2, struct got_object_id *id1,
2046 struct got_object_id *id2, const char *path1, const char *path2,
2047 mode_t mode1, mode_t mode2, struct got_repository *repo)
2049 static const struct got_error *err = NULL;
2050 struct merge_file_cb_arg *a = arg;
2051 struct got_fileindex_entry *ie;
2052 char *ondisk_path = NULL;
2054 unsigned char status;
2055 int local_changes_subsumed;
2057 if (blob1 && blob2) {
2058 ie = got_fileindex_entry_get(a->fileindex, path2,
2061 return (*a->progress_cb)(a->progress_arg,
2062 GOT_STATUS_MISSING, path2);
2064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2066 return got_error_from_errno("asprintf");
2068 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2073 if (status == GOT_STATUS_DELETE) {
2074 err = (*a->progress_cb)(a->progress_arg,
2075 GOT_STATUS_MERGE, path2);
2078 if (status != GOT_STATUS_NO_CHANGE &&
2079 status != GOT_STATUS_MODIFY &&
2080 status != GOT_STATUS_CONFLICT &&
2081 status != GOT_STATUS_ADD) {
2082 err = (*a->progress_cb)(a->progress_arg, status, path2);
2086 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2087 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2088 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2090 ie = got_fileindex_entry_get(a->fileindex, path1,
2093 return (*a->progress_cb)(a->progress_arg,
2094 GOT_STATUS_MISSING, path2);
2096 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2098 return got_error_from_errno("asprintf");
2100 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2106 case GOT_STATUS_NO_CHANGE:
2107 err = (*a->progress_cb)(a->progress_arg,
2108 GOT_STATUS_DELETE, path1);
2111 err = remove_ondisk_file(a->worktree->root_path, path1);
2115 got_fileindex_entry_mark_deleted_from_disk(ie);
2117 case GOT_STATUS_DELETE:
2118 case GOT_STATUS_MISSING:
2119 err = (*a->progress_cb)(a->progress_arg,
2120 GOT_STATUS_DELETE, path1);
2124 got_fileindex_entry_mark_deleted_from_disk(ie);
2126 case GOT_STATUS_ADD:
2127 case GOT_STATUS_MODIFY:
2128 case GOT_STATUS_CONFLICT:
2129 err = (*a->progress_cb)(a->progress_arg,
2130 GOT_STATUS_CANNOT_DELETE, path1);
2134 case GOT_STATUS_OBSTRUCTED:
2135 err = (*a->progress_cb)(a->progress_arg, status, path1);
2143 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2145 return got_error_from_errno("asprintf");
2146 ie = got_fileindex_entry_get(a->fileindex, path2,
2149 err = get_file_status(&status, &sb, ie, ondisk_path,
2153 if (status != GOT_STATUS_NO_CHANGE &&
2154 status != GOT_STATUS_MODIFY &&
2155 status != GOT_STATUS_CONFLICT &&
2156 status != GOT_STATUS_ADD) {
2157 err = (*a->progress_cb)(a->progress_arg,
2161 err = merge_blob(&local_changes_subsumed, a->worktree,
2162 NULL, ondisk_path, path2, sb.st_mode,
2163 a->label_orig, blob2, a->commit_id2, repo,
2166 if (status == GOT_STATUS_DELETE) {
2167 err = update_blob_fileindex_entry(a->worktree,
2168 a->fileindex, ie, ondisk_path, ie->path,
2174 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2175 err = install_blob(a->worktree, ondisk_path, path2,
2176 /* XXX get this from parent tree! */
2177 GOT_DEFAULT_FILE_MODE,
2178 sb.st_mode, blob2, 0, 0, repo,
2179 a->progress_cb, a->progress_arg);
2182 err = got_fileindex_entry_alloc(&ie,
2183 ondisk_path, path2, NULL, NULL);
2186 err = got_fileindex_entry_add(a->fileindex, ie);
2188 got_fileindex_entry_free(ie);
2198 struct check_merge_ok_arg {
2199 struct got_worktree *worktree;
2200 struct got_repository *repo;
2203 static const struct got_error *
2204 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2206 const struct got_error *err = NULL;
2207 struct check_merge_ok_arg *a = arg;
2208 unsigned char status;
2212 /* Reject merges into a work tree with mixed base commits. */
2213 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2214 SHA1_DIGEST_LENGTH))
2215 return got_error(GOT_ERR_MIXED_COMMITS);
2217 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2219 return got_error_from_errno("asprintf");
2221 /* Reject merges into a work tree with conflicted files. */
2222 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2225 if (status == GOT_STATUS_CONFLICT)
2226 return got_error(GOT_ERR_CONFLICTS);
2231 static const struct got_error *
2232 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2233 const char *fileindex_path, struct got_object_id *commit_id1,
2234 struct got_object_id *commit_id2, struct got_repository *repo,
2235 got_worktree_checkout_cb progress_cb, void *progress_arg,
2236 got_cancel_cb cancel_cb, void *cancel_arg)
2238 const struct got_error *err = NULL, *sync_err;
2239 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2240 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2241 struct merge_file_cb_arg arg;
2242 char *label_orig = NULL;
2247 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2248 worktree->path_prefix);
2252 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2256 err = got_object_id_str(&id_str, commit_id1);
2260 if (asprintf(&label_orig, "%s: commit %s",
2261 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2262 err = got_error_from_errno("asprintf");
2269 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2270 worktree->path_prefix);
2274 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2278 arg.worktree = worktree;
2279 arg.fileindex = fileindex;
2280 arg.progress_cb = progress_cb;
2281 arg.progress_arg = progress_arg;
2282 arg.cancel_cb = cancel_cb;
2283 arg.cancel_arg = cancel_arg;
2284 arg.label_orig = label_orig;
2285 arg.commit_id2 = commit_id2;
2286 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2287 sync_err = sync_fileindex(fileindex, fileindex_path);
2288 if (sync_err && err == NULL)
2292 got_object_tree_close(tree1);
2294 got_object_tree_close(tree2);
2299 const struct got_error *
2300 got_worktree_merge_files(struct got_worktree *worktree,
2301 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2302 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2303 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2305 const struct got_error *err, *unlockerr;
2306 char *fileindex_path = NULL;
2307 struct got_fileindex *fileindex = NULL;
2308 struct check_merge_ok_arg mok_arg;
2310 err = lock_worktree(worktree, LOCK_EX);
2314 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2318 mok_arg.worktree = worktree;
2319 mok_arg.repo = repo;
2320 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2325 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2326 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2329 got_fileindex_free(fileindex);
2330 free(fileindex_path);
2331 unlockerr = lock_worktree(worktree, LOCK_SH);
2332 if (unlockerr && err == NULL)
2337 struct diff_dir_cb_arg {
2338 struct got_fileindex *fileindex;
2339 struct got_worktree *worktree;
2340 const char *status_path;
2341 size_t status_path_len;
2342 struct got_repository *repo;
2343 got_worktree_status_cb status_cb;
2345 got_cancel_cb cancel_cb;
2347 /* A pathlist containing per-directory pathlists of ignore patterns. */
2348 struct got_pathlist_head ignores;
2349 int report_unchanged;
2352 static const struct got_error *
2353 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2354 int dirfd, const char *de_name,
2355 got_worktree_status_cb status_cb, void *status_arg,
2356 struct got_repository *repo, int report_unchanged)
2358 const struct got_error *err = NULL;
2359 unsigned char status = GOT_STATUS_NO_CHANGE;
2360 unsigned char staged_status = get_staged_status(ie);
2362 struct got_object_id blob_id, commit_id, staged_blob_id;
2363 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2364 struct got_object_id *staged_blob_idp = NULL;
2366 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2370 if (status == GOT_STATUS_NO_CHANGE &&
2371 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2374 if (got_fileindex_entry_has_blob(ie)) {
2375 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2376 blob_idp = &blob_id;
2378 if (got_fileindex_entry_has_commit(ie)) {
2379 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2380 commit_idp = &commit_id;
2382 if (staged_status == GOT_STATUS_ADD ||
2383 staged_status == GOT_STATUS_MODIFY) {
2384 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2385 SHA1_DIGEST_LENGTH);
2386 staged_blob_idp = &staged_blob_id;
2389 return (*status_cb)(status_arg, status, staged_status,
2390 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2393 static const struct got_error *
2394 status_old_new(void *arg, struct got_fileindex_entry *ie,
2395 struct dirent *de, const char *parent_path, int dirfd)
2397 const struct got_error *err = NULL;
2398 struct diff_dir_cb_arg *a = arg;
2401 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2402 return got_error(GOT_ERR_CANCELLED);
2404 if (got_path_cmp(parent_path, a->status_path,
2405 strlen(parent_path), a->status_path_len) != 0 &&
2406 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2409 if (parent_path[0]) {
2410 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2411 parent_path, de->d_name) == -1)
2412 return got_error_from_errno("asprintf");
2414 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2416 return got_error_from_errno("asprintf");
2419 err = report_file_status(ie, abspath, dirfd, de->d_name,
2420 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2425 static const struct got_error *
2426 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2428 struct diff_dir_cb_arg *a = arg;
2429 struct got_object_id blob_id, commit_id;
2430 unsigned char status;
2432 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2433 return got_error(GOT_ERR_CANCELLED);
2435 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2438 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2439 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2440 if (got_fileindex_entry_has_file_on_disk(ie))
2441 status = GOT_STATUS_MISSING;
2443 status = GOT_STATUS_DELETE;
2444 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2445 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2449 free_ignorelist(struct got_pathlist_head *ignorelist)
2451 struct got_pathlist_entry *pe;
2453 TAILQ_FOREACH(pe, ignorelist, entry)
2454 free((char *)pe->path);
2455 got_pathlist_free(ignorelist);
2459 free_ignores(struct got_pathlist_head *ignores)
2461 struct got_pathlist_entry *pe;
2463 TAILQ_FOREACH(pe, ignores, entry) {
2464 struct got_pathlist_head *ignorelist = pe->data;
2465 free_ignorelist(ignorelist);
2466 free((char *)pe->path);
2468 got_pathlist_free(ignores);
2471 static const struct got_error *
2472 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2474 const struct got_error *err = NULL;
2475 struct got_pathlist_entry *pe = NULL;
2476 struct got_pathlist_head *ignorelist;
2477 char *line = NULL, *pattern, *dirpath = NULL;
2478 size_t linesize = 0;
2481 ignorelist = calloc(1, sizeof(*ignorelist));
2482 if (ignorelist == NULL)
2483 return got_error_from_errno("calloc");
2484 TAILQ_INIT(ignorelist);
2486 while ((linelen = getline(&line, &linesize, f)) != -1) {
2487 if (linelen > 0 && line[linelen - 1] == '\n')
2488 line[linelen - 1] = '\0';
2490 /* Git's ignores may contain comments. */
2494 /* Git's negated patterns are not (yet?) supported. */
2498 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2500 err = got_error_from_errno("asprintf");
2503 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2508 err = got_error_from_errno("getline");
2512 dirpath = strdup(path);
2513 if (dirpath == NULL) {
2514 err = got_error_from_errno("strdup");
2517 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2520 if (err || pe == NULL) {
2522 free_ignorelist(ignorelist);
2528 match_ignores(struct got_pathlist_head *ignores, const char *path)
2530 struct got_pathlist_entry *pe;
2532 /* Handle patterns which match in all directories. */
2533 TAILQ_FOREACH(pe, ignores, entry) {
2534 struct got_pathlist_head *ignorelist = pe->data;
2535 struct got_pathlist_entry *pi;
2537 TAILQ_FOREACH(pi, ignorelist, entry) {
2538 const char *p, *pattern = pi->path;
2540 if (strncmp(pattern, "**/", 3) != 0)
2545 if (fnmatch(pattern, p,
2546 FNM_PATHNAME | FNM_LEADING_DIR)) {
2547 /* Retry in next directory. */
2548 while (*p && *p != '/')
2560 * The ignores pathlist contains ignore lists from children before
2561 * parents, so we can find the most specific ignorelist by walking
2562 * ignores backwards.
2564 pe = TAILQ_LAST(ignores, got_pathlist_head);
2566 if (got_path_is_child(path, pe->path, pe->path_len)) {
2567 struct got_pathlist_head *ignorelist = pe->data;
2568 struct got_pathlist_entry *pi;
2569 TAILQ_FOREACH(pi, ignorelist, entry) {
2570 const char *pattern = pi->path;
2571 int flags = FNM_LEADING_DIR;
2572 if (strstr(pattern, "/**/") == NULL)
2573 flags |= FNM_PATHNAME;
2574 if (fnmatch(pattern, path, flags))
2579 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2585 static const struct got_error *
2586 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2587 const char *path, int dirfd, const char *ignores_filename)
2589 const struct got_error *err = NULL;
2592 FILE *ignoresfile = NULL;
2594 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2595 path[0] ? "/" : "", ignores_filename) == -1)
2596 return got_error_from_errno("asprintf");
2599 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2601 if (errno != ENOENT && errno != EACCES)
2602 err = got_error_from_errno2("openat",
2605 ignoresfile = fdopen(fd, "r");
2606 if (ignoresfile == NULL)
2607 err = got_error_from_errno2("fdopen",
2611 err = read_ignores(ignores, path, ignoresfile);
2615 ignoresfile = fopen(ignorespath, "r");
2616 if (ignoresfile == NULL) {
2617 if (errno != ENOENT && errno != EACCES)
2618 err = got_error_from_errno2("fopen",
2621 err = read_ignores(ignores, path, ignoresfile);
2624 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2625 err = got_error_from_errno2("fclose", path);
2626 if (fd != -1 && close(fd) == -1 && err == NULL)
2627 err = got_error_from_errno2("close", path);
2632 static const struct got_error *
2633 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2635 const struct got_error *err = NULL;
2636 struct diff_dir_cb_arg *a = arg;
2639 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2640 return got_error(GOT_ERR_CANCELLED);
2642 /* XXX ignore symlinks for now */
2643 if (de->d_type == DT_LNK)
2646 if (parent_path[0]) {
2647 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2648 return got_error_from_errno("asprintf");
2653 if (de->d_type == DT_DIR) {
2654 int subdirfd = openat(dirfd, de->d_name,
2655 O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2656 if (subdirfd == -1) {
2657 if (errno != ENOENT && errno != EACCES)
2658 err = got_error_from_errno2("openat", path);
2660 err = add_ignores(&a->ignores, a->worktree->root_path,
2661 path, subdirfd, ".cvsignore");
2663 err = add_ignores(&a->ignores,
2664 a->worktree->root_path, path,
2665 subdirfd, ".gitignore");
2666 if (close(subdirfd) == -1 && err == NULL)
2667 err = got_error_from_errno2("close", path);
2669 } else if (got_path_is_child(path, a->status_path, a->status_path_len)
2670 && !match_ignores(&a->ignores, path))
2671 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2672 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2678 static const struct got_error *
2679 report_single_file_status(const char *path, const char *ondisk_path,
2680 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2681 void *status_arg, struct got_repository *repo, int report_unchanged)
2683 struct got_fileindex_entry *ie;
2686 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2688 return report_file_status(ie, ondisk_path, -1, NULL,
2689 status_cb, status_arg, repo, report_unchanged);
2691 if (lstat(ondisk_path, &sb) == -1) {
2692 if (errno != ENOENT)
2693 return got_error_from_errno2("lstat", ondisk_path);
2694 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2695 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2699 if (S_ISREG(sb.st_mode))
2700 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2701 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2706 static const struct got_error *
2707 worktree_status(struct got_worktree *worktree, const char *path,
2708 struct got_fileindex *fileindex, struct got_repository *repo,
2709 got_worktree_status_cb status_cb, void *status_arg,
2710 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2711 int report_unchanged)
2713 const struct got_error *err = NULL;
2715 struct got_fileindex_diff_dir_cb fdiff_cb;
2716 struct diff_dir_cb_arg arg;
2717 char *ondisk_path = NULL;
2719 if (asprintf(&ondisk_path, "%s%s%s",
2720 worktree->root_path, path[0] ? "/" : "", path) == -1)
2721 return got_error_from_errno("asprintf");
2723 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2725 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2726 err = got_error_from_errno2("open", ondisk_path);
2728 err = report_single_file_status(path, ondisk_path,
2729 fileindex, status_cb, status_arg, repo,
2732 fdiff_cb.diff_old_new = status_old_new;
2733 fdiff_cb.diff_old = status_old;
2734 fdiff_cb.diff_new = status_new;
2735 arg.fileindex = fileindex;
2736 arg.worktree = worktree;
2737 arg.status_path = path;
2738 arg.status_path_len = strlen(path);
2740 arg.status_cb = status_cb;
2741 arg.status_arg = status_arg;
2742 arg.cancel_cb = cancel_cb;
2743 arg.cancel_arg = cancel_arg;
2744 arg.report_unchanged = report_unchanged;
2745 TAILQ_INIT(&arg.ignores);
2747 err = add_ignores(&arg.ignores, worktree->root_path,
2748 path, fd, ".cvsignore");
2750 err = add_ignores(&arg.ignores,
2751 worktree->root_path, path, fd,
2755 err = got_fileindex_diff_dir(fileindex, fd,
2756 worktree->root_path, path, repo, &fdiff_cb, &arg);
2757 free_ignores(&arg.ignores);
2760 if (fd != -1 && close(fd) != 0 && err == NULL)
2761 err = got_error_from_errno("close");
2766 const struct got_error *
2767 got_worktree_status(struct got_worktree *worktree,
2768 struct got_pathlist_head *paths, struct got_repository *repo,
2769 got_worktree_status_cb status_cb, void *status_arg,
2770 got_cancel_cb cancel_cb, void *cancel_arg)
2772 const struct got_error *err = NULL;
2773 char *fileindex_path = NULL;
2774 struct got_fileindex *fileindex = NULL;
2775 struct got_pathlist_entry *pe;
2777 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2781 TAILQ_FOREACH(pe, paths, entry) {
2782 err = worktree_status(worktree, pe->path, fileindex, repo,
2783 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2787 free(fileindex_path);
2788 got_fileindex_free(fileindex);
2792 const struct got_error *
2793 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2796 const struct got_error *err = NULL;
2797 char *resolved, *cwd = NULL, *path = NULL;
2802 resolved = realpath(arg, NULL);
2803 if (resolved == NULL) {
2804 if (errno != ENOENT)
2805 return got_error_from_errno2("realpath", arg);
2806 cwd = getcwd(NULL, 0);
2808 return got_error_from_errno("getcwd");
2809 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2810 err = got_error_from_errno("asprintf");
2815 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2816 strlen(got_worktree_get_root_path(worktree)))) {
2817 err = got_error(GOT_ERR_BAD_PATH);
2821 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2822 err = got_path_skip_common_ancestor(&path,
2823 got_worktree_get_root_path(worktree), resolved);
2829 err = got_error_from_errno("strdup");
2834 /* XXX status walk can't deal with trailing slash! */
2836 while (len > 0 && path[len - 1] == '/') {
2837 path[len - 1] = '\0';
2850 struct schedule_addition_args {
2851 struct got_worktree *worktree;
2852 struct got_fileindex *fileindex;
2853 got_worktree_checkout_cb progress_cb;
2855 struct got_repository *repo;
2858 static const struct got_error *
2859 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2860 const char *relpath, struct got_object_id *blob_id,
2861 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2862 int dirfd, const char *de_name)
2864 struct schedule_addition_args *a = arg;
2865 const struct got_error *err = NULL;
2866 struct got_fileindex_entry *ie;
2870 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2872 return got_error_from_errno("asprintf");
2874 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2876 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2880 /* Re-adding an existing entry is a no-op. */
2881 if (status == GOT_STATUS_ADD)
2883 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2888 if (status != GOT_STATUS_UNVERSIONED) {
2889 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2893 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2897 err = got_fileindex_entry_add(a->fileindex, ie);
2899 got_fileindex_entry_free(ie);
2906 if (status == GOT_STATUS_ADD)
2908 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2911 const struct got_error *
2912 got_worktree_schedule_add(struct got_worktree *worktree,
2913 struct got_pathlist_head *paths,
2914 got_worktree_checkout_cb progress_cb, void *progress_arg,
2915 struct got_repository *repo, int no_ignores)
2917 struct got_fileindex *fileindex = NULL;
2918 char *fileindex_path = NULL;
2919 const struct got_error *err = NULL, *sync_err, *unlockerr;
2920 struct got_pathlist_entry *pe;
2921 struct schedule_addition_args saa;
2923 err = lock_worktree(worktree, LOCK_EX);
2927 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2931 saa.worktree = worktree;
2932 saa.fileindex = fileindex;
2933 saa.progress_cb = progress_cb;
2934 saa.progress_arg = progress_arg;
2937 TAILQ_FOREACH(pe, paths, entry) {
2938 err = worktree_status(worktree, pe->path, fileindex, repo,
2939 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2943 sync_err = sync_fileindex(fileindex, fileindex_path);
2944 if (sync_err && err == NULL)
2947 free(fileindex_path);
2949 got_fileindex_free(fileindex);
2950 unlockerr = lock_worktree(worktree, LOCK_SH);
2951 if (unlockerr && err == NULL)
2956 struct schedule_deletion_args {
2957 struct got_worktree *worktree;
2958 struct got_fileindex *fileindex;
2959 got_worktree_delete_cb progress_cb;
2961 struct got_repository *repo;
2962 int delete_local_mods;
2966 static const struct got_error *
2967 schedule_for_deletion(void *arg, unsigned char status,
2968 unsigned char staged_status, const char *relpath,
2969 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2970 struct got_object_id *commit_id, int dirfd, const char *de_name)
2972 struct schedule_deletion_args *a = arg;
2973 const struct got_error *err = NULL;
2974 struct got_fileindex_entry *ie = NULL;
2978 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2980 return got_error(GOT_ERR_BAD_PATH);
2982 staged_status = get_staged_status(ie);
2983 if (staged_status != GOT_STATUS_NO_CHANGE) {
2984 if (staged_status == GOT_STATUS_DELETE)
2986 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2989 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2991 return got_error_from_errno("asprintf");
2993 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
2998 if (status != GOT_STATUS_NO_CHANGE) {
2999 if (status == GOT_STATUS_DELETE)
3001 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3002 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3005 if (status != GOT_STATUS_MODIFY &&
3006 status != GOT_STATUS_MISSING) {
3007 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3012 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3014 if (unlinkat(dirfd, de_name, 0) != 0) {
3015 err = got_error_from_errno2("unlinkat",
3019 } else if (unlink(ondisk_path) != 0) {
3020 err = got_error_from_errno2("unlink", ondisk_path);
3025 got_fileindex_entry_mark_deleted_from_disk(ie);
3030 if (status == GOT_STATUS_DELETE)
3032 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3033 staged_status, relpath);
3036 const struct got_error *
3037 got_worktree_schedule_delete(struct got_worktree *worktree,
3038 struct got_pathlist_head *paths, int delete_local_mods,
3039 got_worktree_delete_cb progress_cb, void *progress_arg,
3040 struct got_repository *repo, int keep_on_disk)
3042 struct got_fileindex *fileindex = NULL;
3043 char *fileindex_path = NULL;
3044 const struct got_error *err = NULL, *sync_err, *unlockerr;
3045 struct got_pathlist_entry *pe;
3046 struct schedule_deletion_args sda;
3048 err = lock_worktree(worktree, LOCK_EX);
3052 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3056 sda.worktree = worktree;
3057 sda.fileindex = fileindex;
3058 sda.progress_cb = progress_cb;
3059 sda.progress_arg = progress_arg;
3061 sda.delete_local_mods = delete_local_mods;
3062 sda.keep_on_disk = keep_on_disk;
3064 TAILQ_FOREACH(pe, paths, entry) {
3065 err = worktree_status(worktree, pe->path, fileindex, repo,
3066 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3070 sync_err = sync_fileindex(fileindex, fileindex_path);
3071 if (sync_err && err == NULL)
3074 free(fileindex_path);
3076 got_fileindex_free(fileindex);
3077 unlockerr = lock_worktree(worktree, LOCK_SH);
3078 if (unlockerr && err == NULL)
3083 static const struct got_error *
3084 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3086 const struct got_error *err = NULL;
3088 size_t linesize = 0, n;
3091 linelen = getline(&line, &linesize, infile);
3092 if (linelen == -1) {
3093 if (ferror(infile)) {
3094 err = got_error_from_errno("getline");
3100 n = fwrite(line, 1, linelen, outfile);
3102 err = got_ferror(outfile, GOT_ERR_IO);
3107 n = fwrite(line, 1, linelen, rejectfile);
3109 err = got_ferror(outfile, GOT_ERR_IO);
3116 static const struct got_error *
3117 skip_one_line(FILE *f)
3120 size_t linesize = 0;
3123 linelen = getline(&line, &linesize, f);
3124 if (linelen == -1) {
3126 return got_error_from_errno("getline");
3133 static const struct got_error *
3134 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3135 int start_old, int end_old, int start_new, int end_new,
3136 FILE *outfile, FILE *rejectfile)
3138 const struct got_error *err;
3140 /* Copy old file's lines leading up to patch. */
3141 while (!feof(f1) && *line_cur1 < start_old) {
3142 err = copy_one_line(f1, outfile, NULL);
3147 /* Skip new file's lines leading up to patch. */
3148 while (!feof(f2) && *line_cur2 < start_new) {
3150 err = copy_one_line(f2, NULL, rejectfile);
3152 err = skip_one_line(f2);
3157 /* Copy patched lines. */
3158 while (!feof(f2) && *line_cur2 <= end_new) {
3159 err = copy_one_line(f2, outfile, NULL);
3164 /* Skip over old file's replaced lines. */
3165 while (!feof(f1) && *line_cur1 <= end_old) {
3167 err = copy_one_line(f1, NULL, rejectfile);
3169 err = skip_one_line(f1);
3178 static const struct got_error *
3179 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3180 FILE *outfile, FILE *rejectfile)
3182 const struct got_error *err;
3185 /* Copy old file's lines until EOF. */
3187 err = copy_one_line(f1, outfile, NULL);
3194 /* Copy new file's lines until EOF. */
3196 err = copy_one_line(f2, NULL, rejectfile);
3206 static const struct got_error *
3207 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3208 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3209 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3210 int *line_cur2, FILE *outfile, FILE *rejectfile,
3211 got_worktree_patch_cb patch_cb, void *patch_arg)
3213 const struct got_error *err = NULL;
3214 int start_old = change->cv.a;
3215 int end_old = change->cv.b;
3216 int start_new = change->cv.c;
3217 int end_new = change->cv.d;
3221 *choice = GOT_PATCH_CHOICE_NONE;
3223 hunkfile = got_opentemp();
3224 if (hunkfile == NULL)
3225 return got_error_from_errno("got_opentemp");
3230 /* XXX TODO needs error checking */
3231 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3233 if (fseek(f1, pos1, SEEK_SET) == -1) {
3234 err = got_ferror(f1, GOT_ERR_IO);
3237 if (fseek(f2, pos2, SEEK_SET) == -1) {
3238 err = got_ferror(f1, GOT_ERR_IO);
3241 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3242 err = got_ferror(hunkfile, GOT_ERR_IO);
3246 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3247 hunkfile, n, nchanges);
3252 case GOT_PATCH_CHOICE_YES:
3253 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3254 end_old, start_new, end_new, outfile, rejectfile);
3256 case GOT_PATCH_CHOICE_NO:
3257 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3258 end_old, start_new, end_new, rejectfile, outfile);
3260 case GOT_PATCH_CHOICE_QUIT:
3263 err = got_error(GOT_ERR_PATCH_CHOICE);
3267 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3268 err = got_error_from_errno("fclose");
3272 struct revert_file_args {
3273 struct got_worktree *worktree;
3274 struct got_fileindex *fileindex;
3275 got_worktree_checkout_cb progress_cb;
3277 got_worktree_patch_cb patch_cb;
3279 struct got_repository *repo;
3282 static const struct got_error *
3283 create_patched_content(char **path_outfile, int reverse_patch,
3284 struct got_object_id *blob_id, const char *path2,
3285 int dirfd2, const char *de_name2,
3286 const char *relpath, struct got_repository *repo,
3287 got_worktree_patch_cb patch_cb, void *patch_arg)
3289 const struct got_error *err;
3290 struct got_blob_object *blob = NULL;
3291 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3293 char *path1 = NULL, *id_str = NULL;
3294 struct stat sb1, sb2;
3295 struct got_diff_changes *changes = NULL;
3296 struct got_diff_state *ds = NULL;
3297 struct got_diff_args *args = NULL;
3298 struct got_diff_change *change;
3299 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3302 *path_outfile = NULL;
3304 err = got_object_id_str(&id_str, blob_id);
3309 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3311 err = got_error_from_errno2("openat", path2);
3315 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3317 err = got_error_from_errno2("open", path2);
3321 if (fstat(fd2, &sb2) == -1) {
3322 err = got_error_from_errno2("fstat", path2);
3326 f2 = fdopen(fd2, "r");
3328 err = got_error_from_errno2("fdopen", path2);
3333 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3337 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3341 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3345 if (stat(path1, &sb1) == -1) {
3346 err = got_error_from_errno2("stat", path1);
3350 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3351 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3355 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3359 if (fseek(f1, 0L, SEEK_SET) == -1)
3360 return got_ferror(f1, GOT_ERR_IO);
3361 if (fseek(f2, 0L, SEEK_SET) == -1)
3362 return got_ferror(f2, GOT_ERR_IO);
3363 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3365 err = apply_or_reject_change(&choice, change, ++n,
3366 changes->nchanges, ds, args, diff_flags, relpath,
3367 f1, f2, &line_cur1, &line_cur2,
3368 reverse_patch ? NULL : outfile,
3369 reverse_patch ? outfile : NULL,
3370 patch_cb, patch_arg);
3373 if (choice == GOT_PATCH_CHOICE_YES)
3375 else if (choice == GOT_PATCH_CHOICE_QUIT)
3379 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3380 reverse_patch ? NULL : outfile,
3381 reverse_patch ? outfile : NULL);
3385 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3386 err = got_error_from_errno2("chmod", path2);
3393 got_object_blob_close(blob);
3394 if (f1 && fclose(f1) == EOF && err == NULL)
3395 err = got_error_from_errno2("fclose", path1);
3396 if (f2 && fclose(f2) == EOF && err == NULL)
3397 err = got_error_from_errno2("fclose", path2);
3398 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3399 err = got_error_from_errno2("close", path2);
3400 if (outfile && fclose(outfile) == EOF && err == NULL)
3401 err = got_error_from_errno2("fclose", *path_outfile);
3402 if (path1 && unlink(path1) == -1 && err == NULL)
3403 err = got_error_from_errno2("unlink", path1);
3404 if (err || !have_content) {
3405 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3406 err = got_error_from_errno2("unlink", *path_outfile);
3407 free(*path_outfile);
3408 *path_outfile = NULL;
3412 got_diff_state_free(ds);
3416 got_diff_free_changes(changes);
3421 static const struct got_error *
3422 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3423 const char *relpath, struct got_object_id *blob_id,
3424 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3425 int dirfd, const char *de_name)
3427 struct revert_file_args *a = arg;
3428 const struct got_error *err = NULL;
3429 char *parent_path = NULL;
3430 struct got_fileindex_entry *ie;
3431 struct got_tree_object *tree = NULL;
3432 struct got_object_id *tree_id = NULL;
3433 const struct got_tree_entry *te = NULL;
3434 char *tree_path = NULL, *te_name;
3435 char *ondisk_path = NULL, *path_content = NULL;
3436 struct got_blob_object *blob = NULL;
3438 /* Reverting a staged deletion is a no-op. */
3439 if (status == GOT_STATUS_DELETE &&
3440 staged_status != GOT_STATUS_NO_CHANGE)
3443 if (status == GOT_STATUS_UNVERSIONED)
3444 return (*a->progress_cb)(a->progress_arg,
3445 GOT_STATUS_UNVERSIONED, relpath);
3447 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3449 return got_error(GOT_ERR_BAD_PATH);
3451 /* Construct in-repository path of tree which contains this blob. */
3452 err = got_path_dirname(&parent_path, ie->path);
3454 if (err->code != GOT_ERR_BAD_PATH)
3456 parent_path = strdup("/");
3457 if (parent_path == NULL) {
3458 err = got_error_from_errno("strdup");
3462 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3463 tree_path = strdup(parent_path);
3464 if (tree_path == NULL) {
3465 err = got_error_from_errno("strdup");
3469 if (got_path_is_root_dir(parent_path)) {
3470 tree_path = strdup(a->worktree->path_prefix);
3471 if (tree_path == NULL) {
3472 err = got_error_from_errno("strdup");
3476 if (asprintf(&tree_path, "%s/%s",
3477 a->worktree->path_prefix, parent_path) == -1) {
3478 err = got_error_from_errno("asprintf");
3484 err = got_object_id_by_path(&tree_id, a->repo,
3485 a->worktree->base_commit_id, tree_path);
3487 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3488 (status == GOT_STATUS_ADD ||
3489 staged_status == GOT_STATUS_ADD)))
3492 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3496 te_name = basename(ie->path);
3497 if (te_name == NULL) {
3498 err = got_error_from_errno2("basename", ie->path);
3502 te = got_object_tree_find_entry(tree, te_name);
3503 if (te == NULL && status != GOT_STATUS_ADD &&
3504 staged_status != GOT_STATUS_ADD) {
3505 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3511 case GOT_STATUS_ADD:
3513 int choice = GOT_PATCH_CHOICE_NONE;
3514 err = (*a->patch_cb)(&choice, a->patch_arg,
3515 status, ie->path, NULL, 1, 1);
3518 if (choice != GOT_PATCH_CHOICE_YES)
3521 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3525 got_fileindex_entry_remove(a->fileindex, ie);
3527 case GOT_STATUS_DELETE:
3529 int choice = GOT_PATCH_CHOICE_NONE;
3530 err = (*a->patch_cb)(&choice, a->patch_arg,
3531 status, ie->path, NULL, 1, 1);
3534 if (choice != GOT_PATCH_CHOICE_YES)
3538 case GOT_STATUS_MODIFY:
3539 case GOT_STATUS_MODE_CHANGE:
3540 case GOT_STATUS_CONFLICT:
3541 case GOT_STATUS_MISSING: {
3542 struct got_object_id id;
3543 if (staged_status == GOT_STATUS_ADD ||
3544 staged_status == GOT_STATUS_MODIFY) {
3545 memcpy(id.sha1, ie->staged_blob_sha1,
3546 SHA1_DIGEST_LENGTH);
3548 memcpy(id.sha1, ie->blob_sha1,
3549 SHA1_DIGEST_LENGTH);
3550 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3554 if (asprintf(&ondisk_path, "%s/%s",
3555 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3556 err = got_error_from_errno("asprintf");
3560 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3561 status == GOT_STATUS_CONFLICT)) {
3562 err = create_patched_content(&path_content, 1, &id,
3563 ondisk_path, dirfd, de_name, ie->path, a->repo,
3564 a->patch_cb, a->patch_arg);
3565 if (err || path_content == NULL)
3567 if (rename(path_content, ondisk_path) == -1) {
3568 err = got_error_from_errno3("rename",
3569 path_content, ondisk_path);
3573 err = install_blob(a->worktree, ondisk_path, ie->path,
3574 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3575 got_fileindex_perms_to_st(ie), blob, 0, 1,
3576 a->repo, a->progress_cb, a->progress_arg);
3579 if (status == GOT_STATUS_DELETE ||
3580 status == GOT_STATUS_MODE_CHANGE) {
3581 err = update_blob_fileindex_entry(a->worktree,
3582 a->fileindex, ie, ondisk_path, ie->path,
3599 got_object_blob_close(blob);
3601 got_object_tree_close(tree);
3606 const struct got_error *
3607 got_worktree_revert(struct got_worktree *worktree,
3608 struct got_pathlist_head *paths,
3609 got_worktree_checkout_cb progress_cb, void *progress_arg,
3610 got_worktree_patch_cb patch_cb, void *patch_arg,
3611 struct got_repository *repo)
3613 struct got_fileindex *fileindex = NULL;
3614 char *fileindex_path = NULL;
3615 const struct got_error *err = NULL, *unlockerr = NULL;
3616 const struct got_error *sync_err = NULL;
3617 struct got_pathlist_entry *pe;
3618 struct revert_file_args rfa;
3620 err = lock_worktree(worktree, LOCK_EX);
3624 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3628 rfa.worktree = worktree;
3629 rfa.fileindex = fileindex;
3630 rfa.progress_cb = progress_cb;
3631 rfa.progress_arg = progress_arg;
3632 rfa.patch_cb = patch_cb;
3633 rfa.patch_arg = patch_arg;
3635 TAILQ_FOREACH(pe, paths, entry) {
3636 err = worktree_status(worktree, pe->path, fileindex, repo,
3637 revert_file, &rfa, NULL, NULL, 0, 0);
3641 sync_err = sync_fileindex(fileindex, fileindex_path);
3642 if (sync_err && err == NULL)
3645 free(fileindex_path);
3647 got_fileindex_free(fileindex);
3648 unlockerr = lock_worktree(worktree, LOCK_SH);
3649 if (unlockerr && err == NULL)
3655 free_commitable(struct got_commitable *ct)
3658 free(ct->in_repo_path);
3659 free(ct->ondisk_path);
3661 free(ct->base_blob_id);
3662 free(ct->staged_blob_id);
3663 free(ct->base_commit_id);
3667 struct collect_commitables_arg {
3668 struct got_pathlist_head *commitable_paths;
3669 struct got_repository *repo;
3670 struct got_worktree *worktree;
3671 int have_staged_files;
3674 static const struct got_error *
3675 collect_commitables(void *arg, unsigned char status,
3676 unsigned char staged_status, const char *relpath,
3677 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3678 struct got_object_id *commit_id, int dirfd, const char *de_name)
3680 struct collect_commitables_arg *a = arg;
3681 const struct got_error *err = NULL;
3682 struct got_commitable *ct = NULL;
3683 struct got_pathlist_entry *new = NULL;
3684 char *parent_path = NULL, *path = NULL;
3687 if (a->have_staged_files) {
3688 if (staged_status != GOT_STATUS_MODIFY &&
3689 staged_status != GOT_STATUS_ADD &&
3690 staged_status != GOT_STATUS_DELETE)
3693 if (status == GOT_STATUS_CONFLICT)
3694 return got_error(GOT_ERR_COMMIT_CONFLICT);
3696 if (status != GOT_STATUS_MODIFY &&
3697 status != GOT_STATUS_MODE_CHANGE &&
3698 status != GOT_STATUS_ADD &&
3699 status != GOT_STATUS_DELETE)
3703 if (asprintf(&path, "/%s", relpath) == -1) {
3704 err = got_error_from_errno("asprintf");
3707 if (strcmp(path, "/") == 0) {
3708 parent_path = strdup("");
3709 if (parent_path == NULL)
3710 return got_error_from_errno("strdup");
3712 err = got_path_dirname(&parent_path, path);
3717 ct = calloc(1, sizeof(*ct));
3719 err = got_error_from_errno("calloc");
3723 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3725 err = got_error_from_errno("asprintf");
3728 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3729 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3732 if (fstatat(dirfd, de_name, &sb,
3733 AT_SYMLINK_NOFOLLOW) == -1) {
3734 err = got_error_from_errno2("fstatat",
3738 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3739 err = got_error_from_errno2("lstat", ct->ondisk_path);
3742 ct->mode = sb.st_mode;
3745 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3746 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3748 err = got_error_from_errno("asprintf");
3752 ct->status = status;
3753 ct->staged_status = staged_status;
3754 ct->blob_id = NULL; /* will be filled in when blob gets created */
3755 if (ct->status != GOT_STATUS_ADD &&
3756 ct->staged_status != GOT_STATUS_ADD) {
3757 ct->base_blob_id = got_object_id_dup(blob_id);
3758 if (ct->base_blob_id == NULL) {
3759 err = got_error_from_errno("got_object_id_dup");
3762 ct->base_commit_id = got_object_id_dup(commit_id);
3763 if (ct->base_commit_id == NULL) {
3764 err = got_error_from_errno("got_object_id_dup");
3768 if (ct->staged_status == GOT_STATUS_ADD ||
3769 ct->staged_status == GOT_STATUS_MODIFY) {
3770 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3771 if (ct->staged_blob_id == NULL) {
3772 err = got_error_from_errno("got_object_id_dup");
3776 ct->path = strdup(path);
3777 if (ct->path == NULL) {
3778 err = got_error_from_errno("strdup");
3781 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3783 if (ct && (err || new == NULL))
3784 free_commitable(ct);
3790 static const struct got_error *write_tree(struct got_object_id **,
3791 struct got_tree_object *, const char *, struct got_pathlist_head *,
3792 got_worktree_status_cb status_cb, void *status_arg,
3793 struct got_repository *);
3795 static const struct got_error *
3796 write_subtree(struct got_object_id **new_subtree_id,
3797 struct got_tree_entry *te, const char *parent_path,
3798 struct got_pathlist_head *commitable_paths,
3799 got_worktree_status_cb status_cb, void *status_arg,
3800 struct got_repository *repo)
3802 const struct got_error *err = NULL;
3803 struct got_tree_object *subtree;
3806 if (asprintf(&subpath, "%s%s%s", parent_path,
3807 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3808 return got_error_from_errno("asprintf");
3810 err = got_object_open_as_tree(&subtree, repo, &te->id);
3814 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3815 status_cb, status_arg, repo);
3816 got_object_tree_close(subtree);
3821 static const struct got_error *
3822 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3824 const struct got_error *err = NULL;
3825 char *ct_parent_path = NULL;
3829 if (strchr(ct->in_repo_path, '/') == NULL) {
3830 *match = got_path_is_root_dir(path);
3834 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3837 *match = (strcmp(path, ct_parent_path) == 0);
3838 free(ct_parent_path);
3843 get_ct_file_mode(struct got_commitable *ct)
3845 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3848 static const struct got_error *
3849 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3850 struct got_tree_entry *te, struct got_commitable *ct)
3852 const struct got_error *err = NULL;
3856 err = got_object_tree_entry_dup(new_te, te);
3860 (*new_te)->mode = get_ct_file_mode(ct);
3862 if (ct->staged_status == GOT_STATUS_MODIFY)
3863 memcpy(&(*new_te)->id, ct->staged_blob_id,
3864 sizeof((*new_te)->id));
3866 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3868 if (err && *new_te) {
3875 static const struct got_error *
3876 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3877 struct got_commitable *ct)
3879 const struct got_error *err = NULL;
3884 *new_te = calloc(1, sizeof(**new_te));
3885 if (*new_te == NULL)
3886 return got_error_from_errno("calloc");
3888 ct_name = basename(ct->path);
3889 if (ct_name == NULL) {
3890 err = got_error_from_errno2("basename", ct->path);
3893 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3894 sizeof((*new_te)->name)) {
3895 err = got_error(GOT_ERR_NO_SPACE);
3899 (*new_te)->mode = get_ct_file_mode(ct);
3901 if (ct->staged_status == GOT_STATUS_ADD)
3902 memcpy(&(*new_te)->id, ct->staged_blob_id,
3903 sizeof((*new_te)->id));
3905 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3907 if (err && *new_te) {
3914 static const struct got_error *
3915 insert_tree_entry(struct got_tree_entry *new_te,
3916 struct got_pathlist_head *paths)
3918 const struct got_error *err = NULL;
3919 struct got_pathlist_entry *new_pe;
3921 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3925 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3929 static const struct got_error *
3930 report_ct_status(struct got_commitable *ct,
3931 got_worktree_status_cb status_cb, void *status_arg)
3933 const char *ct_path = ct->path;
3934 unsigned char status;
3936 while (ct_path[0] == '/')
3939 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3940 status = ct->staged_status;
3942 status = ct->status;
3944 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3945 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3948 static const struct got_error *
3949 match_modified_subtree(int *modified, struct got_tree_entry *te,
3950 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3952 const struct got_error *err = NULL;
3953 struct got_pathlist_entry *pe;
3958 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3959 got_path_is_root_dir(base_tree_path) ? "" : "/",
3961 return got_error_from_errno("asprintf");
3963 TAILQ_FOREACH(pe, commitable_paths, entry) {
3964 struct got_commitable *ct = pe->data;
3965 *modified = got_path_is_child(ct->in_repo_path, te_path,
3975 static const struct got_error *
3976 match_deleted_or_modified_ct(struct got_commitable **ctp,
3977 struct got_tree_entry *te, const char *base_tree_path,
3978 struct got_pathlist_head *commitable_paths)
3980 const struct got_error *err = NULL;
3981 struct got_pathlist_entry *pe;
3985 TAILQ_FOREACH(pe, commitable_paths, entry) {
3986 struct got_commitable *ct = pe->data;
3987 char *ct_name = NULL;
3990 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3991 if (ct->status != GOT_STATUS_MODIFY &&
3992 ct->status != GOT_STATUS_MODE_CHANGE &&
3993 ct->status != GOT_STATUS_DELETE)
3996 if (ct->staged_status != GOT_STATUS_MODIFY &&
3997 ct->staged_status != GOT_STATUS_DELETE)
4001 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4004 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4010 ct_name = basename(pe->path);
4011 if (ct_name == NULL)
4012 return got_error_from_errno2("basename", pe->path);
4014 if (strcmp(te->name, ct_name) != 0)
4024 static const struct got_error *
4025 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4026 const char *child_path, const char *path_base_tree,
4027 struct got_pathlist_head *commitable_paths,
4028 got_worktree_status_cb status_cb, void *status_arg,
4029 struct got_repository *repo)
4031 const struct got_error *err = NULL;
4032 struct got_tree_entry *new_te;
4034 struct got_object_id *id = NULL;
4038 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4039 got_path_is_root_dir(path_base_tree) ? "" : "/",
4041 return got_error_from_errno("asprintf");
4043 new_te = calloc(1, sizeof(*new_te));
4045 return got_error_from_errno("calloc");
4046 new_te->mode = S_IFDIR;
4048 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4049 sizeof(new_te->name)) {
4050 err = got_error(GOT_ERR_NO_SPACE);
4053 err = write_tree(&id, NULL, subtree_path,
4054 commitable_paths, status_cb, status_arg, repo);
4059 memcpy(&new_te->id, id, sizeof(new_te->id));
4068 static const struct got_error *
4069 write_tree(struct got_object_id **new_tree_id,
4070 struct got_tree_object *base_tree, const char *path_base_tree,
4071 struct got_pathlist_head *commitable_paths,
4072 got_worktree_status_cb status_cb, void *status_arg,
4073 struct got_repository *repo)
4075 const struct got_error *err = NULL;
4076 struct got_pathlist_head paths;
4077 struct got_tree_entry *te, *new_te = NULL;
4078 struct got_pathlist_entry *pe;
4083 /* Insert, and recurse into, newly added entries first. */
4084 TAILQ_FOREACH(pe, commitable_paths, entry) {
4085 struct got_commitable *ct = pe->data;
4086 char *child_path = NULL, *slash;
4088 if ((ct->status != GOT_STATUS_ADD &&
4089 ct->staged_status != GOT_STATUS_ADD) ||
4090 (ct->flags & GOT_COMMITABLE_ADDED))
4093 if (!got_path_is_child(pe->path, path_base_tree,
4094 strlen(path_base_tree)))
4097 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4102 slash = strchr(child_path, '/');
4103 if (slash == NULL) {
4104 err = alloc_added_blob_tree_entry(&new_te, ct);
4107 err = report_ct_status(ct, status_cb, status_arg);
4110 ct->flags |= GOT_COMMITABLE_ADDED;
4111 err = insert_tree_entry(new_te, &paths);
4116 *slash = '\0'; /* trim trailing path components */
4117 if (base_tree == NULL ||
4118 got_object_tree_find_entry(base_tree, child_path)
4120 err = make_subtree_for_added_blob(&new_te,
4121 child_path, path_base_tree,
4122 commitable_paths, status_cb, status_arg,
4126 err = insert_tree_entry(new_te, &paths);
4135 int i, nbase_entries;
4136 /* Handle modified and deleted entries. */
4137 nbase_entries = got_object_tree_get_nentries(base_tree);
4138 for (i = 0; i < nbase_entries; i++) {
4139 struct got_commitable *ct = NULL;
4141 te = got_object_tree_get_entry(base_tree, i);
4142 if (got_object_tree_entry_is_submodule(te)) {
4143 /* Entry is a submodule; just copy it. */
4144 err = got_object_tree_entry_dup(&new_te, te);
4147 err = insert_tree_entry(new_te, &paths);
4154 if (S_ISDIR(te->mode)) {
4156 err = got_object_tree_entry_dup(&new_te, te);
4159 err = match_modified_subtree(&modified, te,
4160 path_base_tree, commitable_paths);
4163 /* Avoid recursion into unmodified subtrees. */
4165 struct got_object_id *new_id;
4166 err = write_subtree(&new_id, te,
4167 path_base_tree, commitable_paths,
4168 status_cb, status_arg, repo);
4171 memcpy(&new_te->id, new_id,
4172 sizeof(new_te->id));
4175 err = insert_tree_entry(new_te, &paths);
4182 err = match_deleted_or_modified_ct(&ct, te,
4183 path_base_tree, commitable_paths);
4187 /* NB: Deleted entries get dropped here. */
4188 if (ct->status == GOT_STATUS_MODIFY ||
4189 ct->status == GOT_STATUS_MODE_CHANGE ||
4190 ct->staged_status == GOT_STATUS_MODIFY) {
4191 err = alloc_modified_blob_tree_entry(
4195 err = insert_tree_entry(new_te, &paths);
4200 err = report_ct_status(ct, status_cb,
4205 /* Entry is unchanged; just copy it. */
4206 err = got_object_tree_entry_dup(&new_te, te);
4209 err = insert_tree_entry(new_te, &paths);
4217 /* Write new list of entries; deleted entries have been dropped. */
4218 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4220 got_pathlist_free(&paths);
4224 static const struct got_error *
4225 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4226 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4227 int have_staged_files)
4229 const struct got_error *err = NULL;
4230 struct got_pathlist_entry *pe;
4232 TAILQ_FOREACH(pe, commitable_paths, entry) {
4233 struct got_fileindex_entry *ie;
4234 struct got_commitable *ct = pe->data;
4236 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4238 if (ct->status == GOT_STATUS_DELETE ||
4239 ct->staged_status == GOT_STATUS_DELETE) {
4240 got_fileindex_entry_remove(fileindex, ie);
4241 got_fileindex_entry_free(ie);
4242 } else if (ct->staged_status == GOT_STATUS_ADD ||
4243 ct->staged_status == GOT_STATUS_MODIFY) {
4244 got_fileindex_entry_stage_set(ie,
4245 GOT_FILEIDX_STAGE_NONE);
4246 err = got_fileindex_entry_update(ie,
4247 ct->ondisk_path, ct->staged_blob_id->sha1,
4248 new_base_commit_id->sha1,
4249 !have_staged_files);
4251 err = got_fileindex_entry_update(ie,
4252 ct->ondisk_path, ct->blob_id->sha1,
4253 new_base_commit_id->sha1,
4254 !have_staged_files);
4256 err = got_fileindex_entry_alloc(&ie,
4257 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4258 new_base_commit_id->sha1);
4261 err = got_fileindex_entry_add(fileindex, ie);
4270 static const struct got_error *
4271 check_out_of_date(const char *in_repo_path, unsigned char status,
4272 unsigned char staged_status, struct got_object_id *base_blob_id,
4273 struct got_object_id *base_commit_id,
4274 struct got_object_id *head_commit_id, struct got_repository *repo,
4277 const struct got_error *err = NULL;
4278 struct got_object_id *id = NULL;
4280 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4281 /* Trivial case: base commit == head commit */
4282 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4285 * Ensure file content which local changes were based
4286 * on matches file content in the branch head.
4288 err = got_object_id_by_path(&id, repo, head_commit_id,
4291 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4292 err = got_error(ood_errcode);
4294 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4295 err = got_error(ood_errcode);
4297 /* Require that added files don't exist in the branch head. */
4298 err = got_object_id_by_path(&id, repo, head_commit_id,
4300 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4302 err = id ? got_error(ood_errcode) : NULL;
4309 const struct got_error *
4310 commit_worktree(struct got_object_id **new_commit_id,
4311 struct got_pathlist_head *commitable_paths,
4312 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4313 const char *author, const char *committer,
4314 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4315 got_worktree_status_cb status_cb, void *status_arg,
4316 struct got_repository *repo)
4318 const struct got_error *err = NULL, *unlockerr = NULL;
4319 struct got_pathlist_entry *pe;
4320 const char *head_ref_name = NULL;
4321 struct got_commit_object *head_commit = NULL;
4322 struct got_reference *head_ref2 = NULL;
4323 struct got_object_id *head_commit_id2 = NULL;
4324 struct got_tree_object *head_tree = NULL;
4325 struct got_object_id *new_tree_id = NULL;
4326 struct got_object_id_queue parent_ids;
4327 struct got_object_qid *pid = NULL;
4328 char *logmsg = NULL;
4330 *new_commit_id = NULL;
4332 SIMPLEQ_INIT(&parent_ids);
4334 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4338 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4342 if (commit_msg_cb != NULL) {
4343 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4348 if (logmsg == NULL || strlen(logmsg) == 0) {
4349 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4353 /* Create blobs from added and modified files and record their IDs. */
4354 TAILQ_FOREACH(pe, commitable_paths, entry) {
4355 struct got_commitable *ct = pe->data;
4358 /* Blobs for staged files already exist. */
4359 if (ct->staged_status == GOT_STATUS_ADD ||
4360 ct->staged_status == GOT_STATUS_MODIFY)
4363 if (ct->status != GOT_STATUS_ADD &&
4364 ct->status != GOT_STATUS_MODIFY &&
4365 ct->status != GOT_STATUS_MODE_CHANGE)
4368 if (asprintf(&ondisk_path, "%s/%s",
4369 worktree->root_path, pe->path) == -1) {
4370 err = got_error_from_errno("asprintf");
4373 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4379 /* Recursively write new tree objects. */
4380 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4381 status_cb, status_arg, repo);
4385 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4388 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4389 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4390 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4391 got_object_qid_free(pid);
4397 /* Check if a concurrent commit to our branch has occurred. */
4398 head_ref_name = got_worktree_get_head_ref_name(worktree);
4399 if (head_ref_name == NULL) {
4400 err = got_error_from_errno("got_worktree_get_head_ref_name");
4403 /* Lock the reference here to prevent concurrent modification. */
4404 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4407 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4410 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4411 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4414 /* Update branch head in repository. */
4415 err = got_ref_change_ref(head_ref2, *new_commit_id);
4418 err = got_ref_write(head_ref2, repo);
4422 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4426 err = ref_base_commit(worktree, repo);
4431 got_object_tree_close(head_tree);
4433 got_object_commit_close(head_commit);
4434 free(head_commit_id2);
4436 unlockerr = got_ref_unlock(head_ref2);
4437 if (unlockerr && err == NULL)
4439 got_ref_close(head_ref2);
4444 static const struct got_error *
4445 check_path_is_commitable(const char *path,
4446 struct got_pathlist_head *commitable_paths)
4448 struct got_pathlist_entry *cpe = NULL;
4449 size_t path_len = strlen(path);
4451 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4452 struct got_commitable *ct = cpe->data;
4453 const char *ct_path = ct->path;
4455 while (ct_path[0] == '/')
4458 if (strcmp(path, ct_path) == 0 ||
4459 got_path_is_child(ct_path, path, path_len))
4464 return got_error_path(path, GOT_ERR_BAD_PATH);
4469 static const struct got_error *
4470 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4472 int *have_staged_files = arg;
4474 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4475 *have_staged_files = 1;
4476 return got_error(GOT_ERR_CANCELLED);
4482 static const struct got_error *
4483 check_non_staged_files(struct got_fileindex *fileindex,
4484 struct got_pathlist_head *paths)
4486 struct got_pathlist_entry *pe;
4487 struct got_fileindex_entry *ie;
4489 TAILQ_FOREACH(pe, paths, entry) {
4490 if (pe->path[0] == '\0')
4492 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4494 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4495 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4496 return got_error_path(pe->path,
4497 GOT_ERR_FILE_NOT_STAGED);
4503 const struct got_error *
4504 got_worktree_commit(struct got_object_id **new_commit_id,
4505 struct got_worktree *worktree, struct got_pathlist_head *paths,
4506 const char *author, const char *committer,
4507 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4508 got_worktree_status_cb status_cb, void *status_arg,
4509 struct got_repository *repo)
4511 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4512 struct got_fileindex *fileindex = NULL;
4513 char *fileindex_path = NULL;
4514 struct got_pathlist_head commitable_paths;
4515 struct collect_commitables_arg cc_arg;
4516 struct got_pathlist_entry *pe;
4517 struct got_reference *head_ref = NULL;
4518 struct got_object_id *head_commit_id = NULL;
4519 int have_staged_files = 0;
4521 *new_commit_id = NULL;
4523 TAILQ_INIT(&commitable_paths);
4525 err = lock_worktree(worktree, LOCK_EX);
4529 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4533 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4537 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4541 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4542 &have_staged_files);
4543 if (err && err->code != GOT_ERR_CANCELLED)
4545 if (have_staged_files) {
4546 err = check_non_staged_files(fileindex, paths);
4551 cc_arg.commitable_paths = &commitable_paths;
4552 cc_arg.worktree = worktree;
4554 cc_arg.have_staged_files = have_staged_files;
4555 TAILQ_FOREACH(pe, paths, entry) {
4556 err = worktree_status(worktree, pe->path, fileindex, repo,
4557 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4562 if (TAILQ_EMPTY(&commitable_paths)) {
4563 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4567 TAILQ_FOREACH(pe, paths, entry) {
4568 err = check_path_is_commitable(pe->path, &commitable_paths);
4573 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4574 struct got_commitable *ct = pe->data;
4575 const char *ct_path = ct->in_repo_path;
4577 while (ct_path[0] == '/')
4579 err = check_out_of_date(ct_path, ct->status,
4580 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4581 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4587 err = commit_worktree(new_commit_id, &commitable_paths,
4588 head_commit_id, worktree, author, committer,
4589 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4593 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4594 fileindex, have_staged_files);
4595 sync_err = sync_fileindex(fileindex, fileindex_path);
4596 if (sync_err && err == NULL)
4600 got_fileindex_free(fileindex);
4601 free(fileindex_path);
4602 unlockerr = lock_worktree(worktree, LOCK_SH);
4603 if (unlockerr && err == NULL)
4605 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4606 struct got_commitable *ct = pe->data;
4607 free_commitable(ct);
4609 got_pathlist_free(&commitable_paths);
4614 got_commitable_get_path(struct got_commitable *ct)
4620 got_commitable_get_status(struct got_commitable *ct)
4625 struct check_rebase_ok_arg {
4626 struct got_worktree *worktree;
4627 struct got_repository *repo;
4630 static const struct got_error *
4631 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4633 const struct got_error *err = NULL;
4634 struct check_rebase_ok_arg *a = arg;
4635 unsigned char status;
4639 /* Reject rebase of a work tree with mixed base commits. */
4640 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4641 SHA1_DIGEST_LENGTH))
4642 return got_error(GOT_ERR_MIXED_COMMITS);
4644 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4646 return got_error_from_errno("asprintf");
4648 /* Reject rebase of a work tree with modified or staged files. */
4649 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4654 if (status != GOT_STATUS_NO_CHANGE)
4655 return got_error(GOT_ERR_MODIFIED);
4656 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4657 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4662 const struct got_error *
4663 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4664 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4665 struct got_worktree *worktree, struct got_reference *branch,
4666 struct got_repository *repo)
4668 const struct got_error *err = NULL;
4669 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4670 char *branch_ref_name = NULL;
4671 char *fileindex_path = NULL;
4672 struct check_rebase_ok_arg ok_arg;
4673 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4675 *new_base_branch_ref = NULL;
4679 err = lock_worktree(worktree, LOCK_EX);
4683 err = open_fileindex(fileindex, &fileindex_path, worktree);
4687 ok_arg.worktree = worktree;
4689 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4694 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4698 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4702 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4706 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4711 err = got_ref_alloc_symref(new_base_branch_ref,
4712 new_base_branch_ref_name, wt_branch);
4715 err = got_ref_write(*new_base_branch_ref, repo);
4719 /* TODO Lock original branch's ref while rebasing? */
4721 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4725 err = got_ref_write(branch_ref, repo);
4729 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4730 worktree->base_commit_id);
4733 err = got_ref_write(*tmp_branch, repo);
4737 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4741 free(fileindex_path);
4742 free(tmp_branch_name);
4743 free(new_base_branch_ref_name);
4744 free(branch_ref_name);
4746 got_ref_close(branch_ref);
4748 got_ref_close(wt_branch);
4750 if (*new_base_branch_ref) {
4751 got_ref_close(*new_base_branch_ref);
4752 *new_base_branch_ref = NULL;
4755 got_ref_close(*tmp_branch);
4759 got_fileindex_free(*fileindex);
4762 lock_worktree(worktree, LOCK_SH);
4767 const struct got_error *
4768 got_worktree_rebase_continue(struct got_object_id **commit_id,
4769 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4770 struct got_reference **branch, struct got_fileindex **fileindex,
4771 struct got_worktree *worktree, struct got_repository *repo)
4773 const struct got_error *err;
4774 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4775 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4776 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4777 char *fileindex_path = NULL;
4778 int have_staged_files = 0;
4781 *new_base_branch = NULL;
4786 err = lock_worktree(worktree, LOCK_EX);
4790 err = open_fileindex(fileindex, &fileindex_path, worktree);
4794 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4795 &have_staged_files);
4796 if (err && err->code != GOT_ERR_CANCELLED)
4798 if (have_staged_files) {
4799 err = got_error(GOT_ERR_STAGED_PATHS);
4803 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4807 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4811 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4815 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4819 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4823 err = got_ref_open(branch, repo,
4824 got_ref_get_symref_target(branch_ref), 0);
4828 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4832 err = got_ref_resolve(commit_id, repo, commit_ref);
4836 err = got_ref_open(new_base_branch, repo,
4837 new_base_branch_ref_name, 0);
4841 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4845 free(commit_ref_name);
4846 free(branch_ref_name);
4847 free(fileindex_path);
4849 got_ref_close(commit_ref);
4851 got_ref_close(branch_ref);
4856 got_ref_close(*tmp_branch);
4859 if (*new_base_branch) {
4860 got_ref_close(*new_base_branch);
4861 *new_base_branch = NULL;
4864 got_ref_close(*branch);
4868 got_fileindex_free(*fileindex);
4871 lock_worktree(worktree, LOCK_SH);
4876 const struct got_error *
4877 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4879 const struct got_error *err;
4880 char *tmp_branch_name = NULL;
4882 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4886 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4887 free(tmp_branch_name);
4891 static const struct got_error *
4892 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4893 char **logmsg, void *arg)
4899 static const struct got_error *
4900 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4901 const char *path, struct got_object_id *blob_id,
4902 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4903 int dirfd, const char *de_name)
4908 struct collect_merged_paths_arg {
4909 got_worktree_checkout_cb progress_cb;
4911 struct got_pathlist_head *merged_paths;
4914 static const struct got_error *
4915 collect_merged_paths(void *arg, unsigned char status, const char *path)
4917 const struct got_error *err;
4918 struct collect_merged_paths_arg *a = arg;
4920 struct got_pathlist_entry *new;
4922 err = (*a->progress_cb)(a->progress_arg, status, path);
4926 if (status != GOT_STATUS_MERGE &&
4927 status != GOT_STATUS_ADD &&
4928 status != GOT_STATUS_DELETE &&
4929 status != GOT_STATUS_CONFLICT)
4934 return got_error_from_errno("strdup");
4936 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4937 if (err || new == NULL)
4943 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4945 struct got_pathlist_entry *pe;
4947 TAILQ_FOREACH(pe, merged_paths, entry)
4948 free((char *)pe->path);
4950 got_pathlist_free(merged_paths);
4953 static const struct got_error *
4954 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4955 struct got_repository *repo)
4957 const struct got_error *err;
4958 struct got_reference *commit_ref = NULL;
4960 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4962 if (err->code != GOT_ERR_NOT_REF)
4964 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4967 err = got_ref_write(commit_ref, repo);
4971 struct got_object_id *stored_id;
4974 err = got_ref_resolve(&stored_id, repo, commit_ref);
4977 cmp = got_object_id_cmp(commit_id, stored_id);
4980 err = got_error(GOT_ERR_REBASE_COMMITID);
4986 got_ref_close(commit_ref);
4990 static const struct got_error *
4991 rebase_merge_files(struct got_pathlist_head *merged_paths,
4992 const char *commit_ref_name, struct got_worktree *worktree,
4993 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4994 struct got_object_id *commit_id, struct got_repository *repo,
4995 got_worktree_checkout_cb progress_cb, void *progress_arg,
4996 got_cancel_cb cancel_cb, void *cancel_arg)
4998 const struct got_error *err;
4999 struct got_reference *commit_ref = NULL;
5000 struct collect_merged_paths_arg cmp_arg;
5001 char *fileindex_path;
5003 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5005 err = get_fileindex_path(&fileindex_path, worktree);
5009 cmp_arg.progress_cb = progress_cb;
5010 cmp_arg.progress_arg = progress_arg;
5011 cmp_arg.merged_paths = merged_paths;
5012 err = merge_files(worktree, fileindex, fileindex_path,
5013 parent_commit_id, commit_id, repo, collect_merged_paths,
5014 &cmp_arg, cancel_cb, cancel_arg);
5016 got_ref_close(commit_ref);
5020 const struct got_error *
5021 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5022 struct got_worktree *worktree, struct got_fileindex *fileindex,
5023 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5024 struct got_repository *repo,
5025 got_worktree_checkout_cb progress_cb, void *progress_arg,
5026 got_cancel_cb cancel_cb, void *cancel_arg)
5028 const struct got_error *err;
5029 char *commit_ref_name;
5031 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5035 err = store_commit_id(commit_ref_name, commit_id, repo);
5039 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5040 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5041 progress_arg, cancel_cb, cancel_arg);
5043 free(commit_ref_name);
5047 const struct got_error *
5048 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5049 struct got_worktree *worktree, struct got_fileindex *fileindex,
5050 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5051 struct got_repository *repo,
5052 got_worktree_checkout_cb progress_cb, void *progress_arg,
5053 got_cancel_cb cancel_cb, void *cancel_arg)
5055 const struct got_error *err;
5056 char *commit_ref_name;
5058 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5062 err = store_commit_id(commit_ref_name, commit_id, repo);
5066 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5067 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5068 progress_arg, cancel_cb, cancel_arg);
5070 free(commit_ref_name);
5074 static const struct got_error *
5075 rebase_commit(struct got_object_id **new_commit_id,
5076 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5077 struct got_worktree *worktree, struct got_fileindex *fileindex,
5078 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5079 const char *new_logmsg, struct got_repository *repo)
5081 const struct got_error *err, *sync_err;
5082 struct got_pathlist_head commitable_paths;
5083 struct collect_commitables_arg cc_arg;
5084 char *fileindex_path = NULL;
5085 struct got_reference *head_ref = NULL;
5086 struct got_object_id *head_commit_id = NULL;
5087 char *logmsg = NULL;
5089 TAILQ_INIT(&commitable_paths);
5090 *new_commit_id = NULL;
5092 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5094 err = get_fileindex_path(&fileindex_path, worktree);
5098 cc_arg.commitable_paths = &commitable_paths;
5099 cc_arg.worktree = worktree;
5101 cc_arg.have_staged_files = 0;
5103 * If possible get the status of individual files directly to
5104 * avoid crawling the entire work tree once per rebased commit.
5105 * TODO: Ideally, merged_paths would contain a list of commitables
5106 * we could use so we could skip worktree_status() entirely.
5109 struct got_pathlist_entry *pe;
5110 if (TAILQ_EMPTY(merged_paths)) {
5111 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5114 TAILQ_FOREACH(pe, merged_paths, entry) {
5115 err = worktree_status(worktree, pe->path, fileindex,
5116 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5122 err = worktree_status(worktree, "", fileindex, repo,
5123 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5128 if (TAILQ_EMPTY(&commitable_paths)) {
5129 /* No-op change; commit will be elided. */
5130 err = got_ref_delete(commit_ref, repo);
5133 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5137 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5141 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5146 logmsg = strdup(new_logmsg);
5147 if (logmsg == NULL) {
5148 err = got_error_from_errno("strdup");
5152 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5157 /* NB: commit_worktree will call free(logmsg) */
5158 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5159 worktree, got_object_commit_get_author(orig_commit),
5160 got_object_commit_get_committer(orig_commit),
5161 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5165 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5169 err = got_ref_delete(commit_ref, repo);
5173 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5175 sync_err = sync_fileindex(fileindex, fileindex_path);
5176 if (sync_err && err == NULL)
5179 free(fileindex_path);
5180 free(head_commit_id);
5182 got_ref_close(head_ref);
5184 free(*new_commit_id);
5185 *new_commit_id = NULL;
5190 const struct got_error *
5191 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5192 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5193 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5194 struct got_commit_object *orig_commit,
5195 struct got_object_id *orig_commit_id, struct got_repository *repo)
5197 const struct got_error *err;
5198 char *commit_ref_name;
5199 struct got_reference *commit_ref = NULL;
5200 struct got_object_id *commit_id = NULL;
5202 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5206 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5209 err = got_ref_resolve(&commit_id, repo, commit_ref);
5212 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5213 err = got_error(GOT_ERR_REBASE_COMMITID);
5217 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5218 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5221 got_ref_close(commit_ref);
5222 free(commit_ref_name);
5227 const struct got_error *
5228 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5229 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5230 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5231 struct got_commit_object *orig_commit,
5232 struct got_object_id *orig_commit_id, const char *new_logmsg,
5233 struct got_repository *repo)
5235 const struct got_error *err;
5236 char *commit_ref_name;
5237 struct got_reference *commit_ref = NULL;
5238 struct got_object_id *commit_id = NULL;
5240 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5244 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5247 err = got_ref_resolve(&commit_id, repo, commit_ref);
5250 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5251 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5255 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5256 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5259 got_ref_close(commit_ref);
5260 free(commit_ref_name);
5265 const struct got_error *
5266 got_worktree_rebase_postpone(struct got_worktree *worktree,
5267 struct got_fileindex *fileindex)
5270 got_fileindex_free(fileindex);
5271 return lock_worktree(worktree, LOCK_SH);
5274 static const struct got_error *
5275 delete_ref(const char *name, struct got_repository *repo)
5277 const struct got_error *err;
5278 struct got_reference *ref;
5280 err = got_ref_open(&ref, repo, name, 0);
5282 if (err->code == GOT_ERR_NOT_REF)
5287 err = got_ref_delete(ref, repo);
5292 static const struct got_error *
5293 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5295 const struct got_error *err;
5296 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5297 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5299 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5302 err = delete_ref(tmp_branch_name, repo);
5306 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5309 err = delete_ref(new_base_branch_ref_name, repo);
5313 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5316 err = delete_ref(branch_ref_name, repo);
5320 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5323 err = delete_ref(commit_ref_name, repo);
5328 free(tmp_branch_name);
5329 free(new_base_branch_ref_name);
5330 free(branch_ref_name);
5331 free(commit_ref_name);
5335 const struct got_error *
5336 got_worktree_rebase_complete(struct got_worktree *worktree,
5337 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5338 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5339 struct got_repository *repo)
5341 const struct got_error *err, *unlockerr;
5342 struct got_object_id *new_head_commit_id = NULL;
5344 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5348 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5352 err = got_ref_write(rebased_branch, repo);
5356 err = got_worktree_set_head_ref(worktree, rebased_branch);
5360 err = delete_rebase_refs(worktree, repo);
5363 got_fileindex_free(fileindex);
5364 free(new_head_commit_id);
5365 unlockerr = lock_worktree(worktree, LOCK_SH);
5366 if (unlockerr && err == NULL)
5371 const struct got_error *
5372 got_worktree_rebase_abort(struct got_worktree *worktree,
5373 struct got_fileindex *fileindex, struct got_repository *repo,
5374 struct got_reference *new_base_branch,
5375 got_worktree_checkout_cb progress_cb, void *progress_arg)
5377 const struct got_error *err, *unlockerr, *sync_err;
5378 struct got_reference *resolved = NULL;
5379 struct got_object_id *commit_id = NULL;
5380 char *fileindex_path = NULL;
5381 struct revert_file_args rfa;
5382 struct got_object_id *tree_id = NULL;
5384 err = lock_worktree(worktree, LOCK_EX);
5388 err = got_ref_open(&resolved, repo,
5389 got_ref_get_symref_target(new_base_branch), 0);
5393 err = got_worktree_set_head_ref(worktree, resolved);
5398 * XXX commits to the base branch could have happened while
5399 * we were busy rebasing; should we store the original commit ID
5400 * when rebase begins and read it back here?
5402 err = got_ref_resolve(&commit_id, repo, resolved);
5406 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5410 err = got_object_id_by_path(&tree_id, repo,
5411 worktree->base_commit_id, worktree->path_prefix);
5415 err = delete_rebase_refs(worktree, repo);
5419 err = get_fileindex_path(&fileindex_path, worktree);
5423 rfa.worktree = worktree;
5424 rfa.fileindex = fileindex;
5425 rfa.progress_cb = progress_cb;
5426 rfa.progress_arg = progress_arg;
5427 rfa.patch_cb = NULL;
5428 rfa.patch_arg = NULL;
5430 err = worktree_status(worktree, "", fileindex, repo,
5431 revert_file, &rfa, NULL, NULL, 0, 0);
5435 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5436 repo, progress_cb, progress_arg, NULL, NULL);
5438 sync_err = sync_fileindex(fileindex, fileindex_path);
5439 if (sync_err && err == NULL)
5442 got_ref_close(resolved);
5446 got_fileindex_free(fileindex);
5447 free(fileindex_path);
5449 unlockerr = lock_worktree(worktree, LOCK_SH);
5450 if (unlockerr && err == NULL)
5455 const struct got_error *
5456 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5457 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5458 struct got_fileindex **fileindex, struct got_worktree *worktree,
5459 struct got_repository *repo)
5461 const struct got_error *err = NULL;
5462 char *tmp_branch_name = NULL;
5463 char *branch_ref_name = NULL;
5464 char *base_commit_ref_name = NULL;
5465 char *fileindex_path = NULL;
5466 struct check_rebase_ok_arg ok_arg;
5467 struct got_reference *wt_branch = NULL;
5468 struct got_reference *base_commit_ref = NULL;
5472 *base_commit_id = NULL;
5475 err = lock_worktree(worktree, LOCK_EX);
5479 err = open_fileindex(fileindex, &fileindex_path, worktree);
5483 ok_arg.worktree = worktree;
5485 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5490 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5494 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5498 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5503 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5508 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5512 err = got_ref_write(*branch_ref, repo);
5516 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5517 worktree->base_commit_id);
5520 err = got_ref_write(base_commit_ref, repo);
5523 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5524 if (*base_commit_id == NULL) {
5525 err = got_error_from_errno("got_object_id_dup");
5529 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5530 worktree->base_commit_id);
5533 err = got_ref_write(*tmp_branch, repo);
5537 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5541 free(fileindex_path);
5542 free(tmp_branch_name);
5543 free(branch_ref_name);
5544 free(base_commit_ref_name);
5546 got_ref_close(wt_branch);
5549 got_ref_close(*branch_ref);
5553 got_ref_close(*tmp_branch);
5556 free(*base_commit_id);
5558 got_fileindex_free(*fileindex);
5561 lock_worktree(worktree, LOCK_SH);
5566 const struct got_error *
5567 got_worktree_histedit_postpone(struct got_worktree *worktree,
5568 struct got_fileindex *fileindex)
5571 got_fileindex_free(fileindex);
5572 return lock_worktree(worktree, LOCK_SH);
5575 const struct got_error *
5576 got_worktree_histedit_in_progress(int *in_progress,
5577 struct got_worktree *worktree)
5579 const struct got_error *err;
5580 char *tmp_branch_name = NULL;
5582 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5586 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5587 free(tmp_branch_name);
5591 const struct got_error *
5592 got_worktree_histedit_continue(struct got_object_id **commit_id,
5593 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5594 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5595 struct got_worktree *worktree, struct got_repository *repo)
5597 const struct got_error *err;
5598 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5599 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5600 struct got_reference *commit_ref = NULL;
5601 struct got_reference *base_commit_ref = NULL;
5602 char *fileindex_path = NULL;
5603 int have_staged_files = 0;
5607 *base_commit_id = NULL;
5610 err = lock_worktree(worktree, LOCK_EX);
5614 err = open_fileindex(fileindex, &fileindex_path, worktree);
5618 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5619 &have_staged_files);
5620 if (err && err->code != GOT_ERR_CANCELLED)
5622 if (have_staged_files) {
5623 err = got_error(GOT_ERR_STAGED_PATHS);
5627 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5631 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5635 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5639 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5644 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5648 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5651 err = got_ref_resolve(commit_id, repo, commit_ref);
5655 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5658 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5662 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5666 free(commit_ref_name);
5667 free(branch_ref_name);
5668 free(fileindex_path);
5670 got_ref_close(commit_ref);
5671 if (base_commit_ref)
5672 got_ref_close(base_commit_ref);
5676 free(*base_commit_id);
5677 *base_commit_id = NULL;
5679 got_ref_close(*tmp_branch);
5683 got_fileindex_free(*fileindex);
5686 lock_worktree(worktree, LOCK_EX);
5691 static const struct got_error *
5692 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5694 const struct got_error *err;
5695 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5696 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5698 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5701 err = delete_ref(tmp_branch_name, repo);
5705 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5709 err = delete_ref(base_commit_ref_name, repo);
5713 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5716 err = delete_ref(branch_ref_name, repo);
5720 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5723 err = delete_ref(commit_ref_name, repo);
5727 free(tmp_branch_name);
5728 free(base_commit_ref_name);
5729 free(branch_ref_name);
5730 free(commit_ref_name);
5734 const struct got_error *
5735 got_worktree_histedit_abort(struct got_worktree *worktree,
5736 struct got_fileindex *fileindex, struct got_repository *repo,
5737 struct got_reference *branch, struct got_object_id *base_commit_id,
5738 got_worktree_checkout_cb progress_cb, void *progress_arg)
5740 const struct got_error *err, *unlockerr, *sync_err;
5741 struct got_reference *resolved = NULL;
5742 char *fileindex_path = NULL;
5743 struct got_object_id *tree_id = NULL;
5744 struct revert_file_args rfa;
5746 err = lock_worktree(worktree, LOCK_EX);
5750 err = got_ref_open(&resolved, repo,
5751 got_ref_get_symref_target(branch), 0);
5755 err = got_worktree_set_head_ref(worktree, resolved);
5759 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5763 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5764 worktree->path_prefix);
5768 err = delete_histedit_refs(worktree, repo);
5772 err = get_fileindex_path(&fileindex_path, worktree);
5776 rfa.worktree = worktree;
5777 rfa.fileindex = fileindex;
5778 rfa.progress_cb = progress_cb;
5779 rfa.progress_arg = progress_arg;
5780 rfa.patch_cb = NULL;
5781 rfa.patch_arg = NULL;
5783 err = worktree_status(worktree, "", fileindex, repo,
5784 revert_file, &rfa, NULL, NULL, 0, 0);
5788 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5789 repo, progress_cb, progress_arg, NULL, NULL);
5791 sync_err = sync_fileindex(fileindex, fileindex_path);
5792 if (sync_err && err == NULL)
5795 got_ref_close(resolved);
5797 free(fileindex_path);
5799 unlockerr = lock_worktree(worktree, LOCK_SH);
5800 if (unlockerr && err == NULL)
5805 const struct got_error *
5806 got_worktree_histedit_complete(struct got_worktree *worktree,
5807 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5808 struct got_reference *edited_branch, struct got_repository *repo)
5810 const struct got_error *err, *unlockerr;
5811 struct got_object_id *new_head_commit_id = NULL;
5812 struct got_reference *resolved = NULL;
5814 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5818 err = got_ref_open(&resolved, repo,
5819 got_ref_get_symref_target(edited_branch), 0);
5823 err = got_ref_change_ref(resolved, new_head_commit_id);
5827 err = got_ref_write(resolved, repo);
5831 err = got_worktree_set_head_ref(worktree, resolved);
5835 err = delete_histedit_refs(worktree, repo);
5838 got_fileindex_free(fileindex);
5839 free(new_head_commit_id);
5840 unlockerr = lock_worktree(worktree, LOCK_SH);
5841 if (unlockerr && err == NULL)
5846 const struct got_error *
5847 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5848 struct got_object_id *commit_id, struct got_repository *repo)
5850 const struct got_error *err;
5851 char *commit_ref_name;
5853 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5857 err = store_commit_id(commit_ref_name, commit_id, repo);
5861 err = delete_ref(commit_ref_name, repo);
5863 free(commit_ref_name);
5867 const struct got_error *
5868 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5869 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5870 struct got_worktree *worktree, const char *refname,
5871 struct got_repository *repo)
5873 const struct got_error *err = NULL;
5874 char *fileindex_path = NULL;
5875 struct check_rebase_ok_arg ok_arg;
5879 *base_branch_ref = NULL;
5881 err = lock_worktree(worktree, LOCK_EX);
5885 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5886 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5887 "cannot integrate a branch into itself; "
5888 "update -b or different branch name required");
5892 err = open_fileindex(fileindex, &fileindex_path, worktree);
5896 /* Preconditions are the same as for rebase. */
5897 ok_arg.worktree = worktree;
5899 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5904 err = got_ref_open(branch_ref, repo, refname, 1);
5908 err = got_ref_open(base_branch_ref, repo,
5909 got_worktree_get_head_ref_name(worktree), 1);
5913 got_ref_close(*branch_ref);
5916 if (*base_branch_ref) {
5917 got_ref_close(*base_branch_ref);
5918 *base_branch_ref = NULL;
5921 got_fileindex_free(*fileindex);
5924 lock_worktree(worktree, LOCK_SH);
5929 const struct got_error *
5930 got_worktree_integrate_continue(struct got_worktree *worktree,
5931 struct got_fileindex *fileindex, struct got_repository *repo,
5932 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5933 got_worktree_checkout_cb progress_cb, void *progress_arg,
5934 got_cancel_cb cancel_cb, void *cancel_arg)
5936 const struct got_error *err = NULL, *sync_err, *unlockerr;
5937 char *fileindex_path = NULL;
5938 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5940 err = get_fileindex_path(&fileindex_path, worktree);
5944 err = got_ref_resolve(&commit_id, repo, branch_ref);
5948 err = got_object_id_by_path(&tree_id, repo, commit_id,
5949 worktree->path_prefix);
5953 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5957 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5958 progress_cb, progress_arg, cancel_cb, cancel_arg);
5962 err = got_ref_change_ref(base_branch_ref, commit_id);
5966 err = got_ref_write(base_branch_ref, repo);
5968 sync_err = sync_fileindex(fileindex, fileindex_path);
5969 if (sync_err && err == NULL)
5973 unlockerr = got_ref_unlock(branch_ref);
5974 if (unlockerr && err == NULL)
5976 got_ref_close(branch_ref);
5978 unlockerr = got_ref_unlock(base_branch_ref);
5979 if (unlockerr && err == NULL)
5981 got_ref_close(base_branch_ref);
5983 got_fileindex_free(fileindex);
5984 free(fileindex_path);
5987 unlockerr = lock_worktree(worktree, LOCK_SH);
5988 if (unlockerr && err == NULL)
5993 const struct got_error *
5994 got_worktree_integrate_abort(struct got_worktree *worktree,
5995 struct got_fileindex *fileindex, struct got_repository *repo,
5996 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5998 const struct got_error *err = NULL, *unlockerr = NULL;
6000 got_fileindex_free(fileindex);
6002 err = lock_worktree(worktree, LOCK_SH);
6004 unlockerr = got_ref_unlock(branch_ref);
6005 if (unlockerr && err == NULL)
6007 got_ref_close(branch_ref);
6009 unlockerr = got_ref_unlock(base_branch_ref);
6010 if (unlockerr && err == NULL)
6012 got_ref_close(base_branch_ref);
6017 struct check_stage_ok_arg {
6018 struct got_object_id *head_commit_id;
6019 struct got_worktree *worktree;
6020 struct got_fileindex *fileindex;
6021 struct got_repository *repo;
6025 const struct got_error *
6026 check_stage_ok(void *arg, unsigned char status,
6027 unsigned char staged_status, const char *relpath,
6028 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6029 struct got_object_id *commit_id, int dirfd, const char *de_name)
6031 struct check_stage_ok_arg *a = arg;
6032 const struct got_error *err = NULL;
6033 struct got_fileindex_entry *ie;
6034 struct got_object_id base_commit_id;
6035 struct got_object_id *base_commit_idp = NULL;
6036 char *in_repo_path = NULL, *p;
6038 if (status == GOT_STATUS_UNVERSIONED ||
6039 status == GOT_STATUS_NO_CHANGE)
6041 if (status == GOT_STATUS_NONEXISTENT)
6042 return got_error_set_errno(ENOENT, relpath);
6044 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6046 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6048 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6049 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6051 return got_error_from_errno("asprintf");
6053 if (got_fileindex_entry_has_commit(ie)) {
6054 memcpy(base_commit_id.sha1, ie->commit_sha1,
6055 SHA1_DIGEST_LENGTH);
6056 base_commit_idp = &base_commit_id;
6059 if (status == GOT_STATUS_CONFLICT) {
6060 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6062 } else if (status != GOT_STATUS_ADD &&
6063 status != GOT_STATUS_MODIFY &&
6064 status != GOT_STATUS_DELETE) {
6065 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6069 a->have_changes = 1;
6074 err = check_out_of_date(p, status, staged_status,
6075 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6076 GOT_ERR_STAGE_OUT_OF_DATE);
6082 struct stage_path_arg {
6083 struct got_worktree *worktree;
6084 struct got_fileindex *fileindex;
6085 struct got_repository *repo;
6086 got_worktree_status_cb status_cb;
6088 got_worktree_patch_cb patch_cb;
6090 int staged_something;
6093 static const struct got_error *
6094 stage_path(void *arg, unsigned char status,
6095 unsigned char staged_status, const char *relpath,
6096 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6097 struct got_object_id *commit_id, int dirfd, const char *de_name)
6099 struct stage_path_arg *a = arg;
6100 const struct got_error *err = NULL;
6101 struct got_fileindex_entry *ie;
6102 char *ondisk_path = NULL, *path_content = NULL;
6104 struct got_object_id *new_staged_blob_id = NULL;
6106 if (status == GOT_STATUS_UNVERSIONED)
6109 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6111 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6113 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6115 return got_error_from_errno("asprintf");
6118 case GOT_STATUS_ADD:
6119 case GOT_STATUS_MODIFY:
6121 if (status == GOT_STATUS_ADD) {
6122 int choice = GOT_PATCH_CHOICE_NONE;
6123 err = (*a->patch_cb)(&choice, a->patch_arg,
6124 status, ie->path, NULL, 1, 1);
6127 if (choice != GOT_PATCH_CHOICE_YES)
6130 err = create_patched_content(&path_content, 0,
6131 staged_blob_id ? staged_blob_id : blob_id,
6132 ondisk_path, dirfd, de_name, ie->path,
6133 a->repo, a->patch_cb, a->patch_arg);
6134 if (err || path_content == NULL)
6138 err = got_object_blob_create(&new_staged_blob_id,
6139 path_content ? path_content : ondisk_path, a->repo);
6142 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6143 SHA1_DIGEST_LENGTH);
6144 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6145 stage = GOT_FILEIDX_STAGE_ADD;
6147 stage = GOT_FILEIDX_STAGE_MODIFY;
6148 got_fileindex_entry_stage_set(ie, stage);
6149 a->staged_something = 1;
6150 if (a->status_cb == NULL)
6152 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6153 get_staged_status(ie), relpath, blob_id,
6154 new_staged_blob_id, NULL, dirfd, de_name);
6156 case GOT_STATUS_DELETE:
6157 if (staged_status == GOT_STATUS_DELETE)
6160 int choice = GOT_PATCH_CHOICE_NONE;
6161 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6162 ie->path, NULL, 1, 1);
6165 if (choice == GOT_PATCH_CHOICE_NO)
6167 if (choice != GOT_PATCH_CHOICE_YES) {
6168 err = got_error(GOT_ERR_PATCH_CHOICE);
6172 stage = GOT_FILEIDX_STAGE_DELETE;
6173 got_fileindex_entry_stage_set(ie, stage);
6174 a->staged_something = 1;
6175 if (a->status_cb == NULL)
6177 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6178 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6181 case GOT_STATUS_NO_CHANGE:
6183 case GOT_STATUS_CONFLICT:
6184 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6186 case GOT_STATUS_NONEXISTENT:
6187 err = got_error_set_errno(ENOENT, relpath);
6190 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6194 if (path_content && unlink(path_content) == -1 && err == NULL)
6195 err = got_error_from_errno2("unlink", path_content);
6198 free(new_staged_blob_id);
6202 const struct got_error *
6203 got_worktree_stage(struct got_worktree *worktree,
6204 struct got_pathlist_head *paths,
6205 got_worktree_status_cb status_cb, void *status_arg,
6206 got_worktree_patch_cb patch_cb, void *patch_arg,
6207 struct got_repository *repo)
6209 const struct got_error *err = NULL, *sync_err, *unlockerr;
6210 struct got_pathlist_entry *pe;
6211 struct got_fileindex *fileindex = NULL;
6212 char *fileindex_path = NULL;
6213 struct got_reference *head_ref = NULL;
6214 struct got_object_id *head_commit_id = NULL;
6215 struct check_stage_ok_arg oka;
6216 struct stage_path_arg spa;
6218 err = lock_worktree(worktree, LOCK_EX);
6222 err = got_ref_open(&head_ref, repo,
6223 got_worktree_get_head_ref_name(worktree), 0);
6226 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6229 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6233 /* Check pre-conditions before staging anything. */
6234 oka.head_commit_id = head_commit_id;
6235 oka.worktree = worktree;
6236 oka.fileindex = fileindex;
6238 oka.have_changes = 0;
6239 TAILQ_FOREACH(pe, paths, entry) {
6240 err = worktree_status(worktree, pe->path, fileindex, repo,
6241 check_stage_ok, &oka, NULL, NULL, 0, 0);
6245 if (!oka.have_changes) {
6246 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6250 spa.worktree = worktree;
6251 spa.fileindex = fileindex;
6253 spa.patch_cb = patch_cb;
6254 spa.patch_arg = patch_arg;
6255 spa.status_cb = status_cb;
6256 spa.status_arg = status_arg;
6257 spa.staged_something = 0;
6258 TAILQ_FOREACH(pe, paths, entry) {
6259 err = worktree_status(worktree, pe->path, fileindex, repo,
6260 stage_path, &spa, NULL, NULL, 0, 0);
6264 if (!spa.staged_something) {
6265 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6269 sync_err = sync_fileindex(fileindex, fileindex_path);
6270 if (sync_err && err == NULL)
6274 got_ref_close(head_ref);
6275 free(head_commit_id);
6276 free(fileindex_path);
6278 got_fileindex_free(fileindex);
6279 unlockerr = lock_worktree(worktree, LOCK_SH);
6280 if (unlockerr && err == NULL)
6285 struct unstage_path_arg {
6286 struct got_worktree *worktree;
6287 struct got_fileindex *fileindex;
6288 struct got_repository *repo;
6289 got_worktree_checkout_cb progress_cb;
6291 got_worktree_patch_cb patch_cb;
6295 static const struct got_error *
6296 create_unstaged_content(char **path_unstaged_content,
6297 char **path_new_staged_content, struct got_object_id *blob_id,
6298 struct got_object_id *staged_blob_id, const char *relpath,
6299 struct got_repository *repo,
6300 got_worktree_patch_cb patch_cb, void *patch_arg)
6302 const struct got_error *err;
6303 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6304 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6305 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6306 struct stat sb1, sb2;
6307 struct got_diff_changes *changes = NULL;
6308 struct got_diff_state *ds = NULL;
6309 struct got_diff_args *args = NULL;
6310 struct got_diff_change *change;
6311 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6312 int have_content = 0, have_rejected_content = 0;
6314 *path_unstaged_content = NULL;
6315 *path_new_staged_content = NULL;
6317 err = got_object_id_str(&label1, blob_id);
6320 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6324 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6328 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6332 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6336 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6340 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6344 if (stat(path1, &sb1) == -1) {
6345 err = got_error_from_errno2("stat", path1);
6349 if (stat(path2, &sb2) == -1) {
6350 err = got_error_from_errno2("stat", path2);
6354 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6355 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6359 err = got_opentemp_named(path_unstaged_content, &outfile,
6360 "got-unstaged-content");
6363 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6364 "got-new-staged-content");
6368 if (fseek(f1, 0L, SEEK_SET) == -1) {
6369 err = got_ferror(f1, GOT_ERR_IO);
6372 if (fseek(f2, 0L, SEEK_SET) == -1) {
6373 err = got_ferror(f2, GOT_ERR_IO);
6376 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6378 err = apply_or_reject_change(&choice, change, ++n,
6379 changes->nchanges, ds, args, diff_flags, relpath,
6380 f1, f2, &line_cur1, &line_cur2,
6381 outfile, rejectfile, patch_cb, patch_arg);
6384 if (choice == GOT_PATCH_CHOICE_YES)
6387 have_rejected_content = 1;
6388 if (choice == GOT_PATCH_CHOICE_QUIT)
6391 if (have_content || have_rejected_content)
6392 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6393 outfile, rejectfile);
6397 got_object_blob_close(blob);
6399 got_object_blob_close(staged_blob);
6400 if (f1 && fclose(f1) == EOF && err == NULL)
6401 err = got_error_from_errno2("fclose", path1);
6402 if (f2 && fclose(f2) == EOF && err == NULL)
6403 err = got_error_from_errno2("fclose", path2);
6404 if (outfile && fclose(outfile) == EOF && err == NULL)
6405 err = got_error_from_errno2("fclose", *path_unstaged_content);
6406 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6407 err = got_error_from_errno2("fclose", *path_new_staged_content);
6408 if (path1 && unlink(path1) == -1 && err == NULL)
6409 err = got_error_from_errno2("unlink", path1);
6410 if (path2 && unlink(path2) == -1 && err == NULL)
6411 err = got_error_from_errno2("unlink", path2);
6412 if (err || !have_content) {
6413 if (*path_unstaged_content &&
6414 unlink(*path_unstaged_content) == -1 && err == NULL)
6415 err = got_error_from_errno2("unlink",
6416 *path_unstaged_content);
6417 free(*path_unstaged_content);
6418 *path_unstaged_content = NULL;
6420 if (err || !have_rejected_content) {
6421 if (*path_new_staged_content &&
6422 unlink(*path_new_staged_content) == -1 && err == NULL)
6423 err = got_error_from_errno2("unlink",
6424 *path_new_staged_content);
6425 free(*path_new_staged_content);
6426 *path_new_staged_content = NULL;
6430 got_diff_state_free(ds);
6434 got_diff_free_changes(changes);
6440 static const struct got_error *
6441 unstage_path(void *arg, unsigned char status,
6442 unsigned char staged_status, const char *relpath,
6443 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6444 struct got_object_id *commit_id, int dirfd, const char *de_name)
6446 const struct got_error *err = NULL;
6447 struct unstage_path_arg *a = arg;
6448 struct got_fileindex_entry *ie;
6449 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6450 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6451 char *path_new_staged_content = NULL;
6452 char *id_str = NULL, *label_orig = NULL;
6453 int local_changes_subsumed;
6456 if (staged_status != GOT_STATUS_ADD &&
6457 staged_status != GOT_STATUS_MODIFY &&
6458 staged_status != GOT_STATUS_DELETE)
6461 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6463 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6465 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6467 return got_error_from_errno("asprintf");
6469 err = got_object_id_str(&id_str,
6470 commit_id ? commit_id : a->worktree->base_commit_id);
6473 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6475 err = got_error_from_errno("asprintf");
6479 switch (staged_status) {
6480 case GOT_STATUS_MODIFY:
6481 err = got_object_open_as_blob(&blob_base, a->repo,
6486 case GOT_STATUS_ADD:
6488 if (staged_status == GOT_STATUS_ADD) {
6489 int choice = GOT_PATCH_CHOICE_NONE;
6490 err = (*a->patch_cb)(&choice, a->patch_arg,
6491 staged_status, ie->path, NULL, 1, 1);
6494 if (choice != GOT_PATCH_CHOICE_YES)
6497 err = create_unstaged_content(
6498 &path_unstaged_content,
6499 &path_new_staged_content, blob_id,
6500 staged_blob_id, ie->path, a->repo,
6501 a->patch_cb, a->patch_arg);
6502 if (err || path_unstaged_content == NULL)
6504 if (path_new_staged_content) {
6505 err = got_object_blob_create(
6507 path_new_staged_content,
6511 memcpy(ie->staged_blob_sha1,
6512 staged_blob_id->sha1,
6513 SHA1_DIGEST_LENGTH);
6515 err = merge_file(&local_changes_subsumed,
6516 a->worktree, blob_base, ondisk_path,
6517 relpath, got_fileindex_perms_to_st(ie),
6518 path_unstaged_content, label_orig,
6519 "unstaged", a->repo, a->progress_cb,
6522 path_new_staged_content == NULL)
6523 got_fileindex_entry_stage_set(ie,
6524 GOT_FILEIDX_STAGE_NONE);
6525 break; /* Done with this file. */
6528 err = got_object_open_as_blob(&blob_staged, a->repo,
6529 staged_blob_id, 8192);
6532 err = merge_blob(&local_changes_subsumed, a->worktree,
6533 blob_base, ondisk_path, relpath,
6534 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6535 commit_id ? commit_id : a->worktree->base_commit_id,
6536 a->repo, a->progress_cb, a->progress_arg);
6538 got_fileindex_entry_stage_set(ie,
6539 GOT_FILEIDX_STAGE_NONE);
6541 case GOT_STATUS_DELETE:
6543 int choice = GOT_PATCH_CHOICE_NONE;
6544 err = (*a->patch_cb)(&choice, a->patch_arg,
6545 staged_status, ie->path, NULL, 1, 1);
6548 if (choice == GOT_PATCH_CHOICE_NO)
6550 if (choice != GOT_PATCH_CHOICE_YES) {
6551 err = got_error(GOT_ERR_PATCH_CHOICE);
6555 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6556 err = get_file_status(&status, &sb, ie, ondisk_path,
6557 dirfd, de_name, a->repo);
6560 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6565 if (path_unstaged_content &&
6566 unlink(path_unstaged_content) == -1 && err == NULL)
6567 err = got_error_from_errno2("unlink", path_unstaged_content);
6568 if (path_new_staged_content &&
6569 unlink(path_new_staged_content) == -1 && err == NULL)
6570 err = got_error_from_errno2("unlink", path_new_staged_content);
6571 free(path_unstaged_content);
6572 free(path_new_staged_content);
6574 got_object_blob_close(blob_base);
6576 got_object_blob_close(blob_staged);
6582 const struct got_error *
6583 got_worktree_unstage(struct got_worktree *worktree,
6584 struct got_pathlist_head *paths,
6585 got_worktree_checkout_cb progress_cb, void *progress_arg,
6586 got_worktree_patch_cb patch_cb, void *patch_arg,
6587 struct got_repository *repo)
6589 const struct got_error *err = NULL, *sync_err, *unlockerr;
6590 struct got_pathlist_entry *pe;
6591 struct got_fileindex *fileindex = NULL;
6592 char *fileindex_path = NULL;
6593 struct unstage_path_arg upa;
6595 err = lock_worktree(worktree, LOCK_EX);
6599 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6603 upa.worktree = worktree;
6604 upa.fileindex = fileindex;
6606 upa.progress_cb = progress_cb;
6607 upa.progress_arg = progress_arg;
6608 upa.patch_cb = patch_cb;
6609 upa.patch_arg = patch_arg;
6610 TAILQ_FOREACH(pe, paths, entry) {
6611 err = worktree_status(worktree, pe->path, fileindex, repo,
6612 unstage_path, &upa, NULL, NULL, 0, 0);
6617 sync_err = sync_fileindex(fileindex, fileindex_path);
6618 if (sync_err && err == NULL)
6621 free(fileindex_path);
6623 got_fileindex_free(fileindex);
6624 unlockerr = lock_worktree(worktree, LOCK_SH);
6625 if (unlockerr && err == NULL)