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(GOT_ERR_BAD_OBJ_ID_STR);
1303 if (asprintf(&label_orig, "%s: commit %s",
1304 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1305 err = got_error_from_errno("asprintf");
1309 err = merge_blob(&update_timestamps, worktree, blob2,
1310 ondisk_path, path, sb.st_mode, label_orig, blob,
1311 worktree->base_commit_id, repo,
1312 progress_cb, progress_arg);
1315 got_object_blob_close(blob2);
1319 * Do not update timestamps of files with local changes.
1320 * Otherwise, a future status walk would treat them as
1321 * unmodified files again.
1323 err = got_fileindex_entry_update(ie, ondisk_path,
1324 blob->id.sha1, worktree->base_commit_id->sha1,
1326 } else if (status == GOT_STATUS_MODE_CHANGE) {
1327 err = got_fileindex_entry_update(ie, ondisk_path,
1328 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1329 } else if (status == GOT_STATUS_DELETE) {
1330 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1333 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1334 ondisk_path, path, blob, 0);
1338 err = install_blob(worktree, ondisk_path, path, te->mode,
1339 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1340 repo, progress_cb, progress_arg);
1343 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1344 ondisk_path, path, blob, 1);
1348 got_object_blob_close(blob);
1354 static const struct got_error *
1355 remove_ondisk_file(const char *root_path, const char *path)
1357 const struct got_error *err = NULL;
1358 char *ondisk_path = NULL;
1360 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1361 return got_error_from_errno("asprintf");
1363 if (unlink(ondisk_path) == -1) {
1364 if (errno != ENOENT)
1365 err = got_error_from_errno2("unlink", ondisk_path);
1367 char *parent = dirname(ondisk_path);
1368 while (parent && strcmp(parent, root_path) != 0) {
1369 if (rmdir(parent) == -1) {
1370 if (errno != ENOTEMPTY)
1371 err = got_error_from_errno2("rmdir",
1375 parent = dirname(parent);
1382 static const struct got_error *
1383 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1384 struct got_fileindex_entry *ie, struct got_repository *repo,
1385 got_worktree_checkout_cb progress_cb, void *progress_arg)
1387 const struct got_error *err = NULL;
1388 unsigned char status;
1392 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1393 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1395 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1397 return got_error_from_errno("asprintf");
1399 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1403 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1404 status == GOT_STATUS_ADD) {
1405 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1409 * Preserve the working file and change the deleted blob's
1410 * entry into a schedule-add entry.
1412 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1417 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1420 if (status == GOT_STATUS_NO_CHANGE) {
1421 err = remove_ondisk_file(worktree->root_path, ie->path);
1425 got_fileindex_entry_remove(fileindex, ie);
1431 struct diff_cb_arg {
1432 struct got_fileindex *fileindex;
1433 struct got_worktree *worktree;
1434 struct got_repository *repo;
1435 got_worktree_checkout_cb progress_cb;
1437 got_cancel_cb cancel_cb;
1441 static const struct got_error *
1442 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1443 struct got_tree_entry *te, const char *parent_path)
1445 struct diff_cb_arg *a = arg;
1447 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1448 return got_error(GOT_ERR_CANCELLED);
1450 return update_blob(a->worktree, a->fileindex, ie, te,
1451 ie->path, a->repo, a->progress_cb, a->progress_arg);
1454 static const struct got_error *
1455 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1457 struct diff_cb_arg *a = arg;
1459 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1460 return got_error(GOT_ERR_CANCELLED);
1462 return delete_blob(a->worktree, a->fileindex, ie,
1463 a->repo, a->progress_cb, a->progress_arg);
1466 static const struct got_error *
1467 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1469 struct diff_cb_arg *a = arg;
1470 const struct got_error *err;
1473 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1474 return got_error(GOT_ERR_CANCELLED);
1476 if (got_object_tree_entry_is_submodule(te))
1479 if (asprintf(&path, "%s%s%s", parent_path,
1480 parent_path[0] ? "/" : "", te->name)
1482 return got_error_from_errno("asprintf");
1484 if (S_ISDIR(te->mode))
1485 err = add_dir_on_disk(a->worktree, path);
1487 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1488 a->repo, a->progress_cb, a->progress_arg);
1494 static const struct got_error *
1495 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1497 const struct got_error *err = NULL;
1498 char *uuidstr = NULL;
1499 uint32_t uuid_status;
1503 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1504 if (uuid_status != uuid_s_ok)
1505 return got_error_uuid(uuid_status, "uuid_to_string");
1507 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1509 err = got_error_from_errno("asprintf");
1516 const struct got_error *
1517 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1519 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1522 static const struct got_error *
1523 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1525 return get_ref_name(refname, worktree,
1526 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1529 static const struct got_error *
1530 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1532 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1535 static const struct got_error *
1536 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1538 return get_ref_name(refname, worktree,
1539 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1542 static const struct got_error *
1543 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1545 return get_ref_name(refname, worktree,
1546 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1549 static const struct got_error *
1550 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1552 return get_ref_name(refname, worktree,
1553 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1556 static const struct got_error *
1557 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1559 return get_ref_name(refname, worktree,
1560 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1563 static const struct got_error *
1564 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1566 return get_ref_name(refname, worktree,
1567 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1570 static const struct got_error *
1571 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1573 return get_ref_name(refname, worktree,
1574 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1577 const struct got_error *
1578 got_worktree_get_histedit_script_path(char **path,
1579 struct got_worktree *worktree)
1581 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1582 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1584 return got_error_from_errno("asprintf");
1590 * Prevent Git's garbage collector from deleting our base commit by
1591 * setting a reference to our base commit's ID.
1593 static const struct got_error *
1594 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1596 const struct got_error *err = NULL;
1597 struct got_reference *ref = NULL;
1600 err = got_worktree_get_base_ref_name(&refname, worktree);
1604 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1608 err = got_ref_write(ref, repo);
1616 static const struct got_error *
1617 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1619 const struct got_error *err = NULL;
1621 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1622 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1623 err = got_error_from_errno("asprintf");
1624 *fileindex_path = NULL;
1630 static const struct got_error *
1631 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1632 struct got_worktree *worktree)
1634 const struct got_error *err = NULL;
1637 *fileindex_path = NULL;
1638 *fileindex = got_fileindex_alloc();
1639 if (*fileindex == NULL)
1640 return got_error_from_errno("got_fileindex_alloc");
1642 err = get_fileindex_path(fileindex_path, worktree);
1646 index = fopen(*fileindex_path, "rb");
1647 if (index == NULL) {
1648 if (errno != ENOENT)
1649 err = got_error_from_errno2("fopen", *fileindex_path);
1651 err = got_fileindex_read(*fileindex, index);
1652 if (fclose(index) != 0 && err == NULL)
1653 err = got_error_from_errno("fclose");
1657 free(*fileindex_path);
1658 *fileindex_path = NULL;
1659 got_fileindex_free(*fileindex);
1665 struct bump_base_commit_id_arg {
1666 struct got_object_id *base_commit_id;
1669 const char *entry_name;
1670 got_worktree_checkout_cb progress_cb;
1674 /* Bump base commit ID of all files within an updated part of the work tree. */
1675 static const struct got_error *
1676 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1678 const struct got_error *err;
1679 struct bump_base_commit_id_arg *a = arg;
1681 if (a->entry_name) {
1682 if (strcmp(ie->path, a->path) != 0)
1684 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1687 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1688 SHA1_DIGEST_LENGTH) == 0)
1691 if (a->progress_cb) {
1692 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1697 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1701 static const struct got_error *
1702 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1704 const struct got_error *err = NULL;
1705 char *new_fileindex_path = NULL;
1706 FILE *new_index = NULL;
1708 err = got_opentemp_named(&new_fileindex_path, &new_index,
1713 err = got_fileindex_write(fileindex, new_index);
1717 if (rename(new_fileindex_path, fileindex_path) != 0) {
1718 err = got_error_from_errno3("rename", new_fileindex_path,
1720 unlink(new_fileindex_path);
1725 free(new_fileindex_path);
1729 static const struct got_error *
1730 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1731 struct got_object_id **tree_id, const char *wt_relpath,
1732 struct got_worktree *worktree, struct got_repository *repo)
1734 const struct got_error *err = NULL;
1735 struct got_object_id *id = NULL;
1736 char *in_repo_path = NULL;
1737 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1739 *entry_type = GOT_OBJ_TYPE_ANY;
1740 *tree_relpath = NULL;
1743 if (wt_relpath[0] == '\0') {
1744 /* Check out all files within the work tree. */
1745 *entry_type = GOT_OBJ_TYPE_TREE;
1746 *tree_relpath = strdup("");
1747 if (*tree_relpath == NULL) {
1748 err = got_error_from_errno("strdup");
1751 err = got_object_id_by_path(tree_id, repo,
1752 worktree->base_commit_id, worktree->path_prefix);
1758 /* Check out a subset of files in the work tree. */
1760 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1761 is_root_wt ? "" : "/", wt_relpath) == -1) {
1762 err = got_error_from_errno("asprintf");
1766 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1772 in_repo_path = NULL;
1774 err = got_object_get_type(entry_type, repo, id);
1778 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1779 /* Check out a single file. */
1780 if (strchr(wt_relpath, '/') == NULL) {
1781 /* Check out a single file in work tree's root dir. */
1782 in_repo_path = strdup(worktree->path_prefix);
1783 if (in_repo_path == NULL) {
1784 err = got_error_from_errno("strdup");
1787 *tree_relpath = strdup("");
1788 if (*tree_relpath == NULL) {
1789 err = got_error_from_errno("strdup");
1793 /* Check out a single file in a subdirectory. */
1794 err = got_path_dirname(tree_relpath, wt_relpath);
1797 if (asprintf(&in_repo_path, "%s%s%s",
1798 worktree->path_prefix, is_root_wt ? "" : "/",
1799 *tree_relpath) == -1) {
1800 err = got_error_from_errno("asprintf");
1804 err = got_object_id_by_path(tree_id, repo,
1805 worktree->base_commit_id, in_repo_path);
1807 /* Check out all files within a subdirectory. */
1808 *tree_id = got_object_id_dup(id);
1809 if (*tree_id == NULL) {
1810 err = got_error_from_errno("got_object_id_dup");
1813 *tree_relpath = strdup(wt_relpath);
1814 if (*tree_relpath == NULL) {
1815 err = got_error_from_errno("strdup");
1823 *entry_type = GOT_OBJ_TYPE_ANY;
1824 free(*tree_relpath);
1825 *tree_relpath = NULL;
1832 static const struct got_error *
1833 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1834 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1835 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1836 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1838 const struct got_error *err = NULL;
1839 struct got_commit_object *commit = NULL;
1840 struct got_tree_object *tree = NULL;
1841 struct got_fileindex_diff_tree_cb diff_cb;
1842 struct diff_cb_arg arg;
1844 err = ref_base_commit(worktree, repo);
1848 err = got_object_open_as_commit(&commit, repo,
1849 worktree->base_commit_id);
1853 err = got_object_open_as_tree(&tree, repo, tree_id);
1858 got_object_tree_find_entry(tree, entry_name) == NULL) {
1859 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1863 diff_cb.diff_old_new = diff_old_new;
1864 diff_cb.diff_old = diff_old;
1865 diff_cb.diff_new = diff_new;
1866 arg.fileindex = fileindex;
1867 arg.worktree = worktree;
1869 arg.progress_cb = progress_cb;
1870 arg.progress_arg = progress_arg;
1871 arg.cancel_cb = cancel_cb;
1872 arg.cancel_arg = cancel_arg;
1873 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1874 entry_name, repo, &diff_cb, &arg);
1877 got_object_tree_close(tree);
1879 got_object_commit_close(commit);
1883 const struct got_error *
1884 got_worktree_checkout_files(struct got_worktree *worktree,
1885 struct got_pathlist_head *paths, struct got_repository *repo,
1886 got_worktree_checkout_cb progress_cb, void *progress_arg,
1887 got_cancel_cb cancel_cb, void *cancel_arg)
1889 const struct got_error *err = NULL, *sync_err, *unlockerr;
1890 struct got_commit_object *commit = NULL;
1891 struct got_tree_object *tree = NULL;
1892 struct got_fileindex *fileindex = NULL;
1893 char *fileindex_path = NULL;
1894 struct got_pathlist_entry *pe;
1895 struct tree_path_data {
1896 SIMPLEQ_ENTRY(tree_path_data) entry;
1897 struct got_object_id *tree_id;
1902 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1904 SIMPLEQ_INIT(&tree_paths);
1906 err = lock_worktree(worktree, LOCK_EX);
1910 /* Map all specified paths to in-repository trees. */
1911 TAILQ_FOREACH(pe, paths, entry) {
1912 tpd = malloc(sizeof(*tpd));
1914 err = got_error_from_errno("malloc");
1918 err = find_tree_entry_for_checkout(&tpd->entry_type,
1919 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1925 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1926 err = got_path_basename(&tpd->entry_name, pe->path);
1934 tpd->entry_name = NULL;
1936 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1940 * Read the file index.
1941 * Checking out files is supposed to be an idempotent operation.
1942 * If the on-disk file index is incomplete we will try to complete it.
1944 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1948 tpd = SIMPLEQ_FIRST(&tree_paths);
1949 TAILQ_FOREACH(pe, paths, entry) {
1950 struct bump_base_commit_id_arg bbc_arg;
1952 err = checkout_files(worktree, fileindex, tpd->relpath,
1953 tpd->tree_id, tpd->entry_name, repo,
1954 progress_cb, progress_arg, cancel_cb, cancel_arg);
1958 bbc_arg.base_commit_id = worktree->base_commit_id;
1959 bbc_arg.entry_name = tpd->entry_name;
1960 bbc_arg.path = pe->path;
1961 bbc_arg.path_len = pe->path_len;
1962 bbc_arg.progress_cb = progress_cb;
1963 bbc_arg.progress_arg = progress_arg;
1964 err = got_fileindex_for_each_entry_safe(fileindex,
1965 bump_base_commit_id, &bbc_arg);
1969 tpd = SIMPLEQ_NEXT(tpd, entry);
1971 sync_err = sync_fileindex(fileindex, fileindex_path);
1972 if (sync_err && err == NULL)
1975 free(fileindex_path);
1977 got_object_tree_close(tree);
1979 got_object_commit_close(commit);
1981 got_fileindex_free(fileindex);
1982 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1983 tpd = SIMPLEQ_FIRST(&tree_paths);
1984 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1989 unlockerr = lock_worktree(worktree, LOCK_SH);
1990 if (unlockerr && err == NULL)
1995 struct merge_file_cb_arg {
1996 struct got_worktree *worktree;
1997 struct got_fileindex *fileindex;
1998 got_worktree_checkout_cb progress_cb;
2000 got_cancel_cb cancel_cb;
2002 const char *label_orig;
2003 struct got_object_id *commit_id2;
2006 static const struct got_error *
2007 merge_file_cb(void *arg, struct got_blob_object *blob1,
2008 struct got_blob_object *blob2, struct got_object_id *id1,
2009 struct got_object_id *id2, const char *path1, const char *path2,
2010 mode_t mode1, mode_t mode2, struct got_repository *repo)
2012 static const struct got_error *err = NULL;
2013 struct merge_file_cb_arg *a = arg;
2014 struct got_fileindex_entry *ie;
2015 char *ondisk_path = NULL;
2017 unsigned char status;
2018 int local_changes_subsumed;
2020 if (blob1 && blob2) {
2021 ie = got_fileindex_entry_get(a->fileindex, path2,
2024 return (*a->progress_cb)(a->progress_arg,
2025 GOT_STATUS_MISSING, path2);
2027 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2029 return got_error_from_errno("asprintf");
2031 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2035 if (status == GOT_STATUS_DELETE) {
2036 err = (*a->progress_cb)(a->progress_arg,
2037 GOT_STATUS_MERGE, path2);
2040 if (status != GOT_STATUS_NO_CHANGE &&
2041 status != GOT_STATUS_MODIFY &&
2042 status != GOT_STATUS_CONFLICT &&
2043 status != GOT_STATUS_ADD) {
2044 err = (*a->progress_cb)(a->progress_arg, status, path2);
2048 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2049 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2050 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2052 ie = got_fileindex_entry_get(a->fileindex, path1,
2055 return (*a->progress_cb)(a->progress_arg,
2056 GOT_STATUS_MISSING, path2);
2058 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2060 return got_error_from_errno("asprintf");
2062 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2067 case GOT_STATUS_NO_CHANGE:
2068 err = (*a->progress_cb)(a->progress_arg,
2069 GOT_STATUS_DELETE, path1);
2072 err = remove_ondisk_file(a->worktree->root_path, path1);
2076 got_fileindex_entry_mark_deleted_from_disk(ie);
2078 case GOT_STATUS_DELETE:
2079 case GOT_STATUS_MISSING:
2080 err = (*a->progress_cb)(a->progress_arg,
2081 GOT_STATUS_DELETE, path1);
2085 got_fileindex_entry_mark_deleted_from_disk(ie);
2087 case GOT_STATUS_ADD:
2088 case GOT_STATUS_MODIFY:
2089 case GOT_STATUS_CONFLICT:
2090 err = (*a->progress_cb)(a->progress_arg,
2091 GOT_STATUS_CANNOT_DELETE, path1);
2095 case GOT_STATUS_OBSTRUCTED:
2096 err = (*a->progress_cb)(a->progress_arg, status, path1);
2104 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2106 return got_error_from_errno("asprintf");
2107 ie = got_fileindex_entry_get(a->fileindex, path2,
2110 err = get_file_status(&status, &sb, ie, ondisk_path,
2114 if (status != GOT_STATUS_NO_CHANGE &&
2115 status != GOT_STATUS_MODIFY &&
2116 status != GOT_STATUS_CONFLICT &&
2117 status != GOT_STATUS_ADD) {
2118 err = (*a->progress_cb)(a->progress_arg,
2122 err = merge_blob(&local_changes_subsumed, a->worktree,
2123 NULL, ondisk_path, path2, sb.st_mode,
2124 a->label_orig, blob2, a->commit_id2, repo,
2127 if (status == GOT_STATUS_DELETE) {
2128 err = update_blob_fileindex_entry(a->worktree,
2129 a->fileindex, ie, ondisk_path, ie->path,
2135 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2136 err = install_blob(a->worktree, ondisk_path, path2,
2137 /* XXX get this from parent tree! */
2138 GOT_DEFAULT_FILE_MODE,
2139 sb.st_mode, blob2, 0, 0, repo,
2140 a->progress_cb, a->progress_arg);
2143 err = got_fileindex_entry_alloc(&ie,
2144 ondisk_path, path2, NULL, NULL);
2147 err = got_fileindex_entry_add(a->fileindex, ie);
2149 got_fileindex_entry_free(ie);
2159 struct check_merge_ok_arg {
2160 struct got_worktree *worktree;
2161 struct got_repository *repo;
2164 static const struct got_error *
2165 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2167 const struct got_error *err = NULL;
2168 struct check_merge_ok_arg *a = arg;
2169 unsigned char status;
2173 /* Reject merges into a work tree with mixed base commits. */
2174 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2175 SHA1_DIGEST_LENGTH))
2176 return got_error(GOT_ERR_MIXED_COMMITS);
2178 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2180 return got_error_from_errno("asprintf");
2182 /* Reject merges into a work tree with conflicted files. */
2183 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2186 if (status == GOT_STATUS_CONFLICT)
2187 return got_error(GOT_ERR_CONFLICTS);
2192 static const struct got_error *
2193 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2194 const char *fileindex_path, struct got_object_id *commit_id1,
2195 struct got_object_id *commit_id2, struct got_repository *repo,
2196 got_worktree_checkout_cb progress_cb, void *progress_arg,
2197 got_cancel_cb cancel_cb, void *cancel_arg)
2199 const struct got_error *err = NULL, *sync_err;
2200 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2201 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2202 struct merge_file_cb_arg arg;
2203 char *label_orig = NULL;
2208 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2209 worktree->path_prefix);
2213 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2217 err = got_object_id_str(&id_str, commit_id1);
2221 if (asprintf(&label_orig, "%s: commit %s",
2222 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2223 err = got_error_from_errno("asprintf");
2230 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2231 worktree->path_prefix);
2235 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2239 arg.worktree = worktree;
2240 arg.fileindex = fileindex;
2241 arg.progress_cb = progress_cb;
2242 arg.progress_arg = progress_arg;
2243 arg.cancel_cb = cancel_cb;
2244 arg.cancel_arg = cancel_arg;
2245 arg.label_orig = label_orig;
2246 arg.commit_id2 = commit_id2;
2247 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2248 sync_err = sync_fileindex(fileindex, fileindex_path);
2249 if (sync_err && err == NULL)
2253 got_object_tree_close(tree1);
2255 got_object_tree_close(tree2);
2260 const struct got_error *
2261 got_worktree_merge_files(struct got_worktree *worktree,
2262 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2263 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2264 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2266 const struct got_error *err, *unlockerr;
2267 char *fileindex_path = NULL;
2268 struct got_fileindex *fileindex = NULL;
2269 struct check_merge_ok_arg mok_arg;
2271 err = lock_worktree(worktree, LOCK_EX);
2275 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2279 mok_arg.worktree = worktree;
2280 mok_arg.repo = repo;
2281 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2286 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2287 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2290 got_fileindex_free(fileindex);
2291 free(fileindex_path);
2292 unlockerr = lock_worktree(worktree, LOCK_SH);
2293 if (unlockerr && err == NULL)
2298 struct diff_dir_cb_arg {
2299 struct got_fileindex *fileindex;
2300 struct got_worktree *worktree;
2301 const char *status_path;
2302 size_t status_path_len;
2303 struct got_repository *repo;
2304 got_worktree_status_cb status_cb;
2306 got_cancel_cb cancel_cb;
2308 /* A pathlist containing per-directory pathlists of ignore patterns. */
2309 struct got_pathlist_head ignores;
2312 static const struct got_error *
2313 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2314 got_worktree_status_cb status_cb, void *status_arg,
2315 struct got_repository *repo)
2317 const struct got_error *err = NULL;
2318 unsigned char status = GOT_STATUS_NO_CHANGE;
2319 unsigned char staged_status = get_staged_status(ie);
2321 struct got_object_id blob_id, commit_id, staged_blob_id;
2322 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2323 struct got_object_id *staged_blob_idp = NULL;
2325 err = get_file_status(&status, &sb, ie, abspath, repo);
2329 if (status == GOT_STATUS_NO_CHANGE &&
2330 staged_status == GOT_STATUS_NO_CHANGE)
2333 if (got_fileindex_entry_has_blob(ie)) {
2334 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2335 blob_idp = &blob_id;
2337 if (got_fileindex_entry_has_commit(ie)) {
2338 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2339 commit_idp = &commit_id;
2341 if (staged_status == GOT_STATUS_ADD ||
2342 staged_status == GOT_STATUS_MODIFY) {
2343 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2344 SHA1_DIGEST_LENGTH);
2345 staged_blob_idp = &staged_blob_id;
2348 return (*status_cb)(status_arg, status, staged_status,
2349 ie->path, blob_idp, staged_blob_idp, commit_idp);
2352 static const struct got_error *
2353 status_old_new(void *arg, struct got_fileindex_entry *ie,
2354 struct dirent *de, const char *parent_path)
2356 const struct got_error *err = NULL;
2357 struct diff_dir_cb_arg *a = arg;
2360 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2361 return got_error(GOT_ERR_CANCELLED);
2363 if (got_path_cmp(parent_path, a->status_path,
2364 strlen(parent_path), a->status_path_len) != 0 &&
2365 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2368 if (parent_path[0]) {
2369 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2370 parent_path, de->d_name) == -1)
2371 return got_error_from_errno("asprintf");
2373 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2375 return got_error_from_errno("asprintf");
2378 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2384 static const struct got_error *
2385 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2387 struct diff_dir_cb_arg *a = arg;
2388 struct got_object_id blob_id, commit_id;
2389 unsigned char status;
2391 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2392 return got_error(GOT_ERR_CANCELLED);
2394 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2397 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2398 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2399 if (got_fileindex_entry_has_file_on_disk(ie))
2400 status = GOT_STATUS_MISSING;
2402 status = GOT_STATUS_DELETE;
2403 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2404 ie->path, &blob_id, NULL, &commit_id);
2408 free_ignorelist(struct got_pathlist_head *ignorelist)
2410 struct got_pathlist_entry *pe;
2412 TAILQ_FOREACH(pe, ignorelist, entry)
2413 free((char *)pe->path);
2414 got_pathlist_free(ignorelist);
2418 free_ignores(struct got_pathlist_head *ignores)
2420 struct got_pathlist_entry *pe;
2422 TAILQ_FOREACH(pe, ignores, entry) {
2423 struct got_pathlist_head *ignorelist = pe->data;
2424 free_ignorelist(ignorelist);
2425 free((char *)pe->path);
2427 got_pathlist_free(ignores);
2430 static const struct got_error *
2431 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2433 const struct got_error *err = NULL;
2434 struct got_pathlist_entry *pe = NULL;
2435 struct got_pathlist_head *ignorelist;
2436 char *line = NULL, *pattern, *dirpath = NULL;
2437 size_t linesize = 0;
2440 ignorelist = calloc(1, sizeof(*ignorelist));
2441 if (ignorelist == NULL)
2442 return got_error_from_errno("calloc");
2443 TAILQ_INIT(ignorelist);
2445 while ((linelen = getline(&line, &linesize, f)) != -1) {
2446 if (linelen > 0 && line[linelen - 1] == '\n')
2447 line[linelen - 1] = '\0';
2449 /* Git's ignores may contain comments. */
2453 /* Git's negated patterns are not (yet?) supported. */
2457 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2459 err = got_error_from_errno("asprintf");
2462 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2467 err = got_error_from_errno("getline");
2471 dirpath = strdup(path);
2472 if (dirpath == NULL) {
2473 err = got_error_from_errno("strdup");
2476 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2479 if (err || pe == NULL) {
2481 free_ignorelist(ignorelist);
2487 match_ignores(struct got_pathlist_head *ignores, const char *path)
2489 struct got_pathlist_entry *pe;
2491 /* Handle patterns which match in all directories. */
2492 TAILQ_FOREACH(pe, ignores, entry) {
2493 struct got_pathlist_head *ignorelist = pe->data;
2494 struct got_pathlist_entry *pi;
2496 TAILQ_FOREACH(pi, ignorelist, entry) {
2497 const char *p, *pattern = pi->path;
2499 if (strncmp(pattern, "**/", 3) != 0)
2504 if (fnmatch(pattern, p,
2505 FNM_PATHNAME | FNM_LEADING_DIR)) {
2506 /* Retry in next directory. */
2507 while (*p && *p != '/')
2519 * The ignores pathlist contains ignore lists from children before
2520 * parents, so we can find the most specific ignorelist by walking
2521 * ignores backwards.
2523 pe = TAILQ_LAST(ignores, got_pathlist_head);
2525 if (got_path_is_child(path, pe->path, pe->path_len)) {
2526 struct got_pathlist_head *ignorelist = pe->data;
2527 struct got_pathlist_entry *pi;
2528 TAILQ_FOREACH(pi, ignorelist, entry) {
2529 const char *pattern = pi->path;
2530 int flags = FNM_LEADING_DIR;
2531 if (strstr(pattern, "/**/") == NULL)
2532 flags |= FNM_PATHNAME;
2533 if (fnmatch(pattern, path, flags))
2538 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2544 static const struct got_error *
2545 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2546 const char *path, const char *ignores_filename)
2548 const struct got_error *err = NULL;
2550 FILE *ignoresfile = NULL;
2552 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2553 path[0] ? "/" : "", ignores_filename) == -1)
2554 return got_error_from_errno("asprintf");
2556 ignoresfile = fopen(ignorespath, "r");
2557 if (ignoresfile == NULL) {
2558 if (errno != ENOENT && errno != EACCES)
2559 err = got_error_from_errno2("fopen",
2562 err = read_ignores(ignores, path, ignoresfile);
2564 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2565 err = got_error_from_errno2("flose", path);
2570 static const struct got_error *
2571 status_new(void *arg, struct dirent *de, const char *parent_path)
2573 const struct got_error *err = NULL;
2574 struct diff_dir_cb_arg *a = arg;
2577 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2578 return got_error(GOT_ERR_CANCELLED);
2580 /* XXX ignore symlinks for now */
2581 if (de->d_type == DT_LNK)
2584 if (parent_path[0]) {
2585 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2586 return got_error_from_errno("asprintf");
2591 if (de->d_type == DT_DIR) {
2592 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2595 err = add_ignores(&a->ignores, a->worktree->root_path,
2596 path, ".gitignore");
2598 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2599 && !match_ignores(&a->ignores, path))
2600 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2601 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2607 static const struct got_error *
2608 report_single_file_status(const char *path, const char *ondisk_path,
2609 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2610 void *status_arg, struct got_repository *repo)
2612 struct got_fileindex_entry *ie;
2615 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2617 return report_file_status(ie, ondisk_path, status_cb,
2620 if (lstat(ondisk_path, &sb) == -1) {
2621 if (errno != ENOENT)
2622 return got_error_from_errno2("lstat", ondisk_path);
2623 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2624 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2628 if (S_ISREG(sb.st_mode))
2629 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2630 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2635 static const struct got_error *
2636 worktree_status(struct got_worktree *worktree, const char *path,
2637 struct got_fileindex *fileindex, struct got_repository *repo,
2638 got_worktree_status_cb status_cb, void *status_arg,
2639 got_cancel_cb cancel_cb, void *cancel_arg)
2641 const struct got_error *err = NULL;
2642 DIR *workdir = NULL;
2643 struct got_fileindex_diff_dir_cb fdiff_cb;
2644 struct diff_dir_cb_arg arg;
2645 char *ondisk_path = NULL;
2647 if (asprintf(&ondisk_path, "%s%s%s",
2648 worktree->root_path, path[0] ? "/" : "", path) == -1)
2649 return got_error_from_errno("asprintf");
2651 workdir = opendir(ondisk_path);
2652 if (workdir == NULL) {
2653 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2654 err = got_error_from_errno2("opendir", ondisk_path);
2656 err = report_single_file_status(path, ondisk_path,
2657 fileindex, status_cb, status_arg, repo);
2659 fdiff_cb.diff_old_new = status_old_new;
2660 fdiff_cb.diff_old = status_old;
2661 fdiff_cb.diff_new = status_new;
2662 arg.fileindex = fileindex;
2663 arg.worktree = worktree;
2664 arg.status_path = path;
2665 arg.status_path_len = strlen(path);
2667 arg.status_cb = status_cb;
2668 arg.status_arg = status_arg;
2669 arg.cancel_cb = cancel_cb;
2670 arg.cancel_arg = cancel_arg;
2671 TAILQ_INIT(&arg.ignores);
2672 err = add_ignores(&arg.ignores, worktree->root_path, path,
2675 err = add_ignores(&arg.ignores, worktree->root_path,
2676 path, ".gitignore");
2678 err = got_fileindex_diff_dir(fileindex, workdir,
2679 worktree->root_path, path, repo, &fdiff_cb, &arg);
2680 free_ignores(&arg.ignores);
2689 const struct got_error *
2690 got_worktree_status(struct got_worktree *worktree,
2691 struct got_pathlist_head *paths, struct got_repository *repo,
2692 got_worktree_status_cb status_cb, void *status_arg,
2693 got_cancel_cb cancel_cb, void *cancel_arg)
2695 const struct got_error *err = NULL;
2696 char *fileindex_path = NULL;
2697 struct got_fileindex *fileindex = NULL;
2698 struct got_pathlist_entry *pe;
2700 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2704 TAILQ_FOREACH(pe, paths, entry) {
2705 err = worktree_status(worktree, pe->path, fileindex, repo,
2706 status_cb, status_arg, cancel_cb, cancel_arg);
2710 free(fileindex_path);
2711 got_fileindex_free(fileindex);
2715 const struct got_error *
2716 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2719 const struct got_error *err = NULL;
2720 char *resolved, *cwd = NULL, *path = NULL;
2725 resolved = realpath(arg, NULL);
2726 if (resolved == NULL) {
2727 if (errno != ENOENT)
2728 return got_error_from_errno2("realpath", arg);
2729 cwd = getcwd(NULL, 0);
2731 return got_error_from_errno("getcwd");
2732 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2733 err = got_error_from_errno("asprintf");
2738 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2739 strlen(got_worktree_get_root_path(worktree)))) {
2740 err = got_error(GOT_ERR_BAD_PATH);
2744 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2745 err = got_path_skip_common_ancestor(&path,
2746 got_worktree_get_root_path(worktree), resolved);
2752 err = got_error_from_errno("strdup");
2757 /* XXX status walk can't deal with trailing slash! */
2759 while (len > 0 && path[len - 1] == '/') {
2760 path[len - 1] = '\0';
2773 static const struct got_error *
2774 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2775 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2776 struct got_repository *repo)
2778 const struct got_error *err = NULL;
2779 struct got_fileindex_entry *ie;
2780 unsigned char status;
2783 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2785 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2788 /* Re-adding an existing entry is a no-op. */
2789 if (status == GOT_STATUS_ADD)
2791 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2794 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2798 err = got_fileindex_entry_add(fileindex, ie);
2800 got_fileindex_entry_free(ie);
2804 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2807 const struct got_error *
2808 got_worktree_schedule_add(struct got_worktree *worktree,
2809 struct got_pathlist_head *paths,
2810 got_worktree_status_cb status_cb, void *status_arg,
2811 struct got_repository *repo)
2813 struct got_fileindex *fileindex = NULL;
2814 char *fileindex_path = NULL;
2815 const struct got_error *err = NULL, *sync_err, *unlockerr;
2816 struct got_pathlist_entry *pe;
2818 err = lock_worktree(worktree, LOCK_EX);
2822 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2826 TAILQ_FOREACH(pe, paths, entry) {
2828 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2830 return got_error_from_errno("asprintf");
2831 err = schedule_addition(ondisk_path, fileindex, pe->path,
2832 status_cb, status_arg, repo);
2837 sync_err = sync_fileindex(fileindex, fileindex_path);
2838 if (sync_err && err == NULL)
2841 free(fileindex_path);
2843 got_fileindex_free(fileindex);
2844 unlockerr = lock_worktree(worktree, LOCK_SH);
2845 if (unlockerr && err == NULL)
2850 static const struct got_error *
2851 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2852 const char *relpath, int delete_local_mods,
2853 got_worktree_status_cb status_cb, void *status_arg,
2854 struct got_repository *repo)
2856 const struct got_error *err = NULL;
2857 struct got_fileindex_entry *ie = NULL;
2858 unsigned char status, staged_status;
2861 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2863 return got_error(GOT_ERR_BAD_PATH);
2865 staged_status = get_staged_status(ie);
2866 if (staged_status != GOT_STATUS_NO_CHANGE) {
2867 if (staged_status == GOT_STATUS_DELETE)
2869 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2872 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2876 if (status != GOT_STATUS_NO_CHANGE) {
2877 if (status == GOT_STATUS_DELETE)
2879 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2880 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2881 if (status != GOT_STATUS_MODIFY &&
2882 status != GOT_STATUS_MISSING)
2883 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2886 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2887 return got_error_from_errno2("unlink", ondisk_path);
2889 got_fileindex_entry_mark_deleted_from_disk(ie);
2890 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2893 const struct got_error *
2894 got_worktree_schedule_delete(struct got_worktree *worktree,
2895 struct got_pathlist_head *paths, int delete_local_mods,
2896 got_worktree_status_cb status_cb, void *status_arg,
2897 struct got_repository *repo)
2899 struct got_fileindex *fileindex = NULL;
2900 char *fileindex_path = NULL;
2901 const struct got_error *err = NULL, *sync_err, *unlockerr;
2902 struct got_pathlist_entry *pe;
2904 err = lock_worktree(worktree, LOCK_EX);
2908 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2912 TAILQ_FOREACH(pe, paths, entry) {
2914 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2916 return got_error_from_errno("asprintf");
2917 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2918 delete_local_mods, status_cb, status_arg, repo);
2923 sync_err = sync_fileindex(fileindex, fileindex_path);
2924 if (sync_err && err == NULL)
2927 free(fileindex_path);
2929 got_fileindex_free(fileindex);
2930 unlockerr = lock_worktree(worktree, LOCK_SH);
2931 if (unlockerr && err == NULL)
2936 static const struct got_error *
2937 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2939 const struct got_error *err = NULL;
2941 size_t linesize = 0, n;
2944 linelen = getline(&line, &linesize, infile);
2945 if (linelen == -1) {
2946 if (ferror(infile)) {
2947 err = got_error_from_errno("getline");
2953 n = fwrite(line, 1, linelen, outfile);
2955 err = got_ferror(outfile, GOT_ERR_IO);
2960 n = fwrite(line, 1, linelen, rejectfile);
2962 err = got_ferror(outfile, GOT_ERR_IO);
2969 static const struct got_error *
2970 skip_one_line(FILE *f)
2973 size_t linesize = 0;
2976 linelen = getline(&line, &linesize, f);
2977 if (linelen == -1) {
2979 return got_error_from_errno("getline");
2986 static const struct got_error *
2987 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2988 int start_old, int end_old, int start_new, int end_new,
2989 FILE *outfile, FILE *rejectfile)
2991 const struct got_error *err;
2993 /* Copy old file's lines leading up to patch. */
2994 while (!feof(f1) && *line_cur1 < start_old) {
2995 err = copy_one_line(f1, outfile, NULL);
3000 /* Skip new file's lines leading up to patch. */
3001 while (!feof(f2) && *line_cur2 < start_new) {
3003 err = copy_one_line(f2, NULL, rejectfile);
3005 err = skip_one_line(f2);
3010 /* Copy patched lines. */
3011 while (!feof(f2) && *line_cur2 <= end_new) {
3012 err = copy_one_line(f2, outfile, NULL);
3017 /* Skip over old file's replaced lines. */
3018 while (!feof(f1) && *line_cur1 <= end_old) {
3020 err = copy_one_line(f1, NULL, rejectfile);
3022 err = skip_one_line(f1);
3031 static const struct got_error *
3032 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3033 FILE *outfile, FILE *rejectfile)
3035 const struct got_error *err;
3038 /* Copy old file's lines until EOF. */
3040 err = copy_one_line(f1, outfile, NULL);
3047 /* Copy new file's lines until EOF. */
3049 err = copy_one_line(f2, NULL, rejectfile);
3059 static const struct got_error *
3060 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3061 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3062 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3063 int *line_cur2, FILE *outfile, FILE *rejectfile,
3064 got_worktree_patch_cb patch_cb, void *patch_arg)
3066 const struct got_error *err = NULL;
3067 int start_old = change->cv.a;
3068 int end_old = change->cv.b;
3069 int start_new = change->cv.c;
3070 int end_new = change->cv.d;
3074 *choice = GOT_PATCH_CHOICE_NONE;
3076 hunkfile = got_opentemp();
3077 if (hunkfile == NULL)
3078 return got_error_from_errno("got_opentemp");
3083 /* XXX TODO needs error checking */
3084 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3086 if (fseek(f1, pos1, SEEK_SET) == -1) {
3087 err = got_ferror(f1, GOT_ERR_IO);
3090 if (fseek(f2, pos2, SEEK_SET) == -1) {
3091 err = got_ferror(f1, GOT_ERR_IO);
3094 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3095 err = got_ferror(hunkfile, GOT_ERR_IO);
3099 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3100 hunkfile, n, nchanges);
3105 case GOT_PATCH_CHOICE_YES:
3106 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3107 end_old, start_new, end_new, outfile, rejectfile);
3109 case GOT_PATCH_CHOICE_NO:
3110 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3111 end_old, start_new, end_new, rejectfile, outfile);
3113 case GOT_PATCH_CHOICE_QUIT:
3116 err = got_error(GOT_ERR_PATCH_CHOICE);
3120 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3121 err = got_error_from_errno("fclose");
3125 struct revert_file_args {
3126 struct got_worktree *worktree;
3127 struct got_fileindex *fileindex;
3128 got_worktree_checkout_cb progress_cb;
3130 got_worktree_patch_cb patch_cb;
3132 struct got_repository *repo;
3135 static const struct got_error *
3136 create_patched_content(char **path_outfile, int reverse_patch,
3137 struct got_object_id *blob_id, const char *path2,
3138 const char *relpath, struct got_repository *repo,
3139 got_worktree_patch_cb patch_cb, void *patch_arg)
3141 const struct got_error *err;
3142 struct got_blob_object *blob = NULL;
3143 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3145 char *path1 = NULL, *id_str = NULL;
3146 struct stat sb1, sb2;
3147 struct got_diff_changes *changes = NULL;
3148 struct got_diff_state *ds = NULL;
3149 struct got_diff_args *args = NULL;
3150 struct got_diff_change *change;
3151 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3154 *path_outfile = NULL;
3156 err = got_object_id_str(&id_str, blob_id);
3160 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3162 err = got_error_from_errno2("open", path2);
3165 if (fstat(fd2, &sb2) == -1) {
3166 err = got_error_from_errno2("fstat", path2);
3170 f2 = fdopen(fd2, "r");
3172 err = got_error_from_errno2("fopen", path2);
3177 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3181 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3185 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3189 if (stat(path1, &sb1) == -1) {
3190 err = got_error_from_errno2("stat", path1);
3194 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3195 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3199 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3203 if (fseek(f1, 0L, SEEK_SET) == -1)
3204 return got_ferror(f1, GOT_ERR_IO);
3205 if (fseek(f2, 0L, SEEK_SET) == -1)
3206 return got_ferror(f2, GOT_ERR_IO);
3207 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3209 err = apply_or_reject_change(&choice, change, ++n,
3210 changes->nchanges, ds, args, diff_flags, relpath,
3211 f1, f2, &line_cur1, &line_cur2,
3212 reverse_patch ? NULL : outfile,
3213 reverse_patch ? outfile : NULL,
3214 patch_cb, patch_arg);
3217 if (choice == GOT_PATCH_CHOICE_YES)
3219 else if (choice == GOT_PATCH_CHOICE_QUIT)
3223 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3224 reverse_patch ? NULL : outfile,
3225 reverse_patch ? outfile : NULL);
3229 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3230 err = got_error_from_errno2("chmod", path2);
3237 got_object_blob_close(blob);
3238 if (f1 && fclose(f1) == EOF && err == NULL)
3239 err = got_error_from_errno2("fclose", path1);
3240 if (f2 && fclose(f2) == EOF && err == NULL)
3241 err = got_error_from_errno2("fclose", path2);
3242 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3243 err = got_error_from_errno2("close", path2);
3244 if (outfile && fclose(outfile) == EOF && err == NULL)
3245 err = got_error_from_errno2("fclose", *path_outfile);
3246 if (path1 && unlink(path1) == -1 && err == NULL)
3247 err = got_error_from_errno2("unlink", path1);
3248 if (err || !have_content) {
3249 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3250 err = got_error_from_errno2("unlink", *path_outfile);
3251 free(*path_outfile);
3252 *path_outfile = NULL;
3256 got_diff_state_free(ds);
3260 got_diff_free_changes(changes);
3265 static const struct got_error *
3266 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3267 const char *relpath, struct got_object_id *blob_id,
3268 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3270 struct revert_file_args *a = arg;
3271 const struct got_error *err = NULL;
3272 char *parent_path = NULL;
3273 struct got_fileindex_entry *ie;
3274 struct got_tree_object *tree = NULL;
3275 struct got_object_id *tree_id = NULL;
3276 const struct got_tree_entry *te = NULL;
3277 char *tree_path = NULL, *te_name;
3278 char *ondisk_path = NULL, *path_content = NULL;
3279 struct got_blob_object *blob = NULL;
3281 /* Reverting a staged deletion is a no-op. */
3282 if (status == GOT_STATUS_DELETE &&
3283 staged_status != GOT_STATUS_NO_CHANGE)
3286 if (status == GOT_STATUS_UNVERSIONED)
3287 return (*a->progress_cb)(a->progress_arg,
3288 GOT_STATUS_UNVERSIONED, relpath);
3290 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3292 return got_error(GOT_ERR_BAD_PATH);
3294 /* Construct in-repository path of tree which contains this blob. */
3295 err = got_path_dirname(&parent_path, ie->path);
3297 if (err->code != GOT_ERR_BAD_PATH)
3299 parent_path = strdup("/");
3300 if (parent_path == NULL) {
3301 err = got_error_from_errno("strdup");
3305 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3306 tree_path = strdup(parent_path);
3307 if (tree_path == NULL) {
3308 err = got_error_from_errno("strdup");
3312 if (got_path_is_root_dir(parent_path)) {
3313 tree_path = strdup(a->worktree->path_prefix);
3314 if (tree_path == NULL) {
3315 err = got_error_from_errno("strdup");
3319 if (asprintf(&tree_path, "%s/%s",
3320 a->worktree->path_prefix, parent_path) == -1) {
3321 err = got_error_from_errno("asprintf");
3327 err = got_object_id_by_path(&tree_id, a->repo,
3328 a->worktree->base_commit_id, tree_path);
3330 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3331 (status == GOT_STATUS_ADD ||
3332 staged_status == GOT_STATUS_ADD)))
3335 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3339 te_name = basename(ie->path);
3340 if (te_name == NULL) {
3341 err = got_error_from_errno2("basename", ie->path);
3345 te = got_object_tree_find_entry(tree, te_name);
3346 if (te == NULL && status != GOT_STATUS_ADD &&
3347 staged_status != GOT_STATUS_ADD) {
3348 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3354 case GOT_STATUS_ADD:
3356 int choice = GOT_PATCH_CHOICE_NONE;
3357 err = (*a->patch_cb)(&choice, a->patch_arg,
3358 status, ie->path, NULL, 1, 1);
3361 if (choice != GOT_PATCH_CHOICE_YES)
3364 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3368 got_fileindex_entry_remove(a->fileindex, ie);
3370 case GOT_STATUS_DELETE:
3372 int choice = GOT_PATCH_CHOICE_NONE;
3373 err = (*a->patch_cb)(&choice, a->patch_arg,
3374 status, ie->path, NULL, 1, 1);
3377 if (choice != GOT_PATCH_CHOICE_YES)
3381 case GOT_STATUS_MODIFY:
3382 case GOT_STATUS_MODE_CHANGE:
3383 case GOT_STATUS_CONFLICT:
3384 case GOT_STATUS_MISSING: {
3385 struct got_object_id id;
3386 if (staged_status == GOT_STATUS_ADD ||
3387 staged_status == GOT_STATUS_MODIFY) {
3388 memcpy(id.sha1, ie->staged_blob_sha1,
3389 SHA1_DIGEST_LENGTH);
3391 memcpy(id.sha1, ie->blob_sha1,
3392 SHA1_DIGEST_LENGTH);
3393 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3397 if (asprintf(&ondisk_path, "%s/%s",
3398 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3399 err = got_error_from_errno("asprintf");
3403 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3404 status == GOT_STATUS_CONFLICT)) {
3405 err = create_patched_content(&path_content, 1, &id,
3406 ondisk_path, ie->path, a->repo,
3407 a->patch_cb, a->patch_arg);
3408 if (err || path_content == NULL)
3410 if (rename(path_content, ondisk_path) == -1) {
3411 err = got_error_from_errno3("rename",
3412 path_content, ondisk_path);
3416 err = install_blob(a->worktree, ondisk_path, ie->path,
3417 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3418 got_fileindex_perms_to_st(ie), blob, 0, 1,
3419 a->repo, a->progress_cb, a->progress_arg);
3422 if (status == GOT_STATUS_DELETE ||
3423 status == GOT_STATUS_MODE_CHANGE) {
3424 err = update_blob_fileindex_entry(a->worktree,
3425 a->fileindex, ie, ondisk_path, ie->path,
3442 got_object_blob_close(blob);
3444 got_object_tree_close(tree);
3449 const struct got_error *
3450 got_worktree_revert(struct got_worktree *worktree,
3451 struct got_pathlist_head *paths,
3452 got_worktree_checkout_cb progress_cb, void *progress_arg,
3453 got_worktree_patch_cb patch_cb, void *patch_arg,
3454 struct got_repository *repo)
3456 struct got_fileindex *fileindex = NULL;
3457 char *fileindex_path = NULL;
3458 const struct got_error *err = NULL, *unlockerr = NULL;
3459 const struct got_error *sync_err = NULL;
3460 struct got_pathlist_entry *pe;
3461 struct revert_file_args rfa;
3463 err = lock_worktree(worktree, LOCK_EX);
3467 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3471 rfa.worktree = worktree;
3472 rfa.fileindex = fileindex;
3473 rfa.progress_cb = progress_cb;
3474 rfa.progress_arg = progress_arg;
3475 rfa.patch_cb = patch_cb;
3476 rfa.patch_arg = patch_arg;
3478 TAILQ_FOREACH(pe, paths, entry) {
3479 err = worktree_status(worktree, pe->path, fileindex, repo,
3480 revert_file, &rfa, NULL, NULL);
3484 sync_err = sync_fileindex(fileindex, fileindex_path);
3485 if (sync_err && err == NULL)
3488 free(fileindex_path);
3490 got_fileindex_free(fileindex);
3491 unlockerr = lock_worktree(worktree, LOCK_SH);
3492 if (unlockerr && err == NULL)
3498 free_commitable(struct got_commitable *ct)
3501 free(ct->in_repo_path);
3502 free(ct->ondisk_path);
3504 free(ct->base_blob_id);
3505 free(ct->staged_blob_id);
3506 free(ct->base_commit_id);
3510 struct collect_commitables_arg {
3511 struct got_pathlist_head *commitable_paths;
3512 struct got_repository *repo;
3513 struct got_worktree *worktree;
3514 int have_staged_files;
3517 static const struct got_error *
3518 collect_commitables(void *arg, unsigned char status,
3519 unsigned char staged_status, const char *relpath,
3520 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3521 struct got_object_id *commit_id)
3523 struct collect_commitables_arg *a = arg;
3524 const struct got_error *err = NULL;
3525 struct got_commitable *ct = NULL;
3526 struct got_pathlist_entry *new = NULL;
3527 char *parent_path = NULL, *path = NULL;
3530 if (a->have_staged_files) {
3531 if (staged_status != GOT_STATUS_MODIFY &&
3532 staged_status != GOT_STATUS_ADD &&
3533 staged_status != GOT_STATUS_DELETE)
3536 if (status == GOT_STATUS_CONFLICT)
3537 return got_error(GOT_ERR_COMMIT_CONFLICT);
3539 if (status != GOT_STATUS_MODIFY &&
3540 status != GOT_STATUS_MODE_CHANGE &&
3541 status != GOT_STATUS_ADD &&
3542 status != GOT_STATUS_DELETE)
3546 if (asprintf(&path, "/%s", relpath) == -1) {
3547 err = got_error_from_errno("asprintf");
3550 if (strcmp(path, "/") == 0) {
3551 parent_path = strdup("");
3552 if (parent_path == NULL)
3553 return got_error_from_errno("strdup");
3555 err = got_path_dirname(&parent_path, path);
3560 ct = calloc(1, sizeof(*ct));
3562 err = got_error_from_errno("calloc");
3566 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3568 err = got_error_from_errno("asprintf");
3571 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3572 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3574 if (lstat(ct->ondisk_path, &sb) != 0) {
3575 err = got_error_from_errno2("lstat", ct->ondisk_path);
3578 ct->mode = sb.st_mode;
3581 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3582 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3584 err = got_error_from_errno("asprintf");
3588 ct->status = status;
3589 ct->staged_status = staged_status;
3590 ct->blob_id = NULL; /* will be filled in when blob gets created */
3591 if (ct->status != GOT_STATUS_ADD &&
3592 ct->staged_status != GOT_STATUS_ADD) {
3593 ct->base_blob_id = got_object_id_dup(blob_id);
3594 if (ct->base_blob_id == NULL) {
3595 err = got_error_from_errno("got_object_id_dup");
3598 ct->base_commit_id = got_object_id_dup(commit_id);
3599 if (ct->base_commit_id == NULL) {
3600 err = got_error_from_errno("got_object_id_dup");
3604 if (ct->staged_status == GOT_STATUS_ADD ||
3605 ct->staged_status == GOT_STATUS_MODIFY) {
3606 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3607 if (ct->staged_blob_id == NULL) {
3608 err = got_error_from_errno("got_object_id_dup");
3612 ct->path = strdup(path);
3613 if (ct->path == NULL) {
3614 err = got_error_from_errno("strdup");
3617 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3619 if (ct && (err || new == NULL))
3620 free_commitable(ct);
3626 static const struct got_error *write_tree(struct got_object_id **,
3627 struct got_tree_object *, const char *, struct got_pathlist_head *,
3628 got_worktree_status_cb status_cb, void *status_arg,
3629 struct got_repository *);
3631 static const struct got_error *
3632 write_subtree(struct got_object_id **new_subtree_id,
3633 struct got_tree_entry *te, const char *parent_path,
3634 struct got_pathlist_head *commitable_paths,
3635 got_worktree_status_cb status_cb, void *status_arg,
3636 struct got_repository *repo)
3638 const struct got_error *err = NULL;
3639 struct got_tree_object *subtree;
3642 if (asprintf(&subpath, "%s%s%s", parent_path,
3643 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3644 return got_error_from_errno("asprintf");
3646 err = got_object_open_as_tree(&subtree, repo, te->id);
3650 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3651 status_cb, status_arg, repo);
3652 got_object_tree_close(subtree);
3657 static const struct got_error *
3658 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3660 const struct got_error *err = NULL;
3661 char *ct_parent_path = NULL;
3665 if (strchr(ct->in_repo_path, '/') == NULL) {
3666 *match = got_path_is_root_dir(path);
3670 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3673 *match = (strcmp(path, ct_parent_path) == 0);
3674 free(ct_parent_path);
3679 get_ct_file_mode(struct got_commitable *ct)
3681 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3684 static const struct got_error *
3685 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3686 struct got_tree_entry *te, struct got_commitable *ct)
3688 const struct got_error *err = NULL;
3692 err = got_object_tree_entry_dup(new_te, te);
3696 (*new_te)->mode = get_ct_file_mode(ct);
3698 free((*new_te)->id);
3699 if (ct->staged_status == GOT_STATUS_MODIFY)
3700 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3702 (*new_te)->id = got_object_id_dup(ct->blob_id);
3703 if ((*new_te)->id == NULL) {
3704 err = got_error_from_errno("got_object_id_dup");
3708 if (err && *new_te) {
3709 got_object_tree_entry_close(*new_te);
3715 static const struct got_error *
3716 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3717 struct got_commitable *ct)
3719 const struct got_error *err = NULL;
3724 *new_te = calloc(1, sizeof(**new_te));
3725 if (*new_te == NULL)
3726 return got_error_from_errno("calloc");
3728 ct_name = basename(ct->path);
3729 if (ct_name == NULL) {
3730 err = got_error_from_errno2("basename", ct->path);
3733 (*new_te)->name = strdup(ct_name);
3734 if ((*new_te)->name == NULL) {
3735 err = got_error_from_errno("strdup");
3739 (*new_te)->mode = get_ct_file_mode(ct);
3741 if (ct->staged_status == GOT_STATUS_ADD)
3742 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3744 (*new_te)->id = got_object_id_dup(ct->blob_id);
3745 if ((*new_te)->id == NULL) {
3746 err = got_error_from_errno("got_object_id_dup");
3750 if (err && *new_te) {
3751 got_object_tree_entry_close(*new_te);
3757 static const struct got_error *
3758 insert_tree_entry(struct got_tree_entry *new_te,
3759 struct got_pathlist_head *paths)
3761 const struct got_error *err = NULL;
3762 struct got_pathlist_entry *new_pe;
3764 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3768 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3772 static const struct got_error *
3773 report_ct_status(struct got_commitable *ct,
3774 got_worktree_status_cb status_cb, void *status_arg)
3776 const char *ct_path = ct->path;
3777 unsigned char status;
3779 while (ct_path[0] == '/')
3782 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3783 status = ct->staged_status;
3785 status = ct->status;
3787 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3788 ct_path, ct->blob_id, NULL, NULL);
3791 static const struct got_error *
3792 match_modified_subtree(int *modified, struct got_tree_entry *te,
3793 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3795 const struct got_error *err = NULL;
3796 struct got_pathlist_entry *pe;
3801 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3802 got_path_is_root_dir(base_tree_path) ? "" : "/",
3804 return got_error_from_errno("asprintf");
3806 TAILQ_FOREACH(pe, commitable_paths, entry) {
3807 struct got_commitable *ct = pe->data;
3808 *modified = got_path_is_child(ct->in_repo_path, te_path,
3818 static const struct got_error *
3819 match_deleted_or_modified_ct(struct got_commitable **ctp,
3820 struct got_tree_entry *te, const char *base_tree_path,
3821 struct got_pathlist_head *commitable_paths)
3823 const struct got_error *err = NULL;
3824 struct got_pathlist_entry *pe;
3828 TAILQ_FOREACH(pe, commitable_paths, entry) {
3829 struct got_commitable *ct = pe->data;
3830 char *ct_name = NULL;
3833 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3834 if (ct->status != GOT_STATUS_MODIFY &&
3835 ct->status != GOT_STATUS_MODE_CHANGE &&
3836 ct->status != GOT_STATUS_DELETE)
3839 if (ct->staged_status != GOT_STATUS_MODIFY &&
3840 ct->staged_status != GOT_STATUS_DELETE)
3844 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3847 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3853 ct_name = basename(pe->path);
3854 if (ct_name == NULL)
3855 return got_error_from_errno2("basename", pe->path);
3857 if (strcmp(te->name, ct_name) != 0)
3867 static const struct got_error *
3868 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3869 const char *child_path, const char *path_base_tree,
3870 struct got_pathlist_head *commitable_paths,
3871 got_worktree_status_cb status_cb, void *status_arg,
3872 struct got_repository *repo)
3874 const struct got_error *err = NULL;
3875 struct got_tree_entry *new_te;
3880 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3881 got_path_is_root_dir(path_base_tree) ? "" : "/",
3883 return got_error_from_errno("asprintf");
3885 new_te = calloc(1, sizeof(*new_te));
3887 return got_error_from_errno("calloc");
3888 new_te->mode = S_IFDIR;
3889 new_te->name = strdup(child_path);
3890 if (new_te->name == NULL) {
3891 err = got_error_from_errno("strdup");
3892 got_object_tree_entry_close(new_te);
3895 err = write_tree(&new_te->id, NULL, subtree_path,
3896 commitable_paths, status_cb, status_arg, repo);
3898 got_object_tree_entry_close(new_te);
3908 static const struct got_error *
3909 write_tree(struct got_object_id **new_tree_id,
3910 struct got_tree_object *base_tree, const char *path_base_tree,
3911 struct got_pathlist_head *commitable_paths,
3912 got_worktree_status_cb status_cb, void *status_arg,
3913 struct got_repository *repo)
3915 const struct got_error *err = NULL;
3916 const struct got_tree_entries *base_entries = NULL;
3917 struct got_pathlist_head paths;
3918 struct got_tree_entries new_tree_entries;
3919 struct got_tree_entry *te, *new_te = NULL;
3920 struct got_pathlist_entry *pe;
3923 new_tree_entries.nentries = 0;
3924 SIMPLEQ_INIT(&new_tree_entries.head);
3926 /* Insert, and recurse into, newly added entries first. */
3927 TAILQ_FOREACH(pe, commitable_paths, entry) {
3928 struct got_commitable *ct = pe->data;
3929 char *child_path = NULL, *slash;
3931 if ((ct->status != GOT_STATUS_ADD &&
3932 ct->staged_status != GOT_STATUS_ADD) ||
3933 (ct->flags & GOT_COMMITABLE_ADDED))
3936 if (!got_path_is_child(pe->path, path_base_tree,
3937 strlen(path_base_tree)))
3940 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3945 slash = strchr(child_path, '/');
3946 if (slash == NULL) {
3947 err = alloc_added_blob_tree_entry(&new_te, ct);
3950 err = report_ct_status(ct, status_cb, status_arg);
3953 ct->flags |= GOT_COMMITABLE_ADDED;
3954 err = insert_tree_entry(new_te, &paths);
3958 *slash = '\0'; /* trim trailing path components */
3959 if (base_tree == NULL ||
3960 got_object_tree_find_entry(base_tree, child_path)
3962 err = make_subtree_for_added_blob(&new_te,
3963 child_path, path_base_tree,
3964 commitable_paths, status_cb, status_arg,
3968 err = insert_tree_entry(new_te, &paths);
3976 /* Handle modified and deleted entries. */
3977 base_entries = got_object_tree_get_entries(base_tree);
3978 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3979 struct got_commitable *ct = NULL;
3981 if (got_object_tree_entry_is_submodule(te)) {
3982 /* Entry is a submodule; just copy it. */
3983 err = got_object_tree_entry_dup(&new_te, te);
3986 err = insert_tree_entry(new_te, &paths);
3992 if (S_ISDIR(te->mode)) {
3994 err = got_object_tree_entry_dup(&new_te, te);
3997 err = match_modified_subtree(&modified, te,
3998 path_base_tree, commitable_paths);
4001 /* Avoid recursion into unmodified subtrees. */
4004 err = write_subtree(&new_te->id, te,
4005 path_base_tree, commitable_paths,
4006 status_cb, status_arg, repo);
4010 err = insert_tree_entry(new_te, &paths);
4016 err = match_deleted_or_modified_ct(&ct, te,
4017 path_base_tree, commitable_paths);
4021 /* NB: Deleted entries get dropped here. */
4022 if (ct->status == GOT_STATUS_MODIFY ||
4023 ct->status == GOT_STATUS_MODE_CHANGE ||
4024 ct->staged_status == GOT_STATUS_MODIFY) {
4025 err = alloc_modified_blob_tree_entry(
4029 err = insert_tree_entry(new_te, &paths);
4033 err = report_ct_status(ct, status_cb,
4038 /* Entry is unchanged; just copy it. */
4039 err = got_object_tree_entry_dup(&new_te, te);
4042 err = insert_tree_entry(new_te, &paths);
4049 /* Write new list of entries; deleted entries have been dropped. */
4050 TAILQ_FOREACH(pe, &paths, entry) {
4051 struct got_tree_entry *te = pe->data;
4052 new_tree_entries.nentries++;
4053 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
4055 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
4057 got_object_tree_entries_close(&new_tree_entries);
4058 got_pathlist_free(&paths);
4062 static const struct got_error *
4063 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4064 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4065 int have_staged_files)
4067 const struct got_error *err = NULL;
4068 struct got_pathlist_entry *pe;
4070 TAILQ_FOREACH(pe, commitable_paths, entry) {
4071 struct got_fileindex_entry *ie;
4072 struct got_commitable *ct = pe->data;
4074 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4076 if (ct->status == GOT_STATUS_DELETE ||
4077 ct->staged_status == GOT_STATUS_DELETE) {
4078 got_fileindex_entry_remove(fileindex, ie);
4079 got_fileindex_entry_free(ie);
4080 } else if (ct->staged_status == GOT_STATUS_ADD ||
4081 ct->staged_status == GOT_STATUS_MODIFY) {
4082 got_fileindex_entry_stage_set(ie,
4083 GOT_FILEIDX_STAGE_NONE);
4084 err = got_fileindex_entry_update(ie,
4085 ct->ondisk_path, ct->staged_blob_id->sha1,
4086 new_base_commit_id->sha1,
4087 !have_staged_files);
4089 err = got_fileindex_entry_update(ie,
4090 ct->ondisk_path, ct->blob_id->sha1,
4091 new_base_commit_id->sha1,
4092 !have_staged_files);
4094 err = got_fileindex_entry_alloc(&ie,
4095 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4096 new_base_commit_id->sha1);
4099 err = got_fileindex_entry_add(fileindex, ie);
4108 static const struct got_error *
4109 check_out_of_date(const char *in_repo_path, unsigned char status,
4110 unsigned char staged_status, struct got_object_id *base_blob_id,
4111 struct got_object_id *base_commit_id,
4112 struct got_object_id *head_commit_id, struct got_repository *repo,
4115 const struct got_error *err = NULL;
4116 struct got_object_id *id = NULL;
4118 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4119 /* Trivial case: base commit == head commit */
4120 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4123 * Ensure file content which local changes were based
4124 * on matches file content in the branch head.
4126 err = got_object_id_by_path(&id, repo, head_commit_id,
4129 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4130 err = got_error(ood_errcode);
4132 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4133 err = got_error(ood_errcode);
4135 /* Require that added files don't exist in the branch head. */
4136 err = got_object_id_by_path(&id, repo, head_commit_id,
4138 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4140 err = id ? got_error(ood_errcode) : NULL;
4147 const struct got_error *
4148 commit_worktree(struct got_object_id **new_commit_id,
4149 struct got_pathlist_head *commitable_paths,
4150 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4151 const char *author, const char *committer,
4152 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4153 got_worktree_status_cb status_cb, void *status_arg,
4154 struct got_repository *repo)
4156 const struct got_error *err = NULL, *unlockerr = NULL;
4157 struct got_pathlist_entry *pe;
4158 const char *head_ref_name = NULL;
4159 struct got_commit_object *head_commit = NULL;
4160 struct got_reference *head_ref2 = NULL;
4161 struct got_object_id *head_commit_id2 = NULL;
4162 struct got_tree_object *head_tree = NULL;
4163 struct got_object_id *new_tree_id = NULL;
4164 struct got_object_id_queue parent_ids;
4165 struct got_object_qid *pid = NULL;
4166 char *logmsg = NULL;
4168 *new_commit_id = NULL;
4170 SIMPLEQ_INIT(&parent_ids);
4172 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4176 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4180 if (commit_msg_cb != NULL) {
4181 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4186 if (logmsg == NULL || strlen(logmsg) == 0) {
4187 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4191 /* Create blobs from added and modified files and record their IDs. */
4192 TAILQ_FOREACH(pe, commitable_paths, entry) {
4193 struct got_commitable *ct = pe->data;
4196 /* Blobs for staged files already exist. */
4197 if (ct->staged_status == GOT_STATUS_ADD ||
4198 ct->staged_status == GOT_STATUS_MODIFY)
4201 if (ct->status != GOT_STATUS_ADD &&
4202 ct->status != GOT_STATUS_MODIFY &&
4203 ct->status != GOT_STATUS_MODE_CHANGE)
4206 if (asprintf(&ondisk_path, "%s/%s",
4207 worktree->root_path, pe->path) == -1) {
4208 err = got_error_from_errno("asprintf");
4211 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4217 /* Recursively write new tree objects. */
4218 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4219 status_cb, status_arg, repo);
4223 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4226 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4227 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4228 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4229 got_object_qid_free(pid);
4235 /* Check if a concurrent commit to our branch has occurred. */
4236 head_ref_name = got_worktree_get_head_ref_name(worktree);
4237 if (head_ref_name == NULL) {
4238 err = got_error_from_errno("got_worktree_get_head_ref_name");
4241 /* Lock the reference here to prevent concurrent modification. */
4242 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4245 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4248 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4249 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4252 /* Update branch head in repository. */
4253 err = got_ref_change_ref(head_ref2, *new_commit_id);
4256 err = got_ref_write(head_ref2, repo);
4260 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4264 err = ref_base_commit(worktree, repo);
4269 got_object_tree_close(head_tree);
4271 got_object_commit_close(head_commit);
4272 free(head_commit_id2);
4274 unlockerr = got_ref_unlock(head_ref2);
4275 if (unlockerr && err == NULL)
4277 got_ref_close(head_ref2);
4282 static const struct got_error *
4283 check_path_is_commitable(const char *path,
4284 struct got_pathlist_head *commitable_paths)
4286 struct got_pathlist_entry *cpe = NULL;
4287 size_t path_len = strlen(path);
4289 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4290 struct got_commitable *ct = cpe->data;
4291 const char *ct_path = ct->path;
4293 while (ct_path[0] == '/')
4296 if (strcmp(path, ct_path) == 0 ||
4297 got_path_is_child(ct_path, path, path_len))
4302 return got_error_path(path, GOT_ERR_BAD_PATH);
4307 static const struct got_error *
4308 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4310 int *have_staged_files = arg;
4312 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4313 *have_staged_files = 1;
4314 return got_error(GOT_ERR_CANCELLED);
4320 static const struct got_error *
4321 check_non_staged_files(struct got_fileindex *fileindex,
4322 struct got_pathlist_head *paths)
4324 struct got_pathlist_entry *pe;
4325 struct got_fileindex_entry *ie;
4327 TAILQ_FOREACH(pe, paths, entry) {
4328 if (pe->path[0] == '\0')
4330 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4332 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4333 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4334 return got_error_path(pe->path,
4335 GOT_ERR_FILE_NOT_STAGED);
4341 const struct got_error *
4342 got_worktree_commit(struct got_object_id **new_commit_id,
4343 struct got_worktree *worktree, struct got_pathlist_head *paths,
4344 const char *author, const char *committer,
4345 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4346 got_worktree_status_cb status_cb, void *status_arg,
4347 struct got_repository *repo)
4349 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4350 struct got_fileindex *fileindex = NULL;
4351 char *fileindex_path = NULL;
4352 struct got_pathlist_head commitable_paths;
4353 struct collect_commitables_arg cc_arg;
4354 struct got_pathlist_entry *pe;
4355 struct got_reference *head_ref = NULL;
4356 struct got_object_id *head_commit_id = NULL;
4357 int have_staged_files = 0;
4359 *new_commit_id = NULL;
4361 TAILQ_INIT(&commitable_paths);
4363 err = lock_worktree(worktree, LOCK_EX);
4367 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4371 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4375 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4379 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4380 &have_staged_files);
4381 if (err && err->code != GOT_ERR_CANCELLED)
4383 if (have_staged_files) {
4384 err = check_non_staged_files(fileindex, paths);
4389 cc_arg.commitable_paths = &commitable_paths;
4390 cc_arg.worktree = worktree;
4392 cc_arg.have_staged_files = have_staged_files;
4393 TAILQ_FOREACH(pe, paths, entry) {
4394 err = worktree_status(worktree, pe->path, fileindex, repo,
4395 collect_commitables, &cc_arg, NULL, NULL);
4400 if (TAILQ_EMPTY(&commitable_paths)) {
4401 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4405 TAILQ_FOREACH(pe, paths, entry) {
4406 err = check_path_is_commitable(pe->path, &commitable_paths);
4411 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4412 struct got_commitable *ct = pe->data;
4413 const char *ct_path = ct->in_repo_path;
4415 while (ct_path[0] == '/')
4417 err = check_out_of_date(ct_path, ct->status,
4418 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4419 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4425 err = commit_worktree(new_commit_id, &commitable_paths,
4426 head_commit_id, worktree, author, committer,
4427 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4431 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4432 fileindex, have_staged_files);
4433 sync_err = sync_fileindex(fileindex, fileindex_path);
4434 if (sync_err && err == NULL)
4438 got_fileindex_free(fileindex);
4439 free(fileindex_path);
4440 unlockerr = lock_worktree(worktree, LOCK_SH);
4441 if (unlockerr && err == NULL)
4443 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4444 struct got_commitable *ct = pe->data;
4445 free_commitable(ct);
4447 got_pathlist_free(&commitable_paths);
4452 got_commitable_get_path(struct got_commitable *ct)
4458 got_commitable_get_status(struct got_commitable *ct)
4463 struct check_rebase_ok_arg {
4464 struct got_worktree *worktree;
4465 struct got_repository *repo;
4468 static const struct got_error *
4469 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4471 const struct got_error *err = NULL;
4472 struct check_rebase_ok_arg *a = arg;
4473 unsigned char status;
4477 /* Reject rebase of a work tree with mixed base commits. */
4478 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4479 SHA1_DIGEST_LENGTH))
4480 return got_error(GOT_ERR_MIXED_COMMITS);
4482 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4484 return got_error_from_errno("asprintf");
4486 /* Reject rebase of a work tree with modified or staged files. */
4487 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4492 if (status != GOT_STATUS_NO_CHANGE)
4493 return got_error(GOT_ERR_MODIFIED);
4494 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4495 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4500 const struct got_error *
4501 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4502 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4503 struct got_worktree *worktree, struct got_reference *branch,
4504 struct got_repository *repo)
4506 const struct got_error *err = NULL;
4507 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4508 char *branch_ref_name = NULL;
4509 char *fileindex_path = NULL;
4510 struct check_rebase_ok_arg ok_arg;
4511 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4513 *new_base_branch_ref = NULL;
4517 err = lock_worktree(worktree, LOCK_EX);
4521 err = open_fileindex(fileindex, &fileindex_path, worktree);
4525 ok_arg.worktree = worktree;
4527 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4532 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4536 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4540 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4544 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4549 err = got_ref_alloc_symref(new_base_branch_ref,
4550 new_base_branch_ref_name, wt_branch);
4553 err = got_ref_write(*new_base_branch_ref, repo);
4557 /* TODO Lock original branch's ref while rebasing? */
4559 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4563 err = got_ref_write(branch_ref, repo);
4567 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4568 worktree->base_commit_id);
4571 err = got_ref_write(*tmp_branch, repo);
4575 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4579 free(fileindex_path);
4580 free(tmp_branch_name);
4581 free(new_base_branch_ref_name);
4582 free(branch_ref_name);
4584 got_ref_close(branch_ref);
4586 got_ref_close(wt_branch);
4588 if (*new_base_branch_ref) {
4589 got_ref_close(*new_base_branch_ref);
4590 *new_base_branch_ref = NULL;
4593 got_ref_close(*tmp_branch);
4597 got_fileindex_free(*fileindex);
4600 lock_worktree(worktree, LOCK_SH);
4605 const struct got_error *
4606 got_worktree_rebase_continue(struct got_object_id **commit_id,
4607 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4608 struct got_reference **branch, struct got_fileindex **fileindex,
4609 struct got_worktree *worktree, struct got_repository *repo)
4611 const struct got_error *err;
4612 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4613 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4614 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4615 char *fileindex_path = NULL;
4616 int have_staged_files = 0;
4619 *new_base_branch = NULL;
4624 err = lock_worktree(worktree, LOCK_EX);
4628 err = open_fileindex(fileindex, &fileindex_path, worktree);
4632 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4633 &have_staged_files);
4634 if (err && err->code != GOT_ERR_CANCELLED)
4636 if (have_staged_files) {
4637 err = got_error(GOT_ERR_STAGED_PATHS);
4641 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4645 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4649 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4653 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4657 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4661 err = got_ref_open(branch, repo,
4662 got_ref_get_symref_target(branch_ref), 0);
4666 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4670 err = got_ref_resolve(commit_id, repo, commit_ref);
4674 err = got_ref_open(new_base_branch, repo,
4675 new_base_branch_ref_name, 0);
4679 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4683 free(commit_ref_name);
4684 free(branch_ref_name);
4685 free(fileindex_path);
4687 got_ref_close(commit_ref);
4689 got_ref_close(branch_ref);
4694 got_ref_close(*tmp_branch);
4697 if (*new_base_branch) {
4698 got_ref_close(*new_base_branch);
4699 *new_base_branch = NULL;
4702 got_ref_close(*branch);
4706 got_fileindex_free(*fileindex);
4709 lock_worktree(worktree, LOCK_SH);
4714 const struct got_error *
4715 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4717 const struct got_error *err;
4718 char *tmp_branch_name = NULL;
4720 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4724 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4725 free(tmp_branch_name);
4729 static const struct got_error *
4730 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4731 char **logmsg, void *arg)
4737 static const struct got_error *
4738 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4739 const char *path, struct got_object_id *blob_id,
4740 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4745 struct collect_merged_paths_arg {
4746 got_worktree_checkout_cb progress_cb;
4748 struct got_pathlist_head *merged_paths;
4751 static const struct got_error *
4752 collect_merged_paths(void *arg, unsigned char status, const char *path)
4754 const struct got_error *err;
4755 struct collect_merged_paths_arg *a = arg;
4757 struct got_pathlist_entry *new;
4759 err = (*a->progress_cb)(a->progress_arg, status, path);
4763 if (status != GOT_STATUS_MERGE &&
4764 status != GOT_STATUS_ADD &&
4765 status != GOT_STATUS_DELETE &&
4766 status != GOT_STATUS_CONFLICT)
4771 return got_error_from_errno("strdup");
4773 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4774 if (err || new == NULL)
4780 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4782 struct got_pathlist_entry *pe;
4784 TAILQ_FOREACH(pe, merged_paths, entry)
4785 free((char *)pe->path);
4787 got_pathlist_free(merged_paths);
4790 static const struct got_error *
4791 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4792 struct got_repository *repo)
4794 const struct got_error *err;
4795 struct got_reference *commit_ref = NULL;
4797 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4799 if (err->code != GOT_ERR_NOT_REF)
4801 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4804 err = got_ref_write(commit_ref, repo);
4808 struct got_object_id *stored_id;
4811 err = got_ref_resolve(&stored_id, repo, commit_ref);
4814 cmp = got_object_id_cmp(commit_id, stored_id);
4817 err = got_error(GOT_ERR_REBASE_COMMITID);
4823 got_ref_close(commit_ref);
4827 static const struct got_error *
4828 rebase_merge_files(struct got_pathlist_head *merged_paths,
4829 const char *commit_ref_name, struct got_worktree *worktree,
4830 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4831 struct got_object_id *commit_id, struct got_repository *repo,
4832 got_worktree_checkout_cb progress_cb, void *progress_arg,
4833 got_cancel_cb cancel_cb, void *cancel_arg)
4835 const struct got_error *err;
4836 struct got_reference *commit_ref = NULL;
4837 struct collect_merged_paths_arg cmp_arg;
4838 char *fileindex_path;
4840 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4842 err = get_fileindex_path(&fileindex_path, worktree);
4846 cmp_arg.progress_cb = progress_cb;
4847 cmp_arg.progress_arg = progress_arg;
4848 cmp_arg.merged_paths = merged_paths;
4849 err = merge_files(worktree, fileindex, fileindex_path,
4850 parent_commit_id, commit_id, repo, collect_merged_paths,
4851 &cmp_arg, cancel_cb, cancel_arg);
4853 got_ref_close(commit_ref);
4857 const struct got_error *
4858 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4859 struct got_worktree *worktree, struct got_fileindex *fileindex,
4860 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4861 struct got_repository *repo,
4862 got_worktree_checkout_cb progress_cb, void *progress_arg,
4863 got_cancel_cb cancel_cb, void *cancel_arg)
4865 const struct got_error *err;
4866 char *commit_ref_name;
4868 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4872 err = store_commit_id(commit_ref_name, commit_id, repo);
4876 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4877 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4878 progress_arg, cancel_cb, cancel_arg);
4880 free(commit_ref_name);
4884 const struct got_error *
4885 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4886 struct got_worktree *worktree, struct got_fileindex *fileindex,
4887 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4888 struct got_repository *repo,
4889 got_worktree_checkout_cb progress_cb, void *progress_arg,
4890 got_cancel_cb cancel_cb, void *cancel_arg)
4892 const struct got_error *err;
4893 char *commit_ref_name;
4895 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4899 err = store_commit_id(commit_ref_name, commit_id, repo);
4903 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4904 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4905 progress_arg, cancel_cb, cancel_arg);
4907 free(commit_ref_name);
4911 static const struct got_error *
4912 rebase_commit(struct got_object_id **new_commit_id,
4913 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4914 struct got_worktree *worktree, struct got_fileindex *fileindex,
4915 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4916 const char *new_logmsg, struct got_repository *repo)
4918 const struct got_error *err, *sync_err;
4919 struct got_pathlist_head commitable_paths;
4920 struct collect_commitables_arg cc_arg;
4921 char *fileindex_path = NULL;
4922 struct got_reference *head_ref = NULL;
4923 struct got_object_id *head_commit_id = NULL;
4924 char *logmsg = NULL;
4926 TAILQ_INIT(&commitable_paths);
4927 *new_commit_id = NULL;
4929 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4931 err = get_fileindex_path(&fileindex_path, worktree);
4935 cc_arg.commitable_paths = &commitable_paths;
4936 cc_arg.worktree = worktree;
4938 cc_arg.have_staged_files = 0;
4940 * If possible get the status of individual files directly to
4941 * avoid crawling the entire work tree once per rebased commit.
4942 * TODO: Ideally, merged_paths would contain a list of commitables
4943 * we could use so we could skip worktree_status() entirely.
4946 struct got_pathlist_entry *pe;
4947 if (TAILQ_EMPTY(merged_paths)) {
4948 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4951 TAILQ_FOREACH(pe, merged_paths, entry) {
4952 err = worktree_status(worktree, pe->path, fileindex,
4953 repo, collect_commitables, &cc_arg, NULL, NULL);
4958 err = worktree_status(worktree, "", fileindex, repo,
4959 collect_commitables, &cc_arg, NULL, NULL);
4964 if (TAILQ_EMPTY(&commitable_paths)) {
4965 /* No-op change; commit will be elided. */
4966 err = got_ref_delete(commit_ref, repo);
4969 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4973 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4977 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4982 logmsg = strdup(new_logmsg);
4983 if (logmsg == NULL) {
4984 err = got_error_from_errno("strdup");
4988 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4993 /* NB: commit_worktree will call free(logmsg) */
4994 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4995 worktree, got_object_commit_get_author(orig_commit),
4996 got_object_commit_get_committer(orig_commit),
4997 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5001 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5005 err = got_ref_delete(commit_ref, repo);
5009 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5011 sync_err = sync_fileindex(fileindex, fileindex_path);
5012 if (sync_err && err == NULL)
5015 free(fileindex_path);
5016 free(head_commit_id);
5018 got_ref_close(head_ref);
5020 free(*new_commit_id);
5021 *new_commit_id = NULL;
5026 const struct got_error *
5027 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5028 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5029 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5030 struct got_commit_object *orig_commit,
5031 struct got_object_id *orig_commit_id, struct got_repository *repo)
5033 const struct got_error *err;
5034 char *commit_ref_name;
5035 struct got_reference *commit_ref = NULL;
5036 struct got_object_id *commit_id = NULL;
5038 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5042 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5045 err = got_ref_resolve(&commit_id, repo, commit_ref);
5048 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5049 err = got_error(GOT_ERR_REBASE_COMMITID);
5053 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5054 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5057 got_ref_close(commit_ref);
5058 free(commit_ref_name);
5063 const struct got_error *
5064 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5065 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5066 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5067 struct got_commit_object *orig_commit,
5068 struct got_object_id *orig_commit_id, const char *new_logmsg,
5069 struct got_repository *repo)
5071 const struct got_error *err;
5072 char *commit_ref_name;
5073 struct got_reference *commit_ref = NULL;
5074 struct got_object_id *commit_id = NULL;
5076 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5080 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5083 err = got_ref_resolve(&commit_id, repo, commit_ref);
5086 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5087 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5091 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5092 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5095 got_ref_close(commit_ref);
5096 free(commit_ref_name);
5101 const struct got_error *
5102 got_worktree_rebase_postpone(struct got_worktree *worktree,
5103 struct got_fileindex *fileindex)
5106 got_fileindex_free(fileindex);
5107 return lock_worktree(worktree, LOCK_SH);
5110 static const struct got_error *
5111 delete_ref(const char *name, struct got_repository *repo)
5113 const struct got_error *err;
5114 struct got_reference *ref;
5116 err = got_ref_open(&ref, repo, name, 0);
5118 if (err->code == GOT_ERR_NOT_REF)
5123 err = got_ref_delete(ref, repo);
5128 static const struct got_error *
5129 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5131 const struct got_error *err;
5132 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5133 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5135 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5138 err = delete_ref(tmp_branch_name, repo);
5142 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5145 err = delete_ref(new_base_branch_ref_name, repo);
5149 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5152 err = delete_ref(branch_ref_name, repo);
5156 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5159 err = delete_ref(commit_ref_name, repo);
5164 free(tmp_branch_name);
5165 free(new_base_branch_ref_name);
5166 free(branch_ref_name);
5167 free(commit_ref_name);
5171 const struct got_error *
5172 got_worktree_rebase_complete(struct got_worktree *worktree,
5173 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5174 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5175 struct got_repository *repo)
5177 const struct got_error *err, *unlockerr;
5178 struct got_object_id *new_head_commit_id = NULL;
5180 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5184 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5188 err = got_ref_write(rebased_branch, repo);
5192 err = got_worktree_set_head_ref(worktree, rebased_branch);
5196 err = delete_rebase_refs(worktree, repo);
5199 got_fileindex_free(fileindex);
5200 free(new_head_commit_id);
5201 unlockerr = lock_worktree(worktree, LOCK_SH);
5202 if (unlockerr && err == NULL)
5207 const struct got_error *
5208 got_worktree_rebase_abort(struct got_worktree *worktree,
5209 struct got_fileindex *fileindex, struct got_repository *repo,
5210 struct got_reference *new_base_branch,
5211 got_worktree_checkout_cb progress_cb, void *progress_arg)
5213 const struct got_error *err, *unlockerr, *sync_err;
5214 struct got_reference *resolved = NULL;
5215 struct got_object_id *commit_id = NULL;
5216 char *fileindex_path = NULL;
5217 struct revert_file_args rfa;
5218 struct got_object_id *tree_id = NULL;
5220 err = lock_worktree(worktree, LOCK_EX);
5224 err = got_ref_open(&resolved, repo,
5225 got_ref_get_symref_target(new_base_branch), 0);
5229 err = got_worktree_set_head_ref(worktree, resolved);
5234 * XXX commits to the base branch could have happened while
5235 * we were busy rebasing; should we store the original commit ID
5236 * when rebase begins and read it back here?
5238 err = got_ref_resolve(&commit_id, repo, resolved);
5242 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5246 err = got_object_id_by_path(&tree_id, repo,
5247 worktree->base_commit_id, worktree->path_prefix);
5251 err = delete_rebase_refs(worktree, repo);
5255 err = get_fileindex_path(&fileindex_path, worktree);
5259 rfa.worktree = worktree;
5260 rfa.fileindex = fileindex;
5261 rfa.progress_cb = progress_cb;
5262 rfa.progress_arg = progress_arg;
5263 rfa.patch_cb = NULL;
5264 rfa.patch_arg = NULL;
5266 err = worktree_status(worktree, "", fileindex, repo,
5267 revert_file, &rfa, NULL, NULL);
5271 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5272 repo, progress_cb, progress_arg, NULL, NULL);
5274 sync_err = sync_fileindex(fileindex, fileindex_path);
5275 if (sync_err && err == NULL)
5278 got_ref_close(resolved);
5282 got_fileindex_free(fileindex);
5283 free(fileindex_path);
5285 unlockerr = lock_worktree(worktree, LOCK_SH);
5286 if (unlockerr && err == NULL)
5291 const struct got_error *
5292 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5293 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5294 struct got_fileindex **fileindex, struct got_worktree *worktree,
5295 struct got_repository *repo)
5297 const struct got_error *err = NULL;
5298 char *tmp_branch_name = NULL;
5299 char *branch_ref_name = NULL;
5300 char *base_commit_ref_name = NULL;
5301 char *fileindex_path = NULL;
5302 struct check_rebase_ok_arg ok_arg;
5303 struct got_reference *wt_branch = NULL;
5304 struct got_reference *base_commit_ref = NULL;
5308 *base_commit_id = NULL;
5311 err = lock_worktree(worktree, LOCK_EX);
5315 err = open_fileindex(fileindex, &fileindex_path, worktree);
5319 ok_arg.worktree = worktree;
5321 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5326 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5330 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5334 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5339 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5344 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5348 err = got_ref_write(*branch_ref, repo);
5352 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5353 worktree->base_commit_id);
5356 err = got_ref_write(base_commit_ref, repo);
5359 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5360 if (*base_commit_id == NULL) {
5361 err = got_error_from_errno("got_object_id_dup");
5365 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5366 worktree->base_commit_id);
5369 err = got_ref_write(*tmp_branch, repo);
5373 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5377 free(fileindex_path);
5378 free(tmp_branch_name);
5379 free(branch_ref_name);
5380 free(base_commit_ref_name);
5382 got_ref_close(wt_branch);
5385 got_ref_close(*branch_ref);
5389 got_ref_close(*tmp_branch);
5392 free(*base_commit_id);
5394 got_fileindex_free(*fileindex);
5397 lock_worktree(worktree, LOCK_SH);
5402 const struct got_error *
5403 got_worktree_histedit_postpone(struct got_worktree *worktree,
5404 struct got_fileindex *fileindex)
5407 got_fileindex_free(fileindex);
5408 return lock_worktree(worktree, LOCK_SH);
5411 const struct got_error *
5412 got_worktree_histedit_in_progress(int *in_progress,
5413 struct got_worktree *worktree)
5415 const struct got_error *err;
5416 char *tmp_branch_name = NULL;
5418 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5422 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5423 free(tmp_branch_name);
5427 const struct got_error *
5428 got_worktree_histedit_continue(struct got_object_id **commit_id,
5429 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5430 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5431 struct got_worktree *worktree, struct got_repository *repo)
5433 const struct got_error *err;
5434 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5435 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5436 struct got_reference *commit_ref = NULL;
5437 struct got_reference *base_commit_ref = NULL;
5438 char *fileindex_path = NULL;
5439 int have_staged_files = 0;
5443 *base_commit_id = NULL;
5446 err = lock_worktree(worktree, LOCK_EX);
5450 err = open_fileindex(fileindex, &fileindex_path, worktree);
5454 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5455 &have_staged_files);
5456 if (err && err->code != GOT_ERR_CANCELLED)
5458 if (have_staged_files) {
5459 err = got_error(GOT_ERR_STAGED_PATHS);
5463 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5467 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5471 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5475 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5480 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5484 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5487 err = got_ref_resolve(commit_id, repo, commit_ref);
5491 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5494 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5498 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5502 free(commit_ref_name);
5503 free(branch_ref_name);
5504 free(fileindex_path);
5506 got_ref_close(commit_ref);
5507 if (base_commit_ref)
5508 got_ref_close(base_commit_ref);
5512 free(*base_commit_id);
5513 *base_commit_id = NULL;
5515 got_ref_close(*tmp_branch);
5519 got_fileindex_free(*fileindex);
5522 lock_worktree(worktree, LOCK_EX);
5527 static const struct got_error *
5528 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5530 const struct got_error *err;
5531 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5532 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5534 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5537 err = delete_ref(tmp_branch_name, repo);
5541 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5545 err = delete_ref(base_commit_ref_name, repo);
5549 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5552 err = delete_ref(branch_ref_name, repo);
5556 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5559 err = delete_ref(commit_ref_name, repo);
5563 free(tmp_branch_name);
5564 free(base_commit_ref_name);
5565 free(branch_ref_name);
5566 free(commit_ref_name);
5570 const struct got_error *
5571 got_worktree_histedit_abort(struct got_worktree *worktree,
5572 struct got_fileindex *fileindex, struct got_repository *repo,
5573 struct got_reference *branch, struct got_object_id *base_commit_id,
5574 got_worktree_checkout_cb progress_cb, void *progress_arg)
5576 const struct got_error *err, *unlockerr, *sync_err;
5577 struct got_reference *resolved = NULL;
5578 char *fileindex_path = NULL;
5579 struct got_object_id *tree_id = NULL;
5580 struct revert_file_args rfa;
5582 err = lock_worktree(worktree, LOCK_EX);
5586 err = got_ref_open(&resolved, repo,
5587 got_ref_get_symref_target(branch), 0);
5591 err = got_worktree_set_head_ref(worktree, resolved);
5595 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5599 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5600 worktree->path_prefix);
5604 err = delete_histedit_refs(worktree, repo);
5608 err = get_fileindex_path(&fileindex_path, worktree);
5612 rfa.worktree = worktree;
5613 rfa.fileindex = fileindex;
5614 rfa.progress_cb = progress_cb;
5615 rfa.progress_arg = progress_arg;
5616 rfa.patch_cb = NULL;
5617 rfa.patch_arg = NULL;
5619 err = worktree_status(worktree, "", fileindex, repo,
5620 revert_file, &rfa, NULL, NULL);
5624 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5625 repo, progress_cb, progress_arg, NULL, NULL);
5627 sync_err = sync_fileindex(fileindex, fileindex_path);
5628 if (sync_err && err == NULL)
5631 got_ref_close(resolved);
5633 free(fileindex_path);
5635 unlockerr = lock_worktree(worktree, LOCK_SH);
5636 if (unlockerr && err == NULL)
5641 const struct got_error *
5642 got_worktree_histedit_complete(struct got_worktree *worktree,
5643 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5644 struct got_reference *edited_branch, struct got_repository *repo)
5646 const struct got_error *err, *unlockerr;
5647 struct got_object_id *new_head_commit_id = NULL;
5648 struct got_reference *resolved = NULL;
5650 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5654 err = got_ref_open(&resolved, repo,
5655 got_ref_get_symref_target(edited_branch), 0);
5659 err = got_ref_change_ref(resolved, new_head_commit_id);
5663 err = got_ref_write(resolved, repo);
5667 err = got_worktree_set_head_ref(worktree, resolved);
5671 err = delete_histedit_refs(worktree, repo);
5674 got_fileindex_free(fileindex);
5675 free(new_head_commit_id);
5676 unlockerr = lock_worktree(worktree, LOCK_SH);
5677 if (unlockerr && err == NULL)
5682 const struct got_error *
5683 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5684 struct got_object_id *commit_id, struct got_repository *repo)
5686 const struct got_error *err;
5687 char *commit_ref_name;
5689 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5693 err = store_commit_id(commit_ref_name, commit_id, repo);
5697 err = delete_ref(commit_ref_name, repo);
5699 free(commit_ref_name);
5703 const struct got_error *
5704 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5705 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5706 struct got_worktree *worktree, const char *refname,
5707 struct got_repository *repo)
5709 const struct got_error *err = NULL;
5710 char *fileindex_path = NULL;
5711 struct check_rebase_ok_arg ok_arg;
5715 *base_branch_ref = NULL;
5717 err = lock_worktree(worktree, LOCK_EX);
5721 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5722 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5723 "cannot integrate a branch into itself; "
5724 "update -b or different branch name required");
5728 err = open_fileindex(fileindex, &fileindex_path, worktree);
5732 /* Preconditions are the same as for rebase. */
5733 ok_arg.worktree = worktree;
5735 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5740 err = got_ref_open(branch_ref, repo, refname, 1);
5744 err = got_ref_open(base_branch_ref, repo,
5745 got_worktree_get_head_ref_name(worktree), 1);
5749 got_ref_close(*branch_ref);
5752 if (*base_branch_ref) {
5753 got_ref_close(*base_branch_ref);
5754 *base_branch_ref = NULL;
5757 got_fileindex_free(*fileindex);
5760 lock_worktree(worktree, LOCK_SH);
5765 const struct got_error *
5766 got_worktree_integrate_continue(struct got_worktree *worktree,
5767 struct got_fileindex *fileindex, struct got_repository *repo,
5768 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5769 got_worktree_checkout_cb progress_cb, void *progress_arg,
5770 got_cancel_cb cancel_cb, void *cancel_arg)
5772 const struct got_error *err = NULL, *sync_err, *unlockerr;
5773 char *fileindex_path = NULL;
5774 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5776 err = get_fileindex_path(&fileindex_path, worktree);
5780 err = got_ref_resolve(&commit_id, repo, branch_ref);
5784 err = got_object_id_by_path(&tree_id, repo, commit_id,
5785 worktree->path_prefix);
5789 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5793 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5794 progress_cb, progress_arg, cancel_cb, cancel_arg);
5798 err = got_ref_change_ref(base_branch_ref, commit_id);
5802 err = got_ref_write(base_branch_ref, repo);
5804 sync_err = sync_fileindex(fileindex, fileindex_path);
5805 if (sync_err && err == NULL)
5809 unlockerr = got_ref_unlock(branch_ref);
5810 if (unlockerr && err == NULL)
5812 got_ref_close(branch_ref);
5814 unlockerr = got_ref_unlock(base_branch_ref);
5815 if (unlockerr && err == NULL)
5817 got_ref_close(base_branch_ref);
5819 got_fileindex_free(fileindex);
5820 free(fileindex_path);
5823 unlockerr = lock_worktree(worktree, LOCK_SH);
5824 if (unlockerr && err == NULL)
5829 const struct got_error *
5830 got_worktree_integrate_abort(struct got_worktree *worktree,
5831 struct got_fileindex *fileindex, struct got_repository *repo,
5832 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5834 const struct got_error *err = NULL, *unlockerr = NULL;
5836 got_fileindex_free(fileindex);
5838 err = lock_worktree(worktree, LOCK_SH);
5840 unlockerr = got_ref_unlock(branch_ref);
5841 if (unlockerr && err == NULL)
5843 got_ref_close(branch_ref);
5845 unlockerr = got_ref_unlock(base_branch_ref);
5846 if (unlockerr && err == NULL)
5848 got_ref_close(base_branch_ref);
5853 struct check_stage_ok_arg {
5854 struct got_object_id *head_commit_id;
5855 struct got_worktree *worktree;
5856 struct got_fileindex *fileindex;
5857 struct got_repository *repo;
5861 const struct got_error *
5862 check_stage_ok(void *arg, unsigned char status,
5863 unsigned char staged_status, const char *relpath,
5864 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5865 struct got_object_id *commit_id)
5867 struct check_stage_ok_arg *a = arg;
5868 const struct got_error *err = NULL;
5869 struct got_fileindex_entry *ie;
5870 struct got_object_id base_commit_id;
5871 struct got_object_id *base_commit_idp = NULL;
5872 char *in_repo_path = NULL, *p;
5874 if (status == GOT_STATUS_UNVERSIONED ||
5875 status == GOT_STATUS_NO_CHANGE)
5877 if (status == GOT_STATUS_NONEXISTENT)
5878 return got_error_set_errno(ENOENT, relpath);
5880 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5882 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5884 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5885 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5887 return got_error_from_errno("asprintf");
5889 if (got_fileindex_entry_has_commit(ie)) {
5890 memcpy(base_commit_id.sha1, ie->commit_sha1,
5891 SHA1_DIGEST_LENGTH);
5892 base_commit_idp = &base_commit_id;
5895 if (status == GOT_STATUS_CONFLICT) {
5896 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5898 } else if (status != GOT_STATUS_ADD &&
5899 status != GOT_STATUS_MODIFY &&
5900 status != GOT_STATUS_DELETE) {
5901 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5905 a->have_changes = 1;
5910 err = check_out_of_date(p, status, staged_status,
5911 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5912 GOT_ERR_STAGE_OUT_OF_DATE);
5918 struct stage_path_arg {
5919 struct got_worktree *worktree;
5920 struct got_fileindex *fileindex;
5921 struct got_repository *repo;
5922 got_worktree_status_cb status_cb;
5924 got_worktree_patch_cb patch_cb;
5926 int staged_something;
5929 static const struct got_error *
5930 stage_path(void *arg, unsigned char status,
5931 unsigned char staged_status, const char *relpath,
5932 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5933 struct got_object_id *commit_id)
5935 struct stage_path_arg *a = arg;
5936 const struct got_error *err = NULL;
5937 struct got_fileindex_entry *ie;
5938 char *ondisk_path = NULL, *path_content = NULL;
5940 struct got_object_id *new_staged_blob_id = NULL;
5942 if (status == GOT_STATUS_UNVERSIONED)
5945 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5947 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5949 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5951 return got_error_from_errno("asprintf");
5954 case GOT_STATUS_ADD:
5955 case GOT_STATUS_MODIFY:
5957 if (status == GOT_STATUS_ADD) {
5958 int choice = GOT_PATCH_CHOICE_NONE;
5959 err = (*a->patch_cb)(&choice, a->patch_arg,
5960 status, ie->path, NULL, 1, 1);
5963 if (choice != GOT_PATCH_CHOICE_YES)
5966 err = create_patched_content(&path_content, 0,
5967 staged_blob_id ? staged_blob_id : blob_id,
5968 ondisk_path, ie->path, a->repo,
5969 a->patch_cb, a->patch_arg);
5970 if (err || path_content == NULL)
5974 err = got_object_blob_create(&new_staged_blob_id,
5975 path_content ? path_content : ondisk_path, a->repo);
5978 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5979 SHA1_DIGEST_LENGTH);
5980 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5981 stage = GOT_FILEIDX_STAGE_ADD;
5983 stage = GOT_FILEIDX_STAGE_MODIFY;
5984 got_fileindex_entry_stage_set(ie, stage);
5985 a->staged_something = 1;
5986 if (a->status_cb == NULL)
5988 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5989 get_staged_status(ie), relpath, blob_id,
5990 new_staged_blob_id, NULL);
5992 case GOT_STATUS_DELETE:
5993 if (staged_status == GOT_STATUS_DELETE)
5996 int choice = GOT_PATCH_CHOICE_NONE;
5997 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5998 ie->path, NULL, 1, 1);
6001 if (choice == GOT_PATCH_CHOICE_NO)
6003 if (choice != GOT_PATCH_CHOICE_YES) {
6004 err = got_error(GOT_ERR_PATCH_CHOICE);
6008 stage = GOT_FILEIDX_STAGE_DELETE;
6009 got_fileindex_entry_stage_set(ie, stage);
6010 a->staged_something = 1;
6011 if (a->status_cb == NULL)
6013 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6014 get_staged_status(ie), relpath, NULL, NULL, NULL);
6016 case GOT_STATUS_NO_CHANGE:
6018 case GOT_STATUS_CONFLICT:
6019 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6021 case GOT_STATUS_NONEXISTENT:
6022 err = got_error_set_errno(ENOENT, relpath);
6025 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6029 if (path_content && unlink(path_content) == -1 && err == NULL)
6030 err = got_error_from_errno2("unlink", path_content);
6033 free(new_staged_blob_id);
6037 const struct got_error *
6038 got_worktree_stage(struct got_worktree *worktree,
6039 struct got_pathlist_head *paths,
6040 got_worktree_status_cb status_cb, void *status_arg,
6041 got_worktree_patch_cb patch_cb, void *patch_arg,
6042 struct got_repository *repo)
6044 const struct got_error *err = NULL, *sync_err, *unlockerr;
6045 struct got_pathlist_entry *pe;
6046 struct got_fileindex *fileindex = NULL;
6047 char *fileindex_path = NULL;
6048 struct got_reference *head_ref = NULL;
6049 struct got_object_id *head_commit_id = NULL;
6050 struct check_stage_ok_arg oka;
6051 struct stage_path_arg spa;
6053 err = lock_worktree(worktree, LOCK_EX);
6057 err = got_ref_open(&head_ref, repo,
6058 got_worktree_get_head_ref_name(worktree), 0);
6061 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6064 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6068 /* Check pre-conditions before staging anything. */
6069 oka.head_commit_id = head_commit_id;
6070 oka.worktree = worktree;
6071 oka.fileindex = fileindex;
6073 oka.have_changes = 0;
6074 TAILQ_FOREACH(pe, paths, entry) {
6075 err = worktree_status(worktree, pe->path, fileindex, repo,
6076 check_stage_ok, &oka, NULL, NULL);
6080 if (!oka.have_changes) {
6081 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6085 spa.worktree = worktree;
6086 spa.fileindex = fileindex;
6088 spa.patch_cb = patch_cb;
6089 spa.patch_arg = patch_arg;
6090 spa.status_cb = status_cb;
6091 spa.status_arg = status_arg;
6092 spa.staged_something = 0;
6093 TAILQ_FOREACH(pe, paths, entry) {
6094 err = worktree_status(worktree, pe->path, fileindex, repo,
6095 stage_path, &spa, NULL, NULL);
6099 if (!spa.staged_something) {
6100 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6104 sync_err = sync_fileindex(fileindex, fileindex_path);
6105 if (sync_err && err == NULL)
6109 got_ref_close(head_ref);
6110 free(head_commit_id);
6111 free(fileindex_path);
6113 got_fileindex_free(fileindex);
6114 unlockerr = lock_worktree(worktree, LOCK_SH);
6115 if (unlockerr && err == NULL)
6120 struct unstage_path_arg {
6121 struct got_worktree *worktree;
6122 struct got_fileindex *fileindex;
6123 struct got_repository *repo;
6124 got_worktree_checkout_cb progress_cb;
6126 got_worktree_patch_cb patch_cb;
6130 static const struct got_error *
6131 create_unstaged_content(char **path_unstaged_content,
6132 char **path_new_staged_content, struct got_object_id *blob_id,
6133 struct got_object_id *staged_blob_id, const char *relpath,
6134 struct got_repository *repo,
6135 got_worktree_patch_cb patch_cb, void *patch_arg)
6137 const struct got_error *err;
6138 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6139 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6140 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6141 struct stat sb1, sb2;
6142 struct got_diff_changes *changes = NULL;
6143 struct got_diff_state *ds = NULL;
6144 struct got_diff_args *args = NULL;
6145 struct got_diff_change *change;
6146 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6147 int have_content = 0, have_rejected_content = 0;
6149 *path_unstaged_content = NULL;
6150 *path_new_staged_content = NULL;
6152 err = got_object_id_str(&label1, blob_id);
6155 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6159 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6163 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6167 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6171 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6175 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6179 if (stat(path1, &sb1) == -1) {
6180 err = got_error_from_errno2("stat", path1);
6184 if (stat(path2, &sb2) == -1) {
6185 err = got_error_from_errno2("stat", path2);
6189 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6190 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6194 err = got_opentemp_named(path_unstaged_content, &outfile,
6195 "got-unstaged-content");
6198 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6199 "got-new-staged-content");
6203 if (fseek(f1, 0L, SEEK_SET) == -1) {
6204 err = got_ferror(f1, GOT_ERR_IO);
6207 if (fseek(f2, 0L, SEEK_SET) == -1) {
6208 err = got_ferror(f2, GOT_ERR_IO);
6211 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6213 err = apply_or_reject_change(&choice, change, ++n,
6214 changes->nchanges, ds, args, diff_flags, relpath,
6215 f1, f2, &line_cur1, &line_cur2,
6216 outfile, rejectfile, patch_cb, patch_arg);
6219 if (choice == GOT_PATCH_CHOICE_YES)
6222 have_rejected_content = 1;
6223 if (choice == GOT_PATCH_CHOICE_QUIT)
6226 if (have_content || have_rejected_content)
6227 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6228 outfile, rejectfile);
6232 got_object_blob_close(blob);
6234 got_object_blob_close(staged_blob);
6235 if (f1 && fclose(f1) == EOF && err == NULL)
6236 err = got_error_from_errno2("fclose", path1);
6237 if (f2 && fclose(f2) == EOF && err == NULL)
6238 err = got_error_from_errno2("fclose", path2);
6239 if (outfile && fclose(outfile) == EOF && err == NULL)
6240 err = got_error_from_errno2("fclose", *path_unstaged_content);
6241 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6242 err = got_error_from_errno2("fclose", *path_new_staged_content);
6243 if (path1 && unlink(path1) == -1 && err == NULL)
6244 err = got_error_from_errno2("unlink", path1);
6245 if (path2 && unlink(path2) == -1 && err == NULL)
6246 err = got_error_from_errno2("unlink", path2);
6247 if (err || !have_content) {
6248 if (*path_unstaged_content &&
6249 unlink(*path_unstaged_content) == -1 && err == NULL)
6250 err = got_error_from_errno2("unlink",
6251 *path_unstaged_content);
6252 free(*path_unstaged_content);
6253 *path_unstaged_content = NULL;
6255 if (err || !have_rejected_content) {
6256 if (*path_new_staged_content &&
6257 unlink(*path_new_staged_content) == -1 && err == NULL)
6258 err = got_error_from_errno2("unlink",
6259 *path_new_staged_content);
6260 free(*path_new_staged_content);
6261 *path_new_staged_content = NULL;
6265 got_diff_state_free(ds);
6269 got_diff_free_changes(changes);
6275 static const struct got_error *
6276 unstage_path(void *arg, unsigned char status,
6277 unsigned char staged_status, const char *relpath,
6278 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6279 struct got_object_id *commit_id)
6281 const struct got_error *err = NULL;
6282 struct unstage_path_arg *a = arg;
6283 struct got_fileindex_entry *ie;
6284 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6285 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6286 char *path_new_staged_content = NULL;
6287 char *id_str = NULL, *label_orig = NULL;
6288 int local_changes_subsumed;
6291 if (staged_status != GOT_STATUS_ADD &&
6292 staged_status != GOT_STATUS_MODIFY &&
6293 staged_status != GOT_STATUS_DELETE)
6296 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6298 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6300 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6302 return got_error_from_errno("asprintf");
6304 err = got_object_id_str(&id_str,
6305 commit_id ? commit_id : a->worktree->base_commit_id);
6308 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6310 err = got_error_from_errno("asprintf");
6314 switch (staged_status) {
6315 case GOT_STATUS_MODIFY:
6316 err = got_object_open_as_blob(&blob_base, a->repo,
6321 case GOT_STATUS_ADD:
6323 if (staged_status == GOT_STATUS_ADD) {
6324 int choice = GOT_PATCH_CHOICE_NONE;
6325 err = (*a->patch_cb)(&choice, a->patch_arg,
6326 staged_status, ie->path, NULL, 1, 1);
6329 if (choice != GOT_PATCH_CHOICE_YES)
6332 err = create_unstaged_content(
6333 &path_unstaged_content,
6334 &path_new_staged_content, blob_id,
6335 staged_blob_id, ie->path, a->repo,
6336 a->patch_cb, a->patch_arg);
6337 if (err || path_unstaged_content == NULL)
6339 if (path_new_staged_content) {
6340 err = got_object_blob_create(
6342 path_new_staged_content,
6346 memcpy(ie->staged_blob_sha1,
6347 staged_blob_id->sha1,
6348 SHA1_DIGEST_LENGTH);
6350 err = merge_file(&local_changes_subsumed,
6351 a->worktree, blob_base, ondisk_path,
6352 relpath, got_fileindex_perms_to_st(ie),
6353 path_unstaged_content, label_orig,
6354 "unstaged", a->repo, a->progress_cb,
6357 path_new_staged_content == NULL)
6358 got_fileindex_entry_stage_set(ie,
6359 GOT_FILEIDX_STAGE_NONE);
6360 break; /* Done with this file. */
6363 err = got_object_open_as_blob(&blob_staged, a->repo,
6364 staged_blob_id, 8192);
6367 err = merge_blob(&local_changes_subsumed, a->worktree,
6368 blob_base, ondisk_path, relpath,
6369 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6370 commit_id ? commit_id : a->worktree->base_commit_id,
6371 a->repo, a->progress_cb, a->progress_arg);
6373 got_fileindex_entry_stage_set(ie,
6374 GOT_FILEIDX_STAGE_NONE);
6376 case GOT_STATUS_DELETE:
6378 int choice = GOT_PATCH_CHOICE_NONE;
6379 err = (*a->patch_cb)(&choice, a->patch_arg,
6380 staged_status, ie->path, NULL, 1, 1);
6383 if (choice == GOT_PATCH_CHOICE_NO)
6385 if (choice != GOT_PATCH_CHOICE_YES) {
6386 err = got_error(GOT_ERR_PATCH_CHOICE);
6390 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6391 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6394 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6399 if (path_unstaged_content &&
6400 unlink(path_unstaged_content) == -1 && err == NULL)
6401 err = got_error_from_errno2("unlink", path_unstaged_content);
6402 if (path_new_staged_content &&
6403 unlink(path_new_staged_content) == -1 && err == NULL)
6404 err = got_error_from_errno2("unlink", path_new_staged_content);
6405 free(path_unstaged_content);
6406 free(path_new_staged_content);
6408 got_object_blob_close(blob_base);
6410 got_object_blob_close(blob_staged);
6416 const struct got_error *
6417 got_worktree_unstage(struct got_worktree *worktree,
6418 struct got_pathlist_head *paths,
6419 got_worktree_checkout_cb progress_cb, void *progress_arg,
6420 got_worktree_patch_cb patch_cb, void *patch_arg,
6421 struct got_repository *repo)
6423 const struct got_error *err = NULL, *sync_err, *unlockerr;
6424 struct got_pathlist_entry *pe;
6425 struct got_fileindex *fileindex = NULL;
6426 char *fileindex_path = NULL;
6427 struct unstage_path_arg upa;
6429 err = lock_worktree(worktree, LOCK_EX);
6433 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6437 upa.worktree = worktree;
6438 upa.fileindex = fileindex;
6440 upa.progress_cb = progress_cb;
6441 upa.progress_arg = progress_arg;
6442 upa.patch_cb = patch_cb;
6443 upa.patch_arg = patch_arg;
6444 TAILQ_FOREACH(pe, paths, entry) {
6445 err = worktree_status(worktree, pe->path, fileindex, repo,
6446 unstage_path, &upa, NULL, NULL);
6451 sync_err = sync_fileindex(fileindex, fileindex_path);
6452 if (sync_err && err == NULL)
6455 free(fileindex_path);
6457 got_fileindex_free(fileindex);
6458 unlockerr = lock_worktree(worktree, LOCK_SH);
6459 if (unlockerr && err == NULL)