2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
69 const struct got_error *err = NULL;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
83 const struct got_error *err = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 err = got_error_from_errno2("open", path);
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
170 (*content)[sb.st_size - 1] = '\0';
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
192 return got_error_from_errno("got_ref_to_str");
194 refstr = strdup(got_ref_get_name(head_ref));
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
210 uint32_t uuid_status;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
223 err = got_ref_resolve(&commit_id, repo, head_ref);
226 err = got_object_get_type(&obj_type, repo, commit_id);
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
421 got_repo_close(repo);
424 free(base_commit_id_str);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
434 (*worktree)->lockfd = fd;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
450 path = dirname(path);
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
535 err = write_head_ref(path_got, head_ref);
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
570 err = got_object_open(&obj, repo, commit_id);
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
595 got_object_close(obj);
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
641 size_t flen1 = 0, flen2 = 0;
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
660 } else if (flen2 == 0) {
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
684 FILE *f1 = NULL, *f2 = NULL;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
700 if (size1 != size2) {
705 f1 = fopen(f1_path, "r");
707 return got_error_from_errno2("open", f1_path);
709 f2 = fopen(f2_path, "r");
711 err = got_error_from_errno2("open", f2_path);
715 err = check_file_contents_equal(same, f1, f2);
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
805 if (chmod(merged_path, st_mode) != 0) {
806 err = got_error_from_errno2("chmod", merged_path);
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
818 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
819 err = got_error_from_errno("close");
820 if (f_orig && fclose(f_orig) != 0 && err == NULL)
821 err = got_error_from_errno("fclose");
824 if (blob_orig_path) {
825 unlink(blob_orig_path);
826 free(blob_orig_path);
832 * Perform a 3-way merge where blob_orig acts as the common ancestor,
833 * blob_deriv acts as the first derived version, and the file on disk
834 * acts as the second derived version.
836 static const struct got_error *
837 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
838 struct got_blob_object *blob_orig, const char *ondisk_path,
839 const char *path, uint16_t st_mode, const char *label_orig,
840 struct got_blob_object *blob_deriv,
841 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg)
844 const struct got_error *err = NULL;
845 FILE *f_deriv = NULL;
846 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
847 char *label_deriv = NULL, *parent;
849 *local_changes_subsumed = 0;
851 parent = dirname(ondisk_path);
853 return got_error_from_errno2("dirname", ondisk_path);
856 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
857 err = got_error_from_errno("asprintf");
862 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
865 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
870 err = got_object_id_str(&id_str, deriv_base_commit_id);
873 if (asprintf(&label_deriv, "%s: commit %s",
874 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
875 err = got_error_from_errno("asprintf");
879 err = merge_file(local_changes_subsumed, worktree, blob_orig,
880 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
881 label_deriv, repo, progress_cb, progress_arg);
883 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
884 err = got_error_from_errno("fclose");
886 if (blob_deriv_path) {
887 unlink(blob_deriv_path);
888 free(blob_deriv_path);
895 static const struct got_error *
896 update_blob_fileindex_entry(struct got_worktree *worktree,
897 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
898 const char *ondisk_path, const char *path, struct got_blob_object *blob,
899 int update_timestamps)
901 const struct got_error *err = NULL;
904 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
906 err = got_fileindex_entry_update(ie, ondisk_path,
907 blob->id.sha1, worktree->base_commit_id->sha1,
910 struct got_fileindex_entry *new_ie;
911 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
912 path, blob->id.sha1, worktree->base_commit_id->sha1);
914 err = got_fileindex_entry_add(fileindex, new_ie);
920 get_ondisk_perms(int executable, mode_t st_mode)
922 mode_t xbits = S_IXUSR;
925 /* Map read bits to execute bits. */
926 if (st_mode & S_IRGRP)
928 if (st_mode & S_IROTH)
930 return st_mode | xbits;
933 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
936 static const struct got_error *
937 install_blob(struct got_worktree *worktree, const char *ondisk_path,
938 const char *path, uint16_t te_mode, uint16_t st_mode,
939 struct got_blob_object *blob, int restoring_missing_file,
940 int reverting_versioned_file, struct got_repository *repo,
941 got_worktree_checkout_cb progress_cb, void *progress_arg)
943 const struct got_error *err = NULL;
947 char *tmppath = NULL;
949 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
950 GOT_DEFAULT_FILE_MODE);
952 if (errno == ENOENT) {
953 char *parent = dirname(path);
955 return got_error_from_errno2("dirname", path);
956 err = add_dir_on_disk(worktree, parent);
959 fd = open(ondisk_path,
960 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
961 GOT_DEFAULT_FILE_MODE);
963 return got_error_from_errno2("open",
965 } else if (errno == EEXIST) {
966 if (!S_ISREG(st_mode)) {
967 /* TODO file is obstructed; do something */
968 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
971 err = got_opentemp_named_fd(&tmppath, &fd,
978 return got_error_from_errno2("open", ondisk_path);
981 if (restoring_missing_file)
982 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
983 else if (reverting_versioned_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
986 err = (*progress_cb)(progress_arg,
987 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
991 hdrlen = got_object_blob_get_hdrlen(blob);
993 const uint8_t *buf = got_object_blob_get_read_buf(blob);
994 err = got_object_blob_read_block(&len, blob);
998 /* Skip blob object header first time around. */
999 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1001 err = got_error_from_errno("write");
1003 } else if (outlen != len - hdrlen) {
1004 err = got_error(GOT_ERR_IO);
1011 if (fsync(fd) != 0) {
1012 err = got_error_from_errno("fsync");
1017 if (rename(tmppath, ondisk_path) != 0) {
1018 err = got_error_from_errno3("rename", tmppath,
1025 if (chmod(ondisk_path,
1026 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1027 err = got_error_from_errno2("chmod", ondisk_path);
1032 if (fd != -1 && close(fd) != 0 && err == NULL)
1033 err = got_error_from_errno("close");
1038 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1039 static const struct got_error *
1040 get_modified_file_content_status(unsigned char *status, FILE *f)
1042 const struct got_error *err = NULL;
1043 const char *markers[3] = {
1044 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1045 GOT_DIFF_CONFLICT_MARKER_SEP,
1046 GOT_DIFF_CONFLICT_MARKER_END
1051 const char delim[3] = {'\0', '\0', '\0'};
1053 while (*status == GOT_STATUS_MODIFY) {
1054 line = fparseln(f, &len, NULL, delim, 0);
1058 err = got_ferror(f, GOT_ERR_IO);
1062 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1063 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1065 *status = GOT_STATUS_CONFLICT;
1075 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1077 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1078 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1082 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1084 return !(ie->ctime_sec == sb->st_ctime &&
1085 ie->ctime_nsec == sb->st_ctimensec &&
1086 ie->mtime_sec == sb->st_mtime &&
1087 ie->mtime_nsec == sb->st_mtimensec &&
1088 ie->size == (sb->st_size & 0xffffffff) &&
1089 !xbit_differs(ie, sb->st_mode));
1092 static unsigned char
1093 get_staged_status(struct got_fileindex_entry *ie)
1095 switch (got_fileindex_entry_stage_get(ie)) {
1096 case GOT_FILEIDX_STAGE_ADD:
1097 return GOT_STATUS_ADD;
1098 case GOT_FILEIDX_STAGE_DELETE:
1099 return GOT_STATUS_DELETE;
1100 case GOT_FILEIDX_STAGE_MODIFY:
1101 return GOT_STATUS_MODIFY;
1103 return GOT_STATUS_NO_CHANGE;
1107 static const struct got_error *
1108 get_file_status(unsigned char *status, struct stat *sb,
1109 struct got_fileindex_entry *ie, const char *abspath,
1110 struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_object_id id;
1117 struct got_blob_object *blob = NULL;
1119 unsigned char staged_status = get_staged_status(ie);
1121 *status = GOT_STATUS_NO_CHANGE;
1123 if (lstat(abspath, sb) == -1) {
1124 if (errno == ENOENT) {
1125 if (got_fileindex_entry_has_file_on_disk(ie))
1126 *status = GOT_STATUS_MISSING;
1128 *status = GOT_STATUS_DELETE;
1131 return got_error_from_errno2("lstat", abspath);
1134 if (!S_ISREG(sb->st_mode)) {
1135 *status = GOT_STATUS_OBSTRUCTED;
1139 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1140 *status = GOT_STATUS_DELETE;
1142 } else if (!got_fileindex_entry_has_blob(ie) &&
1143 staged_status != GOT_STATUS_ADD) {
1144 *status = GOT_STATUS_ADD;
1148 if (!stat_info_differs(ie, sb))
1151 if (staged_status == GOT_STATUS_MODIFY ||
1152 staged_status == GOT_STATUS_ADD)
1153 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1155 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1157 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1161 f = fopen(abspath, "r");
1163 err = got_error_from_errno2("fopen", abspath);
1166 hdrlen = got_object_blob_get_hdrlen(blob);
1168 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1169 err = got_object_blob_read_block(&blen, blob);
1172 /* Skip length of blob object header first time around. */
1173 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1174 if (flen == 0 && ferror(f)) {
1175 err = got_error_from_errno("fread");
1180 *status = GOT_STATUS_MODIFY;
1182 } else if (flen == 0) {
1184 *status = GOT_STATUS_MODIFY;
1186 } else if (blen - hdrlen == flen) {
1187 /* Skip blob object header first time around. */
1188 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1189 *status = GOT_STATUS_MODIFY;
1193 *status = GOT_STATUS_MODIFY;
1199 if (*status == GOT_STATUS_MODIFY) {
1201 err = get_modified_file_content_status(status, f);
1202 } else if (xbit_differs(ie, sb->st_mode))
1203 *status = GOT_STATUS_MODE_CHANGE;
1206 got_object_blob_close(blob);
1213 * Update timestamps in the file index if a file is unmodified and
1214 * we had to run a full content comparison to find out.
1216 static const struct got_error *
1217 sync_timestamps(char *ondisk_path, unsigned char status,
1218 struct got_fileindex_entry *ie, struct stat *sb)
1220 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1221 return got_fileindex_entry_update(ie, ondisk_path,
1222 ie->blob_sha1, ie->commit_sha1, 1);
1227 static const struct got_error *
1228 update_blob(struct got_worktree *worktree,
1229 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1230 struct got_tree_entry *te, const char *path,
1231 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1234 const struct got_error *err = NULL;
1235 struct got_blob_object *blob = NULL;
1237 unsigned char status = GOT_STATUS_NO_CHANGE;
1240 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1241 return got_error_from_errno("asprintf");
1244 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1245 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1248 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1251 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1252 sb.st_mode = got_fileindex_perms_to_st(ie);
1254 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1256 if (status == GOT_STATUS_OBSTRUCTED) {
1257 err = (*progress_cb)(progress_arg, status, path);
1261 if (ie && status != GOT_STATUS_MISSING &&
1262 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1263 if (got_fileindex_entry_has_commit(ie) &&
1264 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1265 SHA1_DIGEST_LENGTH) == 0) {
1266 err = sync_timestamps(ondisk_path, status, ie, &sb);
1269 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1273 if (got_fileindex_entry_has_blob(ie) &&
1274 memcmp(ie->blob_sha1, te->id->sha1,
1275 SHA1_DIGEST_LENGTH) == 0) {
1276 err = sync_timestamps(ondisk_path, status, ie, &sb);
1281 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1285 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1286 int update_timestamps;
1287 struct got_blob_object *blob2 = NULL;
1288 char *label_orig = NULL;
1289 if (got_fileindex_entry_has_blob(ie)) {
1290 struct got_object_id id2;
1291 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1292 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1296 if (got_fileindex_entry_has_commit(ie)) {
1297 char id_str[SHA1_DIGEST_STRING_LENGTH];
1298 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1299 sizeof(id_str)) == NULL) {
1300 err = got_error_path(id_str,
1301 GOT_ERR_BAD_OBJ_ID_STR);
1304 if (asprintf(&label_orig, "%s: commit %s",
1305 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1306 err = got_error_from_errno("asprintf");
1310 err = merge_blob(&update_timestamps, worktree, blob2,
1311 ondisk_path, path, sb.st_mode, label_orig, blob,
1312 worktree->base_commit_id, repo,
1313 progress_cb, progress_arg);
1316 got_object_blob_close(blob2);
1320 * Do not update timestamps of files with local changes.
1321 * Otherwise, a future status walk would treat them as
1322 * unmodified files again.
1324 err = got_fileindex_entry_update(ie, ondisk_path,
1325 blob->id.sha1, worktree->base_commit_id->sha1,
1327 } else if (status == GOT_STATUS_MODE_CHANGE) {
1328 err = got_fileindex_entry_update(ie, ondisk_path,
1329 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1330 } else if (status == GOT_STATUS_DELETE) {
1331 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1334 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1335 ondisk_path, path, blob, 0);
1339 err = install_blob(worktree, ondisk_path, path, te->mode,
1340 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1341 repo, progress_cb, progress_arg);
1344 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1345 ondisk_path, path, blob, 1);
1349 got_object_blob_close(blob);
1355 static const struct got_error *
1356 remove_ondisk_file(const char *root_path, const char *path)
1358 const struct got_error *err = NULL;
1359 char *ondisk_path = NULL;
1361 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1362 return got_error_from_errno("asprintf");
1364 if (unlink(ondisk_path) == -1) {
1365 if (errno != ENOENT)
1366 err = got_error_from_errno2("unlink", ondisk_path);
1368 char *parent = dirname(ondisk_path);
1369 while (parent && strcmp(parent, root_path) != 0) {
1370 if (rmdir(parent) == -1) {
1371 if (errno != ENOTEMPTY)
1372 err = got_error_from_errno2("rmdir",
1376 parent = dirname(parent);
1383 static const struct got_error *
1384 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1385 struct got_fileindex_entry *ie, struct got_repository *repo,
1386 got_worktree_checkout_cb progress_cb, void *progress_arg)
1388 const struct got_error *err = NULL;
1389 unsigned char status;
1393 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1394 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1396 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1398 return got_error_from_errno("asprintf");
1400 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1404 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1405 status == GOT_STATUS_ADD) {
1406 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1410 * Preserve the working file and change the deleted blob's
1411 * entry into a schedule-add entry.
1413 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1418 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1421 if (status == GOT_STATUS_NO_CHANGE) {
1422 err = remove_ondisk_file(worktree->root_path, ie->path);
1426 got_fileindex_entry_remove(fileindex, ie);
1432 struct diff_cb_arg {
1433 struct got_fileindex *fileindex;
1434 struct got_worktree *worktree;
1435 struct got_repository *repo;
1436 got_worktree_checkout_cb progress_cb;
1438 got_cancel_cb cancel_cb;
1442 static const struct got_error *
1443 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1444 struct got_tree_entry *te, const char *parent_path)
1446 struct diff_cb_arg *a = arg;
1448 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1449 return got_error(GOT_ERR_CANCELLED);
1451 return update_blob(a->worktree, a->fileindex, ie, te,
1452 ie->path, a->repo, a->progress_cb, a->progress_arg);
1455 static const struct got_error *
1456 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1458 struct diff_cb_arg *a = arg;
1460 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1461 return got_error(GOT_ERR_CANCELLED);
1463 return delete_blob(a->worktree, a->fileindex, ie,
1464 a->repo, a->progress_cb, a->progress_arg);
1467 static const struct got_error *
1468 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1470 struct diff_cb_arg *a = arg;
1471 const struct got_error *err;
1474 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1475 return got_error(GOT_ERR_CANCELLED);
1477 if (got_object_tree_entry_is_submodule(te))
1480 if (asprintf(&path, "%s%s%s", parent_path,
1481 parent_path[0] ? "/" : "", te->name)
1483 return got_error_from_errno("asprintf");
1485 if (S_ISDIR(te->mode))
1486 err = add_dir_on_disk(a->worktree, path);
1488 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1489 a->repo, a->progress_cb, a->progress_arg);
1495 static const struct got_error *
1496 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1498 const struct got_error *err = NULL;
1499 char *uuidstr = NULL;
1500 uint32_t uuid_status;
1504 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1505 if (uuid_status != uuid_s_ok)
1506 return got_error_uuid(uuid_status, "uuid_to_string");
1508 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1510 err = got_error_from_errno("asprintf");
1517 const struct got_error *
1518 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1520 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1523 static const struct got_error *
1524 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1526 return get_ref_name(refname, worktree,
1527 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1530 static const struct got_error *
1531 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1533 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1536 static const struct got_error *
1537 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1539 return get_ref_name(refname, worktree,
1540 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1543 static const struct got_error *
1544 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1546 return get_ref_name(refname, worktree,
1547 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1550 static const struct got_error *
1551 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1553 return get_ref_name(refname, worktree,
1554 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1557 static const struct got_error *
1558 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1560 return get_ref_name(refname, worktree,
1561 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1564 static const struct got_error *
1565 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1567 return get_ref_name(refname, worktree,
1568 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1571 static const struct got_error *
1572 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1574 return get_ref_name(refname, worktree,
1575 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1578 const struct got_error *
1579 got_worktree_get_histedit_script_path(char **path,
1580 struct got_worktree *worktree)
1582 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1583 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1585 return got_error_from_errno("asprintf");
1591 * Prevent Git's garbage collector from deleting our base commit by
1592 * setting a reference to our base commit's ID.
1594 static const struct got_error *
1595 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1597 const struct got_error *err = NULL;
1598 struct got_reference *ref = NULL;
1601 err = got_worktree_get_base_ref_name(&refname, worktree);
1605 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1609 err = got_ref_write(ref, repo);
1617 static const struct got_error *
1618 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1620 const struct got_error *err = NULL;
1622 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1623 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1624 err = got_error_from_errno("asprintf");
1625 *fileindex_path = NULL;
1631 static const struct got_error *
1632 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1633 struct got_worktree *worktree)
1635 const struct got_error *err = NULL;
1638 *fileindex_path = NULL;
1639 *fileindex = got_fileindex_alloc();
1640 if (*fileindex == NULL)
1641 return got_error_from_errno("got_fileindex_alloc");
1643 err = get_fileindex_path(fileindex_path, worktree);
1647 index = fopen(*fileindex_path, "rb");
1648 if (index == NULL) {
1649 if (errno != ENOENT)
1650 err = got_error_from_errno2("fopen", *fileindex_path);
1652 err = got_fileindex_read(*fileindex, index);
1653 if (fclose(index) != 0 && err == NULL)
1654 err = got_error_from_errno("fclose");
1658 free(*fileindex_path);
1659 *fileindex_path = NULL;
1660 got_fileindex_free(*fileindex);
1666 struct bump_base_commit_id_arg {
1667 struct got_object_id *base_commit_id;
1670 const char *entry_name;
1671 got_worktree_checkout_cb progress_cb;
1675 /* Bump base commit ID of all files within an updated part of the work tree. */
1676 static const struct got_error *
1677 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1679 const struct got_error *err;
1680 struct bump_base_commit_id_arg *a = arg;
1682 if (a->entry_name) {
1683 if (strcmp(ie->path, a->path) != 0)
1685 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1688 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1689 SHA1_DIGEST_LENGTH) == 0)
1692 if (a->progress_cb) {
1693 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1698 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1702 static const struct got_error *
1703 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1705 const struct got_error *err = NULL;
1706 char *new_fileindex_path = NULL;
1707 FILE *new_index = NULL;
1709 err = got_opentemp_named(&new_fileindex_path, &new_index,
1714 err = got_fileindex_write(fileindex, new_index);
1718 if (rename(new_fileindex_path, fileindex_path) != 0) {
1719 err = got_error_from_errno3("rename", new_fileindex_path,
1721 unlink(new_fileindex_path);
1726 free(new_fileindex_path);
1730 static const struct got_error *
1731 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1732 struct got_object_id **tree_id, const char *wt_relpath,
1733 struct got_worktree *worktree, struct got_repository *repo)
1735 const struct got_error *err = NULL;
1736 struct got_object_id *id = NULL;
1737 char *in_repo_path = NULL;
1738 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1740 *entry_type = GOT_OBJ_TYPE_ANY;
1741 *tree_relpath = NULL;
1744 if (wt_relpath[0] == '\0') {
1745 /* Check out all files within the work tree. */
1746 *entry_type = GOT_OBJ_TYPE_TREE;
1747 *tree_relpath = strdup("");
1748 if (*tree_relpath == NULL) {
1749 err = got_error_from_errno("strdup");
1752 err = got_object_id_by_path(tree_id, repo,
1753 worktree->base_commit_id, worktree->path_prefix);
1759 /* Check out a subset of files in the work tree. */
1761 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1762 is_root_wt ? "" : "/", wt_relpath) == -1) {
1763 err = got_error_from_errno("asprintf");
1767 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1773 in_repo_path = NULL;
1775 err = got_object_get_type(entry_type, repo, id);
1779 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1780 /* Check out a single file. */
1781 if (strchr(wt_relpath, '/') == NULL) {
1782 /* Check out a single file in work tree's root dir. */
1783 in_repo_path = strdup(worktree->path_prefix);
1784 if (in_repo_path == NULL) {
1785 err = got_error_from_errno("strdup");
1788 *tree_relpath = strdup("");
1789 if (*tree_relpath == NULL) {
1790 err = got_error_from_errno("strdup");
1794 /* Check out a single file in a subdirectory. */
1795 err = got_path_dirname(tree_relpath, wt_relpath);
1798 if (asprintf(&in_repo_path, "%s%s%s",
1799 worktree->path_prefix, is_root_wt ? "" : "/",
1800 *tree_relpath) == -1) {
1801 err = got_error_from_errno("asprintf");
1805 err = got_object_id_by_path(tree_id, repo,
1806 worktree->base_commit_id, in_repo_path);
1808 /* Check out all files within a subdirectory. */
1809 *tree_id = got_object_id_dup(id);
1810 if (*tree_id == NULL) {
1811 err = got_error_from_errno("got_object_id_dup");
1814 *tree_relpath = strdup(wt_relpath);
1815 if (*tree_relpath == NULL) {
1816 err = got_error_from_errno("strdup");
1824 *entry_type = GOT_OBJ_TYPE_ANY;
1825 free(*tree_relpath);
1826 *tree_relpath = NULL;
1833 static const struct got_error *
1834 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1835 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1836 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1837 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1839 const struct got_error *err = NULL;
1840 struct got_commit_object *commit = NULL;
1841 struct got_tree_object *tree = NULL;
1842 struct got_fileindex_diff_tree_cb diff_cb;
1843 struct diff_cb_arg arg;
1845 err = ref_base_commit(worktree, repo);
1849 err = got_object_open_as_commit(&commit, repo,
1850 worktree->base_commit_id);
1854 err = got_object_open_as_tree(&tree, repo, tree_id);
1859 got_object_tree_find_entry(tree, entry_name) == NULL) {
1860 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1864 diff_cb.diff_old_new = diff_old_new;
1865 diff_cb.diff_old = diff_old;
1866 diff_cb.diff_new = diff_new;
1867 arg.fileindex = fileindex;
1868 arg.worktree = worktree;
1870 arg.progress_cb = progress_cb;
1871 arg.progress_arg = progress_arg;
1872 arg.cancel_cb = cancel_cb;
1873 arg.cancel_arg = cancel_arg;
1874 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1875 entry_name, repo, &diff_cb, &arg);
1878 got_object_tree_close(tree);
1880 got_object_commit_close(commit);
1884 const struct got_error *
1885 got_worktree_checkout_files(struct got_worktree *worktree,
1886 struct got_pathlist_head *paths, struct got_repository *repo,
1887 got_worktree_checkout_cb progress_cb, void *progress_arg,
1888 got_cancel_cb cancel_cb, void *cancel_arg)
1890 const struct got_error *err = NULL, *sync_err, *unlockerr;
1891 struct got_commit_object *commit = NULL;
1892 struct got_tree_object *tree = NULL;
1893 struct got_fileindex *fileindex = NULL;
1894 char *fileindex_path = NULL;
1895 struct got_pathlist_entry *pe;
1896 struct tree_path_data {
1897 SIMPLEQ_ENTRY(tree_path_data) entry;
1898 struct got_object_id *tree_id;
1903 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1905 SIMPLEQ_INIT(&tree_paths);
1907 err = lock_worktree(worktree, LOCK_EX);
1911 /* Map all specified paths to in-repository trees. */
1912 TAILQ_FOREACH(pe, paths, entry) {
1913 tpd = malloc(sizeof(*tpd));
1915 err = got_error_from_errno("malloc");
1919 err = find_tree_entry_for_checkout(&tpd->entry_type,
1920 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1926 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1927 err = got_path_basename(&tpd->entry_name, pe->path);
1935 tpd->entry_name = NULL;
1937 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1941 * Read the file index.
1942 * Checking out files is supposed to be an idempotent operation.
1943 * If the on-disk file index is incomplete we will try to complete it.
1945 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1949 tpd = SIMPLEQ_FIRST(&tree_paths);
1950 TAILQ_FOREACH(pe, paths, entry) {
1951 struct bump_base_commit_id_arg bbc_arg;
1953 err = checkout_files(worktree, fileindex, tpd->relpath,
1954 tpd->tree_id, tpd->entry_name, repo,
1955 progress_cb, progress_arg, cancel_cb, cancel_arg);
1959 bbc_arg.base_commit_id = worktree->base_commit_id;
1960 bbc_arg.entry_name = tpd->entry_name;
1961 bbc_arg.path = pe->path;
1962 bbc_arg.path_len = pe->path_len;
1963 bbc_arg.progress_cb = progress_cb;
1964 bbc_arg.progress_arg = progress_arg;
1965 err = got_fileindex_for_each_entry_safe(fileindex,
1966 bump_base_commit_id, &bbc_arg);
1970 tpd = SIMPLEQ_NEXT(tpd, entry);
1972 sync_err = sync_fileindex(fileindex, fileindex_path);
1973 if (sync_err && err == NULL)
1976 free(fileindex_path);
1978 got_object_tree_close(tree);
1980 got_object_commit_close(commit);
1982 got_fileindex_free(fileindex);
1983 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1984 tpd = SIMPLEQ_FIRST(&tree_paths);
1985 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1990 unlockerr = lock_worktree(worktree, LOCK_SH);
1991 if (unlockerr && err == NULL)
1996 struct merge_file_cb_arg {
1997 struct got_worktree *worktree;
1998 struct got_fileindex *fileindex;
1999 got_worktree_checkout_cb progress_cb;
2001 got_cancel_cb cancel_cb;
2003 const char *label_orig;
2004 struct got_object_id *commit_id2;
2007 static const struct got_error *
2008 merge_file_cb(void *arg, struct got_blob_object *blob1,
2009 struct got_blob_object *blob2, struct got_object_id *id1,
2010 struct got_object_id *id2, const char *path1, const char *path2,
2011 mode_t mode1, mode_t mode2, struct got_repository *repo)
2013 static const struct got_error *err = NULL;
2014 struct merge_file_cb_arg *a = arg;
2015 struct got_fileindex_entry *ie;
2016 char *ondisk_path = NULL;
2018 unsigned char status;
2019 int local_changes_subsumed;
2021 if (blob1 && blob2) {
2022 ie = got_fileindex_entry_get(a->fileindex, path2,
2025 return (*a->progress_cb)(a->progress_arg,
2026 GOT_STATUS_MISSING, path2);
2028 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2030 return got_error_from_errno("asprintf");
2032 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2036 if (status == GOT_STATUS_DELETE) {
2037 err = (*a->progress_cb)(a->progress_arg,
2038 GOT_STATUS_MERGE, path2);
2041 if (status != GOT_STATUS_NO_CHANGE &&
2042 status != GOT_STATUS_MODIFY &&
2043 status != GOT_STATUS_CONFLICT &&
2044 status != GOT_STATUS_ADD) {
2045 err = (*a->progress_cb)(a->progress_arg, status, path2);
2049 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2050 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2051 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2053 ie = got_fileindex_entry_get(a->fileindex, path1,
2056 return (*a->progress_cb)(a->progress_arg,
2057 GOT_STATUS_MISSING, path2);
2059 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2061 return got_error_from_errno("asprintf");
2063 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2068 case GOT_STATUS_NO_CHANGE:
2069 err = (*a->progress_cb)(a->progress_arg,
2070 GOT_STATUS_DELETE, path1);
2073 err = remove_ondisk_file(a->worktree->root_path, path1);
2077 got_fileindex_entry_mark_deleted_from_disk(ie);
2079 case GOT_STATUS_DELETE:
2080 case GOT_STATUS_MISSING:
2081 err = (*a->progress_cb)(a->progress_arg,
2082 GOT_STATUS_DELETE, path1);
2086 got_fileindex_entry_mark_deleted_from_disk(ie);
2088 case GOT_STATUS_ADD:
2089 case GOT_STATUS_MODIFY:
2090 case GOT_STATUS_CONFLICT:
2091 err = (*a->progress_cb)(a->progress_arg,
2092 GOT_STATUS_CANNOT_DELETE, path1);
2096 case GOT_STATUS_OBSTRUCTED:
2097 err = (*a->progress_cb)(a->progress_arg, status, path1);
2105 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2107 return got_error_from_errno("asprintf");
2108 ie = got_fileindex_entry_get(a->fileindex, path2,
2111 err = get_file_status(&status, &sb, ie, ondisk_path,
2115 if (status != GOT_STATUS_NO_CHANGE &&
2116 status != GOT_STATUS_MODIFY &&
2117 status != GOT_STATUS_CONFLICT &&
2118 status != GOT_STATUS_ADD) {
2119 err = (*a->progress_cb)(a->progress_arg,
2123 err = merge_blob(&local_changes_subsumed, a->worktree,
2124 NULL, ondisk_path, path2, sb.st_mode,
2125 a->label_orig, blob2, a->commit_id2, repo,
2128 if (status == GOT_STATUS_DELETE) {
2129 err = update_blob_fileindex_entry(a->worktree,
2130 a->fileindex, ie, ondisk_path, ie->path,
2136 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2137 err = install_blob(a->worktree, ondisk_path, path2,
2138 /* XXX get this from parent tree! */
2139 GOT_DEFAULT_FILE_MODE,
2140 sb.st_mode, blob2, 0, 0, repo,
2141 a->progress_cb, a->progress_arg);
2144 err = got_fileindex_entry_alloc(&ie,
2145 ondisk_path, path2, NULL, NULL);
2148 err = got_fileindex_entry_add(a->fileindex, ie);
2150 got_fileindex_entry_free(ie);
2160 struct check_merge_ok_arg {
2161 struct got_worktree *worktree;
2162 struct got_repository *repo;
2165 static const struct got_error *
2166 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2168 const struct got_error *err = NULL;
2169 struct check_merge_ok_arg *a = arg;
2170 unsigned char status;
2174 /* Reject merges into a work tree with mixed base commits. */
2175 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2176 SHA1_DIGEST_LENGTH))
2177 return got_error(GOT_ERR_MIXED_COMMITS);
2179 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2181 return got_error_from_errno("asprintf");
2183 /* Reject merges into a work tree with conflicted files. */
2184 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2187 if (status == GOT_STATUS_CONFLICT)
2188 return got_error(GOT_ERR_CONFLICTS);
2193 static const struct got_error *
2194 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2195 const char *fileindex_path, struct got_object_id *commit_id1,
2196 struct got_object_id *commit_id2, struct got_repository *repo,
2197 got_worktree_checkout_cb progress_cb, void *progress_arg,
2198 got_cancel_cb cancel_cb, void *cancel_arg)
2200 const struct got_error *err = NULL, *sync_err;
2201 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2202 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2203 struct merge_file_cb_arg arg;
2204 char *label_orig = NULL;
2209 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2210 worktree->path_prefix);
2214 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2218 err = got_object_id_str(&id_str, commit_id1);
2222 if (asprintf(&label_orig, "%s: commit %s",
2223 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2224 err = got_error_from_errno("asprintf");
2231 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2232 worktree->path_prefix);
2236 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2240 arg.worktree = worktree;
2241 arg.fileindex = fileindex;
2242 arg.progress_cb = progress_cb;
2243 arg.progress_arg = progress_arg;
2244 arg.cancel_cb = cancel_cb;
2245 arg.cancel_arg = cancel_arg;
2246 arg.label_orig = label_orig;
2247 arg.commit_id2 = commit_id2;
2248 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2249 sync_err = sync_fileindex(fileindex, fileindex_path);
2250 if (sync_err && err == NULL)
2254 got_object_tree_close(tree1);
2256 got_object_tree_close(tree2);
2261 const struct got_error *
2262 got_worktree_merge_files(struct got_worktree *worktree,
2263 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2264 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2265 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2267 const struct got_error *err, *unlockerr;
2268 char *fileindex_path = NULL;
2269 struct got_fileindex *fileindex = NULL;
2270 struct check_merge_ok_arg mok_arg;
2272 err = lock_worktree(worktree, LOCK_EX);
2276 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2280 mok_arg.worktree = worktree;
2281 mok_arg.repo = repo;
2282 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2287 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2288 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2291 got_fileindex_free(fileindex);
2292 free(fileindex_path);
2293 unlockerr = lock_worktree(worktree, LOCK_SH);
2294 if (unlockerr && err == NULL)
2299 struct diff_dir_cb_arg {
2300 struct got_fileindex *fileindex;
2301 struct got_worktree *worktree;
2302 const char *status_path;
2303 size_t status_path_len;
2304 struct got_repository *repo;
2305 got_worktree_status_cb status_cb;
2307 got_cancel_cb cancel_cb;
2309 /* A pathlist containing per-directory pathlists of ignore patterns. */
2310 struct got_pathlist_head ignores;
2313 static const struct got_error *
2314 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2315 got_worktree_status_cb status_cb, void *status_arg,
2316 struct got_repository *repo)
2318 const struct got_error *err = NULL;
2319 unsigned char status = GOT_STATUS_NO_CHANGE;
2320 unsigned char staged_status = get_staged_status(ie);
2322 struct got_object_id blob_id, commit_id, staged_blob_id;
2323 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2324 struct got_object_id *staged_blob_idp = NULL;
2326 err = get_file_status(&status, &sb, ie, abspath, repo);
2330 if (status == GOT_STATUS_NO_CHANGE &&
2331 staged_status == GOT_STATUS_NO_CHANGE)
2334 if (got_fileindex_entry_has_blob(ie)) {
2335 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2336 blob_idp = &blob_id;
2338 if (got_fileindex_entry_has_commit(ie)) {
2339 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2340 commit_idp = &commit_id;
2342 if (staged_status == GOT_STATUS_ADD ||
2343 staged_status == GOT_STATUS_MODIFY) {
2344 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2345 SHA1_DIGEST_LENGTH);
2346 staged_blob_idp = &staged_blob_id;
2349 return (*status_cb)(status_arg, status, staged_status,
2350 ie->path, blob_idp, staged_blob_idp, commit_idp);
2353 static const struct got_error *
2354 status_old_new(void *arg, struct got_fileindex_entry *ie,
2355 struct dirent *de, const char *parent_path)
2357 const struct got_error *err = NULL;
2358 struct diff_dir_cb_arg *a = arg;
2361 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2362 return got_error(GOT_ERR_CANCELLED);
2364 if (got_path_cmp(parent_path, a->status_path,
2365 strlen(parent_path), a->status_path_len) != 0 &&
2366 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2369 if (parent_path[0]) {
2370 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2371 parent_path, de->d_name) == -1)
2372 return got_error_from_errno("asprintf");
2374 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2376 return got_error_from_errno("asprintf");
2379 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2385 static const struct got_error *
2386 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2388 struct diff_dir_cb_arg *a = arg;
2389 struct got_object_id blob_id, commit_id;
2390 unsigned char status;
2392 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2393 return got_error(GOT_ERR_CANCELLED);
2395 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2398 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2399 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2400 if (got_fileindex_entry_has_file_on_disk(ie))
2401 status = GOT_STATUS_MISSING;
2403 status = GOT_STATUS_DELETE;
2404 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2405 ie->path, &blob_id, NULL, &commit_id);
2409 free_ignorelist(struct got_pathlist_head *ignorelist)
2411 struct got_pathlist_entry *pe;
2413 TAILQ_FOREACH(pe, ignorelist, entry)
2414 free((char *)pe->path);
2415 got_pathlist_free(ignorelist);
2419 free_ignores(struct got_pathlist_head *ignores)
2421 struct got_pathlist_entry *pe;
2423 TAILQ_FOREACH(pe, ignores, entry) {
2424 struct got_pathlist_head *ignorelist = pe->data;
2425 free_ignorelist(ignorelist);
2426 free((char *)pe->path);
2428 got_pathlist_free(ignores);
2431 static const struct got_error *
2432 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2434 const struct got_error *err = NULL;
2435 struct got_pathlist_entry *pe = NULL;
2436 struct got_pathlist_head *ignorelist;
2437 char *line = NULL, *pattern, *dirpath = NULL;
2438 size_t linesize = 0;
2441 ignorelist = calloc(1, sizeof(*ignorelist));
2442 if (ignorelist == NULL)
2443 return got_error_from_errno("calloc");
2444 TAILQ_INIT(ignorelist);
2446 while ((linelen = getline(&line, &linesize, f)) != -1) {
2447 if (linelen > 0 && line[linelen - 1] == '\n')
2448 line[linelen - 1] = '\0';
2450 /* Git's ignores may contain comments. */
2454 /* Git's negated patterns are not (yet?) supported. */
2458 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2460 err = got_error_from_errno("asprintf");
2463 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2468 err = got_error_from_errno("getline");
2472 dirpath = strdup(path);
2473 if (dirpath == NULL) {
2474 err = got_error_from_errno("strdup");
2477 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2480 if (err || pe == NULL) {
2482 free_ignorelist(ignorelist);
2488 match_ignores(struct got_pathlist_head *ignores, const char *path)
2490 struct got_pathlist_entry *pe;
2492 /* Handle patterns which match in all directories. */
2493 TAILQ_FOREACH(pe, ignores, entry) {
2494 struct got_pathlist_head *ignorelist = pe->data;
2495 struct got_pathlist_entry *pi;
2497 TAILQ_FOREACH(pi, ignorelist, entry) {
2498 const char *p, *pattern = pi->path;
2500 if (strncmp(pattern, "**/", 3) != 0)
2505 if (fnmatch(pattern, p,
2506 FNM_PATHNAME | FNM_LEADING_DIR)) {
2507 /* Retry in next directory. */
2508 while (*p && *p != '/')
2520 * The ignores pathlist contains ignore lists from children before
2521 * parents, so we can find the most specific ignorelist by walking
2522 * ignores backwards.
2524 pe = TAILQ_LAST(ignores, got_pathlist_head);
2526 if (got_path_is_child(path, pe->path, pe->path_len)) {
2527 struct got_pathlist_head *ignorelist = pe->data;
2528 struct got_pathlist_entry *pi;
2529 TAILQ_FOREACH(pi, ignorelist, entry) {
2530 const char *pattern = pi->path;
2531 int flags = FNM_LEADING_DIR;
2532 if (strstr(pattern, "/**/") == NULL)
2533 flags |= FNM_PATHNAME;
2534 if (fnmatch(pattern, path, flags))
2539 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2545 static const struct got_error *
2546 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2547 const char *path, const char *ignores_filename)
2549 const struct got_error *err = NULL;
2551 FILE *ignoresfile = NULL;
2553 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2554 path[0] ? "/" : "", ignores_filename) == -1)
2555 return got_error_from_errno("asprintf");
2557 ignoresfile = fopen(ignorespath, "r");
2558 if (ignoresfile == NULL) {
2559 if (errno != ENOENT && errno != EACCES)
2560 err = got_error_from_errno2("fopen",
2563 err = read_ignores(ignores, path, ignoresfile);
2565 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2566 err = got_error_from_errno2("flose", path);
2571 static const struct got_error *
2572 status_new(void *arg, struct dirent *de, const char *parent_path)
2574 const struct got_error *err = NULL;
2575 struct diff_dir_cb_arg *a = arg;
2578 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2579 return got_error(GOT_ERR_CANCELLED);
2581 /* XXX ignore symlinks for now */
2582 if (de->d_type == DT_LNK)
2585 if (parent_path[0]) {
2586 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2587 return got_error_from_errno("asprintf");
2592 if (de->d_type == DT_DIR) {
2593 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2596 err = add_ignores(&a->ignores, a->worktree->root_path,
2597 path, ".gitignore");
2599 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2600 && !match_ignores(&a->ignores, path))
2601 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2602 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2608 static const struct got_error *
2609 report_single_file_status(const char *path, const char *ondisk_path,
2610 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2611 void *status_arg, struct got_repository *repo)
2613 struct got_fileindex_entry *ie;
2616 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2618 return report_file_status(ie, ondisk_path, status_cb,
2621 if (lstat(ondisk_path, &sb) == -1) {
2622 if (errno != ENOENT)
2623 return got_error_from_errno2("lstat", ondisk_path);
2624 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2625 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2629 if (S_ISREG(sb.st_mode))
2630 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2631 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2636 static const struct got_error *
2637 worktree_status(struct got_worktree *worktree, const char *path,
2638 struct got_fileindex *fileindex, struct got_repository *repo,
2639 got_worktree_status_cb status_cb, void *status_arg,
2640 got_cancel_cb cancel_cb, void *cancel_arg)
2642 const struct got_error *err = NULL;
2643 DIR *workdir = NULL;
2644 struct got_fileindex_diff_dir_cb fdiff_cb;
2645 struct diff_dir_cb_arg arg;
2646 char *ondisk_path = NULL;
2648 if (asprintf(&ondisk_path, "%s%s%s",
2649 worktree->root_path, path[0] ? "/" : "", path) == -1)
2650 return got_error_from_errno("asprintf");
2652 workdir = opendir(ondisk_path);
2653 if (workdir == NULL) {
2654 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2655 err = got_error_from_errno2("opendir", ondisk_path);
2657 err = report_single_file_status(path, ondisk_path,
2658 fileindex, status_cb, status_arg, repo);
2660 fdiff_cb.diff_old_new = status_old_new;
2661 fdiff_cb.diff_old = status_old;
2662 fdiff_cb.diff_new = status_new;
2663 arg.fileindex = fileindex;
2664 arg.worktree = worktree;
2665 arg.status_path = path;
2666 arg.status_path_len = strlen(path);
2668 arg.status_cb = status_cb;
2669 arg.status_arg = status_arg;
2670 arg.cancel_cb = cancel_cb;
2671 arg.cancel_arg = cancel_arg;
2672 TAILQ_INIT(&arg.ignores);
2673 err = add_ignores(&arg.ignores, worktree->root_path, path,
2676 err = add_ignores(&arg.ignores, worktree->root_path,
2677 path, ".gitignore");
2679 err = got_fileindex_diff_dir(fileindex, workdir,
2680 worktree->root_path, path, repo, &fdiff_cb, &arg);
2681 free_ignores(&arg.ignores);
2690 const struct got_error *
2691 got_worktree_status(struct got_worktree *worktree,
2692 struct got_pathlist_head *paths, struct got_repository *repo,
2693 got_worktree_status_cb status_cb, void *status_arg,
2694 got_cancel_cb cancel_cb, void *cancel_arg)
2696 const struct got_error *err = NULL;
2697 char *fileindex_path = NULL;
2698 struct got_fileindex *fileindex = NULL;
2699 struct got_pathlist_entry *pe;
2701 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2705 TAILQ_FOREACH(pe, paths, entry) {
2706 err = worktree_status(worktree, pe->path, fileindex, repo,
2707 status_cb, status_arg, cancel_cb, cancel_arg);
2711 free(fileindex_path);
2712 got_fileindex_free(fileindex);
2716 const struct got_error *
2717 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2720 const struct got_error *err = NULL;
2721 char *resolved, *cwd = NULL, *path = NULL;
2726 resolved = realpath(arg, NULL);
2727 if (resolved == NULL) {
2728 if (errno != ENOENT)
2729 return got_error_from_errno2("realpath", arg);
2730 cwd = getcwd(NULL, 0);
2732 return got_error_from_errno("getcwd");
2733 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2734 err = got_error_from_errno("asprintf");
2739 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2740 strlen(got_worktree_get_root_path(worktree)))) {
2741 err = got_error(GOT_ERR_BAD_PATH);
2745 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2746 err = got_path_skip_common_ancestor(&path,
2747 got_worktree_get_root_path(worktree), resolved);
2753 err = got_error_from_errno("strdup");
2758 /* XXX status walk can't deal with trailing slash! */
2760 while (len > 0 && path[len - 1] == '/') {
2761 path[len - 1] = '\0';
2774 static const struct got_error *
2775 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2776 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2777 struct got_repository *repo)
2779 const struct got_error *err = NULL;
2780 struct got_fileindex_entry *ie;
2781 unsigned char status;
2784 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2786 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2789 /* Re-adding an existing entry is a no-op. */
2790 if (status == GOT_STATUS_ADD)
2792 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2795 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2799 err = got_fileindex_entry_add(fileindex, ie);
2801 got_fileindex_entry_free(ie);
2805 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2808 const struct got_error *
2809 got_worktree_schedule_add(struct got_worktree *worktree,
2810 struct got_pathlist_head *paths,
2811 got_worktree_status_cb status_cb, void *status_arg,
2812 struct got_repository *repo)
2814 struct got_fileindex *fileindex = NULL;
2815 char *fileindex_path = NULL;
2816 const struct got_error *err = NULL, *sync_err, *unlockerr;
2817 struct got_pathlist_entry *pe;
2819 err = lock_worktree(worktree, LOCK_EX);
2823 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2827 TAILQ_FOREACH(pe, paths, entry) {
2829 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2831 return got_error_from_errno("asprintf");
2832 err = schedule_addition(ondisk_path, fileindex, pe->path,
2833 status_cb, status_arg, repo);
2838 sync_err = sync_fileindex(fileindex, fileindex_path);
2839 if (sync_err && err == NULL)
2842 free(fileindex_path);
2844 got_fileindex_free(fileindex);
2845 unlockerr = lock_worktree(worktree, LOCK_SH);
2846 if (unlockerr && err == NULL)
2851 static const struct got_error *
2852 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2853 const char *relpath, int delete_local_mods,
2854 got_worktree_status_cb status_cb, void *status_arg,
2855 struct got_repository *repo)
2857 const struct got_error *err = NULL;
2858 struct got_fileindex_entry *ie = NULL;
2859 unsigned char status, staged_status;
2862 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2864 return got_error(GOT_ERR_BAD_PATH);
2866 staged_status = get_staged_status(ie);
2867 if (staged_status != GOT_STATUS_NO_CHANGE) {
2868 if (staged_status == GOT_STATUS_DELETE)
2870 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2873 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2877 if (status != GOT_STATUS_NO_CHANGE) {
2878 if (status == GOT_STATUS_DELETE)
2880 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2881 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2882 if (status != GOT_STATUS_MODIFY &&
2883 status != GOT_STATUS_MISSING)
2884 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2887 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2888 return got_error_from_errno2("unlink", ondisk_path);
2890 got_fileindex_entry_mark_deleted_from_disk(ie);
2891 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2894 const struct got_error *
2895 got_worktree_schedule_delete(struct got_worktree *worktree,
2896 struct got_pathlist_head *paths, int delete_local_mods,
2897 got_worktree_status_cb status_cb, void *status_arg,
2898 struct got_repository *repo)
2900 struct got_fileindex *fileindex = NULL;
2901 char *fileindex_path = NULL;
2902 const struct got_error *err = NULL, *sync_err, *unlockerr;
2903 struct got_pathlist_entry *pe;
2905 err = lock_worktree(worktree, LOCK_EX);
2909 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2913 TAILQ_FOREACH(pe, paths, entry) {
2915 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2917 return got_error_from_errno("asprintf");
2918 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2919 delete_local_mods, status_cb, status_arg, repo);
2924 sync_err = sync_fileindex(fileindex, fileindex_path);
2925 if (sync_err && err == NULL)
2928 free(fileindex_path);
2930 got_fileindex_free(fileindex);
2931 unlockerr = lock_worktree(worktree, LOCK_SH);
2932 if (unlockerr && err == NULL)
2937 static const struct got_error *
2938 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2940 const struct got_error *err = NULL;
2942 size_t linesize = 0, n;
2945 linelen = getline(&line, &linesize, infile);
2946 if (linelen == -1) {
2947 if (ferror(infile)) {
2948 err = got_error_from_errno("getline");
2954 n = fwrite(line, 1, linelen, outfile);
2956 err = got_ferror(outfile, GOT_ERR_IO);
2961 n = fwrite(line, 1, linelen, rejectfile);
2963 err = got_ferror(outfile, GOT_ERR_IO);
2970 static const struct got_error *
2971 skip_one_line(FILE *f)
2974 size_t linesize = 0;
2977 linelen = getline(&line, &linesize, f);
2978 if (linelen == -1) {
2980 return got_error_from_errno("getline");
2987 static const struct got_error *
2988 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2989 int start_old, int end_old, int start_new, int end_new,
2990 FILE *outfile, FILE *rejectfile)
2992 const struct got_error *err;
2994 /* Copy old file's lines leading up to patch. */
2995 while (!feof(f1) && *line_cur1 < start_old) {
2996 err = copy_one_line(f1, outfile, NULL);
3001 /* Skip new file's lines leading up to patch. */
3002 while (!feof(f2) && *line_cur2 < start_new) {
3004 err = copy_one_line(f2, NULL, rejectfile);
3006 err = skip_one_line(f2);
3011 /* Copy patched lines. */
3012 while (!feof(f2) && *line_cur2 <= end_new) {
3013 err = copy_one_line(f2, outfile, NULL);
3018 /* Skip over old file's replaced lines. */
3019 while (!feof(f1) && *line_cur1 <= end_old) {
3021 err = copy_one_line(f1, NULL, rejectfile);
3023 err = skip_one_line(f1);
3032 static const struct got_error *
3033 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3034 FILE *outfile, FILE *rejectfile)
3036 const struct got_error *err;
3039 /* Copy old file's lines until EOF. */
3041 err = copy_one_line(f1, outfile, NULL);
3048 /* Copy new file's lines until EOF. */
3050 err = copy_one_line(f2, NULL, rejectfile);
3060 static const struct got_error *
3061 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3062 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3063 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3064 int *line_cur2, FILE *outfile, FILE *rejectfile,
3065 got_worktree_patch_cb patch_cb, void *patch_arg)
3067 const struct got_error *err = NULL;
3068 int start_old = change->cv.a;
3069 int end_old = change->cv.b;
3070 int start_new = change->cv.c;
3071 int end_new = change->cv.d;
3075 *choice = GOT_PATCH_CHOICE_NONE;
3077 hunkfile = got_opentemp();
3078 if (hunkfile == NULL)
3079 return got_error_from_errno("got_opentemp");
3084 /* XXX TODO needs error checking */
3085 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3087 if (fseek(f1, pos1, SEEK_SET) == -1) {
3088 err = got_ferror(f1, GOT_ERR_IO);
3091 if (fseek(f2, pos2, SEEK_SET) == -1) {
3092 err = got_ferror(f1, GOT_ERR_IO);
3095 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3096 err = got_ferror(hunkfile, GOT_ERR_IO);
3100 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3101 hunkfile, n, nchanges);
3106 case GOT_PATCH_CHOICE_YES:
3107 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3108 end_old, start_new, end_new, outfile, rejectfile);
3110 case GOT_PATCH_CHOICE_NO:
3111 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3112 end_old, start_new, end_new, rejectfile, outfile);
3114 case GOT_PATCH_CHOICE_QUIT:
3117 err = got_error(GOT_ERR_PATCH_CHOICE);
3121 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3122 err = got_error_from_errno("fclose");
3126 struct revert_file_args {
3127 struct got_worktree *worktree;
3128 struct got_fileindex *fileindex;
3129 got_worktree_checkout_cb progress_cb;
3131 got_worktree_patch_cb patch_cb;
3133 struct got_repository *repo;
3136 static const struct got_error *
3137 create_patched_content(char **path_outfile, int reverse_patch,
3138 struct got_object_id *blob_id, const char *path2,
3139 const char *relpath, struct got_repository *repo,
3140 got_worktree_patch_cb patch_cb, void *patch_arg)
3142 const struct got_error *err;
3143 struct got_blob_object *blob = NULL;
3144 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3146 char *path1 = NULL, *id_str = NULL;
3147 struct stat sb1, sb2;
3148 struct got_diff_changes *changes = NULL;
3149 struct got_diff_state *ds = NULL;
3150 struct got_diff_args *args = NULL;
3151 struct got_diff_change *change;
3152 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3155 *path_outfile = NULL;
3157 err = got_object_id_str(&id_str, blob_id);
3161 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3163 err = got_error_from_errno2("open", path2);
3166 if (fstat(fd2, &sb2) == -1) {
3167 err = got_error_from_errno2("fstat", path2);
3171 f2 = fdopen(fd2, "r");
3173 err = got_error_from_errno2("fopen", path2);
3178 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3182 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3186 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3190 if (stat(path1, &sb1) == -1) {
3191 err = got_error_from_errno2("stat", path1);
3195 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3196 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3200 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3204 if (fseek(f1, 0L, SEEK_SET) == -1)
3205 return got_ferror(f1, GOT_ERR_IO);
3206 if (fseek(f2, 0L, SEEK_SET) == -1)
3207 return got_ferror(f2, GOT_ERR_IO);
3208 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3210 err = apply_or_reject_change(&choice, change, ++n,
3211 changes->nchanges, ds, args, diff_flags, relpath,
3212 f1, f2, &line_cur1, &line_cur2,
3213 reverse_patch ? NULL : outfile,
3214 reverse_patch ? outfile : NULL,
3215 patch_cb, patch_arg);
3218 if (choice == GOT_PATCH_CHOICE_YES)
3220 else if (choice == GOT_PATCH_CHOICE_QUIT)
3224 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3225 reverse_patch ? NULL : outfile,
3226 reverse_patch ? outfile : NULL);
3230 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3231 err = got_error_from_errno2("chmod", path2);
3238 got_object_blob_close(blob);
3239 if (f1 && fclose(f1) == EOF && err == NULL)
3240 err = got_error_from_errno2("fclose", path1);
3241 if (f2 && fclose(f2) == EOF && err == NULL)
3242 err = got_error_from_errno2("fclose", path2);
3243 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3244 err = got_error_from_errno2("close", path2);
3245 if (outfile && fclose(outfile) == EOF && err == NULL)
3246 err = got_error_from_errno2("fclose", *path_outfile);
3247 if (path1 && unlink(path1) == -1 && err == NULL)
3248 err = got_error_from_errno2("unlink", path1);
3249 if (err || !have_content) {
3250 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3251 err = got_error_from_errno2("unlink", *path_outfile);
3252 free(*path_outfile);
3253 *path_outfile = NULL;
3257 got_diff_state_free(ds);
3261 got_diff_free_changes(changes);
3266 static const struct got_error *
3267 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3268 const char *relpath, struct got_object_id *blob_id,
3269 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3271 struct revert_file_args *a = arg;
3272 const struct got_error *err = NULL;
3273 char *parent_path = NULL;
3274 struct got_fileindex_entry *ie;
3275 struct got_tree_object *tree = NULL;
3276 struct got_object_id *tree_id = NULL;
3277 const struct got_tree_entry *te = NULL;
3278 char *tree_path = NULL, *te_name;
3279 char *ondisk_path = NULL, *path_content = NULL;
3280 struct got_blob_object *blob = NULL;
3282 /* Reverting a staged deletion is a no-op. */
3283 if (status == GOT_STATUS_DELETE &&
3284 staged_status != GOT_STATUS_NO_CHANGE)
3287 if (status == GOT_STATUS_UNVERSIONED)
3288 return (*a->progress_cb)(a->progress_arg,
3289 GOT_STATUS_UNVERSIONED, relpath);
3291 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3293 return got_error(GOT_ERR_BAD_PATH);
3295 /* Construct in-repository path of tree which contains this blob. */
3296 err = got_path_dirname(&parent_path, ie->path);
3298 if (err->code != GOT_ERR_BAD_PATH)
3300 parent_path = strdup("/");
3301 if (parent_path == NULL) {
3302 err = got_error_from_errno("strdup");
3306 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3307 tree_path = strdup(parent_path);
3308 if (tree_path == NULL) {
3309 err = got_error_from_errno("strdup");
3313 if (got_path_is_root_dir(parent_path)) {
3314 tree_path = strdup(a->worktree->path_prefix);
3315 if (tree_path == NULL) {
3316 err = got_error_from_errno("strdup");
3320 if (asprintf(&tree_path, "%s/%s",
3321 a->worktree->path_prefix, parent_path) == -1) {
3322 err = got_error_from_errno("asprintf");
3328 err = got_object_id_by_path(&tree_id, a->repo,
3329 a->worktree->base_commit_id, tree_path);
3331 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3332 (status == GOT_STATUS_ADD ||
3333 staged_status == GOT_STATUS_ADD)))
3336 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3340 te_name = basename(ie->path);
3341 if (te_name == NULL) {
3342 err = got_error_from_errno2("basename", ie->path);
3346 te = got_object_tree_find_entry(tree, te_name);
3347 if (te == NULL && status != GOT_STATUS_ADD &&
3348 staged_status != GOT_STATUS_ADD) {
3349 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3355 case GOT_STATUS_ADD:
3357 int choice = GOT_PATCH_CHOICE_NONE;
3358 err = (*a->patch_cb)(&choice, a->patch_arg,
3359 status, ie->path, NULL, 1, 1);
3362 if (choice != GOT_PATCH_CHOICE_YES)
3365 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3369 got_fileindex_entry_remove(a->fileindex, ie);
3371 case GOT_STATUS_DELETE:
3373 int choice = GOT_PATCH_CHOICE_NONE;
3374 err = (*a->patch_cb)(&choice, a->patch_arg,
3375 status, ie->path, NULL, 1, 1);
3378 if (choice != GOT_PATCH_CHOICE_YES)
3382 case GOT_STATUS_MODIFY:
3383 case GOT_STATUS_MODE_CHANGE:
3384 case GOT_STATUS_CONFLICT:
3385 case GOT_STATUS_MISSING: {
3386 struct got_object_id id;
3387 if (staged_status == GOT_STATUS_ADD ||
3388 staged_status == GOT_STATUS_MODIFY) {
3389 memcpy(id.sha1, ie->staged_blob_sha1,
3390 SHA1_DIGEST_LENGTH);
3392 memcpy(id.sha1, ie->blob_sha1,
3393 SHA1_DIGEST_LENGTH);
3394 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3398 if (asprintf(&ondisk_path, "%s/%s",
3399 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3400 err = got_error_from_errno("asprintf");
3404 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3405 status == GOT_STATUS_CONFLICT)) {
3406 err = create_patched_content(&path_content, 1, &id,
3407 ondisk_path, ie->path, a->repo,
3408 a->patch_cb, a->patch_arg);
3409 if (err || path_content == NULL)
3411 if (rename(path_content, ondisk_path) == -1) {
3412 err = got_error_from_errno3("rename",
3413 path_content, ondisk_path);
3417 err = install_blob(a->worktree, ondisk_path, ie->path,
3418 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3419 got_fileindex_perms_to_st(ie), blob, 0, 1,
3420 a->repo, a->progress_cb, a->progress_arg);
3423 if (status == GOT_STATUS_DELETE ||
3424 status == GOT_STATUS_MODE_CHANGE) {
3425 err = update_blob_fileindex_entry(a->worktree,
3426 a->fileindex, ie, ondisk_path, ie->path,
3443 got_object_blob_close(blob);
3445 got_object_tree_close(tree);
3450 const struct got_error *
3451 got_worktree_revert(struct got_worktree *worktree,
3452 struct got_pathlist_head *paths,
3453 got_worktree_checkout_cb progress_cb, void *progress_arg,
3454 got_worktree_patch_cb patch_cb, void *patch_arg,
3455 struct got_repository *repo)
3457 struct got_fileindex *fileindex = NULL;
3458 char *fileindex_path = NULL;
3459 const struct got_error *err = NULL, *unlockerr = NULL;
3460 const struct got_error *sync_err = NULL;
3461 struct got_pathlist_entry *pe;
3462 struct revert_file_args rfa;
3464 err = lock_worktree(worktree, LOCK_EX);
3468 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3472 rfa.worktree = worktree;
3473 rfa.fileindex = fileindex;
3474 rfa.progress_cb = progress_cb;
3475 rfa.progress_arg = progress_arg;
3476 rfa.patch_cb = patch_cb;
3477 rfa.patch_arg = patch_arg;
3479 TAILQ_FOREACH(pe, paths, entry) {
3480 err = worktree_status(worktree, pe->path, fileindex, repo,
3481 revert_file, &rfa, NULL, NULL);
3485 sync_err = sync_fileindex(fileindex, fileindex_path);
3486 if (sync_err && err == NULL)
3489 free(fileindex_path);
3491 got_fileindex_free(fileindex);
3492 unlockerr = lock_worktree(worktree, LOCK_SH);
3493 if (unlockerr && err == NULL)
3499 free_commitable(struct got_commitable *ct)
3502 free(ct->in_repo_path);
3503 free(ct->ondisk_path);
3505 free(ct->base_blob_id);
3506 free(ct->staged_blob_id);
3507 free(ct->base_commit_id);
3511 struct collect_commitables_arg {
3512 struct got_pathlist_head *commitable_paths;
3513 struct got_repository *repo;
3514 struct got_worktree *worktree;
3515 int have_staged_files;
3518 static const struct got_error *
3519 collect_commitables(void *arg, unsigned char status,
3520 unsigned char staged_status, const char *relpath,
3521 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3522 struct got_object_id *commit_id)
3524 struct collect_commitables_arg *a = arg;
3525 const struct got_error *err = NULL;
3526 struct got_commitable *ct = NULL;
3527 struct got_pathlist_entry *new = NULL;
3528 char *parent_path = NULL, *path = NULL;
3531 if (a->have_staged_files) {
3532 if (staged_status != GOT_STATUS_MODIFY &&
3533 staged_status != GOT_STATUS_ADD &&
3534 staged_status != GOT_STATUS_DELETE)
3537 if (status == GOT_STATUS_CONFLICT)
3538 return got_error(GOT_ERR_COMMIT_CONFLICT);
3540 if (status != GOT_STATUS_MODIFY &&
3541 status != GOT_STATUS_MODE_CHANGE &&
3542 status != GOT_STATUS_ADD &&
3543 status != GOT_STATUS_DELETE)
3547 if (asprintf(&path, "/%s", relpath) == -1) {
3548 err = got_error_from_errno("asprintf");
3551 if (strcmp(path, "/") == 0) {
3552 parent_path = strdup("");
3553 if (parent_path == NULL)
3554 return got_error_from_errno("strdup");
3556 err = got_path_dirname(&parent_path, path);
3561 ct = calloc(1, sizeof(*ct));
3563 err = got_error_from_errno("calloc");
3567 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3569 err = got_error_from_errno("asprintf");
3572 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3573 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3575 if (lstat(ct->ondisk_path, &sb) != 0) {
3576 err = got_error_from_errno2("lstat", ct->ondisk_path);
3579 ct->mode = sb.st_mode;
3582 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3583 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3585 err = got_error_from_errno("asprintf");
3589 ct->status = status;
3590 ct->staged_status = staged_status;
3591 ct->blob_id = NULL; /* will be filled in when blob gets created */
3592 if (ct->status != GOT_STATUS_ADD &&
3593 ct->staged_status != GOT_STATUS_ADD) {
3594 ct->base_blob_id = got_object_id_dup(blob_id);
3595 if (ct->base_blob_id == NULL) {
3596 err = got_error_from_errno("got_object_id_dup");
3599 ct->base_commit_id = got_object_id_dup(commit_id);
3600 if (ct->base_commit_id == NULL) {
3601 err = got_error_from_errno("got_object_id_dup");
3605 if (ct->staged_status == GOT_STATUS_ADD ||
3606 ct->staged_status == GOT_STATUS_MODIFY) {
3607 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3608 if (ct->staged_blob_id == NULL) {
3609 err = got_error_from_errno("got_object_id_dup");
3613 ct->path = strdup(path);
3614 if (ct->path == NULL) {
3615 err = got_error_from_errno("strdup");
3618 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3620 if (ct && (err || new == NULL))
3621 free_commitable(ct);
3627 static const struct got_error *write_tree(struct got_object_id **,
3628 struct got_tree_object *, const char *, struct got_pathlist_head *,
3629 got_worktree_status_cb status_cb, void *status_arg,
3630 struct got_repository *);
3632 static const struct got_error *
3633 write_subtree(struct got_object_id **new_subtree_id,
3634 struct got_tree_entry *te, const char *parent_path,
3635 struct got_pathlist_head *commitable_paths,
3636 got_worktree_status_cb status_cb, void *status_arg,
3637 struct got_repository *repo)
3639 const struct got_error *err = NULL;
3640 struct got_tree_object *subtree;
3643 if (asprintf(&subpath, "%s%s%s", parent_path,
3644 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3645 return got_error_from_errno("asprintf");
3647 err = got_object_open_as_tree(&subtree, repo, te->id);
3651 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3652 status_cb, status_arg, repo);
3653 got_object_tree_close(subtree);
3658 static const struct got_error *
3659 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3661 const struct got_error *err = NULL;
3662 char *ct_parent_path = NULL;
3666 if (strchr(ct->in_repo_path, '/') == NULL) {
3667 *match = got_path_is_root_dir(path);
3671 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3674 *match = (strcmp(path, ct_parent_path) == 0);
3675 free(ct_parent_path);
3680 get_ct_file_mode(struct got_commitable *ct)
3682 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3685 static const struct got_error *
3686 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3687 struct got_tree_entry *te, struct got_commitable *ct)
3689 const struct got_error *err = NULL;
3693 err = got_object_tree_entry_dup(new_te, te);
3697 (*new_te)->mode = get_ct_file_mode(ct);
3699 free((*new_te)->id);
3700 if (ct->staged_status == GOT_STATUS_MODIFY)
3701 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3703 (*new_te)->id = got_object_id_dup(ct->blob_id);
3704 if ((*new_te)->id == NULL) {
3705 err = got_error_from_errno("got_object_id_dup");
3709 if (err && *new_te) {
3710 got_object_tree_entry_close(*new_te);
3716 static const struct got_error *
3717 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3718 struct got_commitable *ct)
3720 const struct got_error *err = NULL;
3725 *new_te = calloc(1, sizeof(**new_te));
3726 if (*new_te == NULL)
3727 return got_error_from_errno("calloc");
3729 ct_name = basename(ct->path);
3730 if (ct_name == NULL) {
3731 err = got_error_from_errno2("basename", ct->path);
3734 (*new_te)->name = strdup(ct_name);
3735 if ((*new_te)->name == NULL) {
3736 err = got_error_from_errno("strdup");
3740 (*new_te)->mode = get_ct_file_mode(ct);
3742 if (ct->staged_status == GOT_STATUS_ADD)
3743 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3745 (*new_te)->id = got_object_id_dup(ct->blob_id);
3746 if ((*new_te)->id == NULL) {
3747 err = got_error_from_errno("got_object_id_dup");
3751 if (err && *new_te) {
3752 got_object_tree_entry_close(*new_te);
3758 static const struct got_error *
3759 insert_tree_entry(struct got_tree_entry *new_te,
3760 struct got_pathlist_head *paths)
3762 const struct got_error *err = NULL;
3763 struct got_pathlist_entry *new_pe;
3765 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3769 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3773 static const struct got_error *
3774 report_ct_status(struct got_commitable *ct,
3775 got_worktree_status_cb status_cb, void *status_arg)
3777 const char *ct_path = ct->path;
3778 unsigned char status;
3780 while (ct_path[0] == '/')
3783 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3784 status = ct->staged_status;
3786 status = ct->status;
3788 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3789 ct_path, ct->blob_id, NULL, NULL);
3792 static const struct got_error *
3793 match_modified_subtree(int *modified, struct got_tree_entry *te,
3794 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3796 const struct got_error *err = NULL;
3797 struct got_pathlist_entry *pe;
3802 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3803 got_path_is_root_dir(base_tree_path) ? "" : "/",
3805 return got_error_from_errno("asprintf");
3807 TAILQ_FOREACH(pe, commitable_paths, entry) {
3808 struct got_commitable *ct = pe->data;
3809 *modified = got_path_is_child(ct->in_repo_path, te_path,
3819 static const struct got_error *
3820 match_deleted_or_modified_ct(struct got_commitable **ctp,
3821 struct got_tree_entry *te, const char *base_tree_path,
3822 struct got_pathlist_head *commitable_paths)
3824 const struct got_error *err = NULL;
3825 struct got_pathlist_entry *pe;
3829 TAILQ_FOREACH(pe, commitable_paths, entry) {
3830 struct got_commitable *ct = pe->data;
3831 char *ct_name = NULL;
3834 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3835 if (ct->status != GOT_STATUS_MODIFY &&
3836 ct->status != GOT_STATUS_MODE_CHANGE &&
3837 ct->status != GOT_STATUS_DELETE)
3840 if (ct->staged_status != GOT_STATUS_MODIFY &&
3841 ct->staged_status != GOT_STATUS_DELETE)
3845 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3848 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3854 ct_name = basename(pe->path);
3855 if (ct_name == NULL)
3856 return got_error_from_errno2("basename", pe->path);
3858 if (strcmp(te->name, ct_name) != 0)
3868 static const struct got_error *
3869 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3870 const char *child_path, const char *path_base_tree,
3871 struct got_pathlist_head *commitable_paths,
3872 got_worktree_status_cb status_cb, void *status_arg,
3873 struct got_repository *repo)
3875 const struct got_error *err = NULL;
3876 struct got_tree_entry *new_te;
3881 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3882 got_path_is_root_dir(path_base_tree) ? "" : "/",
3884 return got_error_from_errno("asprintf");
3886 new_te = calloc(1, sizeof(*new_te));
3888 return got_error_from_errno("calloc");
3889 new_te->mode = S_IFDIR;
3890 new_te->name = strdup(child_path);
3891 if (new_te->name == NULL) {
3892 err = got_error_from_errno("strdup");
3893 got_object_tree_entry_close(new_te);
3896 err = write_tree(&new_te->id, NULL, subtree_path,
3897 commitable_paths, status_cb, status_arg, repo);
3899 got_object_tree_entry_close(new_te);
3909 static const struct got_error *
3910 write_tree(struct got_object_id **new_tree_id,
3911 struct got_tree_object *base_tree, const char *path_base_tree,
3912 struct got_pathlist_head *commitable_paths,
3913 got_worktree_status_cb status_cb, void *status_arg,
3914 struct got_repository *repo)
3916 const struct got_error *err = NULL;
3917 const struct got_tree_entries *base_entries = NULL;
3918 struct got_pathlist_head paths;
3919 struct got_tree_entries new_tree_entries;
3920 struct got_tree_entry *te, *new_te = NULL;
3921 struct got_pathlist_entry *pe;
3924 new_tree_entries.nentries = 0;
3925 SIMPLEQ_INIT(&new_tree_entries.head);
3927 /* Insert, and recurse into, newly added entries first. */
3928 TAILQ_FOREACH(pe, commitable_paths, entry) {
3929 struct got_commitable *ct = pe->data;
3930 char *child_path = NULL, *slash;
3932 if ((ct->status != GOT_STATUS_ADD &&
3933 ct->staged_status != GOT_STATUS_ADD) ||
3934 (ct->flags & GOT_COMMITABLE_ADDED))
3937 if (!got_path_is_child(pe->path, path_base_tree,
3938 strlen(path_base_tree)))
3941 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3946 slash = strchr(child_path, '/');
3947 if (slash == NULL) {
3948 err = alloc_added_blob_tree_entry(&new_te, ct);
3951 err = report_ct_status(ct, status_cb, status_arg);
3954 ct->flags |= GOT_COMMITABLE_ADDED;
3955 err = insert_tree_entry(new_te, &paths);
3959 *slash = '\0'; /* trim trailing path components */
3960 if (base_tree == NULL ||
3961 got_object_tree_find_entry(base_tree, child_path)
3963 err = make_subtree_for_added_blob(&new_te,
3964 child_path, path_base_tree,
3965 commitable_paths, status_cb, status_arg,
3969 err = insert_tree_entry(new_te, &paths);
3977 /* Handle modified and deleted entries. */
3978 base_entries = got_object_tree_get_entries(base_tree);
3979 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3980 struct got_commitable *ct = NULL;
3982 if (got_object_tree_entry_is_submodule(te)) {
3983 /* Entry is a submodule; just copy it. */
3984 err = got_object_tree_entry_dup(&new_te, te);
3987 err = insert_tree_entry(new_te, &paths);
3993 if (S_ISDIR(te->mode)) {
3995 err = got_object_tree_entry_dup(&new_te, te);
3998 err = match_modified_subtree(&modified, te,
3999 path_base_tree, commitable_paths);
4002 /* Avoid recursion into unmodified subtrees. */
4005 err = write_subtree(&new_te->id, te,
4006 path_base_tree, commitable_paths,
4007 status_cb, status_arg, repo);
4011 err = insert_tree_entry(new_te, &paths);
4017 err = match_deleted_or_modified_ct(&ct, te,
4018 path_base_tree, commitable_paths);
4022 /* NB: Deleted entries get dropped here. */
4023 if (ct->status == GOT_STATUS_MODIFY ||
4024 ct->status == GOT_STATUS_MODE_CHANGE ||
4025 ct->staged_status == GOT_STATUS_MODIFY) {
4026 err = alloc_modified_blob_tree_entry(
4030 err = insert_tree_entry(new_te, &paths);
4034 err = report_ct_status(ct, status_cb,
4039 /* Entry is unchanged; just copy it. */
4040 err = got_object_tree_entry_dup(&new_te, te);
4043 err = insert_tree_entry(new_te, &paths);
4050 /* Write new list of entries; deleted entries have been dropped. */
4051 TAILQ_FOREACH(pe, &paths, entry) {
4052 struct got_tree_entry *te = pe->data;
4053 new_tree_entries.nentries++;
4054 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
4056 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
4058 got_object_tree_entries_close(&new_tree_entries);
4059 got_pathlist_free(&paths);
4063 static const struct got_error *
4064 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4065 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4066 int have_staged_files)
4068 const struct got_error *err = NULL;
4069 struct got_pathlist_entry *pe;
4071 TAILQ_FOREACH(pe, commitable_paths, entry) {
4072 struct got_fileindex_entry *ie;
4073 struct got_commitable *ct = pe->data;
4075 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4077 if (ct->status == GOT_STATUS_DELETE ||
4078 ct->staged_status == GOT_STATUS_DELETE) {
4079 got_fileindex_entry_remove(fileindex, ie);
4080 got_fileindex_entry_free(ie);
4081 } else if (ct->staged_status == GOT_STATUS_ADD ||
4082 ct->staged_status == GOT_STATUS_MODIFY) {
4083 got_fileindex_entry_stage_set(ie,
4084 GOT_FILEIDX_STAGE_NONE);
4085 err = got_fileindex_entry_update(ie,
4086 ct->ondisk_path, ct->staged_blob_id->sha1,
4087 new_base_commit_id->sha1,
4088 !have_staged_files);
4090 err = got_fileindex_entry_update(ie,
4091 ct->ondisk_path, ct->blob_id->sha1,
4092 new_base_commit_id->sha1,
4093 !have_staged_files);
4095 err = got_fileindex_entry_alloc(&ie,
4096 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4097 new_base_commit_id->sha1);
4100 err = got_fileindex_entry_add(fileindex, ie);
4109 static const struct got_error *
4110 check_out_of_date(const char *in_repo_path, unsigned char status,
4111 unsigned char staged_status, struct got_object_id *base_blob_id,
4112 struct got_object_id *base_commit_id,
4113 struct got_object_id *head_commit_id, struct got_repository *repo,
4116 const struct got_error *err = NULL;
4117 struct got_object_id *id = NULL;
4119 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4120 /* Trivial case: base commit == head commit */
4121 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4124 * Ensure file content which local changes were based
4125 * on matches file content in the branch head.
4127 err = got_object_id_by_path(&id, repo, head_commit_id,
4130 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4131 err = got_error(ood_errcode);
4133 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4134 err = got_error(ood_errcode);
4136 /* Require that added files don't exist in the branch head. */
4137 err = got_object_id_by_path(&id, repo, head_commit_id,
4139 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4141 err = id ? got_error(ood_errcode) : NULL;
4148 const struct got_error *
4149 commit_worktree(struct got_object_id **new_commit_id,
4150 struct got_pathlist_head *commitable_paths,
4151 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4152 const char *author, const char *committer,
4153 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4154 got_worktree_status_cb status_cb, void *status_arg,
4155 struct got_repository *repo)
4157 const struct got_error *err = NULL, *unlockerr = NULL;
4158 struct got_pathlist_entry *pe;
4159 const char *head_ref_name = NULL;
4160 struct got_commit_object *head_commit = NULL;
4161 struct got_reference *head_ref2 = NULL;
4162 struct got_object_id *head_commit_id2 = NULL;
4163 struct got_tree_object *head_tree = NULL;
4164 struct got_object_id *new_tree_id = NULL;
4165 struct got_object_id_queue parent_ids;
4166 struct got_object_qid *pid = NULL;
4167 char *logmsg = NULL;
4169 *new_commit_id = NULL;
4171 SIMPLEQ_INIT(&parent_ids);
4173 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4177 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4181 if (commit_msg_cb != NULL) {
4182 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4187 if (logmsg == NULL || strlen(logmsg) == 0) {
4188 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4192 /* Create blobs from added and modified files and record their IDs. */
4193 TAILQ_FOREACH(pe, commitable_paths, entry) {
4194 struct got_commitable *ct = pe->data;
4197 /* Blobs for staged files already exist. */
4198 if (ct->staged_status == GOT_STATUS_ADD ||
4199 ct->staged_status == GOT_STATUS_MODIFY)
4202 if (ct->status != GOT_STATUS_ADD &&
4203 ct->status != GOT_STATUS_MODIFY &&
4204 ct->status != GOT_STATUS_MODE_CHANGE)
4207 if (asprintf(&ondisk_path, "%s/%s",
4208 worktree->root_path, pe->path) == -1) {
4209 err = got_error_from_errno("asprintf");
4212 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4218 /* Recursively write new tree objects. */
4219 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4220 status_cb, status_arg, repo);
4224 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4227 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4228 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4229 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4230 got_object_qid_free(pid);
4236 /* Check if a concurrent commit to our branch has occurred. */
4237 head_ref_name = got_worktree_get_head_ref_name(worktree);
4238 if (head_ref_name == NULL) {
4239 err = got_error_from_errno("got_worktree_get_head_ref_name");
4242 /* Lock the reference here to prevent concurrent modification. */
4243 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4246 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4249 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4250 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4253 /* Update branch head in repository. */
4254 err = got_ref_change_ref(head_ref2, *new_commit_id);
4257 err = got_ref_write(head_ref2, repo);
4261 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4265 err = ref_base_commit(worktree, repo);
4270 got_object_tree_close(head_tree);
4272 got_object_commit_close(head_commit);
4273 free(head_commit_id2);
4275 unlockerr = got_ref_unlock(head_ref2);
4276 if (unlockerr && err == NULL)
4278 got_ref_close(head_ref2);
4283 static const struct got_error *
4284 check_path_is_commitable(const char *path,
4285 struct got_pathlist_head *commitable_paths)
4287 struct got_pathlist_entry *cpe = NULL;
4288 size_t path_len = strlen(path);
4290 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4291 struct got_commitable *ct = cpe->data;
4292 const char *ct_path = ct->path;
4294 while (ct_path[0] == '/')
4297 if (strcmp(path, ct_path) == 0 ||
4298 got_path_is_child(ct_path, path, path_len))
4303 return got_error_path(path, GOT_ERR_BAD_PATH);
4308 static const struct got_error *
4309 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4311 int *have_staged_files = arg;
4313 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4314 *have_staged_files = 1;
4315 return got_error(GOT_ERR_CANCELLED);
4321 static const struct got_error *
4322 check_non_staged_files(struct got_fileindex *fileindex,
4323 struct got_pathlist_head *paths)
4325 struct got_pathlist_entry *pe;
4326 struct got_fileindex_entry *ie;
4328 TAILQ_FOREACH(pe, paths, entry) {
4329 if (pe->path[0] == '\0')
4331 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4333 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4334 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4335 return got_error_path(pe->path,
4336 GOT_ERR_FILE_NOT_STAGED);
4342 const struct got_error *
4343 got_worktree_commit(struct got_object_id **new_commit_id,
4344 struct got_worktree *worktree, struct got_pathlist_head *paths,
4345 const char *author, const char *committer,
4346 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4347 got_worktree_status_cb status_cb, void *status_arg,
4348 struct got_repository *repo)
4350 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4351 struct got_fileindex *fileindex = NULL;
4352 char *fileindex_path = NULL;
4353 struct got_pathlist_head commitable_paths;
4354 struct collect_commitables_arg cc_arg;
4355 struct got_pathlist_entry *pe;
4356 struct got_reference *head_ref = NULL;
4357 struct got_object_id *head_commit_id = NULL;
4358 int have_staged_files = 0;
4360 *new_commit_id = NULL;
4362 TAILQ_INIT(&commitable_paths);
4364 err = lock_worktree(worktree, LOCK_EX);
4368 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4372 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4376 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4380 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4381 &have_staged_files);
4382 if (err && err->code != GOT_ERR_CANCELLED)
4384 if (have_staged_files) {
4385 err = check_non_staged_files(fileindex, paths);
4390 cc_arg.commitable_paths = &commitable_paths;
4391 cc_arg.worktree = worktree;
4393 cc_arg.have_staged_files = have_staged_files;
4394 TAILQ_FOREACH(pe, paths, entry) {
4395 err = worktree_status(worktree, pe->path, fileindex, repo,
4396 collect_commitables, &cc_arg, NULL, NULL);
4401 if (TAILQ_EMPTY(&commitable_paths)) {
4402 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4406 TAILQ_FOREACH(pe, paths, entry) {
4407 err = check_path_is_commitable(pe->path, &commitable_paths);
4412 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4413 struct got_commitable *ct = pe->data;
4414 const char *ct_path = ct->in_repo_path;
4416 while (ct_path[0] == '/')
4418 err = check_out_of_date(ct_path, ct->status,
4419 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4420 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4426 err = commit_worktree(new_commit_id, &commitable_paths,
4427 head_commit_id, worktree, author, committer,
4428 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4432 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4433 fileindex, have_staged_files);
4434 sync_err = sync_fileindex(fileindex, fileindex_path);
4435 if (sync_err && err == NULL)
4439 got_fileindex_free(fileindex);
4440 free(fileindex_path);
4441 unlockerr = lock_worktree(worktree, LOCK_SH);
4442 if (unlockerr && err == NULL)
4444 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4445 struct got_commitable *ct = pe->data;
4446 free_commitable(ct);
4448 got_pathlist_free(&commitable_paths);
4453 got_commitable_get_path(struct got_commitable *ct)
4459 got_commitable_get_status(struct got_commitable *ct)
4464 struct check_rebase_ok_arg {
4465 struct got_worktree *worktree;
4466 struct got_repository *repo;
4469 static const struct got_error *
4470 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4472 const struct got_error *err = NULL;
4473 struct check_rebase_ok_arg *a = arg;
4474 unsigned char status;
4478 /* Reject rebase of a work tree with mixed base commits. */
4479 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4480 SHA1_DIGEST_LENGTH))
4481 return got_error(GOT_ERR_MIXED_COMMITS);
4483 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4485 return got_error_from_errno("asprintf");
4487 /* Reject rebase of a work tree with modified or staged files. */
4488 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4493 if (status != GOT_STATUS_NO_CHANGE)
4494 return got_error(GOT_ERR_MODIFIED);
4495 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4496 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4501 const struct got_error *
4502 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4503 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4504 struct got_worktree *worktree, struct got_reference *branch,
4505 struct got_repository *repo)
4507 const struct got_error *err = NULL;
4508 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4509 char *branch_ref_name = NULL;
4510 char *fileindex_path = NULL;
4511 struct check_rebase_ok_arg ok_arg;
4512 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4514 *new_base_branch_ref = NULL;
4518 err = lock_worktree(worktree, LOCK_EX);
4522 err = open_fileindex(fileindex, &fileindex_path, worktree);
4526 ok_arg.worktree = worktree;
4528 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4533 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4537 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4541 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4545 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4550 err = got_ref_alloc_symref(new_base_branch_ref,
4551 new_base_branch_ref_name, wt_branch);
4554 err = got_ref_write(*new_base_branch_ref, repo);
4558 /* TODO Lock original branch's ref while rebasing? */
4560 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4564 err = got_ref_write(branch_ref, repo);
4568 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4569 worktree->base_commit_id);
4572 err = got_ref_write(*tmp_branch, repo);
4576 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4580 free(fileindex_path);
4581 free(tmp_branch_name);
4582 free(new_base_branch_ref_name);
4583 free(branch_ref_name);
4585 got_ref_close(branch_ref);
4587 got_ref_close(wt_branch);
4589 if (*new_base_branch_ref) {
4590 got_ref_close(*new_base_branch_ref);
4591 *new_base_branch_ref = NULL;
4594 got_ref_close(*tmp_branch);
4598 got_fileindex_free(*fileindex);
4601 lock_worktree(worktree, LOCK_SH);
4606 const struct got_error *
4607 got_worktree_rebase_continue(struct got_object_id **commit_id,
4608 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4609 struct got_reference **branch, struct got_fileindex **fileindex,
4610 struct got_worktree *worktree, struct got_repository *repo)
4612 const struct got_error *err;
4613 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4614 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4615 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4616 char *fileindex_path = NULL;
4617 int have_staged_files = 0;
4620 *new_base_branch = NULL;
4625 err = lock_worktree(worktree, LOCK_EX);
4629 err = open_fileindex(fileindex, &fileindex_path, worktree);
4633 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4634 &have_staged_files);
4635 if (err && err->code != GOT_ERR_CANCELLED)
4637 if (have_staged_files) {
4638 err = got_error(GOT_ERR_STAGED_PATHS);
4642 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4646 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4650 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4654 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4658 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4662 err = got_ref_open(branch, repo,
4663 got_ref_get_symref_target(branch_ref), 0);
4667 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4671 err = got_ref_resolve(commit_id, repo, commit_ref);
4675 err = got_ref_open(new_base_branch, repo,
4676 new_base_branch_ref_name, 0);
4680 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4684 free(commit_ref_name);
4685 free(branch_ref_name);
4686 free(fileindex_path);
4688 got_ref_close(commit_ref);
4690 got_ref_close(branch_ref);
4695 got_ref_close(*tmp_branch);
4698 if (*new_base_branch) {
4699 got_ref_close(*new_base_branch);
4700 *new_base_branch = NULL;
4703 got_ref_close(*branch);
4707 got_fileindex_free(*fileindex);
4710 lock_worktree(worktree, LOCK_SH);
4715 const struct got_error *
4716 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4718 const struct got_error *err;
4719 char *tmp_branch_name = NULL;
4721 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4725 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4726 free(tmp_branch_name);
4730 static const struct got_error *
4731 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4732 char **logmsg, void *arg)
4738 static const struct got_error *
4739 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4740 const char *path, struct got_object_id *blob_id,
4741 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4746 struct collect_merged_paths_arg {
4747 got_worktree_checkout_cb progress_cb;
4749 struct got_pathlist_head *merged_paths;
4752 static const struct got_error *
4753 collect_merged_paths(void *arg, unsigned char status, const char *path)
4755 const struct got_error *err;
4756 struct collect_merged_paths_arg *a = arg;
4758 struct got_pathlist_entry *new;
4760 err = (*a->progress_cb)(a->progress_arg, status, path);
4764 if (status != GOT_STATUS_MERGE &&
4765 status != GOT_STATUS_ADD &&
4766 status != GOT_STATUS_DELETE &&
4767 status != GOT_STATUS_CONFLICT)
4772 return got_error_from_errno("strdup");
4774 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4775 if (err || new == NULL)
4781 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4783 struct got_pathlist_entry *pe;
4785 TAILQ_FOREACH(pe, merged_paths, entry)
4786 free((char *)pe->path);
4788 got_pathlist_free(merged_paths);
4791 static const struct got_error *
4792 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4793 struct got_repository *repo)
4795 const struct got_error *err;
4796 struct got_reference *commit_ref = NULL;
4798 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4800 if (err->code != GOT_ERR_NOT_REF)
4802 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4805 err = got_ref_write(commit_ref, repo);
4809 struct got_object_id *stored_id;
4812 err = got_ref_resolve(&stored_id, repo, commit_ref);
4815 cmp = got_object_id_cmp(commit_id, stored_id);
4818 err = got_error(GOT_ERR_REBASE_COMMITID);
4824 got_ref_close(commit_ref);
4828 static const struct got_error *
4829 rebase_merge_files(struct got_pathlist_head *merged_paths,
4830 const char *commit_ref_name, struct got_worktree *worktree,
4831 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4832 struct got_object_id *commit_id, struct got_repository *repo,
4833 got_worktree_checkout_cb progress_cb, void *progress_arg,
4834 got_cancel_cb cancel_cb, void *cancel_arg)
4836 const struct got_error *err;
4837 struct got_reference *commit_ref = NULL;
4838 struct collect_merged_paths_arg cmp_arg;
4839 char *fileindex_path;
4841 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4843 err = get_fileindex_path(&fileindex_path, worktree);
4847 cmp_arg.progress_cb = progress_cb;
4848 cmp_arg.progress_arg = progress_arg;
4849 cmp_arg.merged_paths = merged_paths;
4850 err = merge_files(worktree, fileindex, fileindex_path,
4851 parent_commit_id, commit_id, repo, collect_merged_paths,
4852 &cmp_arg, cancel_cb, cancel_arg);
4854 got_ref_close(commit_ref);
4858 const struct got_error *
4859 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4860 struct got_worktree *worktree, struct got_fileindex *fileindex,
4861 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4862 struct got_repository *repo,
4863 got_worktree_checkout_cb progress_cb, void *progress_arg,
4864 got_cancel_cb cancel_cb, void *cancel_arg)
4866 const struct got_error *err;
4867 char *commit_ref_name;
4869 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4873 err = store_commit_id(commit_ref_name, commit_id, repo);
4877 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4878 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4879 progress_arg, cancel_cb, cancel_arg);
4881 free(commit_ref_name);
4885 const struct got_error *
4886 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4887 struct got_worktree *worktree, struct got_fileindex *fileindex,
4888 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4889 struct got_repository *repo,
4890 got_worktree_checkout_cb progress_cb, void *progress_arg,
4891 got_cancel_cb cancel_cb, void *cancel_arg)
4893 const struct got_error *err;
4894 char *commit_ref_name;
4896 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4900 err = store_commit_id(commit_ref_name, commit_id, repo);
4904 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4905 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4906 progress_arg, cancel_cb, cancel_arg);
4908 free(commit_ref_name);
4912 static const struct got_error *
4913 rebase_commit(struct got_object_id **new_commit_id,
4914 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4915 struct got_worktree *worktree, struct got_fileindex *fileindex,
4916 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4917 const char *new_logmsg, struct got_repository *repo)
4919 const struct got_error *err, *sync_err;
4920 struct got_pathlist_head commitable_paths;
4921 struct collect_commitables_arg cc_arg;
4922 char *fileindex_path = NULL;
4923 struct got_reference *head_ref = NULL;
4924 struct got_object_id *head_commit_id = NULL;
4925 char *logmsg = NULL;
4927 TAILQ_INIT(&commitable_paths);
4928 *new_commit_id = NULL;
4930 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4932 err = get_fileindex_path(&fileindex_path, worktree);
4936 cc_arg.commitable_paths = &commitable_paths;
4937 cc_arg.worktree = worktree;
4939 cc_arg.have_staged_files = 0;
4941 * If possible get the status of individual files directly to
4942 * avoid crawling the entire work tree once per rebased commit.
4943 * TODO: Ideally, merged_paths would contain a list of commitables
4944 * we could use so we could skip worktree_status() entirely.
4947 struct got_pathlist_entry *pe;
4948 if (TAILQ_EMPTY(merged_paths)) {
4949 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4952 TAILQ_FOREACH(pe, merged_paths, entry) {
4953 err = worktree_status(worktree, pe->path, fileindex,
4954 repo, collect_commitables, &cc_arg, NULL, NULL);
4959 err = worktree_status(worktree, "", fileindex, repo,
4960 collect_commitables, &cc_arg, NULL, NULL);
4965 if (TAILQ_EMPTY(&commitable_paths)) {
4966 /* No-op change; commit will be elided. */
4967 err = got_ref_delete(commit_ref, repo);
4970 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4974 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4978 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4983 logmsg = strdup(new_logmsg);
4984 if (logmsg == NULL) {
4985 err = got_error_from_errno("strdup");
4989 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4994 /* NB: commit_worktree will call free(logmsg) */
4995 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4996 worktree, got_object_commit_get_author(orig_commit),
4997 got_object_commit_get_committer(orig_commit),
4998 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5002 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5006 err = got_ref_delete(commit_ref, repo);
5010 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5012 sync_err = sync_fileindex(fileindex, fileindex_path);
5013 if (sync_err && err == NULL)
5016 free(fileindex_path);
5017 free(head_commit_id);
5019 got_ref_close(head_ref);
5021 free(*new_commit_id);
5022 *new_commit_id = NULL;
5027 const struct got_error *
5028 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5029 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5030 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5031 struct got_commit_object *orig_commit,
5032 struct got_object_id *orig_commit_id, struct got_repository *repo)
5034 const struct got_error *err;
5035 char *commit_ref_name;
5036 struct got_reference *commit_ref = NULL;
5037 struct got_object_id *commit_id = NULL;
5039 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5043 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5046 err = got_ref_resolve(&commit_id, repo, commit_ref);
5049 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5050 err = got_error(GOT_ERR_REBASE_COMMITID);
5054 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5055 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5058 got_ref_close(commit_ref);
5059 free(commit_ref_name);
5064 const struct got_error *
5065 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5066 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5067 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5068 struct got_commit_object *orig_commit,
5069 struct got_object_id *orig_commit_id, const char *new_logmsg,
5070 struct got_repository *repo)
5072 const struct got_error *err;
5073 char *commit_ref_name;
5074 struct got_reference *commit_ref = NULL;
5075 struct got_object_id *commit_id = NULL;
5077 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5081 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5084 err = got_ref_resolve(&commit_id, repo, commit_ref);
5087 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5088 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5092 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5093 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5096 got_ref_close(commit_ref);
5097 free(commit_ref_name);
5102 const struct got_error *
5103 got_worktree_rebase_postpone(struct got_worktree *worktree,
5104 struct got_fileindex *fileindex)
5107 got_fileindex_free(fileindex);
5108 return lock_worktree(worktree, LOCK_SH);
5111 static const struct got_error *
5112 delete_ref(const char *name, struct got_repository *repo)
5114 const struct got_error *err;
5115 struct got_reference *ref;
5117 err = got_ref_open(&ref, repo, name, 0);
5119 if (err->code == GOT_ERR_NOT_REF)
5124 err = got_ref_delete(ref, repo);
5129 static const struct got_error *
5130 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5132 const struct got_error *err;
5133 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5134 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5136 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5139 err = delete_ref(tmp_branch_name, repo);
5143 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5146 err = delete_ref(new_base_branch_ref_name, repo);
5150 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5153 err = delete_ref(branch_ref_name, repo);
5157 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5160 err = delete_ref(commit_ref_name, repo);
5165 free(tmp_branch_name);
5166 free(new_base_branch_ref_name);
5167 free(branch_ref_name);
5168 free(commit_ref_name);
5172 const struct got_error *
5173 got_worktree_rebase_complete(struct got_worktree *worktree,
5174 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5175 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5176 struct got_repository *repo)
5178 const struct got_error *err, *unlockerr;
5179 struct got_object_id *new_head_commit_id = NULL;
5181 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5185 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5189 err = got_ref_write(rebased_branch, repo);
5193 err = got_worktree_set_head_ref(worktree, rebased_branch);
5197 err = delete_rebase_refs(worktree, repo);
5200 got_fileindex_free(fileindex);
5201 free(new_head_commit_id);
5202 unlockerr = lock_worktree(worktree, LOCK_SH);
5203 if (unlockerr && err == NULL)
5208 const struct got_error *
5209 got_worktree_rebase_abort(struct got_worktree *worktree,
5210 struct got_fileindex *fileindex, struct got_repository *repo,
5211 struct got_reference *new_base_branch,
5212 got_worktree_checkout_cb progress_cb, void *progress_arg)
5214 const struct got_error *err, *unlockerr, *sync_err;
5215 struct got_reference *resolved = NULL;
5216 struct got_object_id *commit_id = NULL;
5217 char *fileindex_path = NULL;
5218 struct revert_file_args rfa;
5219 struct got_object_id *tree_id = NULL;
5221 err = lock_worktree(worktree, LOCK_EX);
5225 err = got_ref_open(&resolved, repo,
5226 got_ref_get_symref_target(new_base_branch), 0);
5230 err = got_worktree_set_head_ref(worktree, resolved);
5235 * XXX commits to the base branch could have happened while
5236 * we were busy rebasing; should we store the original commit ID
5237 * when rebase begins and read it back here?
5239 err = got_ref_resolve(&commit_id, repo, resolved);
5243 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5247 err = got_object_id_by_path(&tree_id, repo,
5248 worktree->base_commit_id, worktree->path_prefix);
5252 err = delete_rebase_refs(worktree, repo);
5256 err = get_fileindex_path(&fileindex_path, worktree);
5260 rfa.worktree = worktree;
5261 rfa.fileindex = fileindex;
5262 rfa.progress_cb = progress_cb;
5263 rfa.progress_arg = progress_arg;
5264 rfa.patch_cb = NULL;
5265 rfa.patch_arg = NULL;
5267 err = worktree_status(worktree, "", fileindex, repo,
5268 revert_file, &rfa, NULL, NULL);
5272 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5273 repo, progress_cb, progress_arg, NULL, NULL);
5275 sync_err = sync_fileindex(fileindex, fileindex_path);
5276 if (sync_err && err == NULL)
5279 got_ref_close(resolved);
5283 got_fileindex_free(fileindex);
5284 free(fileindex_path);
5286 unlockerr = lock_worktree(worktree, LOCK_SH);
5287 if (unlockerr && err == NULL)
5292 const struct got_error *
5293 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5294 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5295 struct got_fileindex **fileindex, struct got_worktree *worktree,
5296 struct got_repository *repo)
5298 const struct got_error *err = NULL;
5299 char *tmp_branch_name = NULL;
5300 char *branch_ref_name = NULL;
5301 char *base_commit_ref_name = NULL;
5302 char *fileindex_path = NULL;
5303 struct check_rebase_ok_arg ok_arg;
5304 struct got_reference *wt_branch = NULL;
5305 struct got_reference *base_commit_ref = NULL;
5309 *base_commit_id = NULL;
5312 err = lock_worktree(worktree, LOCK_EX);
5316 err = open_fileindex(fileindex, &fileindex_path, worktree);
5320 ok_arg.worktree = worktree;
5322 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5327 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5331 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5335 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5340 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5345 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5349 err = got_ref_write(*branch_ref, repo);
5353 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5354 worktree->base_commit_id);
5357 err = got_ref_write(base_commit_ref, repo);
5360 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5361 if (*base_commit_id == NULL) {
5362 err = got_error_from_errno("got_object_id_dup");
5366 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5367 worktree->base_commit_id);
5370 err = got_ref_write(*tmp_branch, repo);
5374 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5378 free(fileindex_path);
5379 free(tmp_branch_name);
5380 free(branch_ref_name);
5381 free(base_commit_ref_name);
5383 got_ref_close(wt_branch);
5386 got_ref_close(*branch_ref);
5390 got_ref_close(*tmp_branch);
5393 free(*base_commit_id);
5395 got_fileindex_free(*fileindex);
5398 lock_worktree(worktree, LOCK_SH);
5403 const struct got_error *
5404 got_worktree_histedit_postpone(struct got_worktree *worktree,
5405 struct got_fileindex *fileindex)
5408 got_fileindex_free(fileindex);
5409 return lock_worktree(worktree, LOCK_SH);
5412 const struct got_error *
5413 got_worktree_histedit_in_progress(int *in_progress,
5414 struct got_worktree *worktree)
5416 const struct got_error *err;
5417 char *tmp_branch_name = NULL;
5419 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5423 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5424 free(tmp_branch_name);
5428 const struct got_error *
5429 got_worktree_histedit_continue(struct got_object_id **commit_id,
5430 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5431 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5432 struct got_worktree *worktree, struct got_repository *repo)
5434 const struct got_error *err;
5435 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5436 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5437 struct got_reference *commit_ref = NULL;
5438 struct got_reference *base_commit_ref = NULL;
5439 char *fileindex_path = NULL;
5440 int have_staged_files = 0;
5444 *base_commit_id = NULL;
5447 err = lock_worktree(worktree, LOCK_EX);
5451 err = open_fileindex(fileindex, &fileindex_path, worktree);
5455 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5456 &have_staged_files);
5457 if (err && err->code != GOT_ERR_CANCELLED)
5459 if (have_staged_files) {
5460 err = got_error(GOT_ERR_STAGED_PATHS);
5464 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5468 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5472 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5476 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5481 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5485 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5488 err = got_ref_resolve(commit_id, repo, commit_ref);
5492 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5495 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5499 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5503 free(commit_ref_name);
5504 free(branch_ref_name);
5505 free(fileindex_path);
5507 got_ref_close(commit_ref);
5508 if (base_commit_ref)
5509 got_ref_close(base_commit_ref);
5513 free(*base_commit_id);
5514 *base_commit_id = NULL;
5516 got_ref_close(*tmp_branch);
5520 got_fileindex_free(*fileindex);
5523 lock_worktree(worktree, LOCK_EX);
5528 static const struct got_error *
5529 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5531 const struct got_error *err;
5532 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5533 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5535 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5538 err = delete_ref(tmp_branch_name, repo);
5542 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5546 err = delete_ref(base_commit_ref_name, repo);
5550 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5553 err = delete_ref(branch_ref_name, repo);
5557 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5560 err = delete_ref(commit_ref_name, repo);
5564 free(tmp_branch_name);
5565 free(base_commit_ref_name);
5566 free(branch_ref_name);
5567 free(commit_ref_name);
5571 const struct got_error *
5572 got_worktree_histedit_abort(struct got_worktree *worktree,
5573 struct got_fileindex *fileindex, struct got_repository *repo,
5574 struct got_reference *branch, struct got_object_id *base_commit_id,
5575 got_worktree_checkout_cb progress_cb, void *progress_arg)
5577 const struct got_error *err, *unlockerr, *sync_err;
5578 struct got_reference *resolved = NULL;
5579 char *fileindex_path = NULL;
5580 struct got_object_id *tree_id = NULL;
5581 struct revert_file_args rfa;
5583 err = lock_worktree(worktree, LOCK_EX);
5587 err = got_ref_open(&resolved, repo,
5588 got_ref_get_symref_target(branch), 0);
5592 err = got_worktree_set_head_ref(worktree, resolved);
5596 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5600 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5601 worktree->path_prefix);
5605 err = delete_histedit_refs(worktree, repo);
5609 err = get_fileindex_path(&fileindex_path, worktree);
5613 rfa.worktree = worktree;
5614 rfa.fileindex = fileindex;
5615 rfa.progress_cb = progress_cb;
5616 rfa.progress_arg = progress_arg;
5617 rfa.patch_cb = NULL;
5618 rfa.patch_arg = NULL;
5620 err = worktree_status(worktree, "", fileindex, repo,
5621 revert_file, &rfa, NULL, NULL);
5625 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5626 repo, progress_cb, progress_arg, NULL, NULL);
5628 sync_err = sync_fileindex(fileindex, fileindex_path);
5629 if (sync_err && err == NULL)
5632 got_ref_close(resolved);
5634 free(fileindex_path);
5636 unlockerr = lock_worktree(worktree, LOCK_SH);
5637 if (unlockerr && err == NULL)
5642 const struct got_error *
5643 got_worktree_histedit_complete(struct got_worktree *worktree,
5644 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5645 struct got_reference *edited_branch, struct got_repository *repo)
5647 const struct got_error *err, *unlockerr;
5648 struct got_object_id *new_head_commit_id = NULL;
5649 struct got_reference *resolved = NULL;
5651 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5655 err = got_ref_open(&resolved, repo,
5656 got_ref_get_symref_target(edited_branch), 0);
5660 err = got_ref_change_ref(resolved, new_head_commit_id);
5664 err = got_ref_write(resolved, repo);
5668 err = got_worktree_set_head_ref(worktree, resolved);
5672 err = delete_histedit_refs(worktree, repo);
5675 got_fileindex_free(fileindex);
5676 free(new_head_commit_id);
5677 unlockerr = lock_worktree(worktree, LOCK_SH);
5678 if (unlockerr && err == NULL)
5683 const struct got_error *
5684 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5685 struct got_object_id *commit_id, struct got_repository *repo)
5687 const struct got_error *err;
5688 char *commit_ref_name;
5690 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5694 err = store_commit_id(commit_ref_name, commit_id, repo);
5698 err = delete_ref(commit_ref_name, repo);
5700 free(commit_ref_name);
5704 const struct got_error *
5705 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5706 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5707 struct got_worktree *worktree, const char *refname,
5708 struct got_repository *repo)
5710 const struct got_error *err = NULL;
5711 char *fileindex_path = NULL;
5712 struct check_rebase_ok_arg ok_arg;
5716 *base_branch_ref = NULL;
5718 err = lock_worktree(worktree, LOCK_EX);
5722 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5723 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5724 "cannot integrate a branch into itself; "
5725 "update -b or different branch name required");
5729 err = open_fileindex(fileindex, &fileindex_path, worktree);
5733 /* Preconditions are the same as for rebase. */
5734 ok_arg.worktree = worktree;
5736 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5741 err = got_ref_open(branch_ref, repo, refname, 1);
5745 err = got_ref_open(base_branch_ref, repo,
5746 got_worktree_get_head_ref_name(worktree), 1);
5750 got_ref_close(*branch_ref);
5753 if (*base_branch_ref) {
5754 got_ref_close(*base_branch_ref);
5755 *base_branch_ref = NULL;
5758 got_fileindex_free(*fileindex);
5761 lock_worktree(worktree, LOCK_SH);
5766 const struct got_error *
5767 got_worktree_integrate_continue(struct got_worktree *worktree,
5768 struct got_fileindex *fileindex, struct got_repository *repo,
5769 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5770 got_worktree_checkout_cb progress_cb, void *progress_arg,
5771 got_cancel_cb cancel_cb, void *cancel_arg)
5773 const struct got_error *err = NULL, *sync_err, *unlockerr;
5774 char *fileindex_path = NULL;
5775 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5777 err = get_fileindex_path(&fileindex_path, worktree);
5781 err = got_ref_resolve(&commit_id, repo, branch_ref);
5785 err = got_object_id_by_path(&tree_id, repo, commit_id,
5786 worktree->path_prefix);
5790 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5794 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5795 progress_cb, progress_arg, cancel_cb, cancel_arg);
5799 err = got_ref_change_ref(base_branch_ref, commit_id);
5803 err = got_ref_write(base_branch_ref, repo);
5805 sync_err = sync_fileindex(fileindex, fileindex_path);
5806 if (sync_err && err == NULL)
5810 unlockerr = got_ref_unlock(branch_ref);
5811 if (unlockerr && err == NULL)
5813 got_ref_close(branch_ref);
5815 unlockerr = got_ref_unlock(base_branch_ref);
5816 if (unlockerr && err == NULL)
5818 got_ref_close(base_branch_ref);
5820 got_fileindex_free(fileindex);
5821 free(fileindex_path);
5824 unlockerr = lock_worktree(worktree, LOCK_SH);
5825 if (unlockerr && err == NULL)
5830 const struct got_error *
5831 got_worktree_integrate_abort(struct got_worktree *worktree,
5832 struct got_fileindex *fileindex, struct got_repository *repo,
5833 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5835 const struct got_error *err = NULL, *unlockerr = NULL;
5837 got_fileindex_free(fileindex);
5839 err = lock_worktree(worktree, LOCK_SH);
5841 unlockerr = got_ref_unlock(branch_ref);
5842 if (unlockerr && err == NULL)
5844 got_ref_close(branch_ref);
5846 unlockerr = got_ref_unlock(base_branch_ref);
5847 if (unlockerr && err == NULL)
5849 got_ref_close(base_branch_ref);
5854 struct check_stage_ok_arg {
5855 struct got_object_id *head_commit_id;
5856 struct got_worktree *worktree;
5857 struct got_fileindex *fileindex;
5858 struct got_repository *repo;
5862 const struct got_error *
5863 check_stage_ok(void *arg, unsigned char status,
5864 unsigned char staged_status, const char *relpath,
5865 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5866 struct got_object_id *commit_id)
5868 struct check_stage_ok_arg *a = arg;
5869 const struct got_error *err = NULL;
5870 struct got_fileindex_entry *ie;
5871 struct got_object_id base_commit_id;
5872 struct got_object_id *base_commit_idp = NULL;
5873 char *in_repo_path = NULL, *p;
5875 if (status == GOT_STATUS_UNVERSIONED ||
5876 status == GOT_STATUS_NO_CHANGE)
5878 if (status == GOT_STATUS_NONEXISTENT)
5879 return got_error_set_errno(ENOENT, relpath);
5881 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5883 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5885 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5886 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5888 return got_error_from_errno("asprintf");
5890 if (got_fileindex_entry_has_commit(ie)) {
5891 memcpy(base_commit_id.sha1, ie->commit_sha1,
5892 SHA1_DIGEST_LENGTH);
5893 base_commit_idp = &base_commit_id;
5896 if (status == GOT_STATUS_CONFLICT) {
5897 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5899 } else if (status != GOT_STATUS_ADD &&
5900 status != GOT_STATUS_MODIFY &&
5901 status != GOT_STATUS_DELETE) {
5902 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5906 a->have_changes = 1;
5911 err = check_out_of_date(p, status, staged_status,
5912 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5913 GOT_ERR_STAGE_OUT_OF_DATE);
5919 struct stage_path_arg {
5920 struct got_worktree *worktree;
5921 struct got_fileindex *fileindex;
5922 struct got_repository *repo;
5923 got_worktree_status_cb status_cb;
5925 got_worktree_patch_cb patch_cb;
5927 int staged_something;
5930 static const struct got_error *
5931 stage_path(void *arg, unsigned char status,
5932 unsigned char staged_status, const char *relpath,
5933 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5934 struct got_object_id *commit_id)
5936 struct stage_path_arg *a = arg;
5937 const struct got_error *err = NULL;
5938 struct got_fileindex_entry *ie;
5939 char *ondisk_path = NULL, *path_content = NULL;
5941 struct got_object_id *new_staged_blob_id = NULL;
5943 if (status == GOT_STATUS_UNVERSIONED)
5946 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5948 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5950 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5952 return got_error_from_errno("asprintf");
5955 case GOT_STATUS_ADD:
5956 case GOT_STATUS_MODIFY:
5958 if (status == GOT_STATUS_ADD) {
5959 int choice = GOT_PATCH_CHOICE_NONE;
5960 err = (*a->patch_cb)(&choice, a->patch_arg,
5961 status, ie->path, NULL, 1, 1);
5964 if (choice != GOT_PATCH_CHOICE_YES)
5967 err = create_patched_content(&path_content, 0,
5968 staged_blob_id ? staged_blob_id : blob_id,
5969 ondisk_path, ie->path, a->repo,
5970 a->patch_cb, a->patch_arg);
5971 if (err || path_content == NULL)
5975 err = got_object_blob_create(&new_staged_blob_id,
5976 path_content ? path_content : ondisk_path, a->repo);
5979 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5980 SHA1_DIGEST_LENGTH);
5981 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5982 stage = GOT_FILEIDX_STAGE_ADD;
5984 stage = GOT_FILEIDX_STAGE_MODIFY;
5985 got_fileindex_entry_stage_set(ie, stage);
5986 a->staged_something = 1;
5987 if (a->status_cb == NULL)
5989 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5990 get_staged_status(ie), relpath, blob_id,
5991 new_staged_blob_id, NULL);
5993 case GOT_STATUS_DELETE:
5994 if (staged_status == GOT_STATUS_DELETE)
5997 int choice = GOT_PATCH_CHOICE_NONE;
5998 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5999 ie->path, NULL, 1, 1);
6002 if (choice == GOT_PATCH_CHOICE_NO)
6004 if (choice != GOT_PATCH_CHOICE_YES) {
6005 err = got_error(GOT_ERR_PATCH_CHOICE);
6009 stage = GOT_FILEIDX_STAGE_DELETE;
6010 got_fileindex_entry_stage_set(ie, stage);
6011 a->staged_something = 1;
6012 if (a->status_cb == NULL)
6014 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6015 get_staged_status(ie), relpath, NULL, NULL, NULL);
6017 case GOT_STATUS_NO_CHANGE:
6019 case GOT_STATUS_CONFLICT:
6020 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6022 case GOT_STATUS_NONEXISTENT:
6023 err = got_error_set_errno(ENOENT, relpath);
6026 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6030 if (path_content && unlink(path_content) == -1 && err == NULL)
6031 err = got_error_from_errno2("unlink", path_content);
6034 free(new_staged_blob_id);
6038 const struct got_error *
6039 got_worktree_stage(struct got_worktree *worktree,
6040 struct got_pathlist_head *paths,
6041 got_worktree_status_cb status_cb, void *status_arg,
6042 got_worktree_patch_cb patch_cb, void *patch_arg,
6043 struct got_repository *repo)
6045 const struct got_error *err = NULL, *sync_err, *unlockerr;
6046 struct got_pathlist_entry *pe;
6047 struct got_fileindex *fileindex = NULL;
6048 char *fileindex_path = NULL;
6049 struct got_reference *head_ref = NULL;
6050 struct got_object_id *head_commit_id = NULL;
6051 struct check_stage_ok_arg oka;
6052 struct stage_path_arg spa;
6054 err = lock_worktree(worktree, LOCK_EX);
6058 err = got_ref_open(&head_ref, repo,
6059 got_worktree_get_head_ref_name(worktree), 0);
6062 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6065 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6069 /* Check pre-conditions before staging anything. */
6070 oka.head_commit_id = head_commit_id;
6071 oka.worktree = worktree;
6072 oka.fileindex = fileindex;
6074 oka.have_changes = 0;
6075 TAILQ_FOREACH(pe, paths, entry) {
6076 err = worktree_status(worktree, pe->path, fileindex, repo,
6077 check_stage_ok, &oka, NULL, NULL);
6081 if (!oka.have_changes) {
6082 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6086 spa.worktree = worktree;
6087 spa.fileindex = fileindex;
6089 spa.patch_cb = patch_cb;
6090 spa.patch_arg = patch_arg;
6091 spa.status_cb = status_cb;
6092 spa.status_arg = status_arg;
6093 spa.staged_something = 0;
6094 TAILQ_FOREACH(pe, paths, entry) {
6095 err = worktree_status(worktree, pe->path, fileindex, repo,
6096 stage_path, &spa, NULL, NULL);
6100 if (!spa.staged_something) {
6101 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6105 sync_err = sync_fileindex(fileindex, fileindex_path);
6106 if (sync_err && err == NULL)
6110 got_ref_close(head_ref);
6111 free(head_commit_id);
6112 free(fileindex_path);
6114 got_fileindex_free(fileindex);
6115 unlockerr = lock_worktree(worktree, LOCK_SH);
6116 if (unlockerr && err == NULL)
6121 struct unstage_path_arg {
6122 struct got_worktree *worktree;
6123 struct got_fileindex *fileindex;
6124 struct got_repository *repo;
6125 got_worktree_checkout_cb progress_cb;
6127 got_worktree_patch_cb patch_cb;
6131 static const struct got_error *
6132 create_unstaged_content(char **path_unstaged_content,
6133 char **path_new_staged_content, struct got_object_id *blob_id,
6134 struct got_object_id *staged_blob_id, const char *relpath,
6135 struct got_repository *repo,
6136 got_worktree_patch_cb patch_cb, void *patch_arg)
6138 const struct got_error *err;
6139 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6140 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6141 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6142 struct stat sb1, sb2;
6143 struct got_diff_changes *changes = NULL;
6144 struct got_diff_state *ds = NULL;
6145 struct got_diff_args *args = NULL;
6146 struct got_diff_change *change;
6147 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6148 int have_content = 0, have_rejected_content = 0;
6150 *path_unstaged_content = NULL;
6151 *path_new_staged_content = NULL;
6153 err = got_object_id_str(&label1, blob_id);
6156 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6160 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6164 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6168 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6172 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6176 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6180 if (stat(path1, &sb1) == -1) {
6181 err = got_error_from_errno2("stat", path1);
6185 if (stat(path2, &sb2) == -1) {
6186 err = got_error_from_errno2("stat", path2);
6190 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6191 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6195 err = got_opentemp_named(path_unstaged_content, &outfile,
6196 "got-unstaged-content");
6199 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6200 "got-new-staged-content");
6204 if (fseek(f1, 0L, SEEK_SET) == -1) {
6205 err = got_ferror(f1, GOT_ERR_IO);
6208 if (fseek(f2, 0L, SEEK_SET) == -1) {
6209 err = got_ferror(f2, GOT_ERR_IO);
6212 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6214 err = apply_or_reject_change(&choice, change, ++n,
6215 changes->nchanges, ds, args, diff_flags, relpath,
6216 f1, f2, &line_cur1, &line_cur2,
6217 outfile, rejectfile, patch_cb, patch_arg);
6220 if (choice == GOT_PATCH_CHOICE_YES)
6223 have_rejected_content = 1;
6224 if (choice == GOT_PATCH_CHOICE_QUIT)
6227 if (have_content || have_rejected_content)
6228 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6229 outfile, rejectfile);
6233 got_object_blob_close(blob);
6235 got_object_blob_close(staged_blob);
6236 if (f1 && fclose(f1) == EOF && err == NULL)
6237 err = got_error_from_errno2("fclose", path1);
6238 if (f2 && fclose(f2) == EOF && err == NULL)
6239 err = got_error_from_errno2("fclose", path2);
6240 if (outfile && fclose(outfile) == EOF && err == NULL)
6241 err = got_error_from_errno2("fclose", *path_unstaged_content);
6242 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6243 err = got_error_from_errno2("fclose", *path_new_staged_content);
6244 if (path1 && unlink(path1) == -1 && err == NULL)
6245 err = got_error_from_errno2("unlink", path1);
6246 if (path2 && unlink(path2) == -1 && err == NULL)
6247 err = got_error_from_errno2("unlink", path2);
6248 if (err || !have_content) {
6249 if (*path_unstaged_content &&
6250 unlink(*path_unstaged_content) == -1 && err == NULL)
6251 err = got_error_from_errno2("unlink",
6252 *path_unstaged_content);
6253 free(*path_unstaged_content);
6254 *path_unstaged_content = NULL;
6256 if (err || !have_rejected_content) {
6257 if (*path_new_staged_content &&
6258 unlink(*path_new_staged_content) == -1 && err == NULL)
6259 err = got_error_from_errno2("unlink",
6260 *path_new_staged_content);
6261 free(*path_new_staged_content);
6262 *path_new_staged_content = NULL;
6266 got_diff_state_free(ds);
6270 got_diff_free_changes(changes);
6276 static const struct got_error *
6277 unstage_path(void *arg, unsigned char status,
6278 unsigned char staged_status, const char *relpath,
6279 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6280 struct got_object_id *commit_id)
6282 const struct got_error *err = NULL;
6283 struct unstage_path_arg *a = arg;
6284 struct got_fileindex_entry *ie;
6285 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6286 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6287 char *path_new_staged_content = NULL;
6288 char *id_str = NULL, *label_orig = NULL;
6289 int local_changes_subsumed;
6292 if (staged_status != GOT_STATUS_ADD &&
6293 staged_status != GOT_STATUS_MODIFY &&
6294 staged_status != GOT_STATUS_DELETE)
6297 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6299 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6301 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6303 return got_error_from_errno("asprintf");
6305 err = got_object_id_str(&id_str,
6306 commit_id ? commit_id : a->worktree->base_commit_id);
6309 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6311 err = got_error_from_errno("asprintf");
6315 switch (staged_status) {
6316 case GOT_STATUS_MODIFY:
6317 err = got_object_open_as_blob(&blob_base, a->repo,
6322 case GOT_STATUS_ADD:
6324 if (staged_status == GOT_STATUS_ADD) {
6325 int choice = GOT_PATCH_CHOICE_NONE;
6326 err = (*a->patch_cb)(&choice, a->patch_arg,
6327 staged_status, ie->path, NULL, 1, 1);
6330 if (choice != GOT_PATCH_CHOICE_YES)
6333 err = create_unstaged_content(
6334 &path_unstaged_content,
6335 &path_new_staged_content, blob_id,
6336 staged_blob_id, ie->path, a->repo,
6337 a->patch_cb, a->patch_arg);
6338 if (err || path_unstaged_content == NULL)
6340 if (path_new_staged_content) {
6341 err = got_object_blob_create(
6343 path_new_staged_content,
6347 memcpy(ie->staged_blob_sha1,
6348 staged_blob_id->sha1,
6349 SHA1_DIGEST_LENGTH);
6351 err = merge_file(&local_changes_subsumed,
6352 a->worktree, blob_base, ondisk_path,
6353 relpath, got_fileindex_perms_to_st(ie),
6354 path_unstaged_content, label_orig,
6355 "unstaged", a->repo, a->progress_cb,
6358 path_new_staged_content == NULL)
6359 got_fileindex_entry_stage_set(ie,
6360 GOT_FILEIDX_STAGE_NONE);
6361 break; /* Done with this file. */
6364 err = got_object_open_as_blob(&blob_staged, a->repo,
6365 staged_blob_id, 8192);
6368 err = merge_blob(&local_changes_subsumed, a->worktree,
6369 blob_base, ondisk_path, relpath,
6370 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6371 commit_id ? commit_id : a->worktree->base_commit_id,
6372 a->repo, a->progress_cb, a->progress_arg);
6374 got_fileindex_entry_stage_set(ie,
6375 GOT_FILEIDX_STAGE_NONE);
6377 case GOT_STATUS_DELETE:
6379 int choice = GOT_PATCH_CHOICE_NONE;
6380 err = (*a->patch_cb)(&choice, a->patch_arg,
6381 staged_status, ie->path, NULL, 1, 1);
6384 if (choice == GOT_PATCH_CHOICE_NO)
6386 if (choice != GOT_PATCH_CHOICE_YES) {
6387 err = got_error(GOT_ERR_PATCH_CHOICE);
6391 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6392 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6395 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6400 if (path_unstaged_content &&
6401 unlink(path_unstaged_content) == -1 && err == NULL)
6402 err = got_error_from_errno2("unlink", path_unstaged_content);
6403 if (path_new_staged_content &&
6404 unlink(path_new_staged_content) == -1 && err == NULL)
6405 err = got_error_from_errno2("unlink", path_new_staged_content);
6406 free(path_unstaged_content);
6407 free(path_new_staged_content);
6409 got_object_blob_close(blob_base);
6411 got_object_blob_close(blob_staged);
6417 const struct got_error *
6418 got_worktree_unstage(struct got_worktree *worktree,
6419 struct got_pathlist_head *paths,
6420 got_worktree_checkout_cb progress_cb, void *progress_arg,
6421 got_worktree_patch_cb patch_cb, void *patch_arg,
6422 struct got_repository *repo)
6424 const struct got_error *err = NULL, *sync_err, *unlockerr;
6425 struct got_pathlist_entry *pe;
6426 struct got_fileindex *fileindex = NULL;
6427 char *fileindex_path = NULL;
6428 struct unstage_path_arg upa;
6430 err = lock_worktree(worktree, LOCK_EX);
6434 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6438 upa.worktree = worktree;
6439 upa.fileindex = fileindex;
6441 upa.progress_cb = progress_cb;
6442 upa.progress_arg = progress_arg;
6443 upa.patch_cb = patch_cb;
6444 upa.patch_arg = patch_arg;
6445 TAILQ_FOREACH(pe, paths, entry) {
6446 err = worktree_status(worktree, pe->path, fileindex, repo,
6447 unstage_path, &upa, NULL, NULL);
6452 sync_err = sync_fileindex(fileindex, fileindex_path);
6453 if (sync_err && err == NULL)
6456 free(fileindex_path);
6458 got_fileindex_free(fileindex);
6459 unlockerr = lock_worktree(worktree, LOCK_SH);
6460 if (unlockerr && err == NULL)