2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 static const struct got_error *
64 create_meta_file(const char *path_got, const char *name, const char *content)
66 const struct got_error *err = NULL;
69 if (asprintf(&path, "%s/%s", path_got, name) == -1)
70 return got_error_from_errno("asprintf");
72 err = got_path_create_file(path, content);
77 static const struct got_error *
78 update_meta_file(const char *path_got, const char *name, const char *content)
80 const struct got_error *err = NULL;
85 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
86 err = got_error_from_errno("asprintf");
91 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 int len = fprintf(tmpfile, "%s\n", content);
97 if (len != strlen(content) + 1) {
98 err = got_error_from_errno2("fprintf", tmppath);
103 if (rename(tmppath, path) != 0) {
104 err = got_error_from_errno3("rename", tmppath, path);
110 if (fclose(tmpfile) != 0 && err == NULL)
111 err = got_error_from_errno2("fclose", tmppath);
116 static const struct got_error *
117 read_meta_file(char **content, const char *path_got, const char *name)
119 const struct got_error *err = NULL;
127 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
128 err = got_error_from_errno("asprintf");
133 fd = open(path, O_RDONLY | O_NOFOLLOW);
136 err = got_error_path(path, GOT_ERR_WORKTREE_META);
138 err = got_error_from_errno2("open", path);
141 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
142 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
143 : got_error_from_errno2("flock", path));
147 if (fstat(fd, &sb) != 0) {
148 err = got_error_from_errno2("fstat", path);
151 *content = calloc(1, sb.st_size);
152 if (*content == NULL) {
153 err = got_error_from_errno("calloc");
157 n = read(fd, *content, sb.st_size);
158 if (n != sb.st_size) {
159 err = (n == -1 ? got_error_from_errno2("read", path) :
160 got_error_path(path, GOT_ERR_WORKTREE_META));
163 if ((*content)[sb.st_size - 1] != '\n') {
164 err = got_error_path(path, GOT_ERR_WORKTREE_META);
167 (*content)[sb.st_size - 1] = '\0';
170 if (fd != -1 && close(fd) == -1 && err == NULL)
171 err = got_error_from_errno2("close", path_got);
180 static const struct got_error *
181 write_head_ref(const char *path_got, struct got_reference *head_ref)
183 const struct got_error *err = NULL;
186 if (got_ref_is_symbolic(head_ref)) {
187 refstr = got_ref_to_str(head_ref);
189 return got_error_from_errno("got_ref_to_str");
191 refstr = strdup(got_ref_get_name(head_ref));
193 return got_error_from_errno("strdup");
195 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 const struct got_error *
201 got_worktree_init(const char *path, struct got_reference *head_ref,
202 const char *prefix, struct got_repository *repo)
204 const struct got_error *err = NULL;
205 struct got_object_id *commit_id = NULL;
207 uint32_t uuid_status;
209 char *path_got = NULL;
210 char *formatstr = NULL;
211 char *absprefix = NULL;
212 char *basestr = NULL;
213 char *uuidstr = NULL;
215 if (strcmp(path, got_repo_get_path(repo)) == 0) {
216 err = got_error(GOT_ERR_WORKTREE_REPO);
220 err = got_ref_resolve(&commit_id, repo, head_ref);
223 err = got_object_get_type(&obj_type, repo, commit_id);
226 if (obj_type != GOT_OBJ_TYPE_COMMIT)
227 return got_error(GOT_ERR_OBJ_TYPE);
229 if (!got_path_is_absolute(prefix)) {
230 if (asprintf(&absprefix, "/%s", prefix) == -1)
231 return got_error_from_errno("asprintf");
234 /* Create top-level directory (may already exist). */
235 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
236 err = got_error_from_errno2("mkdir", path);
240 /* Create .got directory (may already exist). */
241 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
242 err = got_error_from_errno("asprintf");
245 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
246 err = got_error_from_errno2("mkdir", path_got);
250 /* Create an empty lock file. */
251 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 /* Create an empty file index. */
256 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 /* Write the HEAD reference. */
261 err = write_head_ref(path_got, head_ref);
265 /* Record our base commit. */
266 err = got_object_id_str(&basestr, commit_id);
269 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 /* Store path to repository. */
274 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
275 got_repo_get_path(repo));
279 /* Store in-repository path prefix. */
280 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
281 absprefix ? absprefix : prefix);
286 uuid_create(&uuid, &uuid_status);
287 if (uuid_status != uuid_s_ok) {
288 err = got_error_uuid(uuid_status, "uuid_create");
291 uuid_to_string(&uuid, &uuidstr, &uuid_status);
292 if (uuid_status != uuid_s_ok) {
293 err = got_error_uuid(uuid_status, "uuid_to_string");
296 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 /* Stamp work tree with format file. */
301 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
302 err = got_error_from_errno("asprintf");
305 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
319 static const struct got_error *
320 open_worktree(struct got_worktree **worktree, const char *path)
322 const struct got_error *err = NULL;
324 char *formatstr = NULL;
325 char *uuidstr = NULL;
326 char *path_lock = NULL;
327 char *base_commit_id_str = NULL;
328 int version, fd = -1;
330 struct got_repository *repo = NULL;
331 uint32_t uuid_status;
335 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
336 err = got_error_from_errno("asprintf");
341 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
342 err = got_error_from_errno("asprintf");
347 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
349 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
350 : got_error_from_errno2("open", path_lock));
354 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 version = strtonum(formatstr, 1, INT_MAX, &errstr);
360 err = got_error_msg(GOT_ERR_WORKTREE_META,
361 "could not parse work tree format version number");
364 if (version != GOT_WORKTREE_FORMAT_VERSION) {
365 err = got_error(GOT_ERR_WORKTREE_VERS);
369 *worktree = calloc(1, sizeof(**worktree));
370 if (*worktree == NULL) {
371 err = got_error_from_errno("calloc");
374 (*worktree)->lockfd = -1;
376 (*worktree)->root_path = strdup(path);
377 if ((*worktree)->root_path == NULL) {
378 err = got_error_from_errno("strdup");
381 err = read_meta_file(&(*worktree)->repo_path, path_got,
382 GOT_WORKTREE_REPOSITORY);
386 err = read_meta_file(&(*worktree)->path_prefix, path_got,
387 GOT_WORKTREE_PATH_PREFIX);
391 err = read_meta_file(&base_commit_id_str, path_got,
392 GOT_WORKTREE_BASE_COMMIT);
396 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
399 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
400 if (uuid_status != uuid_s_ok) {
401 err = got_error_uuid(uuid_status, "uuid_from_string");
405 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
415 GOT_WORKTREE_HEAD_REF);
418 got_repo_close(repo);
421 free(base_commit_id_str);
427 if (*worktree != NULL)
428 got_worktree_close(*worktree);
431 (*worktree)->lockfd = fd;
436 const struct got_error *
437 got_worktree_open(struct got_worktree **worktree, const char *path)
439 const struct got_error *err = NULL;
442 err = open_worktree(worktree, path);
443 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 path = dirname(path);
449 return got_error_from_errno2("dirname", path);
450 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
452 return got_error(GOT_ERR_NOT_WORKTREE);
455 const struct got_error *
456 got_worktree_close(struct got_worktree *worktree)
458 const struct got_error *err = NULL;
459 free(worktree->repo_path);
460 free(worktree->path_prefix);
461 free(worktree->base_commit_id);
462 free(worktree->head_ref_name);
463 if (worktree->lockfd != -1)
464 if (close(worktree->lockfd) != 0)
465 err = got_error_from_errno2("close",
466 got_worktree_get_root_path(worktree));
467 free(worktree->root_path);
473 got_worktree_get_root_path(struct got_worktree *worktree)
475 return worktree->root_path;
479 got_worktree_get_repo_path(struct got_worktree *worktree)
481 return worktree->repo_path;
485 got_worktree_get_path_prefix(struct got_worktree *worktree)
487 return worktree->path_prefix;
490 const struct got_error *
491 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
492 const char *path_prefix)
494 char *absprefix = NULL;
496 if (!got_path_is_absolute(path_prefix)) {
497 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
498 return got_error_from_errno("asprintf");
500 *match = (strcmp(absprefix ? absprefix : path_prefix,
501 worktree->path_prefix) == 0);
507 got_worktree_get_head_ref_name(struct got_worktree *worktree)
509 return worktree->head_ref_name;
512 const struct got_error *
513 got_worktree_set_head_ref(struct got_worktree *worktree,
514 struct got_reference *head_ref)
516 const struct got_error *err = NULL;
517 char *path_got = NULL, *head_ref_name = NULL;
519 if (asprintf(&path_got, "%s/%s", worktree->root_path,
520 GOT_WORKTREE_GOT_DIR) == -1) {
521 err = got_error_from_errno("asprintf");
526 head_ref_name = strdup(got_ref_get_name(head_ref));
527 if (head_ref_name == NULL) {
528 err = got_error_from_errno("strdup");
532 err = write_head_ref(path_got, head_ref);
536 free(worktree->head_ref_name);
537 worktree->head_ref_name = head_ref_name;
545 struct got_object_id *
546 got_worktree_get_base_commit_id(struct got_worktree *worktree)
548 return worktree->base_commit_id;
551 const struct got_error *
552 got_worktree_set_base_commit_id(struct got_worktree *worktree,
553 struct got_repository *repo, struct got_object_id *commit_id)
555 const struct got_error *err;
556 struct got_object *obj = NULL;
558 char *path_got = NULL;
560 if (asprintf(&path_got, "%s/%s", worktree->root_path,
561 GOT_WORKTREE_GOT_DIR) == -1) {
562 err = got_error_from_errno("asprintf");
567 err = got_object_open(&obj, repo, commit_id);
571 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
572 err = got_error(GOT_ERR_OBJ_TYPE);
576 /* Record our base commit. */
577 err = got_object_id_str(&id_str, commit_id);
580 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 free(worktree->base_commit_id);
585 worktree->base_commit_id = got_object_id_dup(commit_id);
586 if (worktree->base_commit_id == NULL) {
587 err = got_error_from_errno("got_object_id_dup");
592 got_object_close(obj);
598 static const struct got_error *
599 lock_worktree(struct got_worktree *worktree, int operation)
601 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
602 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
603 : got_error_from_errno2("flock",
604 got_worktree_get_root_path(worktree)));
608 static const struct got_error *
609 add_dir_on_disk(struct got_worktree *worktree, const char *path)
611 const struct got_error *err = NULL;
614 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
615 return got_error_from_errno("asprintf");
617 err = got_path_mkdir(abspath);
618 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
621 if (lstat(abspath, &sb) == -1) {
622 err = got_error_from_errno2("lstat", abspath);
623 } else if (!S_ISDIR(sb.st_mode)) {
624 /* TODO directory is obstructed; do something */
625 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
632 static const struct got_error *
633 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
635 const struct got_error *err = NULL;
638 size_t flen1 = 0, flen2 = 0;
643 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
644 if (flen1 == 0 && ferror(f1)) {
645 err = got_error_from_errno("fread");
648 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
649 if (flen2 == 0 && ferror(f2)) {
650 err = got_error_from_errno("fread");
657 } else if (flen2 == 0) {
661 } else if (flen1 == flen2) {
662 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
675 static const struct got_error *
676 check_files_equal(int *same, const char *f1_path, const char *f2_path)
678 const struct got_error *err = NULL;
681 FILE *f1 = NULL, *f2 = NULL;
685 if (lstat(f1_path, &sb) != 0) {
686 err = got_error_from_errno2("lstat", f1_path);
691 if (lstat(f2_path, &sb) != 0) {
692 err = got_error_from_errno2("lstat", f2_path);
697 if (size1 != size2) {
702 f1 = fopen(f1_path, "r");
704 return got_error_from_errno2("open", f1_path);
706 f2 = fopen(f2_path, "r");
708 err = got_error_from_errno2("open", f2_path);
712 err = check_file_contents_equal(same, f1, f2);
714 if (f1 && fclose(f1) != 0 && err == NULL)
715 err = got_error_from_errno("fclose");
716 if (f2 && fclose(f2) != 0 && err == NULL)
717 err = got_error_from_errno("fclose");
723 * Perform a 3-way merge where blob_orig acts as the common ancestor,
724 * the file at deriv_path acts as the first derived version, and the
725 * file on disk acts as the second derived version.
727 static const struct got_error *
728 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
729 struct got_blob_object *blob_orig, const char *ondisk_path,
730 const char *path, uint16_t st_mode, const char *deriv_path,
731 const char *label_deriv, struct got_repository *repo,
732 got_worktree_checkout_cb progress_cb, void *progress_arg)
734 const struct got_error *err = NULL;
737 char *blob_orig_path = NULL;
738 char *merged_path = NULL, *base_path = NULL;
742 *local_changes_subsumed = 0;
744 parent = dirname(ondisk_path);
746 return got_error_from_errno2("dirname", ondisk_path);
748 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
749 return got_error_from_errno("asprintf");
751 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
757 err = got_error_from_errno("asprintf");
762 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
766 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
772 * If the file has no blob, this is an "add vs add" conflict,
773 * and we simply use an empty ancestor file to make both files
774 * appear in the merged result in their entirety.
778 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
779 blob_orig_path, ondisk_path, label_deriv, path);
783 err = (*progress_cb)(progress_arg,
784 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
788 if (fsync(merged_fd) != 0) {
789 err = got_error_from_errno("fsync");
793 /* Check if a clean merge has subsumed all local changes. */
794 if (overlapcnt == 0) {
795 err = check_files_equal(local_changes_subsumed, deriv_path,
801 if (chmod(merged_path, st_mode) != 0) {
802 err = got_error_from_errno2("chmod", merged_path);
806 if (rename(merged_path, ondisk_path) != 0) {
807 err = got_error_from_errno3("rename", merged_path,
814 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
815 err = got_error_from_errno("close");
816 if (f_orig && fclose(f_orig) != 0 && err == NULL)
817 err = got_error_from_errno("fclose");
820 if (blob_orig_path) {
821 unlink(blob_orig_path);
822 free(blob_orig_path);
828 * Perform a 3-way merge where blob_orig acts as the common ancestor,
829 * blob_deriv acts as the first derived version, and the file on disk
830 * acts as the second derived version.
832 static const struct got_error *
833 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
834 struct got_blob_object *blob_orig, const char *ondisk_path,
835 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
836 struct got_object_id *deriv_base_commit_id,
837 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
840 const struct got_error *err = NULL;
841 FILE *f_deriv = NULL;
842 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
843 char *label_deriv = NULL, *parent;
845 *local_changes_subsumed = 0;
847 parent = dirname(ondisk_path);
849 return got_error_from_errno2("dirname", ondisk_path);
852 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
853 err = got_error_from_errno("asprintf");
858 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
861 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
866 err = got_object_id_str(&id_str, deriv_base_commit_id);
869 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
870 err = got_error_from_errno("asprintf");
874 err = merge_file(local_changes_subsumed, worktree, blob_orig,
875 ondisk_path, path, st_mode, blob_deriv_path, label_deriv,
876 repo, progress_cb, progress_arg);
878 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
879 err = got_error_from_errno("fclose");
881 if (blob_deriv_path) {
882 unlink(blob_deriv_path);
883 free(blob_deriv_path);
890 static const struct got_error *
891 update_blob_fileindex_entry(struct got_worktree *worktree,
892 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
893 const char *ondisk_path, const char *path, struct got_blob_object *blob,
894 int update_timestamps)
896 const struct got_error *err = NULL;
899 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
901 err = got_fileindex_entry_update(ie, ondisk_path,
902 blob->id.sha1, worktree->base_commit_id->sha1,
905 struct got_fileindex_entry *new_ie;
906 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
907 path, blob->id.sha1, worktree->base_commit_id->sha1);
909 err = got_fileindex_entry_add(fileindex, new_ie);
914 static const struct got_error *
915 install_blob(struct got_worktree *worktree, const char *ondisk_path,
916 const char *path, uint16_t te_mode, uint16_t st_mode,
917 struct got_blob_object *blob, int restoring_missing_file,
918 int reverting_versioned_file, struct got_repository *repo,
919 got_worktree_checkout_cb progress_cb, void *progress_arg)
921 const struct got_error *err = NULL;
925 char *tmppath = NULL;
927 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
928 GOT_DEFAULT_FILE_MODE);
930 if (errno == ENOENT) {
931 char *parent = dirname(path);
933 return got_error_from_errno2("dirname", path);
934 err = add_dir_on_disk(worktree, parent);
937 fd = open(ondisk_path,
938 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
939 GOT_DEFAULT_FILE_MODE);
941 return got_error_from_errno2("open",
943 } else if (errno == EEXIST) {
944 if (!S_ISREG(st_mode)) {
945 /* TODO file is obstructed; do something */
946 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
949 err = got_opentemp_named_fd(&tmppath, &fd,
956 return got_error_from_errno2("open", ondisk_path);
959 if (restoring_missing_file)
960 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
961 else if (reverting_versioned_file)
962 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
964 err = (*progress_cb)(progress_arg,
965 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
969 hdrlen = got_object_blob_get_hdrlen(blob);
971 const uint8_t *buf = got_object_blob_get_read_buf(blob);
972 err = got_object_blob_read_block(&len, blob);
976 /* Skip blob object header first time around. */
977 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
979 err = got_error_from_errno("write");
981 } else if (outlen != len - hdrlen) {
982 err = got_error(GOT_ERR_IO);
989 if (fsync(fd) != 0) {
990 err = got_error_from_errno("fsync");
995 if (rename(tmppath, ondisk_path) != 0) {
996 err = got_error_from_errno3("rename", tmppath,
1003 if (te_mode & S_IXUSR) {
1004 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
1005 err = got_error_from_errno2("chmod", ondisk_path);
1009 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1010 err = got_error_from_errno2("chmod", ondisk_path);
1016 if (fd != -1 && close(fd) != 0 && err == NULL)
1017 err = got_error_from_errno("close");
1022 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1023 static const struct got_error *
1024 get_modified_file_content_status(unsigned char *status, FILE *f)
1026 const struct got_error *err = NULL;
1027 const char *markers[3] = {
1028 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1029 GOT_DIFF_CONFLICT_MARKER_SEP,
1030 GOT_DIFF_CONFLICT_MARKER_END
1035 const char delim[3] = {'\0', '\0', '\0'};
1037 while (*status == GOT_STATUS_MODIFY) {
1038 line = fparseln(f, &len, NULL, delim, 0);
1042 err = got_ferror(f, GOT_ERR_IO);
1046 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1047 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1049 *status = GOT_STATUS_CONFLICT;
1059 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1061 return !(ie->ctime_sec == sb->st_ctime &&
1062 ie->ctime_nsec == sb->st_ctimensec &&
1063 ie->mtime_sec == sb->st_mtime &&
1064 ie->mtime_nsec == sb->st_mtimensec &&
1065 ie->size == (sb->st_size & 0xffffffff));
1068 static unsigned char
1069 get_staged_status(struct got_fileindex_entry *ie)
1071 switch (got_fileindex_entry_stage_get(ie)) {
1072 case GOT_FILEIDX_STAGE_ADD:
1073 return GOT_STATUS_ADD;
1074 case GOT_FILEIDX_STAGE_DELETE:
1075 return GOT_STATUS_DELETE;
1076 case GOT_FILEIDX_STAGE_MODIFY:
1077 return GOT_STATUS_MODIFY;
1079 return GOT_STATUS_NO_CHANGE;
1083 static const struct got_error *
1084 get_file_status(unsigned char *status, struct stat *sb,
1085 struct got_fileindex_entry *ie, const char *abspath,
1086 struct got_repository *repo)
1088 const struct got_error *err = NULL;
1089 struct got_object_id id;
1093 struct got_blob_object *blob = NULL;
1095 unsigned char staged_status = get_staged_status(ie);
1097 *status = GOT_STATUS_NO_CHANGE;
1099 if (lstat(abspath, sb) == -1) {
1100 if (errno == ENOENT) {
1101 if (got_fileindex_entry_has_file_on_disk(ie))
1102 *status = GOT_STATUS_MISSING;
1104 *status = GOT_STATUS_DELETE;
1107 return got_error_from_errno2("lstat", abspath);
1110 if (!S_ISREG(sb->st_mode)) {
1111 *status = GOT_STATUS_OBSTRUCTED;
1115 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1116 *status = GOT_STATUS_DELETE;
1118 } else if (!got_fileindex_entry_has_blob(ie) &&
1119 staged_status != GOT_STATUS_ADD) {
1120 *status = GOT_STATUS_ADD;
1124 if (!stat_info_differs(ie, sb))
1127 if (staged_status == GOT_STATUS_MODIFY ||
1128 staged_status == GOT_STATUS_ADD)
1129 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1131 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1133 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1137 f = fopen(abspath, "r");
1139 err = got_error_from_errno2("fopen", abspath);
1142 hdrlen = got_object_blob_get_hdrlen(blob);
1144 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1145 err = got_object_blob_read_block(&blen, blob);
1148 /* Skip length of blob object header first time around. */
1149 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1150 if (flen == 0 && ferror(f)) {
1151 err = got_error_from_errno("fread");
1156 *status = GOT_STATUS_MODIFY;
1158 } else if (flen == 0) {
1160 *status = GOT_STATUS_MODIFY;
1162 } else if (blen - hdrlen == flen) {
1163 /* Skip blob object header first time around. */
1164 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1165 *status = GOT_STATUS_MODIFY;
1169 *status = GOT_STATUS_MODIFY;
1175 if (*status == GOT_STATUS_MODIFY) {
1177 err = get_modified_file_content_status(status, f);
1181 got_object_blob_close(blob);
1188 * Update timestamps in the file index if a file is unmodified and
1189 * we had to run a full content comparison to find out.
1191 static const struct got_error *
1192 sync_timestamps(char *ondisk_path, unsigned char status,
1193 struct got_fileindex_entry *ie, struct stat *sb)
1195 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1196 return got_fileindex_entry_update(ie, ondisk_path,
1197 ie->blob_sha1, ie->commit_sha1, 1);
1202 static const struct got_error *
1203 update_blob(struct got_worktree *worktree,
1204 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1205 struct got_tree_entry *te, const char *path,
1206 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1209 const struct got_error *err = NULL;
1210 struct got_blob_object *blob = NULL;
1212 unsigned char status = GOT_STATUS_NO_CHANGE;
1215 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1216 return got_error_from_errno("asprintf");
1219 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1220 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1223 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1226 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1227 sb.st_mode = got_fileindex_perms_to_st(ie);
1229 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1231 if (status == GOT_STATUS_OBSTRUCTED) {
1232 err = (*progress_cb)(progress_arg, status, path);
1236 if (ie && status != GOT_STATUS_MISSING) {
1237 if (got_fileindex_entry_has_commit(ie) &&
1238 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1239 SHA1_DIGEST_LENGTH) == 0) {
1240 err = sync_timestamps(ondisk_path, status, ie, &sb);
1243 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1247 if (got_fileindex_entry_has_blob(ie) &&
1248 memcmp(ie->blob_sha1, te->id->sha1,
1249 SHA1_DIGEST_LENGTH) == 0) {
1250 err = sync_timestamps(ondisk_path, status, ie, &sb);
1255 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1259 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1260 int update_timestamps;
1261 struct got_blob_object *blob2 = NULL;
1262 if (got_fileindex_entry_has_blob(ie)) {
1263 struct got_object_id id2;
1264 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1265 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1269 err = merge_blob(&update_timestamps, worktree, blob2,
1270 ondisk_path, path, sb.st_mode, blob,
1271 worktree->base_commit_id, repo,
1272 progress_cb, progress_arg);
1274 got_object_blob_close(blob2);
1278 * Do not update timestamps of files with local changes.
1279 * Otherwise, a future status walk would treat them as
1280 * unmodified files again.
1282 err = got_fileindex_entry_update(ie, ondisk_path,
1283 blob->id.sha1, worktree->base_commit_id->sha1,
1285 } else if (status == GOT_STATUS_DELETE) {
1286 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1289 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1290 ondisk_path, path, blob, 0);
1294 err = install_blob(worktree, ondisk_path, path, te->mode,
1295 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1296 repo, progress_cb, progress_arg);
1299 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1300 ondisk_path, path, blob, 1);
1304 got_object_blob_close(blob);
1310 static const struct got_error *
1311 remove_ondisk_file(const char *root_path, const char *path)
1313 const struct got_error *err = NULL;
1314 char *ondisk_path = NULL;
1316 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1317 return got_error_from_errno("asprintf");
1319 if (unlink(ondisk_path) == -1) {
1320 if (errno != ENOENT)
1321 err = got_error_from_errno2("unlink", ondisk_path);
1323 char *parent = dirname(ondisk_path);
1324 while (parent && strcmp(parent, root_path) != 0) {
1325 if (rmdir(parent) == -1) {
1326 if (errno != ENOTEMPTY)
1327 err = got_error_from_errno2("rmdir",
1331 parent = dirname(parent);
1338 static const struct got_error *
1339 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1340 struct got_fileindex_entry *ie, struct got_repository *repo,
1341 got_worktree_checkout_cb progress_cb, void *progress_arg)
1343 const struct got_error *err = NULL;
1344 unsigned char status;
1348 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1349 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1351 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1353 return got_error_from_errno("asprintf");
1355 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1359 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1360 status == GOT_STATUS_ADD) {
1361 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1365 * Preserve the working file and change the deleted blob's
1366 * entry into a schedule-add entry.
1368 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1373 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1376 if (status == GOT_STATUS_NO_CHANGE) {
1377 err = remove_ondisk_file(worktree->root_path, ie->path);
1381 got_fileindex_entry_remove(fileindex, ie);
1387 struct diff_cb_arg {
1388 struct got_fileindex *fileindex;
1389 struct got_worktree *worktree;
1390 struct got_repository *repo;
1391 got_worktree_checkout_cb progress_cb;
1393 got_cancel_cb cancel_cb;
1397 static const struct got_error *
1398 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1399 struct got_tree_entry *te, const char *parent_path)
1401 struct diff_cb_arg *a = arg;
1403 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1404 return got_error(GOT_ERR_CANCELLED);
1406 return update_blob(a->worktree, a->fileindex, ie, te,
1407 ie->path, a->repo, a->progress_cb, a->progress_arg);
1410 static const struct got_error *
1411 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1413 struct diff_cb_arg *a = arg;
1415 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1416 return got_error(GOT_ERR_CANCELLED);
1418 return delete_blob(a->worktree, a->fileindex, ie,
1419 a->repo, a->progress_cb, a->progress_arg);
1422 static const struct got_error *
1423 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1425 struct diff_cb_arg *a = arg;
1426 const struct got_error *err;
1429 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1430 return got_error(GOT_ERR_CANCELLED);
1432 if (got_object_tree_entry_is_submodule(te))
1435 if (asprintf(&path, "%s%s%s", parent_path,
1436 parent_path[0] ? "/" : "", te->name)
1438 return got_error_from_errno("asprintf");
1440 if (S_ISDIR(te->mode))
1441 err = add_dir_on_disk(a->worktree, path);
1443 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1444 a->repo, a->progress_cb, a->progress_arg);
1450 static const struct got_error *
1451 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1453 const struct got_error *err = NULL;
1454 char *uuidstr = NULL;
1455 uint32_t uuid_status;
1459 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1460 if (uuid_status != uuid_s_ok)
1461 return got_error_uuid(uuid_status, "uuid_to_string");
1463 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1465 err = got_error_from_errno("asprintf");
1472 const struct got_error *
1473 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1475 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1478 static const struct got_error *
1479 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1481 return get_ref_name(refname, worktree,
1482 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1485 static const struct got_error *
1486 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1488 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1491 static const struct got_error *
1492 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1494 return get_ref_name(refname, worktree,
1495 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1498 static const struct got_error *
1499 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1501 return get_ref_name(refname, worktree,
1502 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1505 static const struct got_error *
1506 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1508 return get_ref_name(refname, worktree,
1509 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1512 static const struct got_error *
1513 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1515 return get_ref_name(refname, worktree,
1516 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1519 static const struct got_error *
1520 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1522 return get_ref_name(refname, worktree,
1523 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1526 static const struct got_error *
1527 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1529 return get_ref_name(refname, worktree,
1530 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1533 const struct got_error *
1534 got_worktree_get_histedit_script_path(char **path,
1535 struct got_worktree *worktree)
1537 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1538 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1540 return got_error_from_errno("asprintf");
1546 * Prevent Git's garbage collector from deleting our base commit by
1547 * setting a reference to our base commit's ID.
1549 static const struct got_error *
1550 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1552 const struct got_error *err = NULL;
1553 struct got_reference *ref = NULL;
1556 err = got_worktree_get_base_ref_name(&refname, worktree);
1560 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1564 err = got_ref_write(ref, repo);
1572 static const struct got_error *
1573 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1575 const struct got_error *err = NULL;
1577 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1578 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1579 err = got_error_from_errno("asprintf");
1580 *fileindex_path = NULL;
1586 static const struct got_error *
1587 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1588 struct got_worktree *worktree)
1590 const struct got_error *err = NULL;
1593 *fileindex_path = NULL;
1594 *fileindex = got_fileindex_alloc();
1595 if (*fileindex == NULL)
1596 return got_error_from_errno("got_fileindex_alloc");
1598 err = get_fileindex_path(fileindex_path, worktree);
1602 index = fopen(*fileindex_path, "rb");
1603 if (index == NULL) {
1604 if (errno != ENOENT)
1605 err = got_error_from_errno2("fopen", *fileindex_path);
1607 err = got_fileindex_read(*fileindex, index);
1608 if (fclose(index) != 0 && err == NULL)
1609 err = got_error_from_errno("fclose");
1613 free(*fileindex_path);
1614 *fileindex_path = NULL;
1615 got_fileindex_free(*fileindex);
1621 struct bump_base_commit_id_arg {
1622 struct got_object_id *base_commit_id;
1625 const char *entry_name;
1626 got_worktree_checkout_cb progress_cb;
1630 /* Bump base commit ID of all files within an updated part of the work tree. */
1631 static const struct got_error *
1632 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1634 const struct got_error *err;
1635 struct bump_base_commit_id_arg *a = arg;
1637 if (a->entry_name) {
1638 if (strcmp(ie->path, a->path) != 0)
1640 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1643 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1644 SHA1_DIGEST_LENGTH) == 0)
1647 if (a->progress_cb) {
1648 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1653 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1657 static const struct got_error *
1658 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1660 const struct got_error *err = NULL;
1661 char *new_fileindex_path = NULL;
1662 FILE *new_index = NULL;
1664 err = got_opentemp_named(&new_fileindex_path, &new_index,
1669 err = got_fileindex_write(fileindex, new_index);
1673 if (rename(new_fileindex_path, fileindex_path) != 0) {
1674 err = got_error_from_errno3("rename", new_fileindex_path,
1676 unlink(new_fileindex_path);
1681 free(new_fileindex_path);
1685 static const struct got_error *
1686 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1687 struct got_object_id **tree_id, const char *wt_relpath,
1688 struct got_worktree *worktree, struct got_repository *repo)
1690 const struct got_error *err = NULL;
1691 struct got_object_id *id = NULL;
1692 char *in_repo_path = NULL;
1693 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1695 *entry_type = GOT_OBJ_TYPE_ANY;
1696 *tree_relpath = NULL;
1699 if (wt_relpath[0] == '\0') {
1700 /* Check out all files within the work tree. */
1701 *entry_type = GOT_OBJ_TYPE_TREE;
1702 *tree_relpath = strdup("");
1703 if (*tree_relpath == NULL) {
1704 err = got_error_from_errno("strdup");
1707 err = got_object_id_by_path(tree_id, repo,
1708 worktree->base_commit_id, worktree->path_prefix);
1714 /* Check out a subset of files in the work tree. */
1716 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1717 is_root_wt ? "" : "/", wt_relpath) == -1) {
1718 err = got_error_from_errno("asprintf");
1722 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1728 in_repo_path = NULL;
1730 err = got_object_get_type(entry_type, repo, id);
1734 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1735 /* Check out a single file. */
1736 if (strchr(wt_relpath, '/') == NULL) {
1737 /* Check out a single file in work tree's root dir. */
1738 in_repo_path = strdup(worktree->path_prefix);
1739 if (in_repo_path == NULL) {
1740 err = got_error_from_errno("strdup");
1743 *tree_relpath = strdup("");
1744 if (*tree_relpath == NULL) {
1745 err = got_error_from_errno("strdup");
1749 /* Check out a single file in a subdirectory. */
1750 err = got_path_dirname(tree_relpath, wt_relpath);
1753 if (asprintf(&in_repo_path, "%s%s%s",
1754 worktree->path_prefix, is_root_wt ? "" : "/",
1755 *tree_relpath) == -1) {
1756 err = got_error_from_errno("asprintf");
1760 err = got_object_id_by_path(tree_id, repo,
1761 worktree->base_commit_id, in_repo_path);
1763 /* Check out all files within a subdirectory. */
1764 *tree_id = got_object_id_dup(id);
1765 if (*tree_id == NULL) {
1766 err = got_error_from_errno("got_object_id_dup");
1769 *tree_relpath = strdup(wt_relpath);
1770 if (*tree_relpath == NULL) {
1771 err = got_error_from_errno("strdup");
1779 *entry_type = GOT_OBJ_TYPE_ANY;
1780 free(*tree_relpath);
1781 *tree_relpath = NULL;
1788 static const struct got_error *
1789 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1790 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1791 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1792 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1794 const struct got_error *err = NULL;
1795 struct got_commit_object *commit = NULL;
1796 struct got_tree_object *tree = NULL;
1797 struct got_fileindex_diff_tree_cb diff_cb;
1798 struct diff_cb_arg arg;
1800 err = ref_base_commit(worktree, repo);
1804 err = got_object_open_as_commit(&commit, repo,
1805 worktree->base_commit_id);
1809 err = got_object_open_as_tree(&tree, repo, tree_id);
1814 got_object_tree_find_entry(tree, entry_name) == NULL) {
1815 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1819 diff_cb.diff_old_new = diff_old_new;
1820 diff_cb.diff_old = diff_old;
1821 diff_cb.diff_new = diff_new;
1822 arg.fileindex = fileindex;
1823 arg.worktree = worktree;
1825 arg.progress_cb = progress_cb;
1826 arg.progress_arg = progress_arg;
1827 arg.cancel_cb = cancel_cb;
1828 arg.cancel_arg = cancel_arg;
1829 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1830 entry_name, repo, &diff_cb, &arg);
1833 got_object_tree_close(tree);
1835 got_object_commit_close(commit);
1839 const struct got_error *
1840 got_worktree_checkout_files(struct got_worktree *worktree,
1841 struct got_pathlist_head *paths, struct got_repository *repo,
1842 got_worktree_checkout_cb progress_cb, void *progress_arg,
1843 got_cancel_cb cancel_cb, void *cancel_arg)
1845 const struct got_error *err = NULL, *sync_err, *unlockerr;
1846 struct got_commit_object *commit = NULL;
1847 struct got_tree_object *tree = NULL;
1848 struct got_fileindex *fileindex = NULL;
1849 char *fileindex_path = NULL;
1850 struct got_pathlist_entry *pe;
1851 struct tree_path_data {
1852 SIMPLEQ_ENTRY(tree_path_data) entry;
1853 struct got_object_id *tree_id;
1858 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1860 SIMPLEQ_INIT(&tree_paths);
1862 err = lock_worktree(worktree, LOCK_EX);
1866 /* Map all specified paths to in-repository trees. */
1867 TAILQ_FOREACH(pe, paths, entry) {
1868 tpd = malloc(sizeof(*tpd));
1870 err = got_error_from_errno("malloc");
1874 err = find_tree_entry_for_checkout(&tpd->entry_type,
1875 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1881 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1882 err = got_path_basename(&tpd->entry_name, pe->path);
1890 tpd->entry_name = NULL;
1892 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1896 * Read the file index.
1897 * Checking out files is supposed to be an idempotent operation.
1898 * If the on-disk file index is incomplete we will try to complete it.
1900 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1904 tpd = SIMPLEQ_FIRST(&tree_paths);
1905 TAILQ_FOREACH(pe, paths, entry) {
1906 struct bump_base_commit_id_arg bbc_arg;
1908 err = checkout_files(worktree, fileindex, tpd->relpath,
1909 tpd->tree_id, tpd->entry_name, repo,
1910 progress_cb, progress_arg, cancel_cb, cancel_arg);
1914 bbc_arg.base_commit_id = worktree->base_commit_id;
1915 bbc_arg.entry_name = tpd->entry_name;
1916 bbc_arg.path = pe->path;
1917 bbc_arg.path_len = pe->path_len;
1918 bbc_arg.progress_cb = progress_cb;
1919 bbc_arg.progress_arg = progress_arg;
1920 err = got_fileindex_for_each_entry_safe(fileindex,
1921 bump_base_commit_id, &bbc_arg);
1925 tpd = SIMPLEQ_NEXT(tpd, entry);
1927 sync_err = sync_fileindex(fileindex, fileindex_path);
1928 if (sync_err && err == NULL)
1931 free(fileindex_path);
1933 got_object_tree_close(tree);
1935 got_object_commit_close(commit);
1937 got_fileindex_free(fileindex);
1938 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1939 tpd = SIMPLEQ_FIRST(&tree_paths);
1940 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1945 unlockerr = lock_worktree(worktree, LOCK_SH);
1946 if (unlockerr && err == NULL)
1951 struct merge_file_cb_arg {
1952 struct got_worktree *worktree;
1953 struct got_fileindex *fileindex;
1954 got_worktree_checkout_cb progress_cb;
1956 got_cancel_cb cancel_cb;
1958 struct got_object_id *commit_id2;
1961 static const struct got_error *
1962 merge_file_cb(void *arg, struct got_blob_object *blob1,
1963 struct got_blob_object *blob2, struct got_object_id *id1,
1964 struct got_object_id *id2, const char *path1, const char *path2,
1965 struct got_repository *repo)
1967 static const struct got_error *err = NULL;
1968 struct merge_file_cb_arg *a = arg;
1969 struct got_fileindex_entry *ie;
1970 char *ondisk_path = NULL;
1972 unsigned char status;
1973 int local_changes_subsumed;
1975 if (blob1 && blob2) {
1976 ie = got_fileindex_entry_get(a->fileindex, path2,
1979 return (*a->progress_cb)(a->progress_arg,
1980 GOT_STATUS_MISSING, path2);
1982 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1984 return got_error_from_errno("asprintf");
1986 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1990 if (status == GOT_STATUS_DELETE) {
1991 err = (*a->progress_cb)(a->progress_arg,
1992 GOT_STATUS_MERGE, path2);
1995 if (status != GOT_STATUS_NO_CHANGE &&
1996 status != GOT_STATUS_MODIFY &&
1997 status != GOT_STATUS_CONFLICT &&
1998 status != GOT_STATUS_ADD) {
1999 err = (*a->progress_cb)(a->progress_arg, status, path2);
2003 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2004 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
2005 a->progress_cb, a->progress_arg);
2007 ie = got_fileindex_entry_get(a->fileindex, path1,
2010 return (*a->progress_cb)(a->progress_arg,
2011 GOT_STATUS_MISSING, path2);
2013 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2015 return got_error_from_errno("asprintf");
2017 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2022 case GOT_STATUS_NO_CHANGE:
2023 err = (*a->progress_cb)(a->progress_arg,
2024 GOT_STATUS_DELETE, path1);
2027 err = remove_ondisk_file(a->worktree->root_path, path1);
2031 got_fileindex_entry_mark_deleted_from_disk(ie);
2033 case GOT_STATUS_DELETE:
2034 case GOT_STATUS_MISSING:
2035 err = (*a->progress_cb)(a->progress_arg,
2036 GOT_STATUS_DELETE, path1);
2040 got_fileindex_entry_mark_deleted_from_disk(ie);
2042 case GOT_STATUS_ADD:
2043 case GOT_STATUS_MODIFY:
2044 case GOT_STATUS_CONFLICT:
2045 err = (*a->progress_cb)(a->progress_arg,
2046 GOT_STATUS_CANNOT_DELETE, path1);
2050 case GOT_STATUS_OBSTRUCTED:
2051 err = (*a->progress_cb)(a->progress_arg, status, path1);
2059 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2061 return got_error_from_errno("asprintf");
2062 ie = got_fileindex_entry_get(a->fileindex, path2,
2065 err = get_file_status(&status, &sb, ie, ondisk_path,
2069 if (status != GOT_STATUS_NO_CHANGE &&
2070 status != GOT_STATUS_MODIFY &&
2071 status != GOT_STATUS_CONFLICT &&
2072 status != GOT_STATUS_ADD) {
2073 err = (*a->progress_cb)(a->progress_arg,
2077 err = merge_blob(&local_changes_subsumed, a->worktree,
2078 NULL, ondisk_path, path2, sb.st_mode, blob2,
2079 a->commit_id2, repo,
2080 a->progress_cb, a->progress_arg);
2081 if (status == GOT_STATUS_DELETE) {
2082 err = update_blob_fileindex_entry(a->worktree,
2083 a->fileindex, ie, ondisk_path, ie->path,
2089 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2090 err = install_blob(a->worktree, ondisk_path, path2,
2091 /* XXX get this from parent tree! */
2092 GOT_DEFAULT_FILE_MODE,
2093 sb.st_mode, blob2, 0, 0, repo,
2094 a->progress_cb, a->progress_arg);
2097 err = got_fileindex_entry_alloc(&ie,
2098 ondisk_path, path2, NULL, NULL);
2101 err = got_fileindex_entry_add(a->fileindex, ie);
2103 got_fileindex_entry_free(ie);
2113 struct check_merge_ok_arg {
2114 struct got_worktree *worktree;
2115 struct got_repository *repo;
2118 static const struct got_error *
2119 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2121 const struct got_error *err = NULL;
2122 struct check_merge_ok_arg *a = arg;
2123 unsigned char status;
2127 /* Reject merges into a work tree with mixed base commits. */
2128 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2129 SHA1_DIGEST_LENGTH))
2130 return got_error(GOT_ERR_MIXED_COMMITS);
2132 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2134 return got_error_from_errno("asprintf");
2136 /* Reject merges into a work tree with conflicted files. */
2137 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2140 if (status == GOT_STATUS_CONFLICT)
2141 return got_error(GOT_ERR_CONFLICTS);
2146 static const struct got_error *
2147 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2148 const char *fileindex_path, struct got_object_id *commit_id1,
2149 struct got_object_id *commit_id2, struct got_repository *repo,
2150 got_worktree_checkout_cb progress_cb, void *progress_arg,
2151 got_cancel_cb cancel_cb, void *cancel_arg)
2153 const struct got_error *err = NULL, *sync_err;
2154 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2155 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2156 struct merge_file_cb_arg arg;
2159 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2160 worktree->path_prefix);
2164 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2169 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2170 worktree->path_prefix);
2174 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2178 arg.worktree = worktree;
2179 arg.fileindex = fileindex;
2180 arg.progress_cb = progress_cb;
2181 arg.progress_arg = progress_arg;
2182 arg.cancel_cb = cancel_cb;
2183 arg.cancel_arg = cancel_arg;
2184 arg.commit_id2 = commit_id2;
2185 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2186 sync_err = sync_fileindex(fileindex, fileindex_path);
2187 if (sync_err && err == NULL)
2191 got_object_tree_close(tree1);
2193 got_object_tree_close(tree2);
2197 const struct got_error *
2198 got_worktree_merge_files(struct got_worktree *worktree,
2199 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2200 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2201 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2203 const struct got_error *err, *unlockerr;
2204 char *fileindex_path = NULL;
2205 struct got_fileindex *fileindex = NULL;
2206 struct check_merge_ok_arg mok_arg;
2208 err = lock_worktree(worktree, LOCK_EX);
2212 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2216 mok_arg.worktree = worktree;
2217 mok_arg.repo = repo;
2218 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2223 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2224 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2227 got_fileindex_free(fileindex);
2228 free(fileindex_path);
2229 unlockerr = lock_worktree(worktree, LOCK_SH);
2230 if (unlockerr && err == NULL)
2235 struct diff_dir_cb_arg {
2236 struct got_fileindex *fileindex;
2237 struct got_worktree *worktree;
2238 const char *status_path;
2239 size_t status_path_len;
2240 struct got_repository *repo;
2241 got_worktree_status_cb status_cb;
2243 got_cancel_cb cancel_cb;
2245 /* A pathlist containing per-directory pathlists of ignore patterns. */
2246 struct got_pathlist_head ignores;
2249 static const struct got_error *
2250 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2251 got_worktree_status_cb status_cb, void *status_arg,
2252 struct got_repository *repo)
2254 const struct got_error *err = NULL;
2255 unsigned char status = GOT_STATUS_NO_CHANGE;
2256 unsigned char staged_status = get_staged_status(ie);
2258 struct got_object_id blob_id, commit_id, staged_blob_id;
2259 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2260 struct got_object_id *staged_blob_idp = NULL;
2262 err = get_file_status(&status, &sb, ie, abspath, repo);
2266 if (status == GOT_STATUS_NO_CHANGE &&
2267 staged_status == GOT_STATUS_NO_CHANGE)
2270 if (got_fileindex_entry_has_blob(ie)) {
2271 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2272 blob_idp = &blob_id;
2274 if (got_fileindex_entry_has_commit(ie)) {
2275 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2276 commit_idp = &commit_id;
2278 if (staged_status == GOT_STATUS_ADD ||
2279 staged_status == GOT_STATUS_MODIFY) {
2280 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2281 SHA1_DIGEST_LENGTH);
2282 staged_blob_idp = &staged_blob_id;
2285 return (*status_cb)(status_arg, status, staged_status,
2286 ie->path, blob_idp, staged_blob_idp, commit_idp);
2289 static const struct got_error *
2290 status_old_new(void *arg, struct got_fileindex_entry *ie,
2291 struct dirent *de, const char *parent_path)
2293 const struct got_error *err = NULL;
2294 struct diff_dir_cb_arg *a = arg;
2297 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2298 return got_error(GOT_ERR_CANCELLED);
2300 if (got_path_cmp(parent_path, a->status_path,
2301 strlen(parent_path), a->status_path_len) != 0 &&
2302 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2305 if (parent_path[0]) {
2306 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2307 parent_path, de->d_name) == -1)
2308 return got_error_from_errno("asprintf");
2310 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2312 return got_error_from_errno("asprintf");
2315 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2321 static const struct got_error *
2322 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2324 struct diff_dir_cb_arg *a = arg;
2325 struct got_object_id blob_id, commit_id;
2326 unsigned char status;
2328 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2329 return got_error(GOT_ERR_CANCELLED);
2331 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2334 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2335 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2336 if (got_fileindex_entry_has_file_on_disk(ie))
2337 status = GOT_STATUS_MISSING;
2339 status = GOT_STATUS_DELETE;
2340 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2341 ie->path, &blob_id, NULL, &commit_id);
2345 free_ignorelist(struct got_pathlist_head *ignorelist)
2347 struct got_pathlist_entry *pe;
2349 TAILQ_FOREACH(pe, ignorelist, entry)
2350 free((char *)pe->path);
2351 got_pathlist_free(ignorelist);
2355 free_ignores(struct got_pathlist_head *ignores)
2357 struct got_pathlist_entry *pe;
2359 TAILQ_FOREACH(pe, ignores, entry) {
2360 struct got_pathlist_head *ignorelist = pe->data;
2361 free_ignorelist(ignorelist);
2362 free((char *)pe->path);
2364 got_pathlist_free(ignores);
2367 static const struct got_error *
2368 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2370 const struct got_error *err = NULL;
2371 struct got_pathlist_entry *pe = NULL;
2372 struct got_pathlist_head *ignorelist;
2373 char *line = NULL, *pattern, *dirpath = NULL;
2374 size_t linesize = 0;
2377 ignorelist = calloc(1, sizeof(*ignorelist));
2378 if (ignorelist == NULL)
2379 return got_error_from_errno("calloc");
2380 TAILQ_INIT(ignorelist);
2382 while ((linelen = getline(&line, &linesize, f)) != -1) {
2383 if (linelen > 0 && line[linelen - 1] == '\n')
2384 line[linelen - 1] = '\0';
2386 /* Git's ignores may contain comments. */
2390 /* Git's negated patterns are not (yet?) supported. */
2394 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2396 err = got_error_from_errno("asprintf");
2399 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2404 err = got_error_from_errno("getline");
2408 dirpath = strdup(path);
2409 if (dirpath == NULL) {
2410 err = got_error_from_errno("strdup");
2413 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2416 if (err || pe == NULL) {
2418 free_ignorelist(ignorelist);
2424 match_ignores(struct got_pathlist_head *ignores, const char *path)
2426 struct got_pathlist_entry *pe;
2428 /* Handle patterns which match in all directories. */
2429 TAILQ_FOREACH(pe, ignores, entry) {
2430 struct got_pathlist_head *ignorelist = pe->data;
2431 struct got_pathlist_entry *pi;
2433 TAILQ_FOREACH(pi, ignorelist, entry) {
2434 const char *p, *pattern = pi->path;
2436 if (strncmp(pattern, "**/", 3) != 0)
2441 if (fnmatch(pattern, p,
2442 FNM_PATHNAME | FNM_LEADING_DIR)) {
2443 /* Retry in next directory. */
2444 while (*p && *p != '/')
2456 * The ignores pathlist contains ignore lists from children before
2457 * parents, so we can find the most specific ignorelist by walking
2458 * ignores backwards.
2460 pe = TAILQ_LAST(ignores, got_pathlist_head);
2462 if (got_path_is_child(path, pe->path, pe->path_len)) {
2463 struct got_pathlist_head *ignorelist = pe->data;
2464 struct got_pathlist_entry *pi;
2465 TAILQ_FOREACH(pi, ignorelist, entry) {
2466 const char *pattern = pi->path;
2467 int flags = FNM_LEADING_DIR;
2468 if (strstr(pattern, "/**/") == NULL)
2469 flags |= FNM_PATHNAME;
2470 if (fnmatch(pattern, path, flags))
2475 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2481 static const struct got_error *
2482 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2483 const char *path, const char *ignores_filename)
2485 const struct got_error *err = NULL;
2487 FILE *ignoresfile = NULL;
2489 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2490 path[0] ? "/" : "", ignores_filename) == -1)
2491 return got_error_from_errno("asprintf");
2493 ignoresfile = fopen(ignorespath, "r");
2494 if (ignoresfile == NULL) {
2495 if (errno != ENOENT && errno != EACCES)
2496 err = got_error_from_errno2("fopen",
2499 err = read_ignores(ignores, path, ignoresfile);
2501 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2502 err = got_error_from_errno2("flose", path);
2507 static const struct got_error *
2508 status_new(void *arg, struct dirent *de, const char *parent_path)
2510 const struct got_error *err = NULL;
2511 struct diff_dir_cb_arg *a = arg;
2514 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2515 return got_error(GOT_ERR_CANCELLED);
2517 /* XXX ignore symlinks for now */
2518 if (de->d_type == DT_LNK)
2521 if (parent_path[0]) {
2522 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2523 return got_error_from_errno("asprintf");
2528 if (de->d_type == DT_DIR) {
2529 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2532 err = add_ignores(&a->ignores, a->worktree->root_path,
2533 path, ".gitignore");
2535 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2536 && !match_ignores(&a->ignores, path))
2537 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2538 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2544 static const struct got_error *
2545 report_single_file_status(const char *path, const char *ondisk_path,
2546 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2547 void *status_arg, struct got_repository *repo)
2549 struct got_fileindex_entry *ie;
2552 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2554 return report_file_status(ie, ondisk_path, status_cb,
2557 if (lstat(ondisk_path, &sb) == -1) {
2558 if (errno != ENOENT)
2559 return got_error_from_errno2("lstat", ondisk_path);
2560 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2561 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2565 if (S_ISREG(sb.st_mode))
2566 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2567 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2572 static const struct got_error *
2573 worktree_status(struct got_worktree *worktree, const char *path,
2574 struct got_fileindex *fileindex, struct got_repository *repo,
2575 got_worktree_status_cb status_cb, void *status_arg,
2576 got_cancel_cb cancel_cb, void *cancel_arg)
2578 const struct got_error *err = NULL;
2579 DIR *workdir = NULL;
2580 struct got_fileindex_diff_dir_cb fdiff_cb;
2581 struct diff_dir_cb_arg arg;
2582 char *ondisk_path = NULL;
2584 if (asprintf(&ondisk_path, "%s%s%s",
2585 worktree->root_path, path[0] ? "/" : "", path) == -1)
2586 return got_error_from_errno("asprintf");
2588 workdir = opendir(ondisk_path);
2589 if (workdir == NULL) {
2590 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2591 err = got_error_from_errno2("opendir", ondisk_path);
2593 err = report_single_file_status(path, ondisk_path,
2594 fileindex, status_cb, status_arg, repo);
2596 fdiff_cb.diff_old_new = status_old_new;
2597 fdiff_cb.diff_old = status_old;
2598 fdiff_cb.diff_new = status_new;
2599 arg.fileindex = fileindex;
2600 arg.worktree = worktree;
2601 arg.status_path = path;
2602 arg.status_path_len = strlen(path);
2604 arg.status_cb = status_cb;
2605 arg.status_arg = status_arg;
2606 arg.cancel_cb = cancel_cb;
2607 arg.cancel_arg = cancel_arg;
2608 TAILQ_INIT(&arg.ignores);
2609 err = add_ignores(&arg.ignores, worktree->root_path, path,
2612 err = add_ignores(&arg.ignores, worktree->root_path,
2613 path, ".gitignore");
2615 err = got_fileindex_diff_dir(fileindex, workdir,
2616 worktree->root_path, path, repo, &fdiff_cb, &arg);
2617 free_ignores(&arg.ignores);
2626 const struct got_error *
2627 got_worktree_status(struct got_worktree *worktree,
2628 struct got_pathlist_head *paths, struct got_repository *repo,
2629 got_worktree_status_cb status_cb, void *status_arg,
2630 got_cancel_cb cancel_cb, void *cancel_arg)
2632 const struct got_error *err = NULL;
2633 char *fileindex_path = NULL;
2634 struct got_fileindex *fileindex = NULL;
2635 struct got_pathlist_entry *pe;
2637 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2641 TAILQ_FOREACH(pe, paths, entry) {
2642 err = worktree_status(worktree, pe->path, fileindex, repo,
2643 status_cb, status_arg, cancel_cb, cancel_arg);
2647 free(fileindex_path);
2648 got_fileindex_free(fileindex);
2652 const struct got_error *
2653 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2656 const struct got_error *err = NULL;
2657 char *resolved, *cwd = NULL, *path = NULL;
2662 resolved = realpath(arg, NULL);
2663 if (resolved == NULL) {
2664 if (errno != ENOENT)
2665 return got_error_from_errno2("realpath", arg);
2666 cwd = getcwd(NULL, 0);
2668 return got_error_from_errno("getcwd");
2669 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2670 err = got_error_from_errno("asprintf");
2675 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2676 strlen(got_worktree_get_root_path(worktree)))) {
2677 err = got_error(GOT_ERR_BAD_PATH);
2681 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2682 err = got_path_skip_common_ancestor(&path,
2683 got_worktree_get_root_path(worktree), resolved);
2689 err = got_error_from_errno("strdup");
2694 /* XXX status walk can't deal with trailing slash! */
2696 while (len > 0 && path[len - 1] == '/') {
2697 path[len - 1] = '\0';
2710 static const struct got_error *
2711 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2712 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2713 struct got_repository *repo)
2715 const struct got_error *err = NULL;
2716 struct got_fileindex_entry *ie;
2717 unsigned char status;
2720 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2722 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2725 /* Re-adding an existing entry is a no-op. */
2726 if (status == GOT_STATUS_ADD)
2728 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2731 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2735 err = got_fileindex_entry_add(fileindex, ie);
2737 got_fileindex_entry_free(ie);
2741 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2744 const struct got_error *
2745 got_worktree_schedule_add(struct got_worktree *worktree,
2746 struct got_pathlist_head *paths,
2747 got_worktree_status_cb status_cb, void *status_arg,
2748 struct got_repository *repo)
2750 struct got_fileindex *fileindex = NULL;
2751 char *fileindex_path = NULL;
2752 const struct got_error *err = NULL, *sync_err, *unlockerr;
2753 struct got_pathlist_entry *pe;
2755 err = lock_worktree(worktree, LOCK_EX);
2759 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2763 TAILQ_FOREACH(pe, paths, entry) {
2765 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2767 return got_error_from_errno("asprintf");
2768 err = schedule_addition(ondisk_path, fileindex, pe->path,
2769 status_cb, status_arg, repo);
2774 sync_err = sync_fileindex(fileindex, fileindex_path);
2775 if (sync_err && err == NULL)
2778 free(fileindex_path);
2780 got_fileindex_free(fileindex);
2781 unlockerr = lock_worktree(worktree, LOCK_SH);
2782 if (unlockerr && err == NULL)
2787 static const struct got_error *
2788 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2789 const char *relpath, int delete_local_mods,
2790 got_worktree_status_cb status_cb, void *status_arg,
2791 struct got_repository *repo)
2793 const struct got_error *err = NULL;
2794 struct got_fileindex_entry *ie = NULL;
2795 unsigned char status, staged_status;
2798 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2800 return got_error(GOT_ERR_BAD_PATH);
2802 staged_status = get_staged_status(ie);
2803 if (staged_status != GOT_STATUS_NO_CHANGE) {
2804 if (staged_status == GOT_STATUS_DELETE)
2806 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2809 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2813 if (status != GOT_STATUS_NO_CHANGE) {
2814 if (status == GOT_STATUS_DELETE)
2816 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2817 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2818 if (status != GOT_STATUS_MODIFY &&
2819 status != GOT_STATUS_MISSING)
2820 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2823 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2824 return got_error_from_errno2("unlink", ondisk_path);
2826 got_fileindex_entry_mark_deleted_from_disk(ie);
2827 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2830 const struct got_error *
2831 got_worktree_schedule_delete(struct got_worktree *worktree,
2832 struct got_pathlist_head *paths, int delete_local_mods,
2833 got_worktree_status_cb status_cb, void *status_arg,
2834 struct got_repository *repo)
2836 struct got_fileindex *fileindex = NULL;
2837 char *fileindex_path = NULL;
2838 const struct got_error *err = NULL, *sync_err, *unlockerr;
2839 struct got_pathlist_entry *pe;
2841 err = lock_worktree(worktree, LOCK_EX);
2845 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2849 TAILQ_FOREACH(pe, paths, entry) {
2851 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2853 return got_error_from_errno("asprintf");
2854 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2855 delete_local_mods, status_cb, status_arg, repo);
2860 sync_err = sync_fileindex(fileindex, fileindex_path);
2861 if (sync_err && err == NULL)
2864 free(fileindex_path);
2866 got_fileindex_free(fileindex);
2867 unlockerr = lock_worktree(worktree, LOCK_SH);
2868 if (unlockerr && err == NULL)
2873 static const struct got_error *
2874 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2876 const struct got_error *err = NULL;
2878 size_t linesize = 0, n;
2881 linelen = getline(&line, &linesize, infile);
2882 if (linelen == -1) {
2883 if (ferror(infile)) {
2884 err = got_error_from_errno("getline");
2890 n = fwrite(line, 1, linelen, outfile);
2892 err = got_ferror(outfile, GOT_ERR_IO);
2897 n = fwrite(line, 1, linelen, rejectfile);
2899 err = got_ferror(outfile, GOT_ERR_IO);
2906 static const struct got_error *
2907 skip_one_line(FILE *f)
2910 size_t linesize = 0;
2913 linelen = getline(&line, &linesize, f);
2914 if (linelen == -1) {
2916 return got_error_from_errno("getline");
2923 static const struct got_error *
2924 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2925 int start_old, int end_old, int start_new, int end_new,
2926 FILE *outfile, FILE *rejectfile)
2928 const struct got_error *err;
2930 /* Copy old file's lines leading up to patch. */
2931 while (!feof(f1) && *line_cur1 < start_old) {
2932 err = copy_one_line(f1, outfile, NULL);
2937 /* Skip new file's lines leading up to patch. */
2938 while (!feof(f2) && *line_cur2 < start_new) {
2940 err = copy_one_line(f2, NULL, rejectfile);
2942 err = skip_one_line(f2);
2947 /* Copy patched lines. */
2948 while (!feof(f2) && *line_cur2 <= end_new) {
2949 err = copy_one_line(f2, outfile, NULL);
2954 /* Skip over old file's replaced lines. */
2955 while (!feof(f1) && *line_cur1 <= end_old) {
2957 err = copy_one_line(f1, NULL, rejectfile);
2959 err = skip_one_line(f1);
2968 static const struct got_error *
2969 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2970 FILE *outfile, FILE *rejectfile)
2972 const struct got_error *err;
2975 /* Copy old file's lines until EOF. */
2977 err = copy_one_line(f1, outfile, NULL);
2984 /* Copy new file's lines until EOF. */
2986 err = copy_one_line(f2, NULL, rejectfile);
2996 static const struct got_error *
2997 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2998 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2999 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3000 int *line_cur2, FILE *outfile, FILE *rejectfile,
3001 got_worktree_patch_cb patch_cb, void *patch_arg)
3003 const struct got_error *err = NULL;
3004 int start_old = change->cv.a;
3005 int end_old = change->cv.b;
3006 int start_new = change->cv.c;
3007 int end_new = change->cv.d;
3011 *choice = GOT_PATCH_CHOICE_NONE;
3013 hunkfile = got_opentemp();
3014 if (hunkfile == NULL)
3015 return got_error_from_errno("got_opentemp");
3020 /* XXX TODO needs error checking */
3021 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3023 if (fseek(f1, pos1, SEEK_SET) == -1) {
3024 err = got_ferror(f1, GOT_ERR_IO);
3027 if (fseek(f2, pos2, SEEK_SET) == -1) {
3028 err = got_ferror(f1, GOT_ERR_IO);
3031 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3032 err = got_ferror(hunkfile, GOT_ERR_IO);
3036 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3037 hunkfile, n, nchanges);
3042 case GOT_PATCH_CHOICE_YES:
3043 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3044 end_old, start_new, end_new, outfile, rejectfile);
3046 case GOT_PATCH_CHOICE_NO:
3047 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3048 end_old, start_new, end_new, rejectfile, outfile);
3050 case GOT_PATCH_CHOICE_QUIT:
3053 err = got_error(GOT_ERR_PATCH_CHOICE);
3057 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3058 err = got_error_from_errno("fclose");
3062 struct revert_file_args {
3063 struct got_worktree *worktree;
3064 struct got_fileindex *fileindex;
3065 got_worktree_checkout_cb progress_cb;
3067 got_worktree_patch_cb patch_cb;
3069 struct got_repository *repo;
3072 static const struct got_error *
3073 create_patched_content(char **path_outfile, int reverse_patch,
3074 struct got_object_id *blob_id, const char *path2,
3075 const char *relpath, struct got_repository *repo,
3076 got_worktree_patch_cb patch_cb, void *patch_arg)
3078 const struct got_error *err;
3079 struct got_blob_object *blob = NULL;
3080 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3081 char *path1 = NULL, *id_str = NULL;
3082 struct stat sb1, sb2;
3083 struct got_diff_changes *changes = NULL;
3084 struct got_diff_state *ds = NULL;
3085 struct got_diff_args *args = NULL;
3086 struct got_diff_change *change;
3087 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3090 *path_outfile = NULL;
3092 err = got_object_id_str(&id_str, blob_id);
3096 f2 = fopen(path2, "r");
3098 err = got_error_from_errno2("fopen", path2);
3102 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3106 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3110 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3114 if (stat(path1, &sb1) == -1) {
3115 err = got_error_from_errno2("stat", path1);
3118 if (stat(path2, &sb2) == -1) {
3119 err = got_error_from_errno2("stat", path2);
3123 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3124 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3128 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3132 if (fseek(f1, 0L, SEEK_SET) == -1)
3133 return got_ferror(f1, GOT_ERR_IO);
3134 if (fseek(f2, 0L, SEEK_SET) == -1)
3135 return got_ferror(f2, GOT_ERR_IO);
3136 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3138 err = apply_or_reject_change(&choice, change, ++n,
3139 changes->nchanges, ds, args, diff_flags, relpath,
3140 f1, f2, &line_cur1, &line_cur2,
3141 reverse_patch ? NULL : outfile,
3142 reverse_patch ? outfile : NULL,
3143 patch_cb, patch_arg);
3146 if (choice == GOT_PATCH_CHOICE_YES)
3148 else if (choice == GOT_PATCH_CHOICE_QUIT)
3152 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3153 reverse_patch ? NULL : outfile,
3154 reverse_patch ? outfile : NULL);
3158 got_object_blob_close(blob);
3159 if (f1 && fclose(f1) == EOF && err == NULL)
3160 err = got_error_from_errno2("fclose", path1);
3161 if (f2 && fclose(f2) == EOF && err == NULL)
3162 err = got_error_from_errno2("fclose", path2);
3163 if (outfile && fclose(outfile) == EOF && err == NULL)
3164 err = got_error_from_errno2("fclose", *path_outfile);
3165 if (path1 && unlink(path1) == -1 && err == NULL)
3166 err = got_error_from_errno2("unlink", path1);
3167 if (err || !have_content) {
3168 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3169 err = got_error_from_errno2("unlink", *path_outfile);
3170 free(*path_outfile);
3171 *path_outfile = NULL;
3175 got_diff_state_free(ds);
3179 got_diff_free_changes(changes);
3184 static const struct got_error *
3185 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3186 const char *relpath, struct got_object_id *blob_id,
3187 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3189 struct revert_file_args *a = arg;
3190 const struct got_error *err = NULL;
3191 char *parent_path = NULL;
3192 struct got_fileindex_entry *ie;
3193 struct got_tree_object *tree = NULL;
3194 struct got_object_id *tree_id = NULL;
3195 const struct got_tree_entry *te = NULL;
3196 char *tree_path = NULL, *te_name;
3197 char *ondisk_path = NULL, *path_content = NULL;
3198 struct got_blob_object *blob = NULL;
3200 /* Reverting a staged deletion is a no-op. */
3201 if (status == GOT_STATUS_DELETE &&
3202 staged_status != GOT_STATUS_NO_CHANGE)
3205 if (status == GOT_STATUS_UNVERSIONED)
3206 return (*a->progress_cb)(a->progress_arg,
3207 GOT_STATUS_UNVERSIONED, relpath);
3209 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3211 return got_error(GOT_ERR_BAD_PATH);
3213 /* Construct in-repository path of tree which contains this blob. */
3214 err = got_path_dirname(&parent_path, ie->path);
3216 if (err->code != GOT_ERR_BAD_PATH)
3218 parent_path = strdup("/");
3219 if (parent_path == NULL) {
3220 err = got_error_from_errno("strdup");
3224 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3225 tree_path = strdup(parent_path);
3226 if (tree_path == NULL) {
3227 err = got_error_from_errno("strdup");
3231 if (got_path_is_root_dir(parent_path)) {
3232 tree_path = strdup(a->worktree->path_prefix);
3233 if (tree_path == NULL) {
3234 err = got_error_from_errno("strdup");
3238 if (asprintf(&tree_path, "%s/%s",
3239 a->worktree->path_prefix, parent_path) == -1) {
3240 err = got_error_from_errno("asprintf");
3246 err = got_object_id_by_path(&tree_id, a->repo,
3247 a->worktree->base_commit_id, tree_path);
3249 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3250 (status == GOT_STATUS_ADD ||
3251 staged_status == GOT_STATUS_ADD)))
3254 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3258 te_name = basename(ie->path);
3259 if (te_name == NULL) {
3260 err = got_error_from_errno2("basename", ie->path);
3264 te = got_object_tree_find_entry(tree, te_name);
3265 if (te == NULL && status != GOT_STATUS_ADD &&
3266 staged_status != GOT_STATUS_ADD) {
3267 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3273 case GOT_STATUS_ADD:
3275 int choice = GOT_PATCH_CHOICE_NONE;
3276 err = (*a->patch_cb)(&choice, a->patch_arg,
3277 status, ie->path, NULL, 1, 1);
3280 if (choice != GOT_PATCH_CHOICE_YES)
3283 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3287 got_fileindex_entry_remove(a->fileindex, ie);
3289 case GOT_STATUS_DELETE:
3291 int choice = GOT_PATCH_CHOICE_NONE;
3292 err = (*a->patch_cb)(&choice, a->patch_arg,
3293 status, ie->path, NULL, 1, 1);
3296 if (choice != GOT_PATCH_CHOICE_YES)
3300 case GOT_STATUS_MODIFY:
3301 case GOT_STATUS_CONFLICT:
3302 case GOT_STATUS_MISSING: {
3303 struct got_object_id id;
3304 if (staged_status == GOT_STATUS_ADD ||
3305 staged_status == GOT_STATUS_MODIFY) {
3306 memcpy(id.sha1, ie->staged_blob_sha1,
3307 SHA1_DIGEST_LENGTH);
3309 memcpy(id.sha1, ie->blob_sha1,
3310 SHA1_DIGEST_LENGTH);
3311 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3315 if (asprintf(&ondisk_path, "%s/%s",
3316 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3317 err = got_error_from_errno("asprintf");
3321 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3322 status == GOT_STATUS_CONFLICT)) {
3323 err = create_patched_content(&path_content, 1, &id,
3324 ondisk_path, ie->path, a->repo,
3325 a->patch_cb, a->patch_arg);
3326 if (err || path_content == NULL)
3328 if (rename(path_content, ondisk_path) == -1) {
3329 err = got_error_from_errno3("rename",
3330 path_content, ondisk_path);
3334 err = install_blob(a->worktree, ondisk_path, ie->path,
3335 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3336 got_fileindex_perms_to_st(ie), blob, 0, 1,
3337 a->repo, a->progress_cb, a->progress_arg);
3340 if (status == GOT_STATUS_DELETE) {
3341 err = update_blob_fileindex_entry(a->worktree,
3342 a->fileindex, ie, ondisk_path, ie->path,
3359 got_object_blob_close(blob);
3361 got_object_tree_close(tree);
3366 const struct got_error *
3367 got_worktree_revert(struct got_worktree *worktree,
3368 struct got_pathlist_head *paths,
3369 got_worktree_checkout_cb progress_cb, void *progress_arg,
3370 got_worktree_patch_cb patch_cb, void *patch_arg,
3371 struct got_repository *repo)
3373 struct got_fileindex *fileindex = NULL;
3374 char *fileindex_path = NULL;
3375 const struct got_error *err = NULL, *unlockerr = NULL;
3376 const struct got_error *sync_err = NULL;
3377 struct got_pathlist_entry *pe;
3378 struct revert_file_args rfa;
3380 err = lock_worktree(worktree, LOCK_EX);
3384 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3388 rfa.worktree = worktree;
3389 rfa.fileindex = fileindex;
3390 rfa.progress_cb = progress_cb;
3391 rfa.progress_arg = progress_arg;
3392 rfa.patch_cb = patch_cb;
3393 rfa.patch_arg = patch_arg;
3395 TAILQ_FOREACH(pe, paths, entry) {
3396 err = worktree_status(worktree, pe->path, fileindex, repo,
3397 revert_file, &rfa, NULL, NULL);
3401 sync_err = sync_fileindex(fileindex, fileindex_path);
3402 if (sync_err && err == NULL)
3405 free(fileindex_path);
3407 got_fileindex_free(fileindex);
3408 unlockerr = lock_worktree(worktree, LOCK_SH);
3409 if (unlockerr && err == NULL)
3415 free_commitable(struct got_commitable *ct)
3418 free(ct->in_repo_path);
3419 free(ct->ondisk_path);
3421 free(ct->base_blob_id);
3422 free(ct->staged_blob_id);
3423 free(ct->base_commit_id);
3427 struct collect_commitables_arg {
3428 struct got_pathlist_head *commitable_paths;
3429 struct got_repository *repo;
3430 struct got_worktree *worktree;
3431 int have_staged_files;
3434 static const struct got_error *
3435 collect_commitables(void *arg, unsigned char status,
3436 unsigned char staged_status, const char *relpath,
3437 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3438 struct got_object_id *commit_id)
3440 struct collect_commitables_arg *a = arg;
3441 const struct got_error *err = NULL;
3442 struct got_commitable *ct = NULL;
3443 struct got_pathlist_entry *new = NULL;
3444 char *parent_path = NULL, *path = NULL;
3447 if (a->have_staged_files) {
3448 if (staged_status != GOT_STATUS_MODIFY &&
3449 staged_status != GOT_STATUS_ADD &&
3450 staged_status != GOT_STATUS_DELETE)
3453 if (status == GOT_STATUS_CONFLICT)
3454 return got_error(GOT_ERR_COMMIT_CONFLICT);
3456 if (status != GOT_STATUS_MODIFY &&
3457 status != GOT_STATUS_ADD &&
3458 status != GOT_STATUS_DELETE)
3462 if (asprintf(&path, "/%s", relpath) == -1) {
3463 err = got_error_from_errno("asprintf");
3466 if (strcmp(path, "/") == 0) {
3467 parent_path = strdup("");
3468 if (parent_path == NULL)
3469 return got_error_from_errno("strdup");
3471 err = got_path_dirname(&parent_path, path);
3476 ct = calloc(1, sizeof(*ct));
3478 err = got_error_from_errno("calloc");
3482 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3484 err = got_error_from_errno("asprintf");
3487 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3488 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3490 if (lstat(ct->ondisk_path, &sb) != 0) {
3491 err = got_error_from_errno2("lstat", ct->ondisk_path);
3494 ct->mode = sb.st_mode;
3497 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3498 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3500 err = got_error_from_errno("asprintf");
3504 ct->status = status;
3505 ct->staged_status = staged_status;
3506 ct->blob_id = NULL; /* will be filled in when blob gets created */
3507 if (ct->status != GOT_STATUS_ADD &&
3508 ct->staged_status != GOT_STATUS_ADD) {
3509 ct->base_blob_id = got_object_id_dup(blob_id);
3510 if (ct->base_blob_id == NULL) {
3511 err = got_error_from_errno("got_object_id_dup");
3514 ct->base_commit_id = got_object_id_dup(commit_id);
3515 if (ct->base_commit_id == NULL) {
3516 err = got_error_from_errno("got_object_id_dup");
3520 if (ct->staged_status == GOT_STATUS_ADD ||
3521 ct->staged_status == GOT_STATUS_MODIFY) {
3522 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3523 if (ct->staged_blob_id == NULL) {
3524 err = got_error_from_errno("got_object_id_dup");
3528 ct->path = strdup(path);
3529 if (ct->path == NULL) {
3530 err = got_error_from_errno("strdup");
3533 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3535 if (ct && (err || new == NULL))
3536 free_commitable(ct);
3542 static const struct got_error *write_tree(struct got_object_id **,
3543 struct got_tree_object *, const char *, struct got_pathlist_head *,
3544 got_worktree_status_cb status_cb, void *status_arg,
3545 struct got_repository *);
3547 static const struct got_error *
3548 write_subtree(struct got_object_id **new_subtree_id,
3549 struct got_tree_entry *te, const char *parent_path,
3550 struct got_pathlist_head *commitable_paths,
3551 got_worktree_status_cb status_cb, void *status_arg,
3552 struct got_repository *repo)
3554 const struct got_error *err = NULL;
3555 struct got_tree_object *subtree;
3558 if (asprintf(&subpath, "%s%s%s", parent_path,
3559 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3560 return got_error_from_errno("asprintf");
3562 err = got_object_open_as_tree(&subtree, repo, te->id);
3566 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3567 status_cb, status_arg, repo);
3568 got_object_tree_close(subtree);
3573 static const struct got_error *
3574 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3576 const struct got_error *err = NULL;
3577 char *ct_parent_path = NULL;
3581 if (strchr(ct->in_repo_path, '/') == NULL) {
3582 *match = got_path_is_root_dir(path);
3586 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3589 *match = (strcmp(path, ct_parent_path) == 0);
3590 free(ct_parent_path);
3595 get_ct_file_mode(struct got_commitable *ct)
3597 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3600 static const struct got_error *
3601 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3602 struct got_tree_entry *te, struct got_commitable *ct)
3604 const struct got_error *err = NULL;
3608 err = got_object_tree_entry_dup(new_te, te);
3612 (*new_te)->mode = get_ct_file_mode(ct);
3614 free((*new_te)->id);
3615 if (ct->staged_status == GOT_STATUS_MODIFY)
3616 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3618 (*new_te)->id = got_object_id_dup(ct->blob_id);
3619 if ((*new_te)->id == NULL) {
3620 err = got_error_from_errno("got_object_id_dup");
3624 if (err && *new_te) {
3625 got_object_tree_entry_close(*new_te);
3631 static const struct got_error *
3632 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3633 struct got_commitable *ct)
3635 const struct got_error *err = NULL;
3640 *new_te = calloc(1, sizeof(**new_te));
3641 if (*new_te == NULL)
3642 return got_error_from_errno("calloc");
3644 ct_name = basename(ct->path);
3645 if (ct_name == NULL) {
3646 err = got_error_from_errno2("basename", ct->path);
3649 (*new_te)->name = strdup(ct_name);
3650 if ((*new_te)->name == NULL) {
3651 err = got_error_from_errno("strdup");
3655 (*new_te)->mode = get_ct_file_mode(ct);
3657 if (ct->staged_status == GOT_STATUS_ADD)
3658 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3660 (*new_te)->id = got_object_id_dup(ct->blob_id);
3661 if ((*new_te)->id == NULL) {
3662 err = got_error_from_errno("got_object_id_dup");
3666 if (err && *new_te) {
3667 got_object_tree_entry_close(*new_te);
3673 static const struct got_error *
3674 insert_tree_entry(struct got_tree_entry *new_te,
3675 struct got_pathlist_head *paths)
3677 const struct got_error *err = NULL;
3678 struct got_pathlist_entry *new_pe;
3680 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3684 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3688 static const struct got_error *
3689 report_ct_status(struct got_commitable *ct,
3690 got_worktree_status_cb status_cb, void *status_arg)
3692 const char *ct_path = ct->path;
3693 unsigned char status;
3695 while (ct_path[0] == '/')
3698 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3699 status = ct->staged_status;
3701 status = ct->status;
3703 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3704 ct_path, ct->blob_id, NULL, NULL);
3707 static const struct got_error *
3708 match_modified_subtree(int *modified, struct got_tree_entry *te,
3709 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3711 const struct got_error *err = NULL;
3712 struct got_pathlist_entry *pe;
3717 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3718 got_path_is_root_dir(base_tree_path) ? "" : "/",
3720 return got_error_from_errno("asprintf");
3722 TAILQ_FOREACH(pe, commitable_paths, entry) {
3723 struct got_commitable *ct = pe->data;
3724 *modified = got_path_is_child(ct->in_repo_path, te_path,
3734 static const struct got_error *
3735 match_deleted_or_modified_ct(struct got_commitable **ctp,
3736 struct got_tree_entry *te, const char *base_tree_path,
3737 struct got_pathlist_head *commitable_paths)
3739 const struct got_error *err = NULL;
3740 struct got_pathlist_entry *pe;
3744 TAILQ_FOREACH(pe, commitable_paths, entry) {
3745 struct got_commitable *ct = pe->data;
3746 char *ct_name = NULL;
3749 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3750 if (ct->status != GOT_STATUS_MODIFY &&
3751 ct->status != GOT_STATUS_DELETE)
3754 if (ct->staged_status != GOT_STATUS_MODIFY &&
3755 ct->staged_status != GOT_STATUS_DELETE)
3759 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3762 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3768 ct_name = basename(pe->path);
3769 if (ct_name == NULL)
3770 return got_error_from_errno2("basename", pe->path);
3772 if (strcmp(te->name, ct_name) != 0)
3782 static const struct got_error *
3783 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3784 const char *child_path, const char *path_base_tree,
3785 struct got_pathlist_head *commitable_paths,
3786 got_worktree_status_cb status_cb, void *status_arg,
3787 struct got_repository *repo)
3789 const struct got_error *err = NULL;
3790 struct got_tree_entry *new_te;
3795 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3796 got_path_is_root_dir(path_base_tree) ? "" : "/",
3798 return got_error_from_errno("asprintf");
3800 new_te = calloc(1, sizeof(*new_te));
3802 return got_error_from_errno("calloc");
3803 new_te->mode = S_IFDIR;
3804 new_te->name = strdup(child_path);
3805 if (new_te->name == NULL) {
3806 err = got_error_from_errno("strdup");
3807 got_object_tree_entry_close(new_te);
3810 err = write_tree(&new_te->id, NULL, subtree_path,
3811 commitable_paths, status_cb, status_arg, repo);
3813 got_object_tree_entry_close(new_te);
3823 static const struct got_error *
3824 write_tree(struct got_object_id **new_tree_id,
3825 struct got_tree_object *base_tree, const char *path_base_tree,
3826 struct got_pathlist_head *commitable_paths,
3827 got_worktree_status_cb status_cb, void *status_arg,
3828 struct got_repository *repo)
3830 const struct got_error *err = NULL;
3831 const struct got_tree_entries *base_entries = NULL;
3832 struct got_pathlist_head paths;
3833 struct got_tree_entries new_tree_entries;
3834 struct got_tree_entry *te, *new_te = NULL;
3835 struct got_pathlist_entry *pe;
3838 new_tree_entries.nentries = 0;
3839 SIMPLEQ_INIT(&new_tree_entries.head);
3841 /* Insert, and recurse into, newly added entries first. */
3842 TAILQ_FOREACH(pe, commitable_paths, entry) {
3843 struct got_commitable *ct = pe->data;
3844 char *child_path = NULL, *slash;
3846 if ((ct->status != GOT_STATUS_ADD &&
3847 ct->staged_status != GOT_STATUS_ADD) ||
3848 (ct->flags & GOT_COMMITABLE_ADDED))
3851 if (!got_path_is_child(pe->path, path_base_tree,
3852 strlen(path_base_tree)))
3855 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3860 slash = strchr(child_path, '/');
3861 if (slash == NULL) {
3862 err = alloc_added_blob_tree_entry(&new_te, ct);
3865 err = report_ct_status(ct, status_cb, status_arg);
3868 ct->flags |= GOT_COMMITABLE_ADDED;
3869 err = insert_tree_entry(new_te, &paths);
3873 *slash = '\0'; /* trim trailing path components */
3874 if (base_tree == NULL ||
3875 got_object_tree_find_entry(base_tree, child_path)
3877 err = make_subtree_for_added_blob(&new_te,
3878 child_path, path_base_tree,
3879 commitable_paths, status_cb, status_arg,
3883 err = insert_tree_entry(new_te, &paths);
3891 /* Handle modified and deleted entries. */
3892 base_entries = got_object_tree_get_entries(base_tree);
3893 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3894 struct got_commitable *ct = NULL;
3896 if (got_object_tree_entry_is_submodule(te)) {
3897 /* Entry is a submodule; just copy it. */
3898 err = got_object_tree_entry_dup(&new_te, te);
3901 err = insert_tree_entry(new_te, &paths);
3907 if (S_ISDIR(te->mode)) {
3909 err = got_object_tree_entry_dup(&new_te, te);
3912 err = match_modified_subtree(&modified, te,
3913 path_base_tree, commitable_paths);
3916 /* Avoid recursion into unmodified subtrees. */
3919 err = write_subtree(&new_te->id, te,
3920 path_base_tree, commitable_paths,
3921 status_cb, status_arg, repo);
3925 err = insert_tree_entry(new_te, &paths);
3931 err = match_deleted_or_modified_ct(&ct, te,
3932 path_base_tree, commitable_paths);
3936 /* NB: Deleted entries get dropped here. */
3937 if (ct->status == GOT_STATUS_MODIFY ||
3938 ct->staged_status == GOT_STATUS_MODIFY) {
3939 err = alloc_modified_blob_tree_entry(
3943 err = insert_tree_entry(new_te, &paths);
3947 err = report_ct_status(ct, status_cb,
3952 /* Entry is unchanged; just copy it. */
3953 err = got_object_tree_entry_dup(&new_te, te);
3956 err = insert_tree_entry(new_te, &paths);
3963 /* Write new list of entries; deleted entries have been dropped. */
3964 TAILQ_FOREACH(pe, &paths, entry) {
3965 struct got_tree_entry *te = pe->data;
3966 new_tree_entries.nentries++;
3967 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3969 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3971 got_object_tree_entries_close(&new_tree_entries);
3972 got_pathlist_free(&paths);
3976 static const struct got_error *
3977 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3978 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
3979 int have_staged_files)
3981 const struct got_error *err = NULL;
3982 struct got_pathlist_entry *pe;
3984 TAILQ_FOREACH(pe, commitable_paths, entry) {
3985 struct got_fileindex_entry *ie;
3986 struct got_commitable *ct = pe->data;
3988 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3990 if (ct->status == GOT_STATUS_DELETE ||
3991 ct->staged_status == GOT_STATUS_DELETE) {
3992 got_fileindex_entry_remove(fileindex, ie);
3993 got_fileindex_entry_free(ie);
3994 } else if (ct->staged_status == GOT_STATUS_ADD ||
3995 ct->staged_status == GOT_STATUS_MODIFY) {
3996 got_fileindex_entry_stage_set(ie,
3997 GOT_FILEIDX_STAGE_NONE);
3998 err = got_fileindex_entry_update(ie,
3999 ct->ondisk_path, ct->staged_blob_id->sha1,
4000 new_base_commit_id->sha1,
4001 !have_staged_files);
4003 err = got_fileindex_entry_update(ie,
4004 ct->ondisk_path, ct->blob_id->sha1,
4005 new_base_commit_id->sha1,
4006 !have_staged_files);
4008 err = got_fileindex_entry_alloc(&ie,
4009 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4010 new_base_commit_id->sha1);
4013 err = got_fileindex_entry_add(fileindex, ie);
4022 static const struct got_error *
4023 check_out_of_date(const char *in_repo_path, unsigned char status,
4024 unsigned char staged_status, struct got_object_id *base_blob_id,
4025 struct got_object_id *base_commit_id,
4026 struct got_object_id *head_commit_id, struct got_repository *repo,
4029 const struct got_error *err = NULL;
4030 struct got_object_id *id = NULL;
4032 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4033 /* Trivial case: base commit == head commit */
4034 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4037 * Ensure file content which local changes were based
4038 * on matches file content in the branch head.
4040 err = got_object_id_by_path(&id, repo, head_commit_id,
4043 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4044 err = got_error(ood_errcode);
4046 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4047 err = got_error(ood_errcode);
4049 /* Require that added files don't exist in the branch head. */
4050 err = got_object_id_by_path(&id, repo, head_commit_id,
4052 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4054 err = id ? got_error(ood_errcode) : NULL;
4061 const struct got_error *
4062 commit_worktree(struct got_object_id **new_commit_id,
4063 struct got_pathlist_head *commitable_paths,
4064 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4065 const char *author, const char *committer,
4066 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4067 got_worktree_status_cb status_cb, void *status_arg,
4068 struct got_repository *repo)
4070 const struct got_error *err = NULL, *unlockerr = NULL;
4071 struct got_pathlist_entry *pe;
4072 const char *head_ref_name = NULL;
4073 struct got_commit_object *head_commit = NULL;
4074 struct got_reference *head_ref2 = NULL;
4075 struct got_object_id *head_commit_id2 = NULL;
4076 struct got_tree_object *head_tree = NULL;
4077 struct got_object_id *new_tree_id = NULL;
4078 struct got_object_id_queue parent_ids;
4079 struct got_object_qid *pid = NULL;
4080 char *logmsg = NULL;
4082 *new_commit_id = NULL;
4084 SIMPLEQ_INIT(&parent_ids);
4086 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4090 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4094 if (commit_msg_cb != NULL) {
4095 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4100 if (logmsg == NULL || strlen(logmsg) == 0) {
4101 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4105 /* Create blobs from added and modified files and record their IDs. */
4106 TAILQ_FOREACH(pe, commitable_paths, entry) {
4107 struct got_commitable *ct = pe->data;
4110 /* Blobs for staged files already exist. */
4111 if (ct->staged_status == GOT_STATUS_ADD ||
4112 ct->staged_status == GOT_STATUS_MODIFY)
4115 if (ct->status != GOT_STATUS_ADD &&
4116 ct->status != GOT_STATUS_MODIFY)
4119 if (asprintf(&ondisk_path, "%s/%s",
4120 worktree->root_path, pe->path) == -1) {
4121 err = got_error_from_errno("asprintf");
4124 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4130 /* Recursively write new tree objects. */
4131 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4132 status_cb, status_arg, repo);
4136 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4139 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4140 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4141 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4142 got_object_qid_free(pid);
4148 /* Check if a concurrent commit to our branch has occurred. */
4149 head_ref_name = got_worktree_get_head_ref_name(worktree);
4150 if (head_ref_name == NULL) {
4151 err = got_error_from_errno("got_worktree_get_head_ref_name");
4154 /* Lock the reference here to prevent concurrent modification. */
4155 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4158 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4161 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4162 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4165 /* Update branch head in repository. */
4166 err = got_ref_change_ref(head_ref2, *new_commit_id);
4169 err = got_ref_write(head_ref2, repo);
4173 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4177 err = ref_base_commit(worktree, repo);
4182 got_object_tree_close(head_tree);
4184 got_object_commit_close(head_commit);
4185 free(head_commit_id2);
4187 unlockerr = got_ref_unlock(head_ref2);
4188 if (unlockerr && err == NULL)
4190 got_ref_close(head_ref2);
4195 static const struct got_error *
4196 check_path_is_commitable(const char *path,
4197 struct got_pathlist_head *commitable_paths)
4199 struct got_pathlist_entry *cpe = NULL;
4200 size_t path_len = strlen(path);
4202 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4203 struct got_commitable *ct = cpe->data;
4204 const char *ct_path = ct->path;
4206 while (ct_path[0] == '/')
4209 if (strcmp(path, ct_path) == 0 ||
4210 got_path_is_child(ct_path, path, path_len))
4215 return got_error_path(path, GOT_ERR_BAD_PATH);
4220 static const struct got_error *
4221 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4223 int *have_staged_files = arg;
4225 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4226 *have_staged_files = 1;
4227 return got_error(GOT_ERR_CANCELLED);
4233 static const struct got_error *
4234 check_non_staged_files(struct got_fileindex *fileindex,
4235 struct got_pathlist_head *paths)
4237 struct got_pathlist_entry *pe;
4238 struct got_fileindex_entry *ie;
4240 TAILQ_FOREACH(pe, paths, entry) {
4241 if (pe->path[0] == '\0')
4243 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4245 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4246 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4247 return got_error_path(pe->path,
4248 GOT_ERR_FILE_NOT_STAGED);
4254 const struct got_error *
4255 got_worktree_commit(struct got_object_id **new_commit_id,
4256 struct got_worktree *worktree, struct got_pathlist_head *paths,
4257 const char *author, const char *committer,
4258 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4259 got_worktree_status_cb status_cb, void *status_arg,
4260 struct got_repository *repo)
4262 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4263 struct got_fileindex *fileindex = NULL;
4264 char *fileindex_path = NULL;
4265 struct got_pathlist_head commitable_paths;
4266 struct collect_commitables_arg cc_arg;
4267 struct got_pathlist_entry *pe;
4268 struct got_reference *head_ref = NULL;
4269 struct got_object_id *head_commit_id = NULL;
4270 int have_staged_files = 0;
4272 *new_commit_id = NULL;
4274 TAILQ_INIT(&commitable_paths);
4276 err = lock_worktree(worktree, LOCK_EX);
4280 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4284 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4288 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4292 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4293 &have_staged_files);
4294 if (err && err->code != GOT_ERR_CANCELLED)
4296 if (have_staged_files) {
4297 err = check_non_staged_files(fileindex, paths);
4302 cc_arg.commitable_paths = &commitable_paths;
4303 cc_arg.worktree = worktree;
4305 cc_arg.have_staged_files = have_staged_files;
4306 TAILQ_FOREACH(pe, paths, entry) {
4307 err = worktree_status(worktree, pe->path, fileindex, repo,
4308 collect_commitables, &cc_arg, NULL, NULL);
4313 if (TAILQ_EMPTY(&commitable_paths)) {
4314 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4318 TAILQ_FOREACH(pe, paths, entry) {
4319 err = check_path_is_commitable(pe->path, &commitable_paths);
4324 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4325 struct got_commitable *ct = pe->data;
4326 const char *ct_path = ct->in_repo_path;
4328 while (ct_path[0] == '/')
4330 err = check_out_of_date(ct_path, ct->status,
4331 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4332 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4338 err = commit_worktree(new_commit_id, &commitable_paths,
4339 head_commit_id, worktree, author, committer,
4340 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4344 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4345 fileindex, have_staged_files);
4346 sync_err = sync_fileindex(fileindex, fileindex_path);
4347 if (sync_err && err == NULL)
4351 got_fileindex_free(fileindex);
4352 free(fileindex_path);
4353 unlockerr = lock_worktree(worktree, LOCK_SH);
4354 if (unlockerr && err == NULL)
4356 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4357 struct got_commitable *ct = pe->data;
4358 free_commitable(ct);
4360 got_pathlist_free(&commitable_paths);
4365 got_commitable_get_path(struct got_commitable *ct)
4371 got_commitable_get_status(struct got_commitable *ct)
4376 struct check_rebase_ok_arg {
4377 struct got_worktree *worktree;
4378 struct got_repository *repo;
4381 static const struct got_error *
4382 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4384 const struct got_error *err = NULL;
4385 struct check_rebase_ok_arg *a = arg;
4386 unsigned char status;
4390 /* Reject rebase of a work tree with mixed base commits. */
4391 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4392 SHA1_DIGEST_LENGTH))
4393 return got_error(GOT_ERR_MIXED_COMMITS);
4395 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4397 return got_error_from_errno("asprintf");
4399 /* Reject rebase of a work tree with modified or staged files. */
4400 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4405 if (status != GOT_STATUS_NO_CHANGE)
4406 return got_error(GOT_ERR_MODIFIED);
4407 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4408 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4413 const struct got_error *
4414 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4415 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4416 struct got_worktree *worktree, struct got_reference *branch,
4417 struct got_repository *repo)
4419 const struct got_error *err = NULL;
4420 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4421 char *branch_ref_name = NULL;
4422 char *fileindex_path = NULL;
4423 struct check_rebase_ok_arg ok_arg;
4424 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4426 *new_base_branch_ref = NULL;
4430 err = lock_worktree(worktree, LOCK_EX);
4434 err = open_fileindex(fileindex, &fileindex_path, worktree);
4438 ok_arg.worktree = worktree;
4440 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4445 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4449 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4453 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4457 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4462 err = got_ref_alloc_symref(new_base_branch_ref,
4463 new_base_branch_ref_name, wt_branch);
4466 err = got_ref_write(*new_base_branch_ref, repo);
4470 /* TODO Lock original branch's ref while rebasing? */
4472 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4476 err = got_ref_write(branch_ref, repo);
4480 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4481 worktree->base_commit_id);
4484 err = got_ref_write(*tmp_branch, repo);
4488 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4492 free(fileindex_path);
4493 free(tmp_branch_name);
4494 free(new_base_branch_ref_name);
4495 free(branch_ref_name);
4497 got_ref_close(branch_ref);
4499 got_ref_close(wt_branch);
4501 if (*new_base_branch_ref) {
4502 got_ref_close(*new_base_branch_ref);
4503 *new_base_branch_ref = NULL;
4506 got_ref_close(*tmp_branch);
4510 got_fileindex_free(*fileindex);
4513 lock_worktree(worktree, LOCK_SH);
4518 const struct got_error *
4519 got_worktree_rebase_continue(struct got_object_id **commit_id,
4520 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4521 struct got_reference **branch, struct got_fileindex **fileindex,
4522 struct got_worktree *worktree, struct got_repository *repo)
4524 const struct got_error *err;
4525 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4526 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4527 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4528 char *fileindex_path = NULL;
4529 int have_staged_files = 0;
4532 *new_base_branch = NULL;
4537 err = lock_worktree(worktree, LOCK_EX);
4541 err = open_fileindex(fileindex, &fileindex_path, worktree);
4545 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4546 &have_staged_files);
4547 if (err && err->code != GOT_ERR_CANCELLED)
4549 if (have_staged_files) {
4550 err = got_error(GOT_ERR_STAGED_PATHS);
4554 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4558 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4562 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4566 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4570 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4574 err = got_ref_open(branch, repo,
4575 got_ref_get_symref_target(branch_ref), 0);
4579 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4583 err = got_ref_resolve(commit_id, repo, commit_ref);
4587 err = got_ref_open(new_base_branch, repo,
4588 new_base_branch_ref_name, 0);
4592 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4596 free(commit_ref_name);
4597 free(branch_ref_name);
4598 free(fileindex_path);
4600 got_ref_close(commit_ref);
4602 got_ref_close(branch_ref);
4607 got_ref_close(*tmp_branch);
4610 if (*new_base_branch) {
4611 got_ref_close(*new_base_branch);
4612 *new_base_branch = NULL;
4615 got_ref_close(*branch);
4619 got_fileindex_free(*fileindex);
4622 lock_worktree(worktree, LOCK_SH);
4627 const struct got_error *
4628 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4630 const struct got_error *err;
4631 char *tmp_branch_name = NULL;
4633 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4637 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4638 free(tmp_branch_name);
4642 static const struct got_error *
4643 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4644 char **logmsg, void *arg)
4650 static const struct got_error *
4651 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4652 const char *path, struct got_object_id *blob_id,
4653 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4658 struct collect_merged_paths_arg {
4659 got_worktree_checkout_cb progress_cb;
4661 struct got_pathlist_head *merged_paths;
4664 static const struct got_error *
4665 collect_merged_paths(void *arg, unsigned char status, const char *path)
4667 const struct got_error *err;
4668 struct collect_merged_paths_arg *a = arg;
4670 struct got_pathlist_entry *new;
4672 err = (*a->progress_cb)(a->progress_arg, status, path);
4676 if (status != GOT_STATUS_MERGE &&
4677 status != GOT_STATUS_ADD &&
4678 status != GOT_STATUS_DELETE &&
4679 status != GOT_STATUS_CONFLICT)
4684 return got_error_from_errno("strdup");
4686 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4687 if (err || new == NULL)
4693 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4695 struct got_pathlist_entry *pe;
4697 TAILQ_FOREACH(pe, merged_paths, entry)
4698 free((char *)pe->path);
4700 got_pathlist_free(merged_paths);
4703 static const struct got_error *
4704 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4705 struct got_repository *repo)
4707 const struct got_error *err;
4708 struct got_reference *commit_ref = NULL;
4710 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4712 if (err->code != GOT_ERR_NOT_REF)
4714 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4717 err = got_ref_write(commit_ref, repo);
4721 struct got_object_id *stored_id;
4724 err = got_ref_resolve(&stored_id, repo, commit_ref);
4727 cmp = got_object_id_cmp(commit_id, stored_id);
4730 err = got_error(GOT_ERR_REBASE_COMMITID);
4736 got_ref_close(commit_ref);
4740 static const struct got_error *
4741 rebase_merge_files(struct got_pathlist_head *merged_paths,
4742 const char *commit_ref_name, struct got_worktree *worktree,
4743 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4744 struct got_object_id *commit_id, struct got_repository *repo,
4745 got_worktree_checkout_cb progress_cb, void *progress_arg,
4746 got_cancel_cb cancel_cb, void *cancel_arg)
4748 const struct got_error *err;
4749 struct got_reference *commit_ref = NULL;
4750 struct collect_merged_paths_arg cmp_arg;
4751 char *fileindex_path;
4753 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4755 err = get_fileindex_path(&fileindex_path, worktree);
4759 cmp_arg.progress_cb = progress_cb;
4760 cmp_arg.progress_arg = progress_arg;
4761 cmp_arg.merged_paths = merged_paths;
4762 err = merge_files(worktree, fileindex, fileindex_path,
4763 parent_commit_id, commit_id, repo, collect_merged_paths,
4764 &cmp_arg, cancel_cb, cancel_arg);
4766 got_ref_close(commit_ref);
4770 const struct got_error *
4771 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4772 struct got_worktree *worktree, struct got_fileindex *fileindex,
4773 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4774 struct got_repository *repo,
4775 got_worktree_checkout_cb progress_cb, void *progress_arg,
4776 got_cancel_cb cancel_cb, void *cancel_arg)
4778 const struct got_error *err;
4779 char *commit_ref_name;
4781 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4785 err = store_commit_id(commit_ref_name, commit_id, repo);
4789 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4790 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4791 progress_arg, cancel_cb, cancel_arg);
4793 free(commit_ref_name);
4797 const struct got_error *
4798 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4799 struct got_worktree *worktree, struct got_fileindex *fileindex,
4800 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4801 struct got_repository *repo,
4802 got_worktree_checkout_cb progress_cb, void *progress_arg,
4803 got_cancel_cb cancel_cb, void *cancel_arg)
4805 const struct got_error *err;
4806 char *commit_ref_name;
4808 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4812 err = store_commit_id(commit_ref_name, commit_id, repo);
4816 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4817 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4818 progress_arg, cancel_cb, cancel_arg);
4820 free(commit_ref_name);
4824 static const struct got_error *
4825 rebase_commit(struct got_object_id **new_commit_id,
4826 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4827 struct got_worktree *worktree, struct got_fileindex *fileindex,
4828 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4829 const char *new_logmsg, struct got_repository *repo)
4831 const struct got_error *err, *sync_err;
4832 struct got_pathlist_head commitable_paths;
4833 struct collect_commitables_arg cc_arg;
4834 char *fileindex_path = NULL;
4835 struct got_reference *head_ref = NULL;
4836 struct got_object_id *head_commit_id = NULL;
4837 char *logmsg = NULL;
4839 TAILQ_INIT(&commitable_paths);
4840 *new_commit_id = NULL;
4842 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4844 err = get_fileindex_path(&fileindex_path, worktree);
4848 cc_arg.commitable_paths = &commitable_paths;
4849 cc_arg.worktree = worktree;
4851 cc_arg.have_staged_files = 0;
4853 * If possible get the status of individual files directly to
4854 * avoid crawling the entire work tree once per rebased commit.
4855 * TODO: Ideally, merged_paths would contain a list of commitables
4856 * we could use so we could skip worktree_status() entirely.
4859 struct got_pathlist_entry *pe;
4860 if (TAILQ_EMPTY(merged_paths)) {
4861 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4864 TAILQ_FOREACH(pe, merged_paths, entry) {
4865 err = worktree_status(worktree, pe->path, fileindex,
4866 repo, collect_commitables, &cc_arg, NULL, NULL);
4871 err = worktree_status(worktree, "", fileindex, repo,
4872 collect_commitables, &cc_arg, NULL, NULL);
4877 if (TAILQ_EMPTY(&commitable_paths)) {
4878 /* No-op change; commit will be elided. */
4879 err = got_ref_delete(commit_ref, repo);
4882 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4886 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4890 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4895 logmsg = strdup(new_logmsg);
4896 if (logmsg == NULL) {
4897 err = got_error_from_errno("strdup");
4901 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4906 /* NB: commit_worktree will call free(logmsg) */
4907 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4908 worktree, got_object_commit_get_author(orig_commit),
4909 got_object_commit_get_committer(orig_commit),
4910 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4914 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4918 err = got_ref_delete(commit_ref, repo);
4922 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4924 sync_err = sync_fileindex(fileindex, fileindex_path);
4925 if (sync_err && err == NULL)
4928 free(fileindex_path);
4929 free(head_commit_id);
4931 got_ref_close(head_ref);
4933 free(*new_commit_id);
4934 *new_commit_id = NULL;
4939 const struct got_error *
4940 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4941 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4942 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4943 struct got_commit_object *orig_commit,
4944 struct got_object_id *orig_commit_id, struct got_repository *repo)
4946 const struct got_error *err;
4947 char *commit_ref_name;
4948 struct got_reference *commit_ref = NULL;
4949 struct got_object_id *commit_id = NULL;
4951 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4955 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4958 err = got_ref_resolve(&commit_id, repo, commit_ref);
4961 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4962 err = got_error(GOT_ERR_REBASE_COMMITID);
4966 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4967 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4970 got_ref_close(commit_ref);
4971 free(commit_ref_name);
4976 const struct got_error *
4977 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4978 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4979 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4980 struct got_commit_object *orig_commit,
4981 struct got_object_id *orig_commit_id, const char *new_logmsg,
4982 struct got_repository *repo)
4984 const struct got_error *err;
4985 char *commit_ref_name;
4986 struct got_reference *commit_ref = NULL;
4987 struct got_object_id *commit_id = NULL;
4989 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4993 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4996 err = got_ref_resolve(&commit_id, repo, commit_ref);
4999 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5000 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5004 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5005 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5008 got_ref_close(commit_ref);
5009 free(commit_ref_name);
5014 const struct got_error *
5015 got_worktree_rebase_postpone(struct got_worktree *worktree,
5016 struct got_fileindex *fileindex)
5019 got_fileindex_free(fileindex);
5020 return lock_worktree(worktree, LOCK_SH);
5023 static const struct got_error *
5024 delete_ref(const char *name, struct got_repository *repo)
5026 const struct got_error *err;
5027 struct got_reference *ref;
5029 err = got_ref_open(&ref, repo, name, 0);
5031 if (err->code == GOT_ERR_NOT_REF)
5036 err = got_ref_delete(ref, repo);
5041 static const struct got_error *
5042 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5044 const struct got_error *err;
5045 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5046 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5048 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5051 err = delete_ref(tmp_branch_name, repo);
5055 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5058 err = delete_ref(new_base_branch_ref_name, repo);
5062 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5065 err = delete_ref(branch_ref_name, repo);
5069 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5072 err = delete_ref(commit_ref_name, repo);
5077 free(tmp_branch_name);
5078 free(new_base_branch_ref_name);
5079 free(branch_ref_name);
5080 free(commit_ref_name);
5084 const struct got_error *
5085 got_worktree_rebase_complete(struct got_worktree *worktree,
5086 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5087 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5088 struct got_repository *repo)
5090 const struct got_error *err, *unlockerr;
5091 struct got_object_id *new_head_commit_id = NULL;
5093 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5097 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5101 err = got_ref_write(rebased_branch, repo);
5105 err = got_worktree_set_head_ref(worktree, rebased_branch);
5109 err = delete_rebase_refs(worktree, repo);
5112 got_fileindex_free(fileindex);
5113 free(new_head_commit_id);
5114 unlockerr = lock_worktree(worktree, LOCK_SH);
5115 if (unlockerr && err == NULL)
5120 const struct got_error *
5121 got_worktree_rebase_abort(struct got_worktree *worktree,
5122 struct got_fileindex *fileindex, struct got_repository *repo,
5123 struct got_reference *new_base_branch,
5124 got_worktree_checkout_cb progress_cb, void *progress_arg)
5126 const struct got_error *err, *unlockerr, *sync_err;
5127 struct got_reference *resolved = NULL;
5128 struct got_object_id *commit_id = NULL;
5129 char *fileindex_path = NULL;
5130 struct revert_file_args rfa;
5131 struct got_object_id *tree_id = NULL;
5133 err = lock_worktree(worktree, LOCK_EX);
5137 err = got_ref_open(&resolved, repo,
5138 got_ref_get_symref_target(new_base_branch), 0);
5142 err = got_worktree_set_head_ref(worktree, resolved);
5147 * XXX commits to the base branch could have happened while
5148 * we were busy rebasing; should we store the original commit ID
5149 * when rebase begins and read it back here?
5151 err = got_ref_resolve(&commit_id, repo, resolved);
5155 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5159 err = got_object_id_by_path(&tree_id, repo,
5160 worktree->base_commit_id, worktree->path_prefix);
5164 err = delete_rebase_refs(worktree, repo);
5168 err = get_fileindex_path(&fileindex_path, worktree);
5172 rfa.worktree = worktree;
5173 rfa.fileindex = fileindex;
5174 rfa.progress_cb = progress_cb;
5175 rfa.progress_arg = progress_arg;
5176 rfa.patch_cb = NULL;
5177 rfa.patch_arg = NULL;
5179 err = worktree_status(worktree, "", fileindex, repo,
5180 revert_file, &rfa, NULL, NULL);
5184 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5185 repo, progress_cb, progress_arg, NULL, NULL);
5187 sync_err = sync_fileindex(fileindex, fileindex_path);
5188 if (sync_err && err == NULL)
5191 got_ref_close(resolved);
5195 got_fileindex_free(fileindex);
5196 free(fileindex_path);
5198 unlockerr = lock_worktree(worktree, LOCK_SH);
5199 if (unlockerr && err == NULL)
5204 const struct got_error *
5205 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5206 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5207 struct got_fileindex **fileindex, struct got_worktree *worktree,
5208 struct got_repository *repo)
5210 const struct got_error *err = NULL;
5211 char *tmp_branch_name = NULL;
5212 char *branch_ref_name = NULL;
5213 char *base_commit_ref_name = NULL;
5214 char *fileindex_path = NULL;
5215 struct check_rebase_ok_arg ok_arg;
5216 struct got_reference *wt_branch = NULL;
5217 struct got_reference *base_commit_ref = NULL;
5221 *base_commit_id = NULL;
5224 err = lock_worktree(worktree, LOCK_EX);
5228 err = open_fileindex(fileindex, &fileindex_path, worktree);
5232 ok_arg.worktree = worktree;
5234 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5239 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5243 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5247 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5252 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5257 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5261 err = got_ref_write(*branch_ref, repo);
5265 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5266 worktree->base_commit_id);
5269 err = got_ref_write(base_commit_ref, repo);
5272 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5273 if (*base_commit_id == NULL) {
5274 err = got_error_from_errno("got_object_id_dup");
5278 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5279 worktree->base_commit_id);
5282 err = got_ref_write(*tmp_branch, repo);
5286 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5290 free(fileindex_path);
5291 free(tmp_branch_name);
5292 free(branch_ref_name);
5293 free(base_commit_ref_name);
5295 got_ref_close(wt_branch);
5298 got_ref_close(*branch_ref);
5302 got_ref_close(*tmp_branch);
5305 free(*base_commit_id);
5307 got_fileindex_free(*fileindex);
5310 lock_worktree(worktree, LOCK_SH);
5315 const struct got_error *
5316 got_worktree_histedit_postpone(struct got_worktree *worktree,
5317 struct got_fileindex *fileindex)
5320 got_fileindex_free(fileindex);
5321 return lock_worktree(worktree, LOCK_SH);
5324 const struct got_error *
5325 got_worktree_histedit_in_progress(int *in_progress,
5326 struct got_worktree *worktree)
5328 const struct got_error *err;
5329 char *tmp_branch_name = NULL;
5331 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5335 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5336 free(tmp_branch_name);
5340 const struct got_error *
5341 got_worktree_histedit_continue(struct got_object_id **commit_id,
5342 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5343 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5344 struct got_worktree *worktree, struct got_repository *repo)
5346 const struct got_error *err;
5347 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5348 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5349 struct got_reference *commit_ref = NULL;
5350 struct got_reference *base_commit_ref = NULL;
5351 char *fileindex_path = NULL;
5352 int have_staged_files = 0;
5356 *base_commit_id = NULL;
5359 err = lock_worktree(worktree, LOCK_EX);
5363 err = open_fileindex(fileindex, &fileindex_path, worktree);
5367 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5368 &have_staged_files);
5369 if (err && err->code != GOT_ERR_CANCELLED)
5371 if (have_staged_files) {
5372 err = got_error(GOT_ERR_STAGED_PATHS);
5376 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5380 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5384 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5388 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5393 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5397 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5400 err = got_ref_resolve(commit_id, repo, commit_ref);
5404 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5407 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5411 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5415 free(commit_ref_name);
5416 free(branch_ref_name);
5417 free(fileindex_path);
5419 got_ref_close(commit_ref);
5420 if (base_commit_ref)
5421 got_ref_close(base_commit_ref);
5425 free(*base_commit_id);
5426 *base_commit_id = NULL;
5428 got_ref_close(*tmp_branch);
5432 got_fileindex_free(*fileindex);
5435 lock_worktree(worktree, LOCK_EX);
5440 static const struct got_error *
5441 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5443 const struct got_error *err;
5444 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5445 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5447 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5450 err = delete_ref(tmp_branch_name, repo);
5454 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5458 err = delete_ref(base_commit_ref_name, repo);
5462 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5465 err = delete_ref(branch_ref_name, repo);
5469 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5472 err = delete_ref(commit_ref_name, repo);
5476 free(tmp_branch_name);
5477 free(base_commit_ref_name);
5478 free(branch_ref_name);
5479 free(commit_ref_name);
5483 const struct got_error *
5484 got_worktree_histedit_abort(struct got_worktree *worktree,
5485 struct got_fileindex *fileindex, struct got_repository *repo,
5486 struct got_reference *branch, struct got_object_id *base_commit_id,
5487 got_worktree_checkout_cb progress_cb, void *progress_arg)
5489 const struct got_error *err, *unlockerr, *sync_err;
5490 struct got_reference *resolved = NULL;
5491 char *fileindex_path = NULL;
5492 struct got_object_id *tree_id = NULL;
5493 struct revert_file_args rfa;
5495 err = lock_worktree(worktree, LOCK_EX);
5499 err = got_ref_open(&resolved, repo,
5500 got_ref_get_symref_target(branch), 0);
5504 err = got_worktree_set_head_ref(worktree, resolved);
5508 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5512 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5513 worktree->path_prefix);
5517 err = delete_histedit_refs(worktree, repo);
5521 err = get_fileindex_path(&fileindex_path, worktree);
5525 rfa.worktree = worktree;
5526 rfa.fileindex = fileindex;
5527 rfa.progress_cb = progress_cb;
5528 rfa.progress_arg = progress_arg;
5529 rfa.patch_cb = NULL;
5530 rfa.patch_arg = NULL;
5532 err = worktree_status(worktree, "", fileindex, repo,
5533 revert_file, &rfa, NULL, NULL);
5537 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5538 repo, progress_cb, progress_arg, NULL, NULL);
5540 sync_err = sync_fileindex(fileindex, fileindex_path);
5541 if (sync_err && err == NULL)
5544 got_ref_close(resolved);
5546 free(fileindex_path);
5548 unlockerr = lock_worktree(worktree, LOCK_SH);
5549 if (unlockerr && err == NULL)
5554 const struct got_error *
5555 got_worktree_histedit_complete(struct got_worktree *worktree,
5556 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5557 struct got_reference *edited_branch, struct got_repository *repo)
5559 const struct got_error *err, *unlockerr;
5560 struct got_object_id *new_head_commit_id = NULL;
5561 struct got_reference *resolved = NULL;
5563 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5567 err = got_ref_open(&resolved, repo,
5568 got_ref_get_symref_target(edited_branch), 0);
5572 err = got_ref_change_ref(resolved, new_head_commit_id);
5576 err = got_ref_write(resolved, repo);
5580 err = got_worktree_set_head_ref(worktree, resolved);
5584 err = delete_histedit_refs(worktree, repo);
5587 got_fileindex_free(fileindex);
5588 free(new_head_commit_id);
5589 unlockerr = lock_worktree(worktree, LOCK_SH);
5590 if (unlockerr && err == NULL)
5595 const struct got_error *
5596 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5597 struct got_object_id *commit_id, struct got_repository *repo)
5599 const struct got_error *err;
5600 char *commit_ref_name;
5602 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5606 err = store_commit_id(commit_ref_name, commit_id, repo);
5610 err = delete_ref(commit_ref_name, repo);
5612 free(commit_ref_name);
5616 struct check_stage_ok_arg {
5617 struct got_object_id *head_commit_id;
5618 struct got_worktree *worktree;
5619 struct got_fileindex *fileindex;
5620 struct got_repository *repo;
5624 const struct got_error *
5625 check_stage_ok(void *arg, unsigned char status,
5626 unsigned char staged_status, const char *relpath,
5627 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5628 struct got_object_id *commit_id)
5630 struct check_stage_ok_arg *a = arg;
5631 const struct got_error *err = NULL;
5632 struct got_fileindex_entry *ie;
5633 struct got_object_id base_commit_id;
5634 struct got_object_id *base_commit_idp = NULL;
5635 char *in_repo_path = NULL, *p;
5637 if (status == GOT_STATUS_UNVERSIONED)
5639 if (status == GOT_STATUS_NONEXISTENT)
5640 return got_error_set_errno(ENOENT, relpath);
5642 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5644 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5646 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5647 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5649 return got_error_from_errno("asprintf");
5651 if (got_fileindex_entry_has_commit(ie)) {
5652 memcpy(base_commit_id.sha1, ie->commit_sha1,
5653 SHA1_DIGEST_LENGTH);
5654 base_commit_idp = &base_commit_id;
5657 if (status == GOT_STATUS_NO_CHANGE) {
5658 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5660 } else if (status == GOT_STATUS_CONFLICT) {
5661 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5663 } else if (status != GOT_STATUS_ADD &&
5664 status != GOT_STATUS_MODIFY &&
5665 status != GOT_STATUS_DELETE) {
5666 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5670 a->have_changes = 1;
5675 err = check_out_of_date(p, status, staged_status,
5676 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5677 GOT_ERR_STAGE_OUT_OF_DATE);
5683 struct stage_path_arg {
5684 struct got_worktree *worktree;
5685 struct got_fileindex *fileindex;
5686 struct got_repository *repo;
5687 got_worktree_status_cb status_cb;
5689 got_worktree_patch_cb patch_cb;
5693 static const struct got_error *
5694 stage_path(void *arg, unsigned char status,
5695 unsigned char staged_status, const char *relpath,
5696 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5697 struct got_object_id *commit_id)
5699 struct stage_path_arg *a = arg;
5700 const struct got_error *err = NULL;
5701 struct got_fileindex_entry *ie;
5702 char *ondisk_path = NULL, *path_content = NULL;
5704 struct got_object_id *new_staged_blob_id = NULL;
5706 if (status == GOT_STATUS_UNVERSIONED)
5709 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5711 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5713 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5715 return got_error_from_errno("asprintf");
5718 case GOT_STATUS_ADD:
5719 case GOT_STATUS_MODIFY:
5721 if (status == GOT_STATUS_ADD) {
5722 int choice = GOT_PATCH_CHOICE_NONE;
5723 err = (*a->patch_cb)(&choice, a->patch_arg,
5724 status, ie->path, NULL, 1, 1);
5727 if (choice != GOT_PATCH_CHOICE_YES)
5730 err = create_patched_content(&path_content, 0,
5731 staged_blob_id ? staged_blob_id : blob_id,
5732 ondisk_path, ie->path, a->repo,
5733 a->patch_cb, a->patch_arg);
5734 if (err || path_content == NULL)
5738 err = got_object_blob_create(&new_staged_blob_id,
5739 path_content ? path_content : ondisk_path, a->repo);
5742 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5743 SHA1_DIGEST_LENGTH);
5744 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5745 stage = GOT_FILEIDX_STAGE_ADD;
5747 stage = GOT_FILEIDX_STAGE_MODIFY;
5748 got_fileindex_entry_stage_set(ie, stage);
5749 if (a->status_cb == NULL)
5751 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5752 get_staged_status(ie), relpath, blob_id,
5753 new_staged_blob_id, NULL);
5755 case GOT_STATUS_DELETE:
5756 if (staged_status == GOT_STATUS_DELETE)
5759 int choice = GOT_PATCH_CHOICE_NONE;
5760 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5761 ie->path, NULL, 1, 1);
5764 if (choice == GOT_PATCH_CHOICE_NO)
5766 if (choice != GOT_PATCH_CHOICE_YES) {
5767 err = got_error(GOT_ERR_PATCH_CHOICE);
5771 stage = GOT_FILEIDX_STAGE_DELETE;
5772 got_fileindex_entry_stage_set(ie, stage);
5773 if (a->status_cb == NULL)
5775 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5776 get_staged_status(ie), relpath, NULL, NULL, NULL);
5778 case GOT_STATUS_NO_CHANGE:
5779 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5781 case GOT_STATUS_CONFLICT:
5782 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5784 case GOT_STATUS_NONEXISTENT:
5785 err = got_error_set_errno(ENOENT, relpath);
5788 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5792 if (path_content && unlink(path_content) == -1 && err == NULL)
5793 err = got_error_from_errno2("unlink", path_content);
5796 free(new_staged_blob_id);
5800 const struct got_error *
5801 got_worktree_stage(struct got_worktree *worktree,
5802 struct got_pathlist_head *paths,
5803 got_worktree_status_cb status_cb, void *status_arg,
5804 got_worktree_patch_cb patch_cb, void *patch_arg,
5805 struct got_repository *repo)
5807 const struct got_error *err = NULL, *sync_err, *unlockerr;
5808 struct got_pathlist_entry *pe;
5809 struct got_fileindex *fileindex = NULL;
5810 char *fileindex_path = NULL;
5811 struct got_reference *head_ref = NULL;
5812 struct got_object_id *head_commit_id = NULL;
5813 struct check_stage_ok_arg oka;
5814 struct stage_path_arg spa;
5816 err = lock_worktree(worktree, LOCK_EX);
5820 err = got_ref_open(&head_ref, repo,
5821 got_worktree_get_head_ref_name(worktree), 0);
5824 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5827 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5831 /* Check pre-conditions before staging anything. */
5832 oka.head_commit_id = head_commit_id;
5833 oka.worktree = worktree;
5834 oka.fileindex = fileindex;
5836 oka.have_changes = 0;
5837 TAILQ_FOREACH(pe, paths, entry) {
5838 err = worktree_status(worktree, pe->path, fileindex, repo,
5839 check_stage_ok, &oka, NULL, NULL);
5843 if (!oka.have_changes) {
5844 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5848 spa.worktree = worktree;
5849 spa.fileindex = fileindex;
5851 spa.patch_cb = patch_cb;
5852 spa.patch_arg = patch_arg;
5853 spa.status_cb = status_cb;
5854 spa.status_arg = status_arg;
5855 TAILQ_FOREACH(pe, paths, entry) {
5856 err = worktree_status(worktree, pe->path, fileindex, repo,
5857 stage_path, &spa, NULL, NULL);
5862 sync_err = sync_fileindex(fileindex, fileindex_path);
5863 if (sync_err && err == NULL)
5867 got_ref_close(head_ref);
5868 free(head_commit_id);
5869 free(fileindex_path);
5871 got_fileindex_free(fileindex);
5872 unlockerr = lock_worktree(worktree, LOCK_SH);
5873 if (unlockerr && err == NULL)
5878 struct unstage_path_arg {
5879 struct got_worktree *worktree;
5880 struct got_fileindex *fileindex;
5881 struct got_repository *repo;
5882 got_worktree_checkout_cb progress_cb;
5884 got_worktree_patch_cb patch_cb;
5888 static const struct got_error *
5889 create_unstaged_content(char **path_unstaged_content,
5890 char **path_new_staged_content, struct got_object_id *blob_id,
5891 struct got_object_id *staged_blob_id, const char *relpath,
5892 struct got_repository *repo,
5893 got_worktree_patch_cb patch_cb, void *patch_arg)
5895 const struct got_error *err;
5896 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5897 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5898 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5899 struct stat sb1, sb2;
5900 struct got_diff_changes *changes = NULL;
5901 struct got_diff_state *ds = NULL;
5902 struct got_diff_args *args = NULL;
5903 struct got_diff_change *change;
5904 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5905 int have_content = 0, have_rejected_content = 0;
5907 *path_unstaged_content = NULL;
5908 *path_new_staged_content = NULL;
5910 err = got_object_id_str(&label1, blob_id);
5913 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5917 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5921 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5925 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5929 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5933 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5937 if (stat(path1, &sb1) == -1) {
5938 err = got_error_from_errno2("stat", path1);
5942 if (stat(path2, &sb2) == -1) {
5943 err = got_error_from_errno2("stat", path2);
5947 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5948 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5952 err = got_opentemp_named(path_unstaged_content, &outfile,
5953 "got-unstaged-content");
5956 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5957 "got-new-staged-content");
5961 if (fseek(f1, 0L, SEEK_SET) == -1) {
5962 err = got_ferror(f1, GOT_ERR_IO);
5965 if (fseek(f2, 0L, SEEK_SET) == -1) {
5966 err = got_ferror(f2, GOT_ERR_IO);
5969 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5971 err = apply_or_reject_change(&choice, change, ++n,
5972 changes->nchanges, ds, args, diff_flags, relpath,
5973 f1, f2, &line_cur1, &line_cur2,
5974 outfile, rejectfile, patch_cb, patch_arg);
5977 if (choice == GOT_PATCH_CHOICE_YES)
5980 have_rejected_content = 1;
5981 if (choice == GOT_PATCH_CHOICE_QUIT)
5984 if (have_content || have_rejected_content)
5985 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5986 outfile, rejectfile);
5990 got_object_blob_close(blob);
5992 got_object_blob_close(staged_blob);
5993 if (f1 && fclose(f1) == EOF && err == NULL)
5994 err = got_error_from_errno2("fclose", path1);
5995 if (f2 && fclose(f2) == EOF && err == NULL)
5996 err = got_error_from_errno2("fclose", path2);
5997 if (outfile && fclose(outfile) == EOF && err == NULL)
5998 err = got_error_from_errno2("fclose", *path_unstaged_content);
5999 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6000 err = got_error_from_errno2("fclose", *path_new_staged_content);
6001 if (path1 && unlink(path1) == -1 && err == NULL)
6002 err = got_error_from_errno2("unlink", path1);
6003 if (path2 && unlink(path2) == -1 && err == NULL)
6004 err = got_error_from_errno2("unlink", path2);
6005 if (err || !have_content) {
6006 if (*path_unstaged_content &&
6007 unlink(*path_unstaged_content) == -1 && err == NULL)
6008 err = got_error_from_errno2("unlink",
6009 *path_unstaged_content);
6010 free(*path_unstaged_content);
6011 *path_unstaged_content = NULL;
6013 if (err || !have_rejected_content) {
6014 if (*path_new_staged_content &&
6015 unlink(*path_new_staged_content) == -1 && err == NULL)
6016 err = got_error_from_errno2("unlink",
6017 *path_new_staged_content);
6018 free(*path_new_staged_content);
6019 *path_new_staged_content = NULL;
6023 got_diff_state_free(ds);
6027 got_diff_free_changes(changes);
6033 static const struct got_error *
6034 unstage_path(void *arg, unsigned char status,
6035 unsigned char staged_status, const char *relpath,
6036 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6037 struct got_object_id *commit_id)
6039 const struct got_error *err = NULL;
6040 struct unstage_path_arg *a = arg;
6041 struct got_fileindex_entry *ie;
6042 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6043 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6044 char *path_new_staged_content = NULL;
6045 int local_changes_subsumed;
6048 if (staged_status != GOT_STATUS_ADD &&
6049 staged_status != GOT_STATUS_MODIFY &&
6050 staged_status != GOT_STATUS_DELETE)
6053 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6055 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6057 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6059 return got_error_from_errno("asprintf");
6061 switch (staged_status) {
6062 case GOT_STATUS_MODIFY:
6063 err = got_object_open_as_blob(&blob_base, a->repo,
6068 case GOT_STATUS_ADD:
6070 if (staged_status == GOT_STATUS_ADD) {
6071 int choice = GOT_PATCH_CHOICE_NONE;
6072 err = (*a->patch_cb)(&choice, a->patch_arg,
6073 staged_status, ie->path, NULL, 1, 1);
6076 if (choice != GOT_PATCH_CHOICE_YES)
6079 err = create_unstaged_content(
6080 &path_unstaged_content,
6081 &path_new_staged_content, blob_id,
6082 staged_blob_id, ie->path, a->repo,
6083 a->patch_cb, a->patch_arg);
6084 if (err || path_unstaged_content == NULL)
6086 if (path_new_staged_content) {
6087 err = got_object_blob_create(
6089 path_new_staged_content,
6093 memcpy(ie->staged_blob_sha1,
6094 staged_blob_id->sha1,
6095 SHA1_DIGEST_LENGTH);
6097 err = merge_file(&local_changes_subsumed,
6098 a->worktree, blob_base, ondisk_path,
6099 relpath, got_fileindex_perms_to_st(ie),
6100 path_unstaged_content, "unstaged",
6101 a->repo, a->progress_cb, a->progress_arg);
6103 path_new_staged_content == NULL)
6104 got_fileindex_entry_stage_set(ie,
6105 GOT_FILEIDX_STAGE_NONE);
6106 break; /* Done with this file. */
6109 err = got_object_open_as_blob(&blob_staged, a->repo,
6110 staged_blob_id, 8192);
6113 err = merge_blob(&local_changes_subsumed, a->worktree,
6114 blob_base, ondisk_path, relpath,
6115 got_fileindex_perms_to_st(ie), blob_staged,
6116 commit_id ? commit_id : a->worktree->base_commit_id,
6117 a->repo, a->progress_cb, a->progress_arg);
6119 got_fileindex_entry_stage_set(ie,
6120 GOT_FILEIDX_STAGE_NONE);
6122 case GOT_STATUS_DELETE:
6124 int choice = GOT_PATCH_CHOICE_NONE;
6125 err = (*a->patch_cb)(&choice, a->patch_arg,
6126 staged_status, ie->path, NULL, 1, 1);
6129 if (choice == GOT_PATCH_CHOICE_NO)
6131 if (choice != GOT_PATCH_CHOICE_YES) {
6132 err = got_error(GOT_ERR_PATCH_CHOICE);
6136 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6137 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6140 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6145 if (path_unstaged_content &&
6146 unlink(path_unstaged_content) == -1 && err == NULL)
6147 err = got_error_from_errno2("unlink", path_unstaged_content);
6148 if (path_new_staged_content &&
6149 unlink(path_new_staged_content) == -1 && err == NULL)
6150 err = got_error_from_errno2("unlink", path_new_staged_content);
6151 free(path_unstaged_content);
6152 free(path_new_staged_content);
6154 got_object_blob_close(blob_base);
6156 got_object_blob_close(blob_staged);
6160 const struct got_error *
6161 got_worktree_unstage(struct got_worktree *worktree,
6162 struct got_pathlist_head *paths,
6163 got_worktree_checkout_cb progress_cb, void *progress_arg,
6164 got_worktree_patch_cb patch_cb, void *patch_arg,
6165 struct got_repository *repo)
6167 const struct got_error *err = NULL, *sync_err, *unlockerr;
6168 struct got_pathlist_entry *pe;
6169 struct got_fileindex *fileindex = NULL;
6170 char *fileindex_path = NULL;
6171 struct unstage_path_arg upa;
6173 err = lock_worktree(worktree, LOCK_EX);
6177 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6181 upa.worktree = worktree;
6182 upa.fileindex = fileindex;
6184 upa.progress_cb = progress_cb;
6185 upa.progress_arg = progress_arg;
6186 upa.patch_cb = patch_cb;
6187 upa.patch_arg = patch_arg;
6188 TAILQ_FOREACH(pe, paths, entry) {
6189 err = worktree_status(worktree, pe->path, fileindex, repo,
6190 unstage_path, &upa, NULL, NULL);
6195 sync_err = sync_fileindex(fileindex, fileindex_path);
6196 if (sync_err && err == NULL)
6199 free(fileindex_path);
6201 got_fileindex_free(fileindex);
6202 unlockerr = lock_worktree(worktree, LOCK_SH);
6203 if (unlockerr && err == NULL)