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/limits.h>
19 #include <sys/queue.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 static const struct got_error *
62 create_meta_file(const char *path_got, const char *name, const char *content)
64 const struct got_error *err = NULL;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 return got_error_from_errno("asprintf");
70 err = got_path_create_file(path, content);
75 static const struct got_error *
76 update_meta_file(const char *path_got, const char *name, const char *content)
78 const struct got_error *err = NULL;
83 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 err = got_error_from_errno("asprintf");
89 err = got_opentemp_named(&tmppath, &tmpfile, path);
94 int len = fprintf(tmpfile, "%s\n", content);
95 if (len != strlen(content) + 1) {
96 err = got_error_from_errno2("fprintf", tmppath);
101 if (rename(tmppath, path) != 0) {
102 err = got_error_from_errno3("rename", tmppath, path);
108 if (fclose(tmpfile) != 0 && err == NULL)
109 err = got_error_from_errno2("fclose", tmppath);
114 static const struct got_error *
115 read_meta_file(char **content, const char *path_got, const char *name)
117 const struct got_error *err = NULL;
125 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 err = got_error_from_errno("asprintf");
131 fd = open(path, O_RDONLY | O_NOFOLLOW);
134 err = got_error_path(path, GOT_ERR_WORKTREE_META);
136 err = got_error_from_errno2("open", path);
139 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 : got_error_from_errno2("flock", path));
145 if (fstat(fd, &sb) != 0) {
146 err = got_error_from_errno2("fstat", path);
149 *content = calloc(1, sb.st_size);
150 if (*content == NULL) {
151 err = got_error_from_errno("calloc");
155 n = read(fd, *content, sb.st_size);
156 if (n != sb.st_size) {
157 err = (n == -1 ? got_error_from_errno2("read", path) :
158 got_error_path(path, GOT_ERR_WORKTREE_META));
161 if ((*content)[sb.st_size - 1] != '\n') {
162 err = got_error_path(path, GOT_ERR_WORKTREE_META);
165 (*content)[sb.st_size - 1] = '\0';
168 if (fd != -1 && close(fd) == -1 && err == NULL)
169 err = got_error_from_errno2("close", path_got);
178 static const struct got_error *
179 write_head_ref(const char *path_got, struct got_reference *head_ref)
181 const struct got_error *err = NULL;
184 if (got_ref_is_symbolic(head_ref)) {
185 refstr = got_ref_to_str(head_ref);
187 return got_error_from_errno("got_ref_to_str");
189 refstr = strdup(got_ref_get_name(head_ref));
191 return got_error_from_errno("strdup");
193 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
198 const struct got_error *
199 got_worktree_init(const char *path, struct got_reference *head_ref,
200 const char *prefix, struct got_repository *repo)
202 const struct got_error *err = NULL;
203 struct got_object_id *commit_id = NULL;
205 uint32_t uuid_status;
207 char *path_got = NULL;
208 char *formatstr = NULL;
209 char *absprefix = NULL;
210 char *basestr = NULL;
211 char *uuidstr = NULL;
213 if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 err = got_error(GOT_ERR_WORKTREE_REPO);
218 err = got_ref_resolve(&commit_id, repo, head_ref);
221 err = got_object_get_type(&obj_type, repo, commit_id);
224 if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 return got_error(GOT_ERR_OBJ_TYPE);
227 if (!got_path_is_absolute(prefix)) {
228 if (asprintf(&absprefix, "/%s", prefix) == -1)
229 return got_error_from_errno("asprintf");
232 /* Create top-level directory (may already exist). */
233 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 err = got_error_from_errno2("mkdir", path);
238 /* Create .got directory (may already exist). */
239 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 err = got_error_from_errno("asprintf");
243 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 err = got_error_from_errno2("mkdir", path_got);
248 /* Create an empty lock file. */
249 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
253 /* Create an empty file index. */
254 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
258 /* Write the HEAD reference. */
259 err = write_head_ref(path_got, head_ref);
263 /* Record our base commit. */
264 err = got_object_id_str(&basestr, commit_id);
267 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
271 /* Store path to repository. */
272 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 got_repo_get_path(repo));
277 /* Store in-repository path prefix. */
278 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 absprefix ? absprefix : prefix);
284 uuid_create(&uuid, &uuid_status);
285 if (uuid_status != uuid_s_ok) {
286 err = got_error_uuid(uuid_status);
289 uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status);
294 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
298 /* Stamp work tree with format file. */
299 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 err = got_error_from_errno("asprintf");
303 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
317 static const struct got_error *
318 open_worktree(struct got_worktree **worktree, const char *path)
320 const struct got_error *err = NULL;
322 char *formatstr = NULL;
323 char *uuidstr = NULL;
324 char *path_lock = NULL;
325 char *base_commit_id_str = NULL;
326 int version, fd = -1;
328 struct got_repository *repo = NULL;
329 uint32_t uuid_status;
333 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
339 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 err = got_error_from_errno("asprintf");
345 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
347 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 : got_error_from_errno2("open", path_lock));
352 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
356 version = strtonum(formatstr, 1, INT_MAX, &errstr);
358 err = got_error_msg(GOT_ERR_WORKTREE_META,
359 "could not parse work tree format version number");
362 if (version != GOT_WORKTREE_FORMAT_VERSION) {
363 err = got_error(GOT_ERR_WORKTREE_VERS);
367 *worktree = calloc(1, sizeof(**worktree));
368 if (*worktree == NULL) {
369 err = got_error_from_errno("calloc");
372 (*worktree)->lockfd = -1;
374 (*worktree)->root_path = strdup(path);
375 if ((*worktree)->root_path == NULL) {
376 err = got_error_from_errno("strdup");
379 err = read_meta_file(&(*worktree)->repo_path, path_got,
380 GOT_WORKTREE_REPOSITORY);
384 err = read_meta_file(&(*worktree)->path_prefix, path_got,
385 GOT_WORKTREE_PATH_PREFIX);
389 err = read_meta_file(&base_commit_id_str, path_got,
390 GOT_WORKTREE_BASE_COMMIT);
394 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
397 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
398 if (uuid_status != uuid_s_ok) {
399 err = got_error_uuid(uuid_status);
403 err = got_repo_open(&repo, (*worktree)->repo_path);
407 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
412 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
413 GOT_WORKTREE_HEAD_REF);
416 got_repo_close(repo);
419 free(base_commit_id_str);
425 if (*worktree != NULL)
426 got_worktree_close(*worktree);
429 (*worktree)->lockfd = fd;
434 const struct got_error *
435 got_worktree_open(struct got_worktree **worktree, const char *path)
437 const struct got_error *err = NULL;
440 err = open_worktree(worktree, path);
441 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
445 path = dirname(path);
447 return got_error_from_errno2("dirname", path);
448 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
450 return got_error(GOT_ERR_NOT_WORKTREE);
453 const struct got_error *
454 got_worktree_close(struct got_worktree *worktree)
456 const struct got_error *err = NULL;
457 free(worktree->root_path);
458 free(worktree->repo_path);
459 free(worktree->path_prefix);
460 free(worktree->base_commit_id);
461 free(worktree->head_ref_name);
462 if (worktree->lockfd != -1)
463 if (close(worktree->lockfd) != 0)
464 err = got_error_from_errno2("close",
465 got_worktree_get_root_path(worktree));
471 got_worktree_get_root_path(struct got_worktree *worktree)
473 return worktree->root_path;
477 got_worktree_get_repo_path(struct got_worktree *worktree)
479 return worktree->repo_path;
483 got_worktree_get_path_prefix(struct got_worktree *worktree)
485 return worktree->path_prefix;
488 const struct got_error *
489 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
490 const char *path_prefix)
492 char *absprefix = NULL;
494 if (!got_path_is_absolute(path_prefix)) {
495 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
496 return got_error_from_errno("asprintf");
498 *match = (strcmp(absprefix ? absprefix : path_prefix,
499 worktree->path_prefix) == 0);
505 got_worktree_get_head_ref_name(struct got_worktree *worktree)
507 return worktree->head_ref_name;
510 const struct got_error *
511 got_worktree_set_head_ref(struct got_worktree *worktree,
512 struct got_reference *head_ref)
514 const struct got_error *err = NULL;
515 char *path_got = NULL, *head_ref_name = NULL;
517 if (asprintf(&path_got, "%s/%s", worktree->root_path,
518 GOT_WORKTREE_GOT_DIR) == -1) {
519 err = got_error_from_errno("asprintf");
524 head_ref_name = strdup(got_ref_get_name(head_ref));
525 if (head_ref_name == NULL) {
526 err = got_error_from_errno("strdup");
530 err = write_head_ref(path_got, head_ref);
534 free(worktree->head_ref_name);
535 worktree->head_ref_name = head_ref_name;
543 struct got_object_id *
544 got_worktree_get_base_commit_id(struct got_worktree *worktree)
546 return worktree->base_commit_id;
549 const struct got_error *
550 got_worktree_set_base_commit_id(struct got_worktree *worktree,
551 struct got_repository *repo, struct got_object_id *commit_id)
553 const struct got_error *err;
554 struct got_object *obj = NULL;
556 char *path_got = NULL;
558 if (asprintf(&path_got, "%s/%s", worktree->root_path,
559 GOT_WORKTREE_GOT_DIR) == -1) {
560 err = got_error_from_errno("asprintf");
565 err = got_object_open(&obj, repo, commit_id);
569 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
570 err = got_error(GOT_ERR_OBJ_TYPE);
574 /* Record our base commit. */
575 err = got_object_id_str(&id_str, commit_id);
578 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
582 free(worktree->base_commit_id);
583 worktree->base_commit_id = got_object_id_dup(commit_id);
584 if (worktree->base_commit_id == NULL) {
585 err = got_error_from_errno("got_object_id_dup");
590 got_object_close(obj);
596 static const struct got_error *
597 lock_worktree(struct got_worktree *worktree, int operation)
599 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
600 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
601 : got_error_from_errno2("flock",
602 got_worktree_get_root_path(worktree)));
606 static const struct got_error *
607 add_dir_on_disk(struct got_worktree *worktree, const char *path)
609 const struct got_error *err = NULL;
612 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
613 return got_error_from_errno("asprintf");
615 err = got_path_mkdir(abspath);
616 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
619 if (lstat(abspath, &sb) == -1) {
620 err = got_error_from_errno2("lstat", abspath);
621 } else if (!S_ISDIR(sb.st_mode)) {
622 /* TODO directory is obstructed; do something */
623 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
630 static const struct got_error *
631 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
633 const struct got_error *err = NULL;
636 size_t flen1 = 0, flen2 = 0;
641 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
642 if (flen1 == 0 && ferror(f1)) {
643 err = got_error_from_errno("fread");
646 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
647 if (flen2 == 0 && ferror(f2)) {
648 err = got_error_from_errno("fread");
655 } else if (flen2 == 0) {
659 } else if (flen1 == flen2) {
660 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
673 static const struct got_error *
674 check_files_equal(int *same, const char *f1_path, const char *f2_path)
676 const struct got_error *err = NULL;
679 FILE *f1 = NULL, *f2 = NULL;
683 if (lstat(f1_path, &sb) != 0) {
684 err = got_error_from_errno2("lstat", f1_path);
689 if (lstat(f2_path, &sb) != 0) {
690 err = got_error_from_errno2("lstat", f2_path);
695 if (size1 != size2) {
700 f1 = fopen(f1_path, "r");
702 return got_error_from_errno2("open", f1_path);
704 f2 = fopen(f2_path, "r");
706 err = got_error_from_errno2("open", f2_path);
710 err = check_file_contents_equal(same, f1, f2);
712 if (f1 && fclose(f1) != 0 && err == NULL)
713 err = got_error_from_errno("fclose");
714 if (f2 && fclose(f2) != 0 && err == NULL)
715 err = got_error_from_errno("fclose");
721 * Perform a 3-way merge where blob_orig acts as the common ancestor,
722 * blob_deriv acts as the first derived version, and the file on disk
723 * acts as the second derived version.
725 static const struct got_error *
726 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
727 struct got_blob_object *blob_orig, const char *ondisk_path,
728 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
729 struct got_object_id *deriv_base_commit_id,
730 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
733 const struct got_error *err = NULL;
735 FILE *f_deriv = NULL, *f_orig = NULL;
736 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
737 char *merged_path = NULL, *base_path = NULL;
739 char *label_deriv = NULL;
743 *local_changes_subsumed = 0;
745 parent = dirname(ondisk_path);
747 return got_error_from_errno2("dirname", ondisk_path);
749 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
750 return got_error_from_errno("asprintf");
752 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
757 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
758 err = got_error_from_errno("asprintf");
763 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
766 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
772 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
773 err = got_error_from_errno("asprintf");
778 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
782 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
788 * If the file has no blob, this is an "add vs add" conflict,
789 * and we simply use an empty ancestor file to make both files
790 * appear in the merged result in their entirety.
794 err = got_object_id_str(&id_str, deriv_base_commit_id);
797 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
798 err = got_error_from_errno("asprintf");
802 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
803 blob_orig_path, ondisk_path, label_deriv, path);
807 err = (*progress_cb)(progress_arg,
808 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
812 if (fsync(merged_fd) != 0) {
813 err = got_error_from_errno("fsync");
817 /* Check if a clean merge has subsumed all local changes. */
818 if (overlapcnt == 0) {
819 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
825 if (chmod(merged_path, st_mode) != 0) {
826 err = got_error_from_errno2("chmod", merged_path);
830 if (rename(merged_path, ondisk_path) != 0) {
831 err = got_error_from_errno3("rename", merged_path,
838 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
839 err = got_error_from_errno("close");
840 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
841 err = got_error_from_errno("fclose");
842 if (f_orig && fclose(f_orig) != 0 && err == NULL)
843 err = got_error_from_errno("fclose");
846 if (blob_deriv_path) {
847 unlink(blob_deriv_path);
848 free(blob_deriv_path);
850 if (blob_orig_path) {
851 unlink(blob_orig_path);
852 free(blob_orig_path);
859 static const struct got_error *
860 update_blob_fileindex_entry(struct got_worktree *worktree,
861 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
862 const char *ondisk_path, const char *path, struct got_blob_object *blob,
863 int update_timestamps)
865 const struct got_error *err = NULL;
868 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
870 err = got_fileindex_entry_update(ie, ondisk_path,
871 blob->id.sha1, worktree->base_commit_id->sha1,
874 struct got_fileindex_entry *new_ie;
875 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
876 path, blob->id.sha1, worktree->base_commit_id->sha1);
878 err = got_fileindex_entry_add(fileindex, new_ie);
883 static const struct got_error *
884 install_blob(struct got_worktree *worktree, const char *ondisk_path,
885 const char *path, uint16_t te_mode, uint16_t st_mode,
886 struct got_blob_object *blob, int restoring_missing_file,
887 int reverting_versioned_file, struct got_repository *repo,
888 got_worktree_checkout_cb progress_cb, void *progress_arg)
890 const struct got_error *err = NULL;
894 char *tmppath = NULL;
896 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
897 GOT_DEFAULT_FILE_MODE);
899 if (errno == ENOENT) {
900 char *parent = dirname(path);
902 return got_error_from_errno2("dirname", path);
903 err = add_dir_on_disk(worktree, parent);
906 fd = open(ondisk_path,
907 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
908 GOT_DEFAULT_FILE_MODE);
910 return got_error_from_errno2("open",
912 } else if (errno == EEXIST) {
913 if (!S_ISREG(st_mode)) {
914 /* TODO file is obstructed; do something */
915 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
918 err = got_opentemp_named_fd(&tmppath, &fd,
925 return got_error_from_errno2("open", ondisk_path);
928 if (restoring_missing_file)
929 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
930 else if (reverting_versioned_file)
931 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
933 err = (*progress_cb)(progress_arg,
934 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
938 hdrlen = got_object_blob_get_hdrlen(blob);
940 const uint8_t *buf = got_object_blob_get_read_buf(blob);
941 err = got_object_blob_read_block(&len, blob);
945 /* Skip blob object header first time around. */
946 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
948 err = got_error_from_errno("write");
950 } else if (outlen != len - hdrlen) {
951 err = got_error(GOT_ERR_IO);
958 if (fsync(fd) != 0) {
959 err = got_error_from_errno("fsync");
964 if (rename(tmppath, ondisk_path) != 0) {
965 err = got_error_from_errno3("rename", tmppath,
972 if (te_mode & S_IXUSR) {
973 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
974 err = got_error_from_errno2("chmod", ondisk_path);
978 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
979 err = got_error_from_errno2("chmod", ondisk_path);
985 if (fd != -1 && close(fd) != 0 && err == NULL)
986 err = got_error_from_errno("close");
991 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
992 static const struct got_error *
993 get_modified_file_content_status(unsigned char *status, FILE *f)
995 const struct got_error *err = NULL;
996 const char *markers[3] = {
997 GOT_DIFF_CONFLICT_MARKER_BEGIN,
998 GOT_DIFF_CONFLICT_MARKER_SEP,
999 GOT_DIFF_CONFLICT_MARKER_END
1004 const char delim[3] = {'\0', '\0', '\0'};
1006 while (*status == GOT_STATUS_MODIFY) {
1007 line = fparseln(f, &len, NULL, delim, 0);
1011 err = got_ferror(f, GOT_ERR_IO);
1015 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1016 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1018 *status = GOT_STATUS_CONFLICT;
1028 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1030 return !(ie->ctime_sec == sb->st_ctime &&
1031 ie->ctime_nsec == sb->st_ctimensec &&
1032 ie->mtime_sec == sb->st_mtime &&
1033 ie->mtime_nsec == sb->st_mtimensec &&
1034 ie->size == (sb->st_size & 0xffffffff));
1037 static unsigned char
1038 get_staged_status(struct got_fileindex_entry *ie)
1040 switch (got_fileindex_entry_stage_get(ie)) {
1041 case GOT_FILEIDX_STAGE_ADD:
1042 return GOT_STATUS_ADD;
1043 case GOT_FILEIDX_STAGE_DELETE:
1044 return GOT_STATUS_DELETE;
1045 case GOT_FILEIDX_STAGE_MODIFY:
1046 return GOT_STATUS_MODIFY;
1048 return GOT_STATUS_NO_CHANGE;
1052 static const struct got_error *
1053 get_file_status(unsigned char *status, struct stat *sb,
1054 struct got_fileindex_entry *ie, const char *abspath,
1055 struct got_repository *repo)
1057 const struct got_error *err = NULL;
1058 struct got_object_id id;
1062 struct got_blob_object *blob = NULL;
1064 unsigned char staged_status = get_staged_status(ie);
1066 *status = GOT_STATUS_NO_CHANGE;
1068 if (lstat(abspath, sb) == -1) {
1069 if (errno == ENOENT) {
1070 if (got_fileindex_entry_has_file_on_disk(ie))
1071 *status = GOT_STATUS_MISSING;
1073 *status = GOT_STATUS_DELETE;
1076 return got_error_from_errno2("lstat", abspath);
1079 if (!S_ISREG(sb->st_mode)) {
1080 *status = GOT_STATUS_OBSTRUCTED;
1084 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1085 *status = GOT_STATUS_DELETE;
1087 } else if (!got_fileindex_entry_has_blob(ie) &&
1088 staged_status != GOT_STATUS_ADD) {
1089 *status = GOT_STATUS_ADD;
1093 if (!stat_info_differs(ie, sb))
1096 if (staged_status == GOT_STATUS_MODIFY ||
1097 staged_status == GOT_STATUS_ADD)
1098 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1100 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1102 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1106 f = fopen(abspath, "r");
1108 err = got_error_from_errno2("fopen", abspath);
1111 hdrlen = got_object_blob_get_hdrlen(blob);
1113 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1114 err = got_object_blob_read_block(&blen, blob);
1117 /* Skip length of blob object header first time around. */
1118 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1119 if (flen == 0 && ferror(f)) {
1120 err = got_error_from_errno("fread");
1125 *status = GOT_STATUS_MODIFY;
1127 } else if (flen == 0) {
1129 *status = GOT_STATUS_MODIFY;
1131 } else if (blen - hdrlen == flen) {
1132 /* Skip blob object header first time around. */
1133 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1134 *status = GOT_STATUS_MODIFY;
1138 *status = GOT_STATUS_MODIFY;
1144 if (*status == GOT_STATUS_MODIFY) {
1146 err = get_modified_file_content_status(status, f);
1150 got_object_blob_close(blob);
1157 * Update timestamps in the file index if a file is unmodified and
1158 * we had to run a full content comparison to find out.
1160 static const struct got_error *
1161 sync_timestamps(char *ondisk_path, unsigned char status,
1162 struct got_fileindex_entry *ie, struct stat *sb)
1164 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1165 return got_fileindex_entry_update(ie, ondisk_path,
1166 ie->blob_sha1, ie->commit_sha1, 1);
1171 static const struct got_error *
1172 update_blob(struct got_worktree *worktree,
1173 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1174 struct got_tree_entry *te, const char *path,
1175 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1178 const struct got_error *err = NULL;
1179 struct got_blob_object *blob = NULL;
1181 unsigned char status = GOT_STATUS_NO_CHANGE;
1184 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1185 return got_error_from_errno("asprintf");
1188 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1189 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1192 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1195 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1196 sb.st_mode = got_fileindex_perms_to_st(ie);
1198 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1200 if (status == GOT_STATUS_OBSTRUCTED) {
1201 err = (*progress_cb)(progress_arg, status, path);
1205 if (ie && status != GOT_STATUS_MISSING) {
1206 if (got_fileindex_entry_has_commit(ie) &&
1207 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1208 SHA1_DIGEST_LENGTH) == 0) {
1209 err = sync_timestamps(ondisk_path, status, ie, &sb);
1212 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1216 if (got_fileindex_entry_has_blob(ie) &&
1217 memcmp(ie->blob_sha1, te->id->sha1,
1218 SHA1_DIGEST_LENGTH) == 0) {
1219 err = sync_timestamps(ondisk_path, status, ie, &sb);
1224 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1228 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1229 int update_timestamps;
1230 struct got_blob_object *blob2 = NULL;
1231 if (got_fileindex_entry_has_blob(ie)) {
1232 struct got_object_id id2;
1233 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1234 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1238 err = merge_blob(&update_timestamps, worktree, blob2,
1239 ondisk_path, path, sb.st_mode, blob,
1240 worktree->base_commit_id, repo,
1241 progress_cb, progress_arg);
1243 got_object_blob_close(blob2);
1245 * Do not update timestamps of files with local changes.
1246 * Otherwise, a future status walk would treat them as
1247 * unmodified files again.
1249 err = got_fileindex_entry_update(ie, ondisk_path,
1250 blob->id.sha1, worktree->base_commit_id->sha1,
1252 } else if (status == GOT_STATUS_DELETE) {
1253 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1256 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1257 ondisk_path, path, blob, 0);
1261 err = install_blob(worktree, ondisk_path, path, te->mode,
1262 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1263 repo, progress_cb, progress_arg);
1266 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1267 ondisk_path, path, blob, 1);
1271 got_object_blob_close(blob);
1277 static const struct got_error *
1278 remove_ondisk_file(const char *root_path, const char *path)
1280 const struct got_error *err = NULL;
1281 char *ondisk_path = NULL;
1283 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1284 return got_error_from_errno("asprintf");
1286 if (unlink(ondisk_path) == -1) {
1287 if (errno != ENOENT)
1288 err = got_error_from_errno2("unlink", ondisk_path);
1290 char *parent = dirname(ondisk_path);
1291 while (parent && strcmp(parent, root_path) != 0) {
1292 if (rmdir(parent) == -1) {
1293 if (errno != ENOTEMPTY)
1294 err = got_error_from_errno2("rmdir",
1298 parent = dirname(parent);
1305 static const struct got_error *
1306 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1307 struct got_fileindex_entry *ie, struct got_repository *repo,
1308 got_worktree_checkout_cb progress_cb, void *progress_arg)
1310 const struct got_error *err = NULL;
1311 unsigned char status;
1315 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1316 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1318 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1320 return got_error_from_errno("asprintf");
1322 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1326 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1327 status == GOT_STATUS_ADD) {
1328 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1332 * Preserve the working file and change the deleted blob's
1333 * entry into a schedule-add entry.
1335 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1340 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1343 if (status == GOT_STATUS_NO_CHANGE) {
1344 err = remove_ondisk_file(worktree->root_path, ie->path);
1348 got_fileindex_entry_remove(fileindex, ie);
1354 struct diff_cb_arg {
1355 struct got_fileindex *fileindex;
1356 struct got_worktree *worktree;
1357 struct got_repository *repo;
1358 got_worktree_checkout_cb progress_cb;
1360 got_worktree_cancel_cb cancel_cb;
1364 static const struct got_error *
1365 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1366 struct got_tree_entry *te, const char *parent_path)
1368 struct diff_cb_arg *a = arg;
1370 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1371 return got_error(GOT_ERR_CANCELLED);
1373 return update_blob(a->worktree, a->fileindex, ie, te,
1374 ie->path, a->repo, a->progress_cb, a->progress_arg);
1377 static const struct got_error *
1378 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1380 struct diff_cb_arg *a = arg;
1382 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1383 return got_error(GOT_ERR_CANCELLED);
1385 return delete_blob(a->worktree, a->fileindex, ie,
1386 a->repo, a->progress_cb, a->progress_arg);
1389 static const struct got_error *
1390 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1392 struct diff_cb_arg *a = arg;
1393 const struct got_error *err;
1396 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1397 return got_error(GOT_ERR_CANCELLED);
1399 if (asprintf(&path, "%s%s%s", parent_path,
1400 parent_path[0] ? "/" : "", te->name)
1402 return got_error_from_errno("asprintf");
1404 if (S_ISDIR(te->mode))
1405 err = add_dir_on_disk(a->worktree, path);
1407 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1408 a->repo, a->progress_cb, a->progress_arg);
1414 static const struct got_error *
1415 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1417 const struct got_error *err = NULL;
1418 char *uuidstr = NULL;
1419 uint32_t uuid_status;
1423 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1424 if (uuid_status != uuid_s_ok)
1425 return got_error_uuid(uuid_status);
1427 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1429 err = got_error_from_errno("asprintf");
1436 const struct got_error *
1437 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1439 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1442 static const struct got_error *
1443 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1445 return get_ref_name(refname, worktree,
1446 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1449 static const struct got_error *
1450 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1452 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1455 static const struct got_error *
1456 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1458 return get_ref_name(refname, worktree,
1459 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1462 static const struct got_error *
1463 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1465 return get_ref_name(refname, worktree,
1466 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1469 static const struct got_error *
1470 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1472 return get_ref_name(refname, worktree,
1473 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1476 static const struct got_error *
1477 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1479 return get_ref_name(refname, worktree,
1480 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1483 static const struct got_error *
1484 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1486 return get_ref_name(refname, worktree,
1487 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1490 static const struct got_error *
1491 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1493 return get_ref_name(refname, worktree,
1494 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1497 const struct got_error *
1498 got_worktree_get_histedit_script_path(char **path,
1499 struct got_worktree *worktree)
1501 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1502 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1504 return got_error_from_errno("asprintf");
1510 * Prevent Git's garbage collector from deleting our base commit by
1511 * setting a reference to our base commit's ID.
1513 static const struct got_error *
1514 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1516 const struct got_error *err = NULL;
1517 struct got_reference *ref = NULL;
1520 err = got_worktree_get_base_ref_name(&refname, worktree);
1524 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1528 err = got_ref_write(ref, repo);
1536 static const struct got_error *
1537 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1539 const struct got_error *err = NULL;
1541 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1542 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1543 err = got_error_from_errno("asprintf");
1544 *fileindex_path = NULL;
1550 static const struct got_error *
1551 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1552 struct got_worktree *worktree)
1554 const struct got_error *err = NULL;
1557 *fileindex_path = NULL;
1558 *fileindex = got_fileindex_alloc();
1559 if (*fileindex == NULL)
1560 return got_error_from_errno("got_fileindex_alloc");
1562 err = get_fileindex_path(fileindex_path, worktree);
1566 index = fopen(*fileindex_path, "rb");
1567 if (index == NULL) {
1568 if (errno != ENOENT)
1569 err = got_error_from_errno2("fopen", *fileindex_path);
1571 err = got_fileindex_read(*fileindex, index);
1572 if (fclose(index) != 0 && err == NULL)
1573 err = got_error_from_errno("fclose");
1577 free(*fileindex_path);
1578 *fileindex_path = NULL;
1579 got_fileindex_free(*fileindex);
1585 struct bump_base_commit_id_arg {
1586 struct got_object_id *base_commit_id;
1589 const char *entry_name;
1590 got_worktree_checkout_cb progress_cb;
1594 /* Bump base commit ID of all files within an updated part of the work tree. */
1595 static const struct got_error *
1596 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1598 const struct got_error *err;
1599 struct bump_base_commit_id_arg *a = arg;
1601 if (a->entry_name) {
1602 if (strcmp(ie->path, a->path) != 0)
1604 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1607 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1608 SHA1_DIGEST_LENGTH) == 0)
1611 if (a->progress_cb) {
1612 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1617 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1621 static const struct got_error *
1622 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1624 const struct got_error *err = NULL;
1625 char *new_fileindex_path = NULL;
1626 FILE *new_index = NULL;
1628 err = got_opentemp_named(&new_fileindex_path, &new_index,
1633 err = got_fileindex_write(fileindex, new_index);
1637 if (rename(new_fileindex_path, fileindex_path) != 0) {
1638 err = got_error_from_errno3("rename", new_fileindex_path,
1640 unlink(new_fileindex_path);
1645 free(new_fileindex_path);
1649 static const struct got_error *
1650 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1651 struct got_object_id **tree_id, const char *wt_relpath,
1652 struct got_worktree *worktree, struct got_repository *repo)
1654 const struct got_error *err = NULL;
1655 struct got_object_id *id = NULL;
1656 char *in_repo_path = NULL;
1657 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1659 *entry_type = GOT_OBJ_TYPE_ANY;
1660 *tree_relpath = NULL;
1663 if (wt_relpath[0] == '\0') {
1664 /* Check out all files within the work tree. */
1665 *entry_type = GOT_OBJ_TYPE_TREE;
1666 *tree_relpath = strdup("");
1667 if (*tree_relpath == NULL) {
1668 err = got_error_from_errno("strdup");
1671 err = got_object_id_by_path(tree_id, repo,
1672 worktree->base_commit_id, worktree->path_prefix);
1678 /* Check out a subset of files in the work tree. */
1680 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1681 is_root_wt ? "" : "/", wt_relpath) == -1) {
1682 err = got_error_from_errno("asprintf");
1686 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1692 in_repo_path = NULL;
1694 err = got_object_get_type(entry_type, repo, id);
1698 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1699 /* Check out a single file. */
1700 if (strchr(wt_relpath, '/') == NULL) {
1701 /* Check out a single file in work tree's root dir. */
1702 in_repo_path = strdup(worktree->path_prefix);
1703 if (in_repo_path == NULL) {
1704 err = got_error_from_errno("strdup");
1707 *tree_relpath = strdup("");
1708 if (*tree_relpath == NULL) {
1709 err = got_error_from_errno("strdup");
1713 /* Check out a single file in a subdirectory. */
1714 err = got_path_dirname(tree_relpath, wt_relpath);
1717 if (asprintf(&in_repo_path, "%s%s%s",
1718 worktree->path_prefix, is_root_wt ? "" : "/",
1719 *tree_relpath) == -1) {
1720 err = got_error_from_errno("asprintf");
1724 err = got_object_id_by_path(tree_id, repo,
1725 worktree->base_commit_id, in_repo_path);
1727 /* Check out all files within a subdirectory. */
1728 *tree_id = got_object_id_dup(id);
1729 if (*tree_id == NULL) {
1730 err = got_error_from_errno("got_object_id_dup");
1733 *tree_relpath = strdup(wt_relpath);
1734 if (*tree_relpath == NULL) {
1735 err = got_error_from_errno("strdup");
1743 *entry_type = GOT_OBJ_TYPE_ANY;
1744 free(*tree_relpath);
1745 *tree_relpath = NULL;
1752 static const struct got_error *
1753 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1754 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1755 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1756 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1758 const struct got_error *err = NULL;
1759 struct got_commit_object *commit = NULL;
1760 struct got_tree_object *tree = NULL;
1761 struct got_fileindex_diff_tree_cb diff_cb;
1762 struct diff_cb_arg arg;
1764 err = ref_base_commit(worktree, repo);
1768 err = got_object_open_as_commit(&commit, repo,
1769 worktree->base_commit_id);
1773 err = got_object_open_as_tree(&tree, repo, tree_id);
1778 got_object_tree_find_entry(tree, entry_name) == NULL) {
1779 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1783 diff_cb.diff_old_new = diff_old_new;
1784 diff_cb.diff_old = diff_old;
1785 diff_cb.diff_new = diff_new;
1786 arg.fileindex = fileindex;
1787 arg.worktree = worktree;
1789 arg.progress_cb = progress_cb;
1790 arg.progress_arg = progress_arg;
1791 arg.cancel_cb = cancel_cb;
1792 arg.cancel_arg = cancel_arg;
1793 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1794 entry_name, repo, &diff_cb, &arg);
1797 got_object_tree_close(tree);
1799 got_object_commit_close(commit);
1803 const struct got_error *
1804 got_worktree_checkout_files(struct got_worktree *worktree,
1805 struct got_pathlist_head *paths, struct got_repository *repo,
1806 got_worktree_checkout_cb progress_cb, void *progress_arg,
1807 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1809 const struct got_error *err = NULL, *sync_err, *unlockerr;
1810 struct got_commit_object *commit = NULL;
1811 struct got_tree_object *tree = NULL;
1812 struct got_fileindex *fileindex = NULL;
1813 char *fileindex_path = NULL;
1814 struct got_pathlist_entry *pe;
1815 struct tree_path_data {
1816 SIMPLEQ_ENTRY(tree_path_data) entry;
1817 struct got_object_id *tree_id;
1822 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1824 SIMPLEQ_INIT(&tree_paths);
1826 err = lock_worktree(worktree, LOCK_EX);
1830 /* Map all specified paths to in-repository trees. */
1831 TAILQ_FOREACH(pe, paths, entry) {
1832 tpd = malloc(sizeof(*tpd));
1834 err = got_error_from_errno("malloc");
1838 err = find_tree_entry_for_checkout(&tpd->entry_type,
1839 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1845 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1846 err = got_path_basename(&tpd->entry_name, pe->path);
1854 tpd->entry_name = NULL;
1856 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1860 * Read the file index.
1861 * Checking out files is supposed to be an idempotent operation.
1862 * If the on-disk file index is incomplete we will try to complete it.
1864 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1868 tpd = SIMPLEQ_FIRST(&tree_paths);
1869 TAILQ_FOREACH(pe, paths, entry) {
1870 struct bump_base_commit_id_arg bbc_arg;
1872 err = checkout_files(worktree, fileindex, tpd->relpath,
1873 tpd->tree_id, tpd->entry_name, repo,
1874 progress_cb, progress_arg, cancel_cb, cancel_arg);
1878 bbc_arg.base_commit_id = worktree->base_commit_id;
1879 bbc_arg.entry_name = tpd->entry_name;
1880 bbc_arg.path = pe->path;
1881 bbc_arg.path_len = pe->path_len;
1882 bbc_arg.progress_cb = progress_cb;
1883 bbc_arg.progress_arg = progress_arg;
1884 err = got_fileindex_for_each_entry_safe(fileindex,
1885 bump_base_commit_id, &bbc_arg);
1889 tpd = SIMPLEQ_NEXT(tpd, entry);
1891 sync_err = sync_fileindex(fileindex, fileindex_path);
1892 if (sync_err && err == NULL)
1895 free(fileindex_path);
1897 got_object_tree_close(tree);
1899 got_object_commit_close(commit);
1901 got_fileindex_free(fileindex);
1902 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1903 tpd = SIMPLEQ_FIRST(&tree_paths);
1904 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1909 unlockerr = lock_worktree(worktree, LOCK_SH);
1910 if (unlockerr && err == NULL)
1915 struct merge_file_cb_arg {
1916 struct got_worktree *worktree;
1917 struct got_fileindex *fileindex;
1918 got_worktree_checkout_cb progress_cb;
1920 got_worktree_cancel_cb cancel_cb;
1922 struct got_object_id *commit_id2;
1925 static const struct got_error *
1926 merge_file_cb(void *arg, struct got_blob_object *blob1,
1927 struct got_blob_object *blob2, struct got_object_id *id1,
1928 struct got_object_id *id2, const char *path1, const char *path2,
1929 struct got_repository *repo)
1931 static const struct got_error *err = NULL;
1932 struct merge_file_cb_arg *a = arg;
1933 struct got_fileindex_entry *ie;
1934 char *ondisk_path = NULL;
1936 unsigned char status;
1937 int local_changes_subsumed;
1939 if (blob1 && blob2) {
1940 ie = got_fileindex_entry_get(a->fileindex, path2,
1943 return (*a->progress_cb)(a->progress_arg,
1944 GOT_STATUS_MISSING, path2);
1946 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1948 return got_error_from_errno("asprintf");
1950 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1954 if (status == GOT_STATUS_DELETE) {
1955 err = (*a->progress_cb)(a->progress_arg,
1956 GOT_STATUS_MERGE, path2);
1959 if (status != GOT_STATUS_NO_CHANGE &&
1960 status != GOT_STATUS_MODIFY &&
1961 status != GOT_STATUS_CONFLICT &&
1962 status != GOT_STATUS_ADD) {
1963 err = (*a->progress_cb)(a->progress_arg, status, path2);
1967 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1968 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1969 a->progress_cb, a->progress_arg);
1971 ie = got_fileindex_entry_get(a->fileindex, path1,
1974 return (*a->progress_cb)(a->progress_arg,
1975 GOT_STATUS_MISSING, path2);
1977 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1979 return got_error_from_errno("asprintf");
1981 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1986 case GOT_STATUS_NO_CHANGE:
1987 err = (*a->progress_cb)(a->progress_arg,
1988 GOT_STATUS_DELETE, path1);
1991 err = remove_ondisk_file(a->worktree->root_path, path1);
1995 got_fileindex_entry_mark_deleted_from_disk(ie);
1997 case GOT_STATUS_DELETE:
1998 case GOT_STATUS_MISSING:
1999 err = (*a->progress_cb)(a->progress_arg,
2000 GOT_STATUS_DELETE, path1);
2004 got_fileindex_entry_mark_deleted_from_disk(ie);
2006 case GOT_STATUS_ADD:
2007 case GOT_STATUS_MODIFY:
2008 case GOT_STATUS_CONFLICT:
2009 err = (*a->progress_cb)(a->progress_arg,
2010 GOT_STATUS_CANNOT_DELETE, path1);
2014 case GOT_STATUS_OBSTRUCTED:
2015 err = (*a->progress_cb)(a->progress_arg, status, path1);
2023 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2025 return got_error_from_errno("asprintf");
2026 ie = got_fileindex_entry_get(a->fileindex, path2,
2029 err = get_file_status(&status, &sb, ie, ondisk_path,
2033 if (status != GOT_STATUS_NO_CHANGE &&
2034 status != GOT_STATUS_MODIFY &&
2035 status != GOT_STATUS_CONFLICT &&
2036 status != GOT_STATUS_ADD) {
2037 err = (*a->progress_cb)(a->progress_arg,
2041 err = merge_blob(&local_changes_subsumed, a->worktree,
2042 NULL, ondisk_path, path2, sb.st_mode, blob2,
2043 a->commit_id2, repo,
2044 a->progress_cb, a->progress_arg);
2045 if (status == GOT_STATUS_DELETE) {
2046 err = update_blob_fileindex_entry(a->worktree,
2047 a->fileindex, ie, ondisk_path, ie->path,
2053 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2054 err = install_blob(a->worktree, ondisk_path, path2,
2055 /* XXX get this from parent tree! */
2056 GOT_DEFAULT_FILE_MODE,
2057 sb.st_mode, blob2, 0, 0, repo,
2058 a->progress_cb, a->progress_arg);
2061 err = got_fileindex_entry_alloc(&ie,
2062 ondisk_path, path2, NULL, NULL);
2065 err = got_fileindex_entry_add(a->fileindex, ie);
2067 got_fileindex_entry_free(ie);
2077 struct check_merge_ok_arg {
2078 struct got_worktree *worktree;
2079 struct got_repository *repo;
2082 static const struct got_error *
2083 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2085 const struct got_error *err = NULL;
2086 struct check_merge_ok_arg *a = arg;
2087 unsigned char status;
2091 /* Reject merges into a work tree with mixed base commits. */
2092 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2093 SHA1_DIGEST_LENGTH))
2094 return got_error(GOT_ERR_MIXED_COMMITS);
2096 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2098 return got_error_from_errno("asprintf");
2100 /* Reject merges into a work tree with conflicted files. */
2101 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2104 if (status == GOT_STATUS_CONFLICT)
2105 return got_error(GOT_ERR_CONFLICTS);
2110 static const struct got_error *
2111 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2112 const char *fileindex_path, struct got_object_id *commit_id1,
2113 struct got_object_id *commit_id2, struct got_repository *repo,
2114 got_worktree_checkout_cb progress_cb, void *progress_arg,
2115 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2117 const struct got_error *err = NULL, *sync_err;
2118 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2119 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2120 struct merge_file_cb_arg arg;
2123 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2124 worktree->path_prefix);
2128 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2133 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2134 worktree->path_prefix);
2138 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2142 arg.worktree = worktree;
2143 arg.fileindex = fileindex;
2144 arg.progress_cb = progress_cb;
2145 arg.progress_arg = progress_arg;
2146 arg.cancel_cb = cancel_cb;
2147 arg.cancel_arg = cancel_arg;
2148 arg.commit_id2 = commit_id2;
2149 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2150 sync_err = sync_fileindex(fileindex, fileindex_path);
2151 if (sync_err && err == NULL)
2155 got_object_tree_close(tree1);
2157 got_object_tree_close(tree2);
2161 const struct got_error *
2162 got_worktree_merge_files(struct got_worktree *worktree,
2163 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2164 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2165 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2167 const struct got_error *err, *unlockerr;
2168 char *fileindex_path = NULL;
2169 struct got_fileindex *fileindex = NULL;
2170 struct check_merge_ok_arg mok_arg;
2172 err = lock_worktree(worktree, LOCK_EX);
2176 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2180 mok_arg.worktree = worktree;
2181 mok_arg.repo = repo;
2182 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2187 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2188 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2191 got_fileindex_free(fileindex);
2192 free(fileindex_path);
2193 unlockerr = lock_worktree(worktree, LOCK_SH);
2194 if (unlockerr && err == NULL)
2199 struct diff_dir_cb_arg {
2200 struct got_fileindex *fileindex;
2201 struct got_worktree *worktree;
2202 const char *status_path;
2203 size_t status_path_len;
2204 struct got_repository *repo;
2205 got_worktree_status_cb status_cb;
2207 got_worktree_cancel_cb cancel_cb;
2211 static const struct got_error *
2212 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2213 got_worktree_status_cb status_cb, void *status_arg,
2214 struct got_repository *repo)
2216 const struct got_error *err = NULL;
2217 unsigned char status = GOT_STATUS_NO_CHANGE;
2218 unsigned char staged_status = get_staged_status(ie);
2220 struct got_object_id blob_id, commit_id, staged_blob_id;
2221 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2222 struct got_object_id *staged_blob_idp = NULL;
2224 err = get_file_status(&status, &sb, ie, abspath, repo);
2228 if (status == GOT_STATUS_NO_CHANGE &&
2229 staged_status == GOT_STATUS_NO_CHANGE)
2232 if (got_fileindex_entry_has_blob(ie)) {
2233 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2234 blob_idp = &blob_id;
2236 if (got_fileindex_entry_has_commit(ie)) {
2237 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2238 commit_idp = &commit_id;
2240 if (staged_status == GOT_STATUS_ADD ||
2241 staged_status == GOT_STATUS_MODIFY) {
2242 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2243 SHA1_DIGEST_LENGTH);
2244 staged_blob_idp = &staged_blob_id;
2247 return (*status_cb)(status_arg, status, staged_status,
2248 ie->path, blob_idp, staged_blob_idp, commit_idp);
2251 static const struct got_error *
2252 status_old_new(void *arg, struct got_fileindex_entry *ie,
2253 struct dirent *de, const char *parent_path)
2255 const struct got_error *err = NULL;
2256 struct diff_dir_cb_arg *a = arg;
2259 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2260 return got_error(GOT_ERR_CANCELLED);
2262 if (got_path_cmp(parent_path, a->status_path,
2263 strlen(parent_path), a->status_path_len) != 0 &&
2264 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2267 if (parent_path[0]) {
2268 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2269 parent_path, de->d_name) == -1)
2270 return got_error_from_errno("asprintf");
2272 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2274 return got_error_from_errno("asprintf");
2277 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2283 static const struct got_error *
2284 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2286 struct diff_dir_cb_arg *a = arg;
2287 struct got_object_id blob_id, commit_id;
2288 unsigned char status;
2290 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 return got_error(GOT_ERR_CANCELLED);
2293 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2296 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2297 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2298 if (got_fileindex_entry_has_file_on_disk(ie))
2299 status = GOT_STATUS_MISSING;
2301 status = GOT_STATUS_DELETE;
2302 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2303 ie->path, &blob_id, NULL, &commit_id);
2306 static const struct got_error *
2307 status_new(void *arg, struct dirent *de, const char *parent_path)
2309 const struct got_error *err = NULL;
2310 struct diff_dir_cb_arg *a = arg;
2313 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2314 return got_error(GOT_ERR_CANCELLED);
2316 if (de->d_type == DT_DIR)
2319 /* XXX ignore symlinks for now */
2320 if (de->d_type == DT_LNK)
2323 if (parent_path[0]) {
2324 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2325 return got_error_from_errno("asprintf");
2330 if (got_path_is_child(path, a->status_path, a->status_path_len))
2331 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2332 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2338 static const struct got_error *
2339 report_single_file_status(const char *path, const char *ondisk_path,
2340 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2341 void *status_arg, struct got_repository *repo)
2343 struct got_fileindex_entry *ie;
2346 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2348 return report_file_status(ie, ondisk_path, status_cb,
2351 if (lstat(ondisk_path, &sb) == -1) {
2352 if (errno != ENOENT)
2353 return got_error_from_errno2("lstat", ondisk_path);
2357 if (S_ISREG(sb.st_mode))
2358 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2359 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2364 static const struct got_error *
2365 worktree_status(struct got_worktree *worktree, const char *path,
2366 struct got_fileindex *fileindex, struct got_repository *repo,
2367 got_worktree_status_cb status_cb, void *status_arg,
2368 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2370 const struct got_error *err = NULL;
2371 DIR *workdir = NULL;
2372 struct got_fileindex_diff_dir_cb fdiff_cb;
2373 struct diff_dir_cb_arg arg;
2374 char *ondisk_path = NULL;
2376 if (asprintf(&ondisk_path, "%s%s%s",
2377 worktree->root_path, path[0] ? "/" : "", path) == -1)
2378 return got_error_from_errno("asprintf");
2380 workdir = opendir(ondisk_path);
2381 if (workdir == NULL) {
2382 if (errno != ENOTDIR && errno != ENOENT)
2383 err = got_error_from_errno2("opendir", ondisk_path);
2385 err = report_single_file_status(path, ondisk_path,
2386 fileindex, status_cb, status_arg, repo);
2388 fdiff_cb.diff_old_new = status_old_new;
2389 fdiff_cb.diff_old = status_old;
2390 fdiff_cb.diff_new = status_new;
2391 arg.fileindex = fileindex;
2392 arg.worktree = worktree;
2393 arg.status_path = path;
2394 arg.status_path_len = strlen(path);
2396 arg.status_cb = status_cb;
2397 arg.status_arg = status_arg;
2398 arg.cancel_cb = cancel_cb;
2399 arg.cancel_arg = cancel_arg;
2400 err = got_fileindex_diff_dir(fileindex, workdir,
2401 worktree->root_path, path, repo, &fdiff_cb, &arg);
2410 const struct got_error *
2411 got_worktree_status(struct got_worktree *worktree,
2412 struct got_pathlist_head *paths, struct got_repository *repo,
2413 got_worktree_status_cb status_cb, void *status_arg,
2414 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2416 const struct got_error *err = NULL;
2417 char *fileindex_path = NULL;
2418 struct got_fileindex *fileindex = NULL;
2419 struct got_pathlist_entry *pe;
2421 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2425 TAILQ_FOREACH(pe, paths, entry) {
2426 err = worktree_status(worktree, pe->path, fileindex, repo,
2427 status_cb, status_arg, cancel_cb, cancel_arg);
2431 free(fileindex_path);
2432 got_fileindex_free(fileindex);
2436 const struct got_error *
2437 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2440 const struct got_error *err = NULL;
2441 char *resolved, *cwd = NULL, *path = NULL;
2446 resolved = realpath(arg, NULL);
2447 if (resolved == NULL) {
2448 if (errno != ENOENT)
2449 return got_error_from_errno2("realpath", arg);
2450 cwd = getcwd(NULL, 0);
2452 return got_error_from_errno("getcwd");
2453 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2454 err = got_error_from_errno("asprintf");
2459 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2460 strlen(got_worktree_get_root_path(worktree)))) {
2461 err = got_error(GOT_ERR_BAD_PATH);
2465 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2466 err = got_path_skip_common_ancestor(&path,
2467 got_worktree_get_root_path(worktree), resolved);
2473 err = got_error_from_errno("strdup");
2478 /* XXX status walk can't deal with trailing slash! */
2480 while (len > 0 && path[len - 1] == '/') {
2481 path[len - 1] = '\0';
2494 static const struct got_error *
2495 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2496 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2497 struct got_repository *repo)
2499 const struct got_error *err = NULL;
2500 struct got_fileindex_entry *ie;
2501 unsigned char status;
2504 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2506 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2509 /* Re-adding an existing entry is a no-op. */
2510 if (status == GOT_STATUS_ADD)
2512 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2515 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2519 err = got_fileindex_entry_add(fileindex, ie);
2521 got_fileindex_entry_free(ie);
2525 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2528 const struct got_error *
2529 got_worktree_schedule_add(struct got_worktree *worktree,
2530 struct got_pathlist_head *paths,
2531 got_worktree_status_cb status_cb, void *status_arg,
2532 struct got_repository *repo)
2534 struct got_fileindex *fileindex = NULL;
2535 char *fileindex_path = NULL;
2536 const struct got_error *err = NULL, *sync_err, *unlockerr;
2537 struct got_pathlist_entry *pe;
2539 err = lock_worktree(worktree, LOCK_EX);
2543 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2547 TAILQ_FOREACH(pe, paths, entry) {
2549 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2551 return got_error_from_errno("asprintf");
2552 err = schedule_addition(ondisk_path, fileindex, pe->path,
2553 status_cb, status_arg, repo);
2558 sync_err = sync_fileindex(fileindex, fileindex_path);
2559 if (sync_err && err == NULL)
2562 free(fileindex_path);
2564 got_fileindex_free(fileindex);
2565 unlockerr = lock_worktree(worktree, LOCK_SH);
2566 if (unlockerr && err == NULL)
2571 static const struct got_error *
2572 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2573 const char *relpath, int delete_local_mods,
2574 got_worktree_status_cb status_cb, void *status_arg,
2575 struct got_repository *repo)
2577 const struct got_error *err = NULL;
2578 struct got_fileindex_entry *ie = NULL;
2579 unsigned char status, staged_status;
2582 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2584 return got_error(GOT_ERR_BAD_PATH);
2586 staged_status = get_staged_status(ie);
2587 if (staged_status != GOT_STATUS_NO_CHANGE) {
2588 if (staged_status == GOT_STATUS_DELETE)
2590 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2593 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2597 if (status != GOT_STATUS_NO_CHANGE) {
2598 if (status == GOT_STATUS_DELETE)
2600 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2601 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2602 if (status != GOT_STATUS_MODIFY &&
2603 status != GOT_STATUS_MISSING)
2604 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2607 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2608 return got_error_from_errno2("unlink", ondisk_path);
2610 got_fileindex_entry_mark_deleted_from_disk(ie);
2611 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2614 const struct got_error *
2615 got_worktree_schedule_delete(struct got_worktree *worktree,
2616 struct got_pathlist_head *paths, int delete_local_mods,
2617 got_worktree_status_cb status_cb, void *status_arg,
2618 struct got_repository *repo)
2620 struct got_fileindex *fileindex = NULL;
2621 char *fileindex_path = NULL;
2622 const struct got_error *err = NULL, *sync_err, *unlockerr;
2623 struct got_pathlist_entry *pe;
2625 err = lock_worktree(worktree, LOCK_EX);
2629 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2633 TAILQ_FOREACH(pe, paths, entry) {
2635 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2637 return got_error_from_errno("asprintf");
2638 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2639 delete_local_mods, status_cb, status_arg, repo);
2644 sync_err = sync_fileindex(fileindex, fileindex_path);
2645 if (sync_err && err == NULL)
2648 free(fileindex_path);
2650 got_fileindex_free(fileindex);
2651 unlockerr = lock_worktree(worktree, LOCK_SH);
2652 if (unlockerr && err == NULL)
2657 static const struct got_error *
2658 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2659 const char *ondisk_path,
2660 got_worktree_checkout_cb progress_cb, void *progress_arg,
2661 struct got_repository *repo)
2663 const struct got_error *err = NULL;
2664 char *relpath = NULL, *parent_path = NULL;
2665 struct got_fileindex_entry *ie;
2666 struct got_tree_object *tree = NULL;
2667 struct got_object_id *tree_id = NULL;
2668 const struct got_tree_entry *te = NULL;
2669 char *tree_path = NULL, *te_name;
2670 struct got_blob_object *blob = NULL;
2671 unsigned char status, staged_status;
2674 err = got_path_skip_common_ancestor(&relpath,
2675 got_worktree_get_root_path(worktree), ondisk_path);
2679 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2681 err = got_error(GOT_ERR_BAD_PATH);
2685 /* Construct in-repository path of tree which contains this blob. */
2686 err = got_path_dirname(&parent_path, ie->path);
2688 if (err->code != GOT_ERR_BAD_PATH)
2690 parent_path = strdup("/");
2691 if (parent_path == NULL) {
2692 err = got_error_from_errno("strdup");
2696 if (got_path_is_root_dir(worktree->path_prefix)) {
2697 tree_path = strdup(parent_path);
2698 if (tree_path == NULL) {
2699 err = got_error_from_errno("strdup");
2703 if (got_path_is_root_dir(parent_path)) {
2704 tree_path = strdup(worktree->path_prefix);
2705 if (tree_path == NULL) {
2706 err = got_error_from_errno("strdup");
2710 if (asprintf(&tree_path, "%s/%s",
2711 worktree->path_prefix, parent_path) == -1) {
2712 err = got_error_from_errno("asprintf");
2718 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2721 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2722 sb.st_mode = got_fileindex_perms_to_st(ie);
2724 staged_status = get_staged_status(ie);
2725 if (status == GOT_STATUS_DELETE &&
2726 staged_status != GOT_STATUS_NO_CHANGE) {
2727 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2731 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2734 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2735 (status == GOT_STATUS_ADD ||
2736 staged_status == GOT_STATUS_ADD)))
2739 err = got_object_open_as_tree(&tree, repo, tree_id);
2743 te_name = basename(ie->path);
2744 if (te_name == NULL) {
2745 err = got_error_from_errno2("basename", ie->path);
2749 te = got_object_tree_find_entry(tree, te_name);
2750 if (te == NULL && status != GOT_STATUS_ADD &&
2751 staged_status != GOT_STATUS_ADD) {
2752 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2758 case GOT_STATUS_ADD:
2759 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2762 got_fileindex_entry_remove(fileindex, ie);
2764 case GOT_STATUS_DELETE:
2765 case GOT_STATUS_MODIFY:
2766 case GOT_STATUS_CONFLICT:
2767 case GOT_STATUS_MISSING: {
2768 struct got_object_id id;
2769 if (staged_status == GOT_STATUS_ADD ||
2770 staged_status == GOT_STATUS_MODIFY) {
2771 memcpy(id.sha1, ie->staged_blob_sha1,
2772 SHA1_DIGEST_LENGTH);
2774 memcpy(id.sha1, ie->blob_sha1,
2775 SHA1_DIGEST_LENGTH);
2776 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2779 err = install_blob(worktree, ondisk_path, ie->path,
2780 te ? te->mode : GOT_DEFAULT_FILE_MODE, sb.st_mode,
2781 blob, 0, 1, repo, progress_cb, progress_arg);
2784 if (status == GOT_STATUS_DELETE) {
2785 err = update_blob_fileindex_entry(worktree,
2786 fileindex, ie, ondisk_path, ie->path, blob, 1);
2800 got_object_blob_close(blob);
2802 got_object_tree_close(tree);
2807 const struct got_error *
2808 got_worktree_revert(struct got_worktree *worktree,
2809 struct got_pathlist_head *ondisk_paths,
2810 got_worktree_checkout_cb progress_cb, void *progress_arg,
2811 struct got_repository *repo)
2813 struct got_fileindex *fileindex = NULL;
2814 char *fileindex_path = NULL;
2815 const struct got_error *err = NULL, *unlockerr = NULL;
2816 const struct got_error *sync_err = NULL;
2817 struct got_pathlist_entry *pe;
2819 err = lock_worktree(worktree, LOCK_EX);
2823 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2827 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2829 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2831 return got_error_from_errno("asprintf");
2832 err = revert_file(worktree, fileindex, ondisk_path,
2833 progress_cb, progress_arg, repo);
2838 sync_err = sync_fileindex(fileindex, fileindex_path);
2839 if (sync_err && err == NULL)
2842 free(fileindex_path);
2844 got_fileindex_free(fileindex);
2845 unlockerr = lock_worktree(worktree, LOCK_SH);
2846 if (unlockerr && err == NULL)
2852 free_commitable(struct got_commitable *ct)
2855 free(ct->in_repo_path);
2856 free(ct->ondisk_path);
2858 free(ct->base_blob_id);
2859 free(ct->staged_blob_id);
2860 free(ct->base_commit_id);
2864 struct collect_commitables_arg {
2865 struct got_pathlist_head *commitable_paths;
2866 struct got_repository *repo;
2867 struct got_worktree *worktree;
2868 int have_staged_files;
2871 static const struct got_error *
2872 collect_commitables(void *arg, unsigned char status,
2873 unsigned char staged_status, const char *relpath,
2874 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2875 struct got_object_id *commit_id)
2877 struct collect_commitables_arg *a = arg;
2878 const struct got_error *err = NULL;
2879 struct got_commitable *ct = NULL;
2880 struct got_pathlist_entry *new = NULL;
2881 char *parent_path = NULL, *path = NULL;
2884 if (a->have_staged_files) {
2885 if (staged_status != GOT_STATUS_MODIFY &&
2886 staged_status != GOT_STATUS_ADD &&
2887 staged_status != GOT_STATUS_DELETE)
2890 if (status == GOT_STATUS_CONFLICT)
2891 return got_error(GOT_ERR_COMMIT_CONFLICT);
2893 if (status != GOT_STATUS_MODIFY &&
2894 status != GOT_STATUS_ADD &&
2895 status != GOT_STATUS_DELETE)
2899 if (asprintf(&path, "/%s", relpath) == -1) {
2900 err = got_error_from_errno("asprintf");
2903 if (strcmp(path, "/") == 0) {
2904 parent_path = strdup("");
2905 if (parent_path == NULL)
2906 return got_error_from_errno("strdup");
2908 err = got_path_dirname(&parent_path, path);
2913 ct = calloc(1, sizeof(*ct));
2915 err = got_error_from_errno("calloc");
2919 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2921 err = got_error_from_errno("asprintf");
2924 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
2925 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2927 if (lstat(ct->ondisk_path, &sb) != 0) {
2928 err = got_error_from_errno2("lstat", ct->ondisk_path);
2931 ct->mode = sb.st_mode;
2934 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2935 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2937 err = got_error_from_errno("asprintf");
2941 ct->status = status;
2942 ct->staged_status = staged_status;
2943 ct->blob_id = NULL; /* will be filled in when blob gets created */
2944 if (ct->status != GOT_STATUS_ADD &&
2945 ct->staged_status != GOT_STATUS_ADD) {
2946 ct->base_blob_id = got_object_id_dup(blob_id);
2947 if (ct->base_blob_id == NULL) {
2948 err = got_error_from_errno("got_object_id_dup");
2951 ct->base_commit_id = got_object_id_dup(commit_id);
2952 if (ct->base_commit_id == NULL) {
2953 err = got_error_from_errno("got_object_id_dup");
2957 if (ct->staged_status == GOT_STATUS_ADD ||
2958 ct->staged_status == GOT_STATUS_MODIFY) {
2959 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
2960 if (ct->staged_blob_id == NULL) {
2961 err = got_error_from_errno("got_object_id_dup");
2965 ct->path = strdup(path);
2966 if (ct->path == NULL) {
2967 err = got_error_from_errno("strdup");
2970 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2972 if (ct && (err || new == NULL))
2973 free_commitable(ct);
2979 static const struct got_error *write_tree(struct got_object_id **,
2980 struct got_tree_object *, const char *, struct got_pathlist_head *,
2981 got_worktree_status_cb status_cb, void *status_arg,
2982 struct got_repository *);
2984 static const struct got_error *
2985 write_subtree(struct got_object_id **new_subtree_id,
2986 struct got_tree_entry *te, const char *parent_path,
2987 struct got_pathlist_head *commitable_paths,
2988 got_worktree_status_cb status_cb, void *status_arg,
2989 struct got_repository *repo)
2991 const struct got_error *err = NULL;
2992 struct got_tree_object *subtree;
2995 if (asprintf(&subpath, "%s%s%s", parent_path,
2996 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2997 return got_error_from_errno("asprintf");
2999 err = got_object_open_as_tree(&subtree, repo, te->id);
3003 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3004 status_cb, status_arg, repo);
3005 got_object_tree_close(subtree);
3010 static const struct got_error *
3011 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3013 const struct got_error *err = NULL;
3014 char *ct_parent_path = NULL;
3018 if (strchr(ct->in_repo_path, '/') == NULL) {
3019 *match = got_path_is_root_dir(path);
3023 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3026 *match = (strcmp(path, ct_parent_path) == 0);
3027 free(ct_parent_path);
3032 get_ct_file_mode(struct got_commitable *ct)
3034 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3037 static const struct got_error *
3038 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3039 struct got_tree_entry *te, struct got_commitable *ct)
3041 const struct got_error *err = NULL;
3045 err = got_object_tree_entry_dup(new_te, te);
3049 (*new_te)->mode = get_ct_file_mode(ct);
3051 free((*new_te)->id);
3052 if (ct->staged_status == GOT_STATUS_MODIFY)
3053 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3055 (*new_te)->id = got_object_id_dup(ct->blob_id);
3056 if ((*new_te)->id == NULL) {
3057 err = got_error_from_errno("got_object_id_dup");
3061 if (err && *new_te) {
3062 got_object_tree_entry_close(*new_te);
3068 static const struct got_error *
3069 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3070 struct got_commitable *ct)
3072 const struct got_error *err = NULL;
3077 *new_te = calloc(1, sizeof(**new_te));
3078 if (*new_te == NULL)
3079 return got_error_from_errno("calloc");
3081 ct_name = basename(ct->path);
3082 if (ct_name == NULL) {
3083 err = got_error_from_errno2("basename", ct->path);
3086 (*new_te)->name = strdup(ct_name);
3087 if ((*new_te)->name == NULL) {
3088 err = got_error_from_errno("strdup");
3092 (*new_te)->mode = get_ct_file_mode(ct);
3094 if (ct->staged_status == GOT_STATUS_ADD)
3095 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3097 (*new_te)->id = got_object_id_dup(ct->blob_id);
3098 if ((*new_te)->id == NULL) {
3099 err = got_error_from_errno("got_object_id_dup");
3103 if (err && *new_te) {
3104 got_object_tree_entry_close(*new_te);
3110 static const struct got_error *
3111 insert_tree_entry(struct got_tree_entry *new_te,
3112 struct got_pathlist_head *paths)
3114 const struct got_error *err = NULL;
3115 struct got_pathlist_entry *new_pe;
3117 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3121 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3125 static const struct got_error *
3126 report_ct_status(struct got_commitable *ct,
3127 got_worktree_status_cb status_cb, void *status_arg)
3129 const char *ct_path = ct->path;
3130 unsigned char status;
3132 while (ct_path[0] == '/')
3135 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3136 status = ct->staged_status;
3138 status = ct->status;
3140 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3141 ct_path, ct->blob_id, NULL, NULL);
3144 static const struct got_error *
3145 match_modified_subtree(int *modified, struct got_tree_entry *te,
3146 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3148 const struct got_error *err = NULL;
3149 struct got_pathlist_entry *pe;
3154 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3155 got_path_is_root_dir(base_tree_path) ? "" : "/",
3157 return got_error_from_errno("asprintf");
3159 TAILQ_FOREACH(pe, commitable_paths, entry) {
3160 struct got_commitable *ct = pe->data;
3161 *modified = got_path_is_child(ct->in_repo_path, te_path,
3171 static const struct got_error *
3172 match_deleted_or_modified_ct(struct got_commitable **ctp,
3173 struct got_tree_entry *te, const char *base_tree_path,
3174 struct got_pathlist_head *commitable_paths)
3176 const struct got_error *err = NULL;
3177 struct got_pathlist_entry *pe;
3181 TAILQ_FOREACH(pe, commitable_paths, entry) {
3182 struct got_commitable *ct = pe->data;
3183 char *ct_name = NULL;
3186 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3187 if (ct->status != GOT_STATUS_MODIFY &&
3188 ct->status != GOT_STATUS_DELETE)
3191 if (ct->staged_status != GOT_STATUS_MODIFY &&
3192 ct->staged_status != GOT_STATUS_DELETE)
3196 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3199 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3205 ct_name = basename(pe->path);
3206 if (ct_name == NULL)
3207 return got_error_from_errno2("basename", pe->path);
3209 if (strcmp(te->name, ct_name) != 0)
3219 static const struct got_error *
3220 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3221 const char *child_path, const char *path_base_tree,
3222 struct got_pathlist_head *commitable_paths,
3223 got_worktree_status_cb status_cb, void *status_arg,
3224 struct got_repository *repo)
3226 const struct got_error *err = NULL;
3227 struct got_tree_entry *new_te;
3232 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3233 got_path_is_root_dir(path_base_tree) ? "" : "/",
3235 return got_error_from_errno("asprintf");
3237 new_te = calloc(1, sizeof(*new_te));
3238 new_te->mode = S_IFDIR;
3239 new_te->name = strdup(child_path);
3240 if (new_te->name == NULL) {
3241 err = got_error_from_errno("strdup");
3242 got_object_tree_entry_close(new_te);
3245 err = write_tree(&new_te->id, NULL, subtree_path,
3246 commitable_paths, status_cb, status_arg, repo);
3248 got_object_tree_entry_close(new_te);
3258 static const struct got_error *
3259 write_tree(struct got_object_id **new_tree_id,
3260 struct got_tree_object *base_tree, const char *path_base_tree,
3261 struct got_pathlist_head *commitable_paths,
3262 got_worktree_status_cb status_cb, void *status_arg,
3263 struct got_repository *repo)
3265 const struct got_error *err = NULL;
3266 const struct got_tree_entries *base_entries = NULL;
3267 struct got_pathlist_head paths;
3268 struct got_tree_entries new_tree_entries;
3269 struct got_tree_entry *te, *new_te = NULL;
3270 struct got_pathlist_entry *pe;
3273 new_tree_entries.nentries = 0;
3274 SIMPLEQ_INIT(&new_tree_entries.head);
3276 /* Insert, and recurse into, newly added entries first. */
3277 TAILQ_FOREACH(pe, commitable_paths, entry) {
3278 struct got_commitable *ct = pe->data;
3279 char *child_path = NULL, *slash;
3281 if ((ct->status != GOT_STATUS_ADD &&
3282 ct->staged_status != GOT_STATUS_ADD) ||
3283 (ct->flags & GOT_COMMITABLE_ADDED))
3286 if (!got_path_is_child(pe->path, path_base_tree,
3287 strlen(path_base_tree)))
3290 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3295 slash = strchr(child_path, '/');
3296 if (slash == NULL) {
3297 err = alloc_added_blob_tree_entry(&new_te, ct);
3300 err = report_ct_status(ct, status_cb, status_arg);
3303 ct->flags |= GOT_COMMITABLE_ADDED;
3304 err = insert_tree_entry(new_te, &paths);
3308 *slash = '\0'; /* trim trailing path components */
3309 if (base_tree == NULL ||
3310 got_object_tree_find_entry(base_tree, child_path)
3312 err = make_subtree_for_added_blob(&new_te,
3313 child_path, path_base_tree,
3314 commitable_paths, status_cb, status_arg,
3318 err = insert_tree_entry(new_te, &paths);
3326 /* Handle modified and deleted entries. */
3327 base_entries = got_object_tree_get_entries(base_tree);
3328 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3329 struct got_commitable *ct = NULL;
3331 if (S_ISDIR(te->mode)) {
3333 err = got_object_tree_entry_dup(&new_te, te);
3336 err = match_modified_subtree(&modified, te,
3337 path_base_tree, commitable_paths);
3340 /* Avoid recursion into unmodified subtrees. */
3343 err = write_subtree(&new_te->id, te,
3344 path_base_tree, commitable_paths,
3345 status_cb, status_arg, repo);
3349 err = insert_tree_entry(new_te, &paths);
3355 err = match_deleted_or_modified_ct(&ct, te,
3356 path_base_tree, commitable_paths);
3358 /* NB: Deleted entries get dropped here. */
3359 if (ct->status == GOT_STATUS_MODIFY ||
3360 ct->staged_status == GOT_STATUS_MODIFY) {
3361 err = alloc_modified_blob_tree_entry(
3365 err = insert_tree_entry(new_te, &paths);
3369 err = report_ct_status(ct, status_cb,
3374 /* Entry is unchanged; just copy it. */
3375 err = got_object_tree_entry_dup(&new_te, te);
3378 err = insert_tree_entry(new_te, &paths);
3385 /* Write new list of entries; deleted entries have been dropped. */
3386 TAILQ_FOREACH(pe, &paths, entry) {
3387 struct got_tree_entry *te = pe->data;
3388 new_tree_entries.nentries++;
3389 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3391 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3393 got_object_tree_entries_close(&new_tree_entries);
3394 got_pathlist_free(&paths);
3398 static const struct got_error *
3399 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3400 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3402 const struct got_error *err = NULL;
3403 struct got_pathlist_entry *pe;
3405 TAILQ_FOREACH(pe, commitable_paths, entry) {
3406 struct got_fileindex_entry *ie;
3407 struct got_commitable *ct = pe->data;
3409 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3411 if (ct->status == GOT_STATUS_DELETE ||
3412 ct->staged_status == GOT_STATUS_DELETE) {
3413 got_fileindex_entry_remove(fileindex, ie);
3414 got_fileindex_entry_free(ie);
3415 } else if (ct->staged_status == GOT_STATUS_ADD ||
3416 ct->staged_status == GOT_STATUS_MODIFY) {
3417 got_fileindex_entry_stage_set(ie,
3418 GOT_FILEIDX_STAGE_NONE);
3419 err = got_fileindex_entry_update(ie,
3420 ct->ondisk_path, ct->staged_blob_id->sha1,
3421 new_base_commit_id->sha1, 1);
3423 err = got_fileindex_entry_update(ie,
3424 ct->ondisk_path, ct->blob_id->sha1,
3425 new_base_commit_id->sha1, 1);
3427 err = got_fileindex_entry_alloc(&ie,
3428 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3429 new_base_commit_id->sha1);
3432 err = got_fileindex_entry_add(fileindex, ie);
3441 static const struct got_error *
3442 check_out_of_date(const char *in_repo_path, unsigned char status,
3443 unsigned char staged_status, struct got_object_id *base_blob_id,
3444 struct got_object_id *base_commit_id,
3445 struct got_object_id *head_commit_id, struct got_repository *repo,
3448 const struct got_error *err = NULL;
3449 struct got_object_id *id = NULL;
3451 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3452 /* Trivial case: base commit == head commit */
3453 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3456 * Ensure file content which local changes were based
3457 * on matches file content in the branch head.
3459 err = got_object_id_by_path(&id, repo, head_commit_id,
3462 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3463 err = got_error(ood_errcode);
3465 } else if (got_object_id_cmp(id, base_blob_id) != 0)
3466 err = got_error(ood_errcode);
3468 /* Require that added files don't exist in the branch head. */
3469 err = got_object_id_by_path(&id, repo, head_commit_id,
3471 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3473 err = id ? got_error(ood_errcode) : NULL;
3480 const struct got_error *
3481 commit_worktree(struct got_object_id **new_commit_id,
3482 struct got_pathlist_head *commitable_paths,
3483 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3484 const char *author, const char *committer,
3485 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3486 got_worktree_status_cb status_cb, void *status_arg,
3487 struct got_repository *repo)
3489 const struct got_error *err = NULL, *unlockerr = NULL;
3490 struct got_pathlist_entry *pe;
3491 const char *head_ref_name = NULL;
3492 struct got_commit_object *head_commit = NULL;
3493 struct got_reference *head_ref2 = NULL;
3494 struct got_object_id *head_commit_id2 = NULL;
3495 struct got_tree_object *head_tree = NULL;
3496 struct got_object_id *new_tree_id = NULL;
3497 struct got_object_id_queue parent_ids;
3498 struct got_object_qid *pid = NULL;
3499 char *logmsg = NULL;
3501 *new_commit_id = NULL;
3503 SIMPLEQ_INIT(&parent_ids);
3505 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3509 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3513 if (commit_msg_cb != NULL) {
3514 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3519 if (logmsg == NULL || strlen(logmsg) == 0) {
3520 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3524 /* Create blobs from added and modified files and record their IDs. */
3525 TAILQ_FOREACH(pe, commitable_paths, entry) {
3526 struct got_commitable *ct = pe->data;
3529 /* Blobs for staged files already exist. */
3530 if (ct->staged_status == GOT_STATUS_ADD ||
3531 ct->staged_status == GOT_STATUS_MODIFY)
3534 if (ct->status != GOT_STATUS_ADD &&
3535 ct->status != GOT_STATUS_MODIFY)
3538 if (asprintf(&ondisk_path, "%s/%s",
3539 worktree->root_path, pe->path) == -1) {
3540 err = got_error_from_errno("asprintf");
3543 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3549 /* Recursively write new tree objects. */
3550 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3551 status_cb, status_arg, repo);
3555 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3558 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3559 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3560 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3561 got_object_qid_free(pid);
3567 /* Check if a concurrent commit to our branch has occurred. */
3568 head_ref_name = got_worktree_get_head_ref_name(worktree);
3569 if (head_ref_name == NULL) {
3570 err = got_error_from_errno("got_worktree_get_head_ref_name");
3573 /* Lock the reference here to prevent concurrent modification. */
3574 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3577 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3580 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3581 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3584 /* Update branch head in repository. */
3585 err = got_ref_change_ref(head_ref2, *new_commit_id);
3588 err = got_ref_write(head_ref2, repo);
3592 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3596 err = ref_base_commit(worktree, repo);
3601 got_object_tree_close(head_tree);
3603 got_object_commit_close(head_commit);
3604 free(head_commit_id2);
3606 unlockerr = got_ref_unlock(head_ref2);
3607 if (unlockerr && err == NULL)
3609 got_ref_close(head_ref2);
3614 static const struct got_error *
3615 check_path_is_commitable(const char *path,
3616 struct got_pathlist_head *commitable_paths)
3618 struct got_pathlist_entry *cpe = NULL;
3619 size_t path_len = strlen(path);
3621 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3622 struct got_commitable *ct = cpe->data;
3623 const char *ct_path = ct->path;
3625 while (ct_path[0] == '/')
3628 if (strcmp(path, ct_path) == 0 ||
3629 got_path_is_child(ct_path, path, path_len))
3634 return got_error_path(path, GOT_ERR_BAD_PATH);
3639 static const struct got_error *
3640 check_staged_file(void *arg, struct got_fileindex_entry *ie)
3642 int *have_staged_files = arg;
3644 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
3645 *have_staged_files = 1;
3646 return got_error(GOT_ERR_CANCELLED);
3652 static const struct got_error *
3653 check_non_staged_files(struct got_fileindex *fileindex,
3654 struct got_pathlist_head *paths)
3656 struct got_pathlist_entry *pe;
3657 struct got_fileindex_entry *ie;
3659 TAILQ_FOREACH(pe, paths, entry) {
3660 if (pe->path[0] == '\0')
3662 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3664 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
3665 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
3666 return got_error_path(pe->path,
3667 GOT_ERR_FILE_NOT_STAGED);
3673 const struct got_error *
3674 got_worktree_commit(struct got_object_id **new_commit_id,
3675 struct got_worktree *worktree, struct got_pathlist_head *paths,
3676 const char *author, const char *committer,
3677 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3678 got_worktree_status_cb status_cb, void *status_arg,
3679 struct got_repository *repo)
3681 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3682 struct got_fileindex *fileindex = NULL;
3683 char *fileindex_path = NULL;
3684 struct got_pathlist_head commitable_paths;
3685 struct collect_commitables_arg cc_arg;
3686 struct got_pathlist_entry *pe;
3687 struct got_reference *head_ref = NULL;
3688 struct got_object_id *head_commit_id = NULL;
3689 int have_staged_files = 0;
3691 *new_commit_id = NULL;
3693 TAILQ_INIT(&commitable_paths);
3695 err = lock_worktree(worktree, LOCK_EX);
3699 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3703 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3707 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3711 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
3712 &have_staged_files);
3713 if (err && err->code != GOT_ERR_CANCELLED)
3715 if (have_staged_files) {
3716 err = check_non_staged_files(fileindex, paths);
3721 cc_arg.commitable_paths = &commitable_paths;
3722 cc_arg.worktree = worktree;
3724 cc_arg.have_staged_files = have_staged_files;
3725 TAILQ_FOREACH(pe, paths, entry) {
3726 err = worktree_status(worktree, pe->path, fileindex, repo,
3727 collect_commitables, &cc_arg, NULL, NULL);
3732 if (TAILQ_EMPTY(&commitable_paths)) {
3733 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3737 TAILQ_FOREACH(pe, paths, entry) {
3738 err = check_path_is_commitable(pe->path, &commitable_paths);
3743 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3744 struct got_commitable *ct = pe->data;
3745 const char *ct_path = ct->in_repo_path;
3747 while (ct_path[0] == '/')
3749 err = check_out_of_date(ct_path, ct->status,
3750 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
3751 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
3757 err = commit_worktree(new_commit_id, &commitable_paths,
3758 head_commit_id, worktree, author, committer,
3759 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3763 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3765 sync_err = sync_fileindex(fileindex, fileindex_path);
3766 if (sync_err && err == NULL)
3770 got_fileindex_free(fileindex);
3771 free(fileindex_path);
3772 unlockerr = lock_worktree(worktree, LOCK_SH);
3773 if (unlockerr && err == NULL)
3775 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3776 struct got_commitable *ct = pe->data;
3777 free_commitable(ct);
3779 got_pathlist_free(&commitable_paths);
3784 got_commitable_get_path(struct got_commitable *ct)
3790 got_commitable_get_status(struct got_commitable *ct)
3795 struct check_rebase_ok_arg {
3796 struct got_worktree *worktree;
3797 struct got_repository *repo;
3800 static const struct got_error *
3801 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3803 const struct got_error *err = NULL;
3804 struct check_rebase_ok_arg *a = arg;
3805 unsigned char status;
3809 /* Reject rebase of a work tree with mixed base commits. */
3810 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3811 SHA1_DIGEST_LENGTH))
3812 return got_error(GOT_ERR_MIXED_COMMITS);
3814 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3816 return got_error_from_errno("asprintf");
3818 /* Reject rebase of a work tree with modified or staged files. */
3819 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3824 if (status != GOT_STATUS_NO_CHANGE)
3825 return got_error(GOT_ERR_MODIFIED);
3826 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
3827 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
3832 const struct got_error *
3833 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3834 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3835 struct got_worktree *worktree, struct got_reference *branch,
3836 struct got_repository *repo)
3838 const struct got_error *err = NULL;
3839 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3840 char *branch_ref_name = NULL;
3841 char *fileindex_path = NULL;
3842 struct check_rebase_ok_arg ok_arg;
3843 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3845 *new_base_branch_ref = NULL;
3849 err = lock_worktree(worktree, LOCK_EX);
3853 err = open_fileindex(fileindex, &fileindex_path, worktree);
3857 ok_arg.worktree = worktree;
3859 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3864 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3868 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3872 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3876 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3881 err = got_ref_alloc_symref(new_base_branch_ref,
3882 new_base_branch_ref_name, wt_branch);
3885 err = got_ref_write(*new_base_branch_ref, repo);
3889 /* TODO Lock original branch's ref while rebasing? */
3891 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3895 err = got_ref_write(branch_ref, repo);
3899 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3900 worktree->base_commit_id);
3903 err = got_ref_write(*tmp_branch, repo);
3907 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3911 free(fileindex_path);
3912 free(tmp_branch_name);
3913 free(new_base_branch_ref_name);
3914 free(branch_ref_name);
3916 got_ref_close(branch_ref);
3918 got_ref_close(wt_branch);
3920 if (*new_base_branch_ref) {
3921 got_ref_close(*new_base_branch_ref);
3922 *new_base_branch_ref = NULL;
3925 got_ref_close(*tmp_branch);
3929 got_fileindex_free(*fileindex);
3932 lock_worktree(worktree, LOCK_SH);
3937 const struct got_error *
3938 got_worktree_rebase_continue(struct got_object_id **commit_id,
3939 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3940 struct got_reference **branch, struct got_fileindex **fileindex,
3941 struct got_worktree *worktree, struct got_repository *repo)
3943 const struct got_error *err;
3944 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3945 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3946 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3947 char *fileindex_path = NULL;
3948 int have_staged_files = 0;
3951 *new_base_branch = NULL;
3956 err = lock_worktree(worktree, LOCK_EX);
3960 err = open_fileindex(fileindex, &fileindex_path, worktree);
3964 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
3965 &have_staged_files);
3966 if (err && err->code != GOT_ERR_CANCELLED)
3968 if (have_staged_files) {
3969 err = got_error(GOT_ERR_STAGED_PATHS);
3973 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3977 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3981 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3985 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3989 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3993 err = got_ref_open(branch, repo,
3994 got_ref_get_symref_target(branch_ref), 0);
3998 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4002 err = got_ref_resolve(commit_id, repo, commit_ref);
4006 err = got_ref_open(new_base_branch, repo,
4007 new_base_branch_ref_name, 0);
4011 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4015 free(commit_ref_name);
4016 free(branch_ref_name);
4017 free(fileindex_path);
4019 got_ref_close(commit_ref);
4021 got_ref_close(branch_ref);
4026 got_ref_close(*tmp_branch);
4029 if (*new_base_branch) {
4030 got_ref_close(*new_base_branch);
4031 *new_base_branch = NULL;
4034 got_ref_close(*branch);
4038 got_fileindex_free(*fileindex);
4041 lock_worktree(worktree, LOCK_SH);
4046 const struct got_error *
4047 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4049 const struct got_error *err;
4050 char *tmp_branch_name = NULL;
4052 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4056 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4057 free(tmp_branch_name);
4061 static const struct got_error *
4062 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4063 char **logmsg, void *arg)
4069 static const struct got_error *
4070 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4071 const char *path, struct got_object_id *blob_id,
4072 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4077 struct collect_merged_paths_arg {
4078 got_worktree_checkout_cb progress_cb;
4080 struct got_pathlist_head *merged_paths;
4083 static const struct got_error *
4084 collect_merged_paths(void *arg, unsigned char status, const char *path)
4086 const struct got_error *err;
4087 struct collect_merged_paths_arg *a = arg;
4089 struct got_pathlist_entry *new;
4091 err = (*a->progress_cb)(a->progress_arg, status, path);
4095 if (status != GOT_STATUS_MERGE &&
4096 status != GOT_STATUS_ADD &&
4097 status != GOT_STATUS_DELETE &&
4098 status != GOT_STATUS_CONFLICT)
4103 return got_error_from_errno("strdup");
4105 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4106 if (err || new == NULL)
4112 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4114 struct got_pathlist_entry *pe;
4116 TAILQ_FOREACH(pe, merged_paths, entry)
4117 free((char *)pe->path);
4119 got_pathlist_free(merged_paths);
4122 static const struct got_error *
4123 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4124 struct got_repository *repo)
4126 const struct got_error *err;
4127 struct got_reference *commit_ref = NULL;
4129 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4131 if (err->code != GOT_ERR_NOT_REF)
4133 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4136 err = got_ref_write(commit_ref, repo);
4140 struct got_object_id *stored_id;
4143 err = got_ref_resolve(&stored_id, repo, commit_ref);
4146 cmp = got_object_id_cmp(commit_id, stored_id);
4149 err = got_error(GOT_ERR_REBASE_COMMITID);
4155 got_ref_close(commit_ref);
4159 static const struct got_error *
4160 rebase_merge_files(struct got_pathlist_head *merged_paths,
4161 const char *commit_ref_name, struct got_worktree *worktree,
4162 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4163 struct got_object_id *commit_id, struct got_repository *repo,
4164 got_worktree_checkout_cb progress_cb, void *progress_arg,
4165 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4167 const struct got_error *err;
4168 struct got_reference *commit_ref = NULL;
4169 struct collect_merged_paths_arg cmp_arg;
4170 char *fileindex_path;
4172 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4174 err = get_fileindex_path(&fileindex_path, worktree);
4178 cmp_arg.progress_cb = progress_cb;
4179 cmp_arg.progress_arg = progress_arg;
4180 cmp_arg.merged_paths = merged_paths;
4181 err = merge_files(worktree, fileindex, fileindex_path,
4182 parent_commit_id, commit_id, repo, collect_merged_paths,
4183 &cmp_arg, cancel_cb, cancel_arg);
4185 got_ref_close(commit_ref);
4189 const struct got_error *
4190 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4191 struct got_worktree *worktree, struct got_fileindex *fileindex,
4192 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4193 struct got_repository *repo,
4194 got_worktree_checkout_cb progress_cb, void *progress_arg,
4195 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4197 const struct got_error *err;
4198 char *commit_ref_name;
4200 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4204 err = store_commit_id(commit_ref_name, commit_id, repo);
4208 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4209 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4210 progress_arg, cancel_cb, cancel_arg);
4212 free(commit_ref_name);
4216 const struct got_error *
4217 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4218 struct got_worktree *worktree, struct got_fileindex *fileindex,
4219 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4220 struct got_repository *repo,
4221 got_worktree_checkout_cb progress_cb, void *progress_arg,
4222 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4224 const struct got_error *err;
4225 char *commit_ref_name;
4227 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4231 err = store_commit_id(commit_ref_name, commit_id, repo);
4235 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4236 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4237 progress_arg, cancel_cb, cancel_arg);
4239 free(commit_ref_name);
4243 static const struct got_error *
4244 rebase_commit(struct got_object_id **new_commit_id,
4245 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4246 struct got_worktree *worktree, struct got_fileindex *fileindex,
4247 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4248 const char *new_logmsg, struct got_repository *repo)
4250 const struct got_error *err, *sync_err;
4251 struct got_pathlist_head commitable_paths;
4252 struct collect_commitables_arg cc_arg;
4253 char *fileindex_path = NULL;
4254 struct got_reference *head_ref = NULL;
4255 struct got_object_id *head_commit_id = NULL;
4256 char *logmsg = NULL;
4258 TAILQ_INIT(&commitable_paths);
4259 *new_commit_id = NULL;
4261 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4263 err = get_fileindex_path(&fileindex_path, worktree);
4267 cc_arg.commitable_paths = &commitable_paths;
4268 cc_arg.worktree = worktree;
4270 cc_arg.have_staged_files = 0;
4272 * If possible get the status of individual files directly to
4273 * avoid crawling the entire work tree once per rebased commit.
4274 * TODO: Ideally, merged_paths would contain a list of commitables
4275 * we could use so we could skip worktree_status() entirely.
4278 struct got_pathlist_entry *pe;
4279 if (TAILQ_EMPTY(merged_paths)) {
4280 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4283 TAILQ_FOREACH(pe, merged_paths, entry) {
4284 err = worktree_status(worktree, pe->path, fileindex,
4285 repo, collect_commitables, &cc_arg, NULL, NULL);
4290 err = worktree_status(worktree, "", fileindex, repo,
4291 collect_commitables, &cc_arg, NULL, NULL);
4296 if (TAILQ_EMPTY(&commitable_paths)) {
4297 /* No-op change; commit will be elided. */
4298 err = got_ref_delete(commit_ref, repo);
4301 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4305 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4309 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4314 logmsg = strdup(new_logmsg);
4316 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4318 return got_error_from_errno("strdup");
4320 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4321 worktree, got_object_commit_get_author(orig_commit),
4322 got_object_commit_get_committer(orig_commit),
4323 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4327 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4331 err = got_ref_delete(commit_ref, repo);
4335 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4337 sync_err = sync_fileindex(fileindex, fileindex_path);
4338 if (sync_err && err == NULL)
4341 free(fileindex_path);
4342 free(head_commit_id);
4344 got_ref_close(head_ref);
4346 free(*new_commit_id);
4347 *new_commit_id = NULL;
4352 const struct got_error *
4353 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4354 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4355 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4356 struct got_commit_object *orig_commit,
4357 struct got_object_id *orig_commit_id, struct got_repository *repo)
4359 const struct got_error *err;
4360 char *commit_ref_name;
4361 struct got_reference *commit_ref = NULL;
4362 struct got_object_id *commit_id = NULL;
4364 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4368 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4371 err = got_ref_resolve(&commit_id, repo, commit_ref);
4374 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4375 err = got_error(GOT_ERR_REBASE_COMMITID);
4379 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4380 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4383 got_ref_close(commit_ref);
4384 free(commit_ref_name);
4389 const struct got_error *
4390 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4391 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4392 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4393 struct got_commit_object *orig_commit,
4394 struct got_object_id *orig_commit_id, const char *new_logmsg,
4395 struct got_repository *repo)
4397 const struct got_error *err;
4398 char *commit_ref_name;
4399 struct got_reference *commit_ref = NULL;
4400 struct got_object_id *commit_id = NULL;
4402 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4406 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4409 err = got_ref_resolve(&commit_id, repo, commit_ref);
4412 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4413 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4417 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4418 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4421 got_ref_close(commit_ref);
4422 free(commit_ref_name);
4427 const struct got_error *
4428 got_worktree_rebase_postpone(struct got_worktree *worktree,
4429 struct got_fileindex *fileindex)
4432 got_fileindex_free(fileindex);
4433 return lock_worktree(worktree, LOCK_SH);
4436 static const struct got_error *
4437 delete_ref(const char *name, struct got_repository *repo)
4439 const struct got_error *err;
4440 struct got_reference *ref;
4442 err = got_ref_open(&ref, repo, name, 0);
4444 if (err->code == GOT_ERR_NOT_REF)
4449 err = got_ref_delete(ref, repo);
4454 static const struct got_error *
4455 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4457 const struct got_error *err;
4458 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4459 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4461 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4464 err = delete_ref(tmp_branch_name, repo);
4468 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4471 err = delete_ref(new_base_branch_ref_name, repo);
4475 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4478 err = delete_ref(branch_ref_name, repo);
4482 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4485 err = delete_ref(commit_ref_name, repo);
4490 free(tmp_branch_name);
4491 free(new_base_branch_ref_name);
4492 free(branch_ref_name);
4493 free(commit_ref_name);
4497 const struct got_error *
4498 got_worktree_rebase_complete(struct got_worktree *worktree,
4499 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4500 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4501 struct got_repository *repo)
4503 const struct got_error *err, *unlockerr;
4504 struct got_object_id *new_head_commit_id = NULL;
4506 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4510 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4514 err = got_ref_write(rebased_branch, repo);
4518 err = got_worktree_set_head_ref(worktree, rebased_branch);
4522 err = delete_rebase_refs(worktree, repo);
4525 got_fileindex_free(fileindex);
4526 free(new_head_commit_id);
4527 unlockerr = lock_worktree(worktree, LOCK_SH);
4528 if (unlockerr && err == NULL)
4533 struct collect_revertible_paths_arg {
4534 struct got_pathlist_head *revertible_paths;
4535 struct got_worktree *worktree;
4538 static const struct got_error *
4539 collect_revertible_paths(void *arg, unsigned char status,
4540 unsigned char staged_status, const char *relpath,
4541 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4542 struct got_object_id *commit_id)
4544 struct collect_revertible_paths_arg *a = arg;
4545 const struct got_error *err = NULL;
4546 struct got_pathlist_entry *new = NULL;
4549 if (status != GOT_STATUS_ADD &&
4550 status != GOT_STATUS_DELETE &&
4551 status != GOT_STATUS_MODIFY &&
4552 status != GOT_STATUS_CONFLICT &&
4553 status != GOT_STATUS_MISSING)
4556 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4557 return got_error_from_errno("asprintf");
4559 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4560 if (err || new == NULL)
4565 const struct got_error *
4566 got_worktree_rebase_abort(struct got_worktree *worktree,
4567 struct got_fileindex *fileindex, struct got_repository *repo,
4568 struct got_reference *new_base_branch,
4569 got_worktree_checkout_cb progress_cb, void *progress_arg)
4571 const struct got_error *err, *unlockerr, *sync_err;
4572 struct got_reference *resolved = NULL;
4573 struct got_object_id *commit_id = NULL;
4574 char *fileindex_path = NULL;
4575 struct got_pathlist_head revertible_paths;
4576 struct got_pathlist_entry *pe;
4577 struct collect_revertible_paths_arg crp_arg;
4578 struct got_object_id *tree_id = NULL;
4580 TAILQ_INIT(&revertible_paths);
4582 err = lock_worktree(worktree, LOCK_EX);
4586 err = got_ref_open(&resolved, repo,
4587 got_ref_get_symref_target(new_base_branch), 0);
4591 err = got_worktree_set_head_ref(worktree, resolved);
4596 * XXX commits to the base branch could have happened while
4597 * we were busy rebasing; should we store the original commit ID
4598 * when rebase begins and read it back here?
4600 err = got_ref_resolve(&commit_id, repo, resolved);
4604 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4608 err = got_object_id_by_path(&tree_id, repo,
4609 worktree->base_commit_id, worktree->path_prefix);
4613 err = delete_rebase_refs(worktree, repo);
4617 err = get_fileindex_path(&fileindex_path, worktree);
4621 crp_arg.revertible_paths = &revertible_paths;
4622 crp_arg.worktree = worktree;
4623 err = worktree_status(worktree, "", fileindex, repo,
4624 collect_revertible_paths, &crp_arg, NULL, NULL);
4628 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4629 err = revert_file(worktree, fileindex, pe->path,
4630 progress_cb, progress_arg, repo);
4635 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4636 repo, progress_cb, progress_arg, NULL, NULL);
4638 sync_err = sync_fileindex(fileindex, fileindex_path);
4639 if (sync_err && err == NULL)
4642 got_ref_close(resolved);
4646 got_fileindex_free(fileindex);
4647 free(fileindex_path);
4648 TAILQ_FOREACH(pe, &revertible_paths, entry)
4649 free((char *)pe->path);
4650 got_pathlist_free(&revertible_paths);
4652 unlockerr = lock_worktree(worktree, LOCK_SH);
4653 if (unlockerr && err == NULL)
4658 const struct got_error *
4659 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4660 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4661 struct got_fileindex **fileindex, struct got_worktree *worktree,
4662 struct got_repository *repo)
4664 const struct got_error *err = NULL;
4665 char *tmp_branch_name = NULL;
4666 char *branch_ref_name = NULL;
4667 char *base_commit_ref_name = NULL;
4668 char *fileindex_path = NULL;
4669 struct check_rebase_ok_arg ok_arg;
4670 struct got_reference *wt_branch = NULL;
4671 struct got_reference *base_commit_ref = NULL;
4675 *base_commit_id = NULL;
4678 err = lock_worktree(worktree, LOCK_EX);
4682 err = open_fileindex(fileindex, &fileindex_path, worktree);
4686 ok_arg.worktree = worktree;
4688 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4693 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4697 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4701 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4706 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4711 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4715 err = got_ref_write(*branch_ref, repo);
4719 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4720 worktree->base_commit_id);
4723 err = got_ref_write(base_commit_ref, repo);
4726 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4727 if (*base_commit_id == NULL) {
4728 err = got_error_from_errno("got_object_id_dup");
4732 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4733 worktree->base_commit_id);
4736 err = got_ref_write(*tmp_branch, repo);
4740 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4744 free(fileindex_path);
4745 free(tmp_branch_name);
4746 free(branch_ref_name);
4747 free(base_commit_ref_name);
4749 got_ref_close(wt_branch);
4752 got_ref_close(*branch_ref);
4756 got_ref_close(*tmp_branch);
4759 free(*base_commit_id);
4761 got_fileindex_free(*fileindex);
4764 lock_worktree(worktree, LOCK_SH);
4769 const struct got_error *
4770 got_worktree_histedit_postpone(struct got_worktree *worktree,
4771 struct got_fileindex *fileindex)
4774 got_fileindex_free(fileindex);
4775 return lock_worktree(worktree, LOCK_SH);
4778 const struct got_error *
4779 got_worktree_histedit_in_progress(int *in_progress,
4780 struct got_worktree *worktree)
4782 const struct got_error *err;
4783 char *tmp_branch_name = NULL;
4785 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4789 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4790 free(tmp_branch_name);
4794 const struct got_error *
4795 got_worktree_histedit_continue(struct got_object_id **commit_id,
4796 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4797 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4798 struct got_worktree *worktree, struct got_repository *repo)
4800 const struct got_error *err;
4801 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4802 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4803 struct got_reference *commit_ref = NULL;
4804 struct got_reference *base_commit_ref = NULL;
4805 char *fileindex_path = NULL;
4806 int have_staged_files = 0;
4810 *base_commit_id = NULL;
4813 err = lock_worktree(worktree, LOCK_EX);
4817 err = open_fileindex(fileindex, &fileindex_path, worktree);
4821 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4822 &have_staged_files);
4823 if (err && err->code != GOT_ERR_CANCELLED)
4825 if (have_staged_files) {
4826 err = got_error(GOT_ERR_STAGED_PATHS);
4830 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4834 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4838 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4842 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4847 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4851 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4854 err = got_ref_resolve(commit_id, repo, commit_ref);
4858 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4861 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4865 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4869 free(commit_ref_name);
4870 free(branch_ref_name);
4871 free(fileindex_path);
4873 got_ref_close(commit_ref);
4874 if (base_commit_ref)
4875 got_ref_close(base_commit_ref);
4879 free(*base_commit_id);
4880 *base_commit_id = NULL;
4882 got_ref_close(*tmp_branch);
4886 got_fileindex_free(*fileindex);
4889 lock_worktree(worktree, LOCK_EX);
4894 static const struct got_error *
4895 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4897 const struct got_error *err;
4898 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4899 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4901 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4904 err = delete_ref(tmp_branch_name, repo);
4908 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4912 err = delete_ref(base_commit_ref_name, repo);
4916 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4919 err = delete_ref(branch_ref_name, repo);
4923 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4926 err = delete_ref(commit_ref_name, repo);
4930 free(tmp_branch_name);
4931 free(base_commit_ref_name);
4932 free(branch_ref_name);
4933 free(commit_ref_name);
4937 const struct got_error *
4938 got_worktree_histedit_abort(struct got_worktree *worktree,
4939 struct got_fileindex *fileindex, struct got_repository *repo,
4940 struct got_reference *branch, struct got_object_id *base_commit_id,
4941 got_worktree_checkout_cb progress_cb, void *progress_arg)
4943 const struct got_error *err, *unlockerr, *sync_err;
4944 struct got_reference *resolved = NULL;
4945 char *fileindex_path = NULL;
4946 struct got_pathlist_head revertible_paths;
4947 struct got_pathlist_entry *pe;
4948 struct collect_revertible_paths_arg crp_arg;
4949 struct got_object_id *tree_id = NULL;
4951 TAILQ_INIT(&revertible_paths);
4953 err = lock_worktree(worktree, LOCK_EX);
4957 err = got_ref_open(&resolved, repo,
4958 got_ref_get_symref_target(branch), 0);
4962 err = got_worktree_set_head_ref(worktree, resolved);
4966 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4970 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4971 worktree->path_prefix);
4975 err = delete_histedit_refs(worktree, repo);
4979 err = get_fileindex_path(&fileindex_path, worktree);
4983 crp_arg.revertible_paths = &revertible_paths;
4984 crp_arg.worktree = worktree;
4985 err = worktree_status(worktree, "", fileindex, repo,
4986 collect_revertible_paths, &crp_arg, NULL, NULL);
4990 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4991 err = revert_file(worktree, fileindex, pe->path,
4992 progress_cb, progress_arg, repo);
4997 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4998 repo, progress_cb, progress_arg, NULL, NULL);
5000 sync_err = sync_fileindex(fileindex, fileindex_path);
5001 if (sync_err && err == NULL)
5004 got_ref_close(resolved);
5006 free(fileindex_path);
5007 TAILQ_FOREACH(pe, &revertible_paths, entry)
5008 free((char *)pe->path);
5009 got_pathlist_free(&revertible_paths);
5011 unlockerr = lock_worktree(worktree, LOCK_SH);
5012 if (unlockerr && err == NULL)
5017 const struct got_error *
5018 got_worktree_histedit_complete(struct got_worktree *worktree,
5019 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5020 struct got_reference *edited_branch, struct got_repository *repo)
5022 const struct got_error *err, *unlockerr;
5023 struct got_object_id *new_head_commit_id = NULL;
5024 struct got_reference *resolved = NULL;
5026 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5030 err = got_ref_open(&resolved, repo,
5031 got_ref_get_symref_target(edited_branch), 0);
5035 err = got_ref_change_ref(resolved, new_head_commit_id);
5039 err = got_ref_write(resolved, repo);
5043 err = got_worktree_set_head_ref(worktree, resolved);
5047 err = delete_histedit_refs(worktree, repo);
5050 got_fileindex_free(fileindex);
5051 free(new_head_commit_id);
5052 unlockerr = lock_worktree(worktree, LOCK_SH);
5053 if (unlockerr && err == NULL)
5058 const struct got_error *
5059 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5060 struct got_object_id *commit_id, struct got_repository *repo)
5062 const struct got_error *err;
5063 char *commit_ref_name;
5065 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5069 err = store_commit_id(commit_ref_name, commit_id, repo);
5073 err = delete_ref(commit_ref_name, repo);
5075 free(commit_ref_name);
5079 struct check_stage_ok_arg {
5080 struct got_object_id *head_commit_id;
5081 struct got_worktree *worktree;
5082 struct got_fileindex *fileindex;
5083 struct got_repository *repo;
5087 const struct got_error *
5088 check_stage_ok(void *arg, unsigned char status,
5089 unsigned char staged_status, const char *relpath,
5090 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5091 struct got_object_id *commit_id)
5093 struct check_stage_ok_arg *a = arg;
5094 const struct got_error *err = NULL;
5095 struct got_fileindex_entry *ie;
5096 struct got_object_id base_commit_id;
5097 struct got_object_id *base_commit_idp = NULL;
5098 char *in_repo_path = NULL, *p;
5100 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5102 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5104 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5105 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5107 return got_error_from_errno("asprintf");
5109 if (got_fileindex_entry_has_commit(ie)) {
5110 memcpy(base_commit_id.sha1, ie->commit_sha1,
5111 SHA1_DIGEST_LENGTH);
5112 base_commit_idp = &base_commit_id;
5115 if (status == GOT_STATUS_NO_CHANGE) {
5116 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5118 } else if (status == GOT_STATUS_CONFLICT) {
5119 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5121 } else if (status != GOT_STATUS_ADD &&
5122 status != GOT_STATUS_MODIFY &&
5123 status != GOT_STATUS_DELETE) {
5124 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5128 a->have_changes = 1;
5133 err = check_out_of_date(p, status, staged_status,
5134 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5135 GOT_ERR_STAGE_OUT_OF_DATE);
5141 static const struct got_error *
5142 copy_one_line(FILE *infile, FILE *outfile)
5144 const struct got_error *err = NULL;
5146 size_t linesize = 0, n;
5149 linelen = getline(&line, &linesize, infile);
5150 if (linelen == -1) {
5151 if (ferror(infile)) {
5152 err = got_error_from_errno("getline");
5157 n = fwrite(line, 1, linelen, outfile);
5159 err = got_ferror(outfile, GOT_ERR_IO);
5165 static const struct got_error *
5166 skip_one_line(FILE *f)
5169 size_t linesize = 0;
5172 linelen = getline(&line, &linesize, f);
5174 if (linelen == -1 && ferror(f))
5175 return got_error_from_errno("getline");
5179 static const struct got_error *
5180 apply_or_reject_change(int *choice, struct got_diff_change *change,
5181 struct got_diff_state *ds, struct got_diff_args *args, int diff_flags,
5182 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
5183 FILE *outfile, got_worktree_patch_cb patch_cb, void *patch_arg)
5185 const struct got_error *err = NULL;
5186 int start_old = change->cv.a;
5187 int end_old = change->cv.b;
5188 int start_new = change->cv.c;
5189 int end_new = change->cv.d;
5193 *choice = GOT_PATCH_CHOICE_NONE;
5195 hunkfile = got_opentemp();
5196 if (hunkfile == NULL)
5197 return got_error_from_errno("got_opentemp");
5202 /* XXX TODO needs error checking */
5203 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
5205 if (fseek(f1, pos1, SEEK_SET) == -1) {
5206 err = got_ferror(f1, GOT_ERR_IO);
5209 if (fseek(f2, pos2, SEEK_SET) == -1) {
5210 err = got_ferror(f1, GOT_ERR_IO);
5213 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
5214 err = got_ferror(hunkfile, GOT_ERR_IO);
5218 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
5224 case GOT_PATCH_CHOICE_YES:
5225 /* Copy old file's lines leading up to patch. */
5226 while (!feof(f1) && *line_cur1 < start_old) {
5227 err = copy_one_line(f1, outfile);
5232 /* Skip new file's lines leading up to patch. */
5233 while (!feof(f2) && *line_cur2 < start_new) {
5234 err = skip_one_line(f2);
5239 /* Copy patched lines. */
5240 while (!feof(f2) && *line_cur2 <= end_new) {
5241 err = copy_one_line(f2, outfile);
5246 /* Skip over old file's replaced lines. */
5247 while (!feof(f1) && *line_cur1 <= end_new) {
5248 err = skip_one_line(f1);
5253 /* Copy old file's lines after patch. */
5254 while (!feof(f1) && *line_cur1 <= end_old) {
5255 err = skip_one_line(f1);
5261 case GOT_PATCH_CHOICE_NO:
5262 /* Copy old file's lines. */
5263 while (!feof(f1) && *line_cur1 <= end_old) {
5264 err = copy_one_line(f1, outfile);
5269 /* Skip over new file's lines. */
5270 while (!feof(f2) && *line_cur2 <= end_new) {
5271 err = skip_one_line(f2);
5277 case GOT_PATCH_CHOICE_QUIT:
5278 /* Copy old file's lines until EOF. */
5280 err = copy_one_line(f1, outfile);
5287 err = got_error(GOT_ERR_PATCH_CHOICE);
5291 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
5292 err = got_error_from_errno("fclose");
5296 static const struct got_error *
5297 create_staged_content(char **path_outfile, struct got_object_id *blob_id,
5298 const char *path2, const char *relpath, struct got_repository *repo,
5299 got_worktree_patch_cb patch_cb, void *patch_arg)
5301 const struct got_error *err;
5302 struct got_blob_object *blob = NULL;
5303 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5304 char *path1 = NULL, *id_str = NULL;
5305 struct stat sb1, sb2;
5306 struct got_diff_changes *changes = NULL;
5307 struct got_diff_state *ds = NULL;
5308 struct got_diff_args *args = NULL;
5309 struct got_diff_change *change;
5310 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
5312 *path_outfile = NULL;
5314 err = got_object_id_str(&id_str, blob_id);
5318 f2 = fopen(path2, "r");
5320 err = got_error_from_errno2("fopen", path2);
5324 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5328 err = got_opentemp_named(&path1, &f1, "got-stage-blob");
5332 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5336 if (stat(path1, &sb1) == -1) {
5337 err = got_error_from_errno2("stat", path1);
5340 if (stat(path2, &sb2) == -1) {
5341 err = got_error_from_errno2("stat", path2);
5345 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5346 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
5350 err = got_opentemp_named(path_outfile, &outfile, "got-stage-content");
5354 if (fseek(f1, 0L, SEEK_SET) == -1)
5355 return got_ferror(f1, GOT_ERR_IO);
5356 if (fseek(f2, 0L, SEEK_SET) == -1)
5357 return got_ferror(f2, GOT_ERR_IO);
5358 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5360 err = apply_or_reject_change(&choice, change, ds, args,
5361 diff_flags, relpath, f1, f2, &line_cur1, &line_cur2,
5362 outfile, patch_cb, patch_arg);
5365 if (choice == GOT_PATCH_CHOICE_YES)
5367 else if (choice == GOT_PATCH_CHOICE_QUIT)
5373 got_object_blob_close(blob);
5374 if (f1 && fclose(f1) == EOF && err == NULL)
5375 err = got_error_from_errno2("fclose", path1);
5376 if (f2 && fclose(f2) == EOF && err == NULL)
5377 err = got_error_from_errno2("fclose", path2);
5378 if (outfile && fclose(outfile) == EOF && err == NULL)
5379 err = got_error_from_errno2("fclose", *path_outfile);
5380 if (path1 && unlink(path1) == -1 && err == NULL)
5381 err = got_error_from_errno2("unlink", path1);
5382 if (err || !have_content) {
5383 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5384 err = got_error_from_errno2("unlink", *path_outfile);
5385 free(*path_outfile);
5386 *path_outfile = NULL;
5390 got_diff_state_free(ds);
5394 got_diff_free_changes(changes);
5399 struct stage_path_arg {
5400 struct got_worktree *worktree;
5401 struct got_fileindex *fileindex;
5402 struct got_repository *repo;
5403 got_worktree_status_cb status_cb;
5405 got_worktree_patch_cb patch_cb;
5409 static const struct got_error *
5410 stage_path(void *arg, unsigned char status,
5411 unsigned char staged_status, const char *relpath,
5412 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5413 struct got_object_id *commit_id)
5415 struct stage_path_arg *a = arg;
5416 const struct got_error *err = NULL;
5417 struct got_fileindex_entry *ie;
5418 char *ondisk_path = NULL, *path_content = NULL;
5421 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5423 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5425 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5427 return got_error_from_errno("asprintf");
5430 case GOT_STATUS_ADD:
5431 case GOT_STATUS_MODIFY:
5433 if (status == GOT_STATUS_ADD) {
5434 int choice = GOT_PATCH_CHOICE_NONE;
5435 err = (*a->patch_cb)(&choice, a->patch_arg,
5436 status, ie->path, NULL);
5439 if (choice != GOT_PATCH_CHOICE_YES)
5442 err = create_staged_content(&path_content,
5443 blob_id, ondisk_path, ie->path, a->repo,
5444 a->patch_cb, a->patch_arg);
5445 if (err || path_content == NULL)
5449 err = got_object_blob_create(&staged_blob_id,
5450 path_content ? path_content : ondisk_path, a->repo);
5453 memcpy(ie->staged_blob_sha1, staged_blob_id->sha1,
5454 SHA1_DIGEST_LENGTH);
5455 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5456 stage = GOT_FILEIDX_STAGE_ADD;
5458 stage = GOT_FILEIDX_STAGE_MODIFY;
5459 got_fileindex_entry_stage_set(ie, stage);
5460 if (a->status_cb == NULL)
5462 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5463 get_staged_status(ie), relpath, blob_id,
5464 staged_blob_id, NULL);
5466 case GOT_STATUS_DELETE:
5467 if (staged_status == GOT_STATUS_DELETE)
5470 int choice = GOT_PATCH_CHOICE_NONE;
5471 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5475 if (choice != GOT_PATCH_CHOICE_YES)
5478 stage = GOT_FILEIDX_STAGE_DELETE;
5479 got_fileindex_entry_stage_set(ie, stage);
5480 if (a->status_cb == NULL)
5482 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5483 get_staged_status(ie), relpath, NULL, NULL, NULL);
5485 case GOT_STATUS_NO_CHANGE:
5486 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5488 case GOT_STATUS_CONFLICT:
5489 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5492 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5496 if (path_content && unlink(path_content) == -1 && err == NULL)
5497 err = got_error_from_errno2("unlink", path_content);
5503 const struct got_error *
5504 got_worktree_stage(struct got_worktree *worktree,
5505 struct got_pathlist_head *paths,
5506 got_worktree_status_cb status_cb, void *status_arg,
5507 got_worktree_patch_cb patch_cb, void *patch_arg,
5508 struct got_repository *repo)
5510 const struct got_error *err = NULL, *sync_err, *unlockerr;
5511 struct got_pathlist_entry *pe;
5512 struct got_fileindex *fileindex = NULL;
5513 char *fileindex_path = NULL;
5514 struct got_reference *head_ref = NULL;
5515 struct got_object_id *head_commit_id = NULL;
5516 struct check_stage_ok_arg oka;
5517 struct stage_path_arg spa;
5519 err = lock_worktree(worktree, LOCK_EX);
5523 err = got_ref_open(&head_ref, repo,
5524 got_worktree_get_head_ref_name(worktree), 0);
5527 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5530 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5534 /* Check pre-conditions before staging anything. */
5535 oka.head_commit_id = head_commit_id;
5536 oka.worktree = worktree;
5537 oka.fileindex = fileindex;
5539 oka.have_changes = 0;
5540 TAILQ_FOREACH(pe, paths, entry) {
5541 err = worktree_status(worktree, pe->path, fileindex, repo,
5542 check_stage_ok, &oka, NULL, NULL);
5546 if (!oka.have_changes) {
5547 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5551 spa.worktree = worktree;
5552 spa.fileindex = fileindex;
5554 spa.patch_cb = patch_cb;
5555 spa.patch_arg = patch_arg;
5556 spa.status_cb = status_cb;
5557 spa.status_arg = status_arg;
5558 TAILQ_FOREACH(pe, paths, entry) {
5559 err = worktree_status(worktree, pe->path, fileindex, repo,
5560 stage_path, &spa, NULL, NULL);
5565 sync_err = sync_fileindex(fileindex, fileindex_path);
5566 if (sync_err && err == NULL)
5570 got_ref_close(head_ref);
5571 free(head_commit_id);
5572 free(fileindex_path);
5574 got_fileindex_free(fileindex);
5575 unlockerr = lock_worktree(worktree, LOCK_SH);
5576 if (unlockerr && err == NULL)
5581 struct unstage_path_arg {
5582 struct got_worktree *worktree;
5583 struct got_fileindex *fileindex;
5584 struct got_repository *repo;
5585 got_worktree_checkout_cb progress_cb;
5589 static const struct got_error *
5590 unstage_path(void *arg, unsigned char status,
5591 unsigned char staged_status, const char *relpath,
5592 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5593 struct got_object_id *commit_id)
5595 const struct got_error *err = NULL;
5596 struct unstage_path_arg *a = arg;
5597 struct got_fileindex_entry *ie;
5598 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5599 char *ondisk_path = NULL;
5600 int local_changes_subsumed;
5603 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5605 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5607 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
5609 return got_error_from_errno("asprintf");
5611 switch (staged_status) {
5612 case GOT_STATUS_MODIFY:
5613 err = got_object_open_as_blob(&blob_base, a->repo,
5618 case GOT_STATUS_ADD:
5619 err = got_object_open_as_blob(&blob_staged, a->repo,
5620 staged_blob_id, 8192);
5625 err = merge_blob(&local_changes_subsumed, a->worktree,
5626 blob_base, ondisk_path, relpath,
5627 got_fileindex_perms_to_st(ie), blob_staged,
5628 commit_id ? commit_id : a->worktree->base_commit_id,
5629 a->repo, a->progress_cb, a->progress_arg);
5631 got_fileindex_entry_stage_set(ie,
5632 GOT_FILEIDX_STAGE_NONE);
5634 case GOT_STATUS_DELETE:
5635 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
5636 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
5639 err = (*a->progress_cb)(a->progress_arg, status, relpath);
5645 got_object_blob_close(blob_base);
5647 got_object_blob_close(blob_staged);
5651 const struct got_error *
5652 got_worktree_unstage(struct got_worktree *worktree,
5653 struct got_pathlist_head *paths,
5654 got_worktree_checkout_cb progress_cb, void *progress_arg,
5655 struct got_repository *repo)
5657 const struct got_error *err = NULL, *sync_err, *unlockerr;
5658 struct got_pathlist_entry *pe;
5659 struct got_fileindex *fileindex = NULL;
5660 char *fileindex_path = NULL;
5661 struct unstage_path_arg upa;
5663 err = lock_worktree(worktree, LOCK_EX);
5667 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5671 upa.worktree = worktree;
5672 upa.fileindex = fileindex;
5674 upa.progress_cb = progress_cb;
5675 upa.progress_arg = progress_arg;
5676 TAILQ_FOREACH(pe, paths, entry) {
5677 err = worktree_status(worktree, pe->path, fileindex, repo,
5678 unstage_path, &upa, NULL, NULL);
5683 sync_err = sync_fileindex(fileindex, fileindex_path);
5684 if (sync_err && err == NULL)
5687 free(fileindex_path);
5689 got_fileindex_free(fileindex);
5690 unlockerr = lock_worktree(worktree, LOCK_SH);
5691 if (unlockerr && err == NULL)