2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/limits.h>
19 #include <sys/queue.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
45 #include "got_lib_worktree.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 static const struct got_error *
61 create_meta_file(const char *path_got, const char *name, const char *content)
63 const struct got_error *err = NULL;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
68 err = got_error_prefix_errno("asprintf");
73 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
74 GOT_DEFAULT_FILE_MODE);
76 err = got_error_prefix_errno2("open", path);
81 int len = dprintf(fd, "%s\n", content);
82 if (len != strlen(content) + 1) {
83 err = got_error_prefix_errno("dprintf");
89 if (fd != -1 && close(fd) == -1 && err == NULL)
90 err = got_error_prefix_errno("close");
95 static const struct got_error *
96 update_meta_file(const char *path_got, const char *name, const char *content)
98 const struct got_error *err = NULL;
100 char *tmppath = NULL;
103 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
104 err = got_error_prefix_errno("asprintf");
109 err = got_opentemp_named(&tmppath, &tmpfile, path);
114 int len = fprintf(tmpfile, "%s\n", content);
115 if (len != strlen(content) + 1) {
116 err = got_error_prefix_errno2("fprintf", tmppath);
121 if (rename(tmppath, path) != 0) {
122 err = got_error_prefix_errno3("rename", tmppath, path);
128 if (fclose(tmpfile) != 0 && err == NULL)
129 err = got_error_prefix_errno2("fclose", tmppath);
134 static const struct got_error *
135 read_meta_file(char **content, const char *path_got, const char *name)
137 const struct got_error *err = NULL;
145 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
146 err = got_error_prefix_errno("asprintf");
151 fd = open(path, O_RDONLY | O_NOFOLLOW);
154 err = got_error(GOT_ERR_WORKTREE_META);
156 err = got_error_prefix_errno2("open", path);
159 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
160 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
161 : got_error_prefix_errno2("flock", path));
165 if (lstat(path, &sb) != 0) {
166 err = got_error_prefix_errno2("lstat", path);
169 *content = calloc(1, sb.st_size);
170 if (*content == NULL) {
171 err = got_error_prefix_errno("calloc");
175 n = read(fd, *content, sb.st_size);
176 if (n != sb.st_size) {
177 err = (n == -1 ? got_error_prefix_errno2("read", path) :
178 got_error(GOT_ERR_WORKTREE_META));
181 if ((*content)[sb.st_size - 1] != '\n') {
182 err = got_error(GOT_ERR_WORKTREE_META);
185 (*content)[sb.st_size - 1] = '\0';
188 if (fd != -1 && close(fd) == -1 && err == NULL)
189 err = got_error_prefix_errno2("close", path_got);
198 const struct got_error *
199 got_worktree_init(const char *path, struct got_reference *head_ref,
200 const char *prefix, struct got_repository *repo)
202 const struct got_error *err = NULL;
203 struct got_object_id *commit_id = NULL;
205 uint32_t uuid_status;
207 char *path_got = NULL;
209 char *formatstr = NULL;
210 char *absprefix = NULL;
211 char *basestr = NULL;
212 char *uuidstr = NULL;
214 if (strcmp(path, got_repo_get_path(repo)) == 0) {
215 err = got_error(GOT_ERR_WORKTREE_REPO);
219 err = got_ref_resolve(&commit_id, repo, head_ref);
222 err = got_object_get_type(&obj_type, repo, commit_id);
225 if (obj_type != GOT_OBJ_TYPE_COMMIT)
226 return got_error(GOT_ERR_OBJ_TYPE);
228 if (!got_path_is_absolute(prefix)) {
229 if (asprintf(&absprefix, "/%s", prefix) == -1)
230 return got_error_prefix_errno("asprintf");
233 /* Create top-level directory (may already exist). */
234 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
235 err = got_error_prefix_errno2("mkdir", path);
239 /* Create .got directory (may already exist). */
240 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
241 err = got_error_prefix_errno("asprintf");
244 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
245 err = got_error_prefix_errno2("mkdir", path_got);
249 /* Create an empty lock file. */
250 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
254 /* Create an empty file index. */
255 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
259 /* Write the HEAD reference. */
260 refstr = got_ref_to_str(head_ref);
261 if (refstr == NULL) {
262 err = got_error_prefix_errno("got_ref_to_str");
265 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status);
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status);
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_prefix_errno("asprintf");
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
324 static const struct got_error *
325 open_worktree(struct got_worktree **worktree, const char *path)
327 const struct got_error *err = NULL;
329 char *formatstr = NULL;
330 char *uuidstr = NULL;
331 char *path_lock = NULL;
332 char *base_commit_id_str = NULL;
333 int version, fd = -1;
335 struct got_repository *repo = NULL;
336 uint32_t uuid_status;
340 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
341 err = got_error_prefix_errno("asprintf");
346 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
347 err = got_error_prefix_errno("asprintf");
352 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
354 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
355 : got_error_prefix_errno2("open", path_lock));
359 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
363 version = strtonum(formatstr, 1, INT_MAX, &errstr);
365 err = got_error(GOT_ERR_WORKTREE_META);
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_prefix_errno("calloc");
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = strdup(path);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_prefix_errno("strdup");
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status);
409 err = got_repo_open(&repo, (*worktree)->repo_path);
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
422 got_repo_close(repo);
425 free(base_commit_id_str);
431 if (*worktree != NULL)
432 got_worktree_close(*worktree);
435 (*worktree)->lockfd = fd;
440 const struct got_error *
441 got_worktree_open(struct got_worktree **worktree, const char *path)
443 const struct got_error *err = NULL;
446 err = open_worktree(worktree, path);
447 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
451 path = dirname(path);
453 return got_error_prefix_errno2("dirname", path);
454 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
456 return got_error(GOT_ERR_NOT_WORKTREE);
459 const struct got_error *
460 got_worktree_close(struct got_worktree *worktree)
462 const struct got_error *err = NULL;
463 free(worktree->root_path);
464 free(worktree->repo_path);
465 free(worktree->path_prefix);
466 free(worktree->base_commit_id);
467 free(worktree->head_ref_name);
468 if (worktree->lockfd != -1)
469 if (close(worktree->lockfd) != 0)
470 err = got_error_prefix_errno2("close",
471 got_worktree_get_root_path(worktree));
477 got_worktree_get_root_path(struct got_worktree *worktree)
479 return worktree->root_path;
483 got_worktree_get_repo_path(struct got_worktree *worktree)
485 return worktree->repo_path;
489 got_worktree_get_path_prefix(struct got_worktree *worktree)
491 return worktree->path_prefix;
494 const struct got_error *
495 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
496 const char *path_prefix)
498 char *absprefix = NULL;
500 if (!got_path_is_absolute(path_prefix)) {
501 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
502 return got_error_prefix_errno("asprintf");
504 *match = (strcmp(absprefix ? absprefix : path_prefix,
505 worktree->path_prefix) == 0);
511 got_worktree_get_head_ref_name(struct got_worktree *worktree)
513 return worktree->head_ref_name;
516 struct got_object_id *
517 got_worktree_get_base_commit_id(struct got_worktree *worktree)
519 return worktree->base_commit_id;
522 const struct got_error *
523 got_worktree_set_base_commit_id(struct got_worktree *worktree,
524 struct got_repository *repo, struct got_object_id *commit_id)
526 const struct got_error *err;
527 struct got_object *obj = NULL;
529 char *path_got = NULL;
531 if (asprintf(&path_got, "%s/%s", worktree->root_path,
532 GOT_WORKTREE_GOT_DIR) == -1) {
533 err = got_error_prefix_errno("asprintf");
538 err = got_object_open(&obj, repo, commit_id);
542 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
543 err = got_error(GOT_ERR_OBJ_TYPE);
547 /* Record our base commit. */
548 err = got_object_id_str(&id_str, commit_id);
551 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
555 free(worktree->base_commit_id);
556 worktree->base_commit_id = got_object_id_dup(commit_id);
557 if (worktree->base_commit_id == NULL) {
558 err = got_error_prefix_errno("got_object_id_dup");
563 got_object_close(obj);
569 static const struct got_error *
570 lock_worktree(struct got_worktree *worktree, int operation)
572 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
573 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
574 : got_error_prefix_errno2("flock",
575 got_worktree_get_root_path(worktree)));
579 static const struct got_error *
580 add_dir_on_disk(struct got_worktree *worktree, const char *path)
582 const struct got_error *err = NULL;
585 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
586 return got_error_prefix_errno("asprintf");
588 err = got_path_mkdir(abspath);
589 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
592 if (lstat(abspath, &sb) == -1) {
593 err = got_error_prefix_errno2("lstat", abspath);
594 } else if (!S_ISDIR(sb.st_mode)) {
595 /* TODO directory is obstructed; do something */
596 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
603 static const struct got_error *
604 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
606 const struct got_error *err = NULL;
609 size_t flen1 = 0, flen2 = 0;
614 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
615 if (flen1 == 0 && ferror(f1)) {
616 err = got_error_prefix_errno("fread");
619 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
620 if (flen2 == 0 && ferror(f2)) {
621 err = got_error_prefix_errno("fread");
628 } else if (flen2 == 0) {
632 } else if (flen1 == flen2) {
633 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
646 static const struct got_error *
647 check_files_equal(int *same, const char *f1_path, const char *f2_path)
649 const struct got_error *err = NULL;
652 FILE *f1 = NULL, *f2 = NULL;
656 if (lstat(f1_path, &sb) != 0) {
657 err = got_error_prefix_errno2("lstat", f1_path);
662 if (lstat(f2_path, &sb) != 0) {
663 err = got_error_prefix_errno2("lstat", f2_path);
668 if (size1 != size2) {
673 f1 = fopen(f1_path, "r");
675 return got_error_prefix_errno2("open", f1_path);
677 f2 = fopen(f2_path, "r");
679 err = got_error_prefix_errno2("open", f2_path);
683 err = check_file_contents_equal(same, f1, f2);
685 if (f1 && fclose(f1) != 0 && err == NULL)
686 err = got_error_prefix_errno("fclose");
687 if (f2 && fclose(f2) != 0 && err == NULL)
688 err = got_error_prefix_errno("fclose");
694 * Perform a 3-way merge where the file's version in the file index (blob2)
695 * acts as the common ancestor, the incoming blob (blob1) acts as the first
696 * derived version, and the file on disk acts as the second derived version.
698 static const struct got_error *
699 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
700 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
701 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
702 struct got_repository *repo,
703 got_worktree_checkout_cb progress_cb, void *progress_arg)
705 const struct got_error *err = NULL;
707 struct got_blob_object *blob2 = NULL;
708 FILE *f1 = NULL, *f2 = NULL;
709 char *blob1_path = NULL, *blob2_path = NULL;
710 char *merged_path = NULL, *base_path = NULL;
713 int overlapcnt = 0, update_timestamps = 0;
716 parent = dirname(ondisk_path);
718 return got_error_prefix_errno2("dirname", ondisk_path);
720 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
721 return got_error_prefix_errno("asprintf");
723 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
728 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
729 err = got_error_prefix_errno("asprintf");
734 err = got_opentemp_named(&blob1_path, &f1, base_path);
737 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
742 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
743 err = got_error_prefix_errno("asprintf");
748 err = got_opentemp_named(&blob2_path, &f2, base_path);
751 if (got_fileindex_entry_has_blob(ie)) {
752 struct got_object_id id2;
753 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
754 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
757 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
762 * If the file has no blob, this is an "add vs add" conflict,
763 * and we simply use an empty ancestor file to make both files
764 * appear in the merged result in their entirety.
768 err = got_object_id_str(&id_str, worktree->base_commit_id);
771 if (asprintf(&label1, "commit %s", id_str) == -1) {
772 err = got_error_prefix_errno("asprintf");
776 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
777 blob2_path, ondisk_path, label1, path);
781 (*progress_cb)(progress_arg,
782 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
784 if (fsync(merged_fd) != 0) {
785 err = got_error_prefix_errno("fsync");
789 /* Check if a clean merge has subsumed all local changes. */
790 if (overlapcnt == 0) {
791 err = check_files_equal(&update_timestamps, blob1_path,
797 if (chmod(merged_path, st_mode) != 0) {
798 err = got_error_prefix_errno2("chmod", merged_path);
802 if (rename(merged_path, ondisk_path) != 0) {
803 err = got_error_prefix_errno3("rename", merged_path,
810 * Do not update timestamps of already modified files. Otherwise,
811 * a future status walk would treat them as unmodified files again.
813 err = got_fileindex_entry_update(ie, ondisk_path,
814 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
816 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
817 err = got_error_prefix_errno("close");
818 if (f1 && fclose(f1) != 0 && err == NULL)
819 err = got_error_prefix_errno("fclose");
820 if (f2 && fclose(f2) != 0 && err == NULL)
821 err = got_error_prefix_errno("fclose");
823 got_object_blob_close(blob2);
839 static const struct got_error *
840 update_blob_fileindex_entry(struct got_worktree *worktree,
841 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
842 const char *ondisk_path, const char *path, struct got_blob_object *blob,
843 int update_timestamps)
845 const struct got_error *err = NULL;
848 ie = got_fileindex_entry_get(fileindex, path);
850 err = got_fileindex_entry_update(ie, ondisk_path,
851 blob->id.sha1, worktree->base_commit_id->sha1,
854 struct got_fileindex_entry *new_ie;
855 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
856 path, blob->id.sha1, worktree->base_commit_id->sha1);
858 err = got_fileindex_entry_add(fileindex, new_ie);
863 static const struct got_error *
864 install_blob(struct got_worktree *worktree, const char *ondisk_path,
865 const char *path, uint16_t te_mode, uint16_t st_mode,
866 struct got_blob_object *blob, int restoring_missing_file,
867 int reverting_versioned_file, struct got_repository *repo,
868 got_worktree_checkout_cb progress_cb, void *progress_arg)
870 const struct got_error *err = NULL;
874 char *tmppath = NULL;
876 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
877 GOT_DEFAULT_FILE_MODE);
879 if (errno == ENOENT) {
880 char *parent = dirname(path);
882 return got_error_prefix_errno2("dirname", path);
883 err = add_dir_on_disk(worktree, parent);
886 fd = open(ondisk_path,
887 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
888 GOT_DEFAULT_FILE_MODE);
890 return got_error_prefix_errno2("open",
892 } else if (errno == EEXIST) {
893 if (!S_ISREG(st_mode)) {
894 /* TODO file is obstructed; do something */
895 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
898 err = got_opentemp_named_fd(&tmppath, &fd,
905 return got_error_prefix_errno2("open", ondisk_path);
908 if (restoring_missing_file)
909 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
910 else if (reverting_versioned_file)
911 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
913 (*progress_cb)(progress_arg,
914 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
916 hdrlen = got_object_blob_get_hdrlen(blob);
918 const uint8_t *buf = got_object_blob_get_read_buf(blob);
919 err = got_object_blob_read_block(&len, blob);
923 /* Skip blob object header first time around. */
924 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
926 err = got_error_prefix_errno("write");
928 } else if (outlen != len - hdrlen) {
929 err = got_error(GOT_ERR_IO);
936 if (fsync(fd) != 0) {
937 err = got_error_prefix_errno("fsync");
942 if (rename(tmppath, ondisk_path) != 0) {
943 err = got_error_prefix_errno3("rename", tmppath,
950 if (te_mode & S_IXUSR) {
951 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
952 err = got_error_prefix_errno2("chmod", ondisk_path);
956 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
957 err = got_error_prefix_errno2("chmod", ondisk_path);
963 if (fd != -1 && close(fd) != 0 && err == NULL)
964 err = got_error_prefix_errno("close");
969 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
970 static const struct got_error *
971 get_modified_file_content_status(unsigned char *status, FILE *f)
973 const struct got_error *err = NULL;
974 const char *markers[3] = {
975 GOT_DIFF_CONFLICT_MARKER_BEGIN,
976 GOT_DIFF_CONFLICT_MARKER_SEP,
977 GOT_DIFF_CONFLICT_MARKER_END
982 const char delim[3] = {'\0', '\0', '\0'};
984 while (*status == GOT_STATUS_MODIFY) {
985 line = fparseln(f, &len, NULL, delim, 0);
989 err = got_ferror(f, GOT_ERR_IO);
993 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
994 if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
995 *status = GOT_STATUS_CONFLICT;
1004 static const struct got_error *
1005 get_file_status(unsigned char *status, struct stat *sb,
1006 struct got_fileindex_entry *ie, const char *abspath,
1007 struct got_repository *repo)
1009 const struct got_error *err = NULL;
1010 struct got_object_id id;
1014 struct got_blob_object *blob = NULL;
1017 *status = GOT_STATUS_NO_CHANGE;
1019 if (lstat(abspath, sb) == -1) {
1020 if (errno == ENOENT) {
1022 if (got_fileindex_entry_has_file_on_disk(ie))
1023 *status = GOT_STATUS_MISSING;
1025 *status = GOT_STATUS_DELETE;
1027 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1028 & (S_IRWXU | S_IRWXG | S_IRWXO));
1030 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1033 return got_error_prefix_errno2("lstat", abspath);
1036 if (!S_ISREG(sb->st_mode)) {
1037 *status = GOT_STATUS_OBSTRUCTED;
1044 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1045 *status = GOT_STATUS_DELETE;
1047 } else if (!got_fileindex_entry_has_blob(ie)) {
1048 *status = GOT_STATUS_ADD;
1052 if (ie->ctime_sec == sb->st_ctime &&
1053 ie->ctime_nsec == sb->st_ctimensec &&
1054 ie->mtime_sec == sb->st_mtime &&
1055 ie->mtime_sec == sb->st_mtime &&
1056 ie->mtime_nsec == sb->st_mtimensec &&
1057 ie->size == (sb->st_size & 0xffffffff))
1060 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1061 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1065 f = fopen(abspath, "r");
1067 err = got_error_prefix_errno2("fopen", abspath);
1070 hdrlen = got_object_blob_get_hdrlen(blob);
1072 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1073 err = got_object_blob_read_block(&blen, blob);
1076 /* Skip length of blob object header first time around. */
1077 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1078 if (flen == 0 && ferror(f)) {
1079 err = got_error_prefix_errno("fread");
1084 *status = GOT_STATUS_MODIFY;
1086 } else if (flen == 0) {
1088 *status = GOT_STATUS_MODIFY;
1090 } else if (blen - hdrlen == flen) {
1091 /* Skip blob object header first time around. */
1092 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1093 *status = GOT_STATUS_MODIFY;
1097 *status = GOT_STATUS_MODIFY;
1103 if (*status == GOT_STATUS_MODIFY) {
1105 err = get_modified_file_content_status(status, f);
1109 got_object_blob_close(blob);
1115 static const struct got_error *
1116 update_blob(struct got_worktree *worktree,
1117 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1118 struct got_tree_entry *te, const char *path,
1119 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1122 const struct got_error *err = NULL;
1123 struct got_blob_object *blob = NULL;
1125 unsigned char status = GOT_STATUS_NO_CHANGE;
1128 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1129 return got_error_prefix_errno("asprintf");
1131 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1135 if (status == GOT_STATUS_OBSTRUCTED) {
1136 (*progress_cb)(progress_arg, status, path);
1140 if (ie && status != GOT_STATUS_MISSING) {
1141 if (got_fileindex_entry_has_commit(ie) &&
1142 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1143 SHA1_DIGEST_LENGTH) == 0) {
1144 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1148 if (got_fileindex_entry_has_blob(ie) &&
1149 memcmp(ie->blob_sha1, te->id->sha1,
1150 SHA1_DIGEST_LENGTH) == 0)
1154 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1158 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1159 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1160 te->mode, sb.st_mode, blob, repo, progress_cb,
1162 else if (status == GOT_STATUS_DELETE) {
1163 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1164 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1165 ondisk_path, path, blob, 0);
1169 err = install_blob(worktree, ondisk_path, path, te->mode,
1170 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1171 repo, progress_cb, progress_arg);
1174 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1175 ondisk_path, path, blob, 1);
1179 got_object_blob_close(blob);
1185 static const struct got_error *
1186 remove_ondisk_file(const char *root_path, const char *path)
1188 const struct got_error *err = NULL;
1189 char *ondisk_path = NULL;
1191 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1192 return got_error_prefix_errno("asprintf");
1194 if (unlink(ondisk_path) == -1) {
1195 if (errno != ENOENT)
1196 err = got_error_prefix_errno2("unlink", ondisk_path);
1198 char *parent = dirname(ondisk_path);
1199 while (parent && strcmp(parent, root_path) != 0) {
1200 if (rmdir(parent) == -1) {
1201 if (errno != ENOTEMPTY)
1202 err = got_error_prefix_errno2("rmdir",
1206 parent = dirname(parent);
1213 static const struct got_error *
1214 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1215 struct got_fileindex_entry *ie, const char *parent_path,
1216 struct got_repository *repo,
1217 got_worktree_checkout_cb progress_cb, void *progress_arg)
1219 const struct got_error *err = NULL;
1220 unsigned char status;
1224 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1226 return got_error_prefix_errno("asprintf");
1228 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1232 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1233 status == GOT_STATUS_ADD) {
1234 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1236 * Preserve the working file and change the deleted blob's
1237 * entry into a schedule-add entry.
1239 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1244 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1245 if (status == GOT_STATUS_NO_CHANGE) {
1246 err = remove_ondisk_file(worktree->root_path, ie->path);
1250 got_fileindex_entry_remove(fileindex, ie);
1256 struct diff_cb_arg {
1257 struct got_fileindex *fileindex;
1258 struct got_worktree *worktree;
1259 struct got_repository *repo;
1260 got_worktree_checkout_cb progress_cb;
1262 got_worktree_cancel_cb cancel_cb;
1266 static const struct got_error *
1267 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1268 struct got_tree_entry *te, const char *parent_path)
1270 struct diff_cb_arg *a = arg;
1272 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1273 return got_error(GOT_ERR_CANCELLED);
1275 return update_blob(a->worktree, a->fileindex, ie, te,
1276 ie->path, a->repo, a->progress_cb, a->progress_arg);
1279 static const struct got_error *
1280 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1282 struct diff_cb_arg *a = arg;
1284 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1285 return got_error(GOT_ERR_CANCELLED);
1287 return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1288 a->repo, a->progress_cb, a->progress_arg);
1291 static const struct got_error *
1292 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1294 struct diff_cb_arg *a = arg;
1295 const struct got_error *err;
1298 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1299 return got_error(GOT_ERR_CANCELLED);
1301 if (asprintf(&path, "%s%s%s", parent_path,
1302 parent_path[0] ? "/" : "", te->name)
1304 return got_error_prefix_errno("asprintf");
1306 if (S_ISDIR(te->mode))
1307 err = add_dir_on_disk(a->worktree, path);
1309 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1310 a->repo, a->progress_cb, a->progress_arg);
1316 const struct got_error *
1317 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1319 const struct got_error *err = NULL;
1320 char *uuidstr = NULL;
1321 uint32_t uuid_status;
1325 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1326 if (uuid_status != uuid_s_ok)
1327 return got_error_uuid(uuid_status);
1329 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1331 err = got_error_prefix_errno("asprintf");
1339 * Prevent Git's garbage collector from deleting our base commit by
1340 * setting a reference to our base commit's ID.
1342 static const struct got_error *
1343 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1345 const struct got_error *err = NULL;
1346 struct got_reference *ref = NULL;
1349 err = got_worktree_get_base_ref_name(&refname, worktree);
1353 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1357 err = got_ref_write(ref, repo);
1365 static const struct got_error *
1366 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1367 struct got_worktree *worktree)
1369 const struct got_error *err = NULL;
1372 *fileindex_path = NULL;
1373 *fileindex = got_fileindex_alloc();
1374 if (*fileindex == NULL)
1375 return got_error_prefix_errno("got_fileindex_alloc");
1377 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1378 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1379 err = got_error_prefix_errno("asprintf");
1380 *fileindex_path = NULL;
1384 index = fopen(*fileindex_path, "rb");
1385 if (index == NULL) {
1386 if (errno != ENOENT)
1387 err = got_error_prefix_errno2("fopen", *fileindex_path);
1389 err = got_fileindex_read(*fileindex, index);
1390 if (fclose(index) != 0 && err == NULL)
1391 err = got_error_prefix_errno("fclose");
1395 free(*fileindex_path);
1396 *fileindex_path = NULL;
1403 const struct got_error *
1404 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1405 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1406 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1408 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1409 struct got_commit_object *commit = NULL;
1410 struct got_object_id *tree_id = NULL;
1411 struct got_tree_object *tree = NULL;
1412 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1413 struct got_fileindex *fileindex = NULL;
1414 FILE *new_index = NULL;
1415 struct got_fileindex_diff_tree_cb diff_cb;
1416 struct diff_cb_arg arg;
1417 char *relpath = NULL, *entry_name = NULL;
1419 err = lock_worktree(worktree, LOCK_EX);
1424 * Read the file index.
1425 * Checking out files is supposed to be an idempotent operation.
1426 * If the on-disk file index is incomplete we will try to complete it.
1428 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1432 err = got_opentemp_named(&new_fileindex_path, &new_index,
1437 err = ref_base_commit(worktree, repo);
1441 err = got_object_open_as_commit(&commit, repo,
1442 worktree->base_commit_id);
1449 relpath = strdup(path);
1450 if (relpath == NULL) {
1451 err = got_error_prefix_errno("strdup");
1454 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1455 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1457 err = got_error_prefix_errno("asprintf");
1460 err = got_object_id_by_path(&tree_id, repo,
1461 worktree->base_commit_id, tree_path);
1465 err = got_object_get_type(&obj_type, repo, tree_id);
1468 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1469 /* Split provided path into parent dir + entry name. */
1470 if (strchr(path, '/') == NULL) {
1471 relpath = strdup("");
1472 if (relpath == NULL) {
1473 err = got_error_prefix_errno("strdup");
1476 tree_path = strdup(worktree->path_prefix);
1477 if (tree_path == NULL) {
1478 err = got_error_prefix_errno("strdup");
1482 err = got_path_dirname(&relpath, path);
1485 if (asprintf(&tree_path, "%s%s%s",
1486 worktree->path_prefix,
1487 got_path_is_root_dir(
1488 worktree->path_prefix) ? "" : "/",
1490 err = got_error_prefix_errno("asprintf");
1494 err = got_object_id_by_path(&tree_id, repo,
1495 worktree->base_commit_id, tree_path);
1499 entry_name = basename(path);
1500 if (entry_name == NULL) {
1501 err = got_error_prefix_errno2("basename", path);
1506 relpath = strdup("");
1507 if (relpath == NULL) {
1508 err = got_error_prefix_errno("strdup");
1511 err = got_object_id_by_path(&tree_id, repo,
1512 worktree->base_commit_id, worktree->path_prefix);
1517 err = got_object_open_as_tree(&tree, repo, tree_id);
1522 got_object_tree_find_entry(tree, entry_name) == NULL) {
1523 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1527 diff_cb.diff_old_new = diff_old_new;
1528 diff_cb.diff_old = diff_old;
1529 diff_cb.diff_new = diff_new;
1530 arg.fileindex = fileindex;
1531 arg.worktree = worktree;
1533 arg.progress_cb = progress_cb;
1534 arg.progress_arg = progress_arg;
1535 arg.cancel_cb = cancel_cb;
1536 arg.cancel_arg = cancel_arg;
1537 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1538 entry_name, repo, &diff_cb, &arg);
1540 /* Try to sync the fileindex back to disk in any case. */
1541 err = got_fileindex_write(fileindex, new_index);
1545 if (rename(new_fileindex_path, fileindex_path) != 0) {
1546 err = got_error_prefix_errno3("rename", new_fileindex_path,
1548 unlink(new_fileindex_path);
1552 free(new_fileindex_path);
1553 new_fileindex_path = NULL;
1558 got_object_tree_close(tree);
1560 got_object_commit_close(commit);
1561 if (new_fileindex_path)
1562 unlink(new_fileindex_path);
1565 free(new_fileindex_path);
1566 free(fileindex_path);
1567 got_fileindex_free(fileindex);
1570 unlockerr = lock_worktree(worktree, LOCK_SH);
1571 if (unlockerr && err == NULL)
1576 struct diff_dir_cb_arg {
1577 struct got_fileindex *fileindex;
1578 struct got_worktree *worktree;
1579 const char *status_path;
1580 size_t status_path_len;
1581 struct got_repository *repo;
1582 got_worktree_status_cb status_cb;
1584 got_worktree_cancel_cb cancel_cb;
1588 static const struct got_error *
1589 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1590 got_worktree_status_cb status_cb, void *status_arg,
1591 struct got_repository *repo)
1593 const struct got_error *err = NULL;
1594 unsigned char status = GOT_STATUS_NO_CHANGE;
1596 struct got_object_id id;
1598 err = get_file_status(&status, &sb, ie, abspath, repo);
1599 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1600 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1601 err = (*status_cb)(status_arg, status, ie->path, &id);
1606 static const struct got_error *
1607 status_old_new(void *arg, struct got_fileindex_entry *ie,
1608 struct dirent *de, const char *parent_path)
1610 const struct got_error *err = NULL;
1611 struct diff_dir_cb_arg *a = arg;
1614 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1615 return got_error(GOT_ERR_CANCELLED);
1617 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1618 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1621 if (parent_path[0]) {
1622 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1623 parent_path, de->d_name) == -1)
1624 return got_error_prefix_errno("asprintf");
1626 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1628 return got_error_prefix_errno("asprintf");
1631 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1637 static const struct got_error *
1638 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1640 struct diff_dir_cb_arg *a = arg;
1641 struct got_object_id id;
1642 unsigned char status;
1644 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1645 return got_error(GOT_ERR_CANCELLED);
1647 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1650 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1651 if (got_fileindex_entry_has_file_on_disk(ie))
1652 status = GOT_STATUS_MISSING;
1654 status = GOT_STATUS_DELETE;
1655 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1658 static const struct got_error *
1659 status_new(void *arg, struct dirent *de, const char *parent_path)
1661 const struct got_error *err = NULL;
1662 struct diff_dir_cb_arg *a = arg;
1665 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1666 return got_error(GOT_ERR_CANCELLED);
1668 if (de->d_type == DT_DIR)
1671 /* XXX ignore symlinks for now */
1672 if (de->d_type == DT_LNK)
1675 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1678 if (parent_path[0]) {
1679 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1680 return got_error_prefix_errno("asprintf");
1685 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1692 const struct got_error *
1693 got_worktree_status(struct got_worktree *worktree, const char *path,
1694 struct got_repository *repo, got_worktree_status_cb status_cb,
1695 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1697 const struct got_error *err = NULL;
1698 DIR *workdir = NULL;
1699 char *fileindex_path = NULL;
1700 struct got_fileindex *fileindex = NULL;
1702 struct got_fileindex_diff_dir_cb fdiff_cb;
1703 struct diff_dir_cb_arg arg;
1704 char *ondisk_path = NULL;
1706 fileindex = got_fileindex_alloc();
1707 if (fileindex == NULL) {
1708 err = got_error_prefix_errno("got_fileindex_alloc");
1712 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1713 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1714 err = got_error_prefix_errno("asprintf");
1715 fileindex_path = NULL;
1719 index = fopen(fileindex_path, "rb");
1720 if (index == NULL) {
1721 if (errno != ENOENT) {
1722 err = got_error_prefix_errno2("fopen", fileindex_path);
1726 err = got_fileindex_read(fileindex, index);
1732 if (asprintf(&ondisk_path, "%s%s%s",
1733 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1734 err = got_error_prefix_errno("asprintf");
1737 workdir = opendir(ondisk_path);
1738 if (workdir == NULL) {
1739 if (errno == ENOTDIR || errno == ENOENT) {
1740 struct got_fileindex_entry *ie;
1741 ie = got_fileindex_entry_get(fileindex, path);
1743 err = got_error(GOT_ERR_BAD_PATH);
1746 err = report_file_status(ie, ondisk_path,
1747 status_cb, status_arg, repo);
1750 err = got_error_prefix_errno2("opendir", ondisk_path);
1754 fdiff_cb.diff_old_new = status_old_new;
1755 fdiff_cb.diff_old = status_old;
1756 fdiff_cb.diff_new = status_new;
1757 arg.fileindex = fileindex;
1758 arg.worktree = worktree;
1759 arg.status_path = path;
1760 arg.status_path_len = strlen(path);
1762 arg.status_cb = status_cb;
1763 arg.status_arg = status_arg;
1764 arg.cancel_cb = cancel_cb;
1765 arg.cancel_arg = cancel_arg;
1766 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1767 path, repo, &fdiff_cb, &arg);
1772 free(fileindex_path);
1773 got_fileindex_free(fileindex);
1777 const struct got_error *
1778 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1781 const struct got_error *err = NULL;
1782 char *resolved, *path = NULL;
1787 resolved = realpath(arg, NULL);
1788 if (resolved == NULL)
1789 return got_error_prefix_errno2("realpath", arg);
1791 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1792 strlen(got_worktree_get_root_path(worktree)))) {
1793 err = got_error(GOT_ERR_BAD_PATH);
1797 path = strdup(resolved +
1798 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1800 err = got_error_prefix_errno("strdup");
1804 /* XXX status walk can't deal with trailing slash! */
1806 while (path[len - 1] == '/') {
1807 path[len - 1] = '\0';
1819 const struct got_error *
1820 got_worktree_schedule_add(struct got_worktree *worktree,
1821 struct got_pathlist_head *ondisk_paths,
1822 got_worktree_status_cb status_cb, void *status_arg,
1823 struct got_repository *repo)
1825 struct got_fileindex *fileindex = NULL;
1826 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1827 FILE *index = NULL, *new_index = NULL;
1828 const struct got_error *err = NULL, *unlockerr = NULL;
1829 struct got_pathlist_entry *pe;
1831 err = lock_worktree(worktree, LOCK_EX);
1836 fileindex = got_fileindex_alloc();
1837 if (fileindex == NULL) {
1838 err = got_error_prefix_errno("got_fileindex_alloc");
1842 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1843 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1844 err = got_error_prefix_errno("asprintf");
1845 fileindex_path = NULL;
1849 index = fopen(fileindex_path, "rb");
1850 if (index == NULL) {
1851 err = got_error_prefix_errno2("fopen", fileindex_path);
1855 err = got_fileindex_read(fileindex, index);
1859 TAILQ_FOREACH(pe, ondisk_paths, entry) {
1860 struct got_fileindex_entry *ie = NULL;
1863 err = got_path_skip_common_ancestor(&relpath,
1864 got_worktree_get_root_path(worktree), pe->path);
1868 /* Re-adding an existing entry is a no-op. */
1869 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
1872 err = got_fileindex_entry_alloc(&ie, pe->path, relpath,
1878 err = got_fileindex_entry_add(fileindex, ie);
1880 got_fileindex_entry_free(ie);
1884 err = report_file_status(ie, pe->path, status_cb, status_arg,
1890 err = got_opentemp_named(&new_fileindex_path, &new_index,
1895 err = got_fileindex_write(fileindex, new_index);
1899 if (rename(new_fileindex_path, fileindex_path) != 0) {
1900 err = got_error_prefix_errno3("rename", new_fileindex_path,
1905 free(new_fileindex_path);
1906 new_fileindex_path = NULL;
1910 if (fclose(index) != 0 && err == NULL)
1911 err = got_error_prefix_errno("fclose");
1913 if (new_fileindex_path) {
1914 if (unlink(new_fileindex_path) != 0 && err == NULL)
1915 err = got_error_prefix_errno2("unlink",
1916 new_fileindex_path);
1917 free(new_fileindex_path);
1920 got_fileindex_free(fileindex);
1921 unlockerr = lock_worktree(worktree, LOCK_SH);
1922 if (unlockerr && err == NULL)
1927 const struct got_error *
1928 got_worktree_schedule_delete(struct got_worktree *worktree,
1929 const char *ondisk_path, int delete_local_mods,
1930 got_worktree_status_cb status_cb, void *status_arg,
1931 struct got_repository *repo)
1933 struct got_fileindex *fileindex = NULL;
1934 struct got_fileindex_entry *ie = NULL;
1935 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1936 FILE *index = NULL, *new_index = NULL;
1937 const struct got_error *err = NULL, *unlockerr = NULL;
1938 unsigned char status;
1941 err = lock_worktree(worktree, LOCK_EX);
1945 err = got_path_skip_common_ancestor(&relpath,
1946 got_worktree_get_root_path(worktree), ondisk_path);
1950 fileindex = got_fileindex_alloc();
1951 if (fileindex == NULL) {
1952 err = got_error_prefix_errno("got_fileindex_alloc");
1956 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1957 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1958 err = got_error_prefix_errno("asprintf");
1959 fileindex_path = NULL;
1963 index = fopen(fileindex_path, "rb");
1964 if (index == NULL) {
1965 err = got_error_prefix_errno2("fopen", fileindex_path);
1969 err = got_fileindex_read(fileindex, index);
1973 ie = got_fileindex_entry_get(fileindex, relpath);
1975 err = got_error(GOT_ERR_BAD_PATH);
1979 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1983 if (status != GOT_STATUS_NO_CHANGE) {
1984 if (status == GOT_STATUS_DELETE) {
1985 err = got_error_set_errno(ENOENT, ondisk_path);
1988 if (status != GOT_STATUS_MODIFY) {
1989 err = got_error(GOT_ERR_FILE_STATUS);
1992 if (!delete_local_mods) {
1993 err = got_error(GOT_ERR_FILE_MODIFIED);
1998 if (unlink(ondisk_path) != 0) {
1999 err = got_error_prefix_errno2("unlink", ondisk_path);
2003 got_fileindex_entry_mark_deleted_from_disk(ie);
2005 err = got_opentemp_named(&new_fileindex_path, &new_index,
2010 err = got_fileindex_write(fileindex, new_index);
2014 if (rename(new_fileindex_path, fileindex_path) != 0) {
2015 err = got_error_prefix_errno3("rename", new_fileindex_path,
2020 free(new_fileindex_path);
2021 new_fileindex_path = NULL;
2023 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2027 if (fclose(index) != 0 && err == NULL)
2028 err = got_error_prefix_errno("fclose");
2030 if (new_fileindex_path) {
2031 if (unlink(new_fileindex_path) != 0 && err == NULL)
2032 err = got_error_prefix_errno2("unlink",
2033 new_fileindex_path);
2034 free(new_fileindex_path);
2037 got_fileindex_free(fileindex);
2038 unlockerr = lock_worktree(worktree, LOCK_SH);
2039 if (unlockerr && err == NULL)
2044 const struct got_error *
2045 got_worktree_revert(struct got_worktree *worktree,
2046 const char *ondisk_path,
2047 got_worktree_checkout_cb progress_cb, void *progress_arg,
2048 struct got_repository *repo)
2050 struct got_fileindex *fileindex = NULL;
2051 struct got_fileindex_entry *ie = NULL;
2052 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2053 char *tree_path = NULL, *parent_path, *te_name;
2054 FILE *index = NULL, *new_index = NULL;
2055 const struct got_error *err = NULL, *unlockerr = NULL;
2056 struct got_tree_object *tree = NULL;
2057 struct got_object_id id, *tree_id = NULL;
2058 const struct got_tree_entry *te;
2059 struct got_blob_object *blob = NULL;
2060 unsigned char status;
2063 err = lock_worktree(worktree, LOCK_EX);
2067 err = got_path_skip_common_ancestor(&relpath,
2068 got_worktree_get_root_path(worktree), ondisk_path);
2072 fileindex = got_fileindex_alloc();
2073 if (fileindex == NULL) {
2074 err = got_error_prefix_errno("got_fileindex_alloc");
2078 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2079 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2080 err = got_error_prefix_errno("asprintf");
2081 fileindex_path = NULL;
2085 index = fopen(fileindex_path, "rb");
2086 if (index == NULL) {
2087 err = got_error_prefix_errno2("fopen", fileindex_path);
2091 err = got_fileindex_read(fileindex, index);
2095 ie = got_fileindex_entry_get(fileindex, relpath);
2097 err = got_error(GOT_ERR_BAD_PATH);
2101 /* Construct in-repository path of tree which contains this blob. */
2102 err = got_path_dirname(&parent_path, ie->path);
2104 if (err->code != GOT_ERR_BAD_PATH)
2108 if (got_path_is_root_dir(worktree->path_prefix)) {
2109 tree_path = strdup(parent_path);
2110 if (tree_path == NULL) {
2111 err = got_error_prefix_errno("strdup");
2115 if (got_path_is_root_dir(parent_path)) {
2116 tree_path = strdup(worktree->path_prefix);
2117 if (tree_path == NULL) {
2118 err = got_error_prefix_errno("strdup");
2122 if (asprintf(&tree_path, "%s/%s",
2123 worktree->path_prefix, parent_path) == -1) {
2124 err = got_error_prefix_errno("asprintf");
2130 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2135 err = got_object_open_as_tree(&tree, repo, tree_id);
2139 te_name = basename(ie->path);
2140 if (te_name == NULL) {
2141 err = got_error_prefix_errno2("basename", ie->path);
2145 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2149 te = got_object_tree_find_entry(tree, te_name);
2150 if (te == NULL && status != GOT_STATUS_ADD) {
2151 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2156 case GOT_STATUS_ADD:
2157 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2158 got_fileindex_entry_remove(fileindex, ie);
2160 case GOT_STATUS_DELETE:
2161 case GOT_STATUS_MODIFY:
2162 case GOT_STATUS_CONFLICT:
2163 case GOT_STATUS_MISSING:
2164 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2165 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2168 err = install_blob(worktree, ondisk_path, ie->path,
2169 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2173 if (status == GOT_STATUS_DELETE) {
2174 err = update_blob_fileindex_entry(worktree,
2175 fileindex, ie, ondisk_path, ie->path, blob, 1);
2184 err = got_opentemp_named(&new_fileindex_path, &new_index,
2189 err = got_fileindex_write(fileindex, new_index);
2193 if (rename(new_fileindex_path, fileindex_path) != 0) {
2194 err = got_error_prefix_errno3("rename", new_fileindex_path,
2199 free(new_fileindex_path);
2200 new_fileindex_path = NULL;
2205 got_object_blob_close(blob);
2207 got_object_tree_close(tree);
2210 if (fclose(index) != 0 && err == NULL)
2211 err = got_error_prefix_errno("fclose");
2213 if (new_fileindex_path) {
2214 if (unlink(new_fileindex_path) != 0 && err == NULL)
2215 err = got_error_prefix_errno2("unlink",
2216 new_fileindex_path);
2217 free(new_fileindex_path);
2220 got_fileindex_free(fileindex);
2221 unlockerr = lock_worktree(worktree, LOCK_SH);
2222 if (unlockerr && err == NULL)
2231 unsigned char status;
2232 struct got_object_id *blob_id;
2233 struct got_object_id *base_id;
2238 free_commitable(struct commitable *ct)
2241 free(ct->in_repo_path);
2242 free(ct->ondisk_path);
2248 struct collect_commitables_arg {
2249 struct got_pathlist_head *commitable_paths;
2250 struct got_repository *repo;
2251 struct got_worktree *worktree;
2254 static const struct got_error *
2255 collect_commitables(void *arg, unsigned char status, const char *relpath,
2256 struct got_object_id *id)
2258 struct collect_commitables_arg *a = arg;
2259 const struct got_error *err = NULL;
2260 struct commitable *ct = NULL;
2261 struct got_pathlist_entry *new = NULL;
2262 char *parent_path = NULL, *path = NULL;
2265 if (status == GOT_STATUS_CONFLICT)
2266 return got_error(GOT_ERR_COMMIT_CONFLICT);
2268 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2269 status != GOT_STATUS_DELETE)
2272 if (asprintf(&path, "/%s", relpath) == -1) {
2273 err = got_error_prefix_errno("asprintf");
2276 if (strcmp(path, "/") == 0) {
2277 parent_path = strdup("");
2278 if (parent_path == NULL)
2279 return got_error_prefix_errno("strdup");
2281 err = got_path_dirname(&parent_path, path);
2286 ct = calloc(1, sizeof(*ct));
2288 err = got_error_prefix_errno("calloc");
2292 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2294 err = got_error_prefix_errno("asprintf");
2297 if (status == GOT_STATUS_DELETE) {
2298 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2300 if (lstat(ct->ondisk_path, &sb) != 0) {
2301 err = got_error_prefix_errno2("lstat", ct->ondisk_path);
2304 ct->mode = sb.st_mode;
2307 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2308 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2310 err = got_error_prefix_errno("asprintf");
2314 ct->status = status;
2315 ct->blob_id = NULL; /* will be filled in when blob gets created */
2316 if (ct->status != GOT_STATUS_ADD) {
2317 ct->base_id = got_object_id_dup(id);
2318 if (ct->base_id == NULL) {
2319 err = got_error_prefix_errno("got_object_id_dup");
2323 ct->path = strdup(path);
2324 if (ct->path == NULL) {
2325 err = got_error_prefix_errno("strdup");
2328 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2330 if (ct && (err || new == NULL))
2331 free_commitable(ct);
2337 static const struct got_error *write_tree(struct got_object_id **,
2338 struct got_tree_object *, const char *, struct got_pathlist_head *,
2339 got_worktree_status_cb status_cb, void *status_arg,
2340 struct got_repository *);
2342 static const struct got_error *
2343 write_subtree(struct got_object_id **new_subtree_id,
2344 struct got_tree_entry *te, const char *parent_path,
2345 struct got_pathlist_head *commitable_paths,
2346 got_worktree_status_cb status_cb, void *status_arg,
2347 struct got_repository *repo)
2349 const struct got_error *err = NULL;
2350 struct got_tree_object *subtree;
2353 if (asprintf(&subpath, "%s%s%s", parent_path,
2354 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2355 return got_error_prefix_errno("asprintf");
2357 err = got_object_open_as_tree(&subtree, repo, te->id);
2361 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2362 status_cb, status_arg, repo);
2363 got_object_tree_close(subtree);
2368 static const struct got_error *
2369 match_ct_parent_path(int *match, struct commitable *ct, const char *path)
2371 const struct got_error *err = NULL;
2372 char *ct_parent_path = NULL;
2376 if (strchr(ct->path, '/') == NULL) {
2377 *match = got_path_is_root_dir(path);
2381 err = got_path_dirname(&ct_parent_path, ct->path);
2384 *match = (strcmp(path, ct_parent_path) == 0);
2385 free(ct_parent_path);
2390 get_ct_file_mode(struct commitable *ct)
2392 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2395 static const struct got_error *
2396 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2397 struct got_tree_entry *te, struct commitable *ct)
2399 const struct got_error *err = NULL;
2403 err = got_object_tree_entry_dup(new_te, te);
2407 (*new_te)->mode = get_ct_file_mode(ct);
2409 free((*new_te)->id);
2410 (*new_te)->id = got_object_id_dup(ct->blob_id);
2411 if ((*new_te)->id == NULL) {
2412 err = got_error_prefix_errno("got_object_id_dup");
2416 if (err && *new_te) {
2417 got_object_tree_entry_close(*new_te);
2423 static const struct got_error *
2424 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2425 struct commitable *ct)
2427 const struct got_error *err = NULL;
2432 *new_te = calloc(1, sizeof(**new_te));
2433 if (*new_te == NULL)
2434 return got_error_prefix_errno("calloc");
2436 ct_name = basename(ct->path);
2437 if (ct_name == NULL) {
2438 err = got_error_prefix_errno2("basename", ct->path);
2441 (*new_te)->name = strdup(ct_name);
2442 if ((*new_te)->name == NULL) {
2443 err = got_error_prefix_errno("strdup");
2447 (*new_te)->mode = get_ct_file_mode(ct);
2449 (*new_te)->id = got_object_id_dup(ct->blob_id);
2450 if ((*new_te)->id == NULL) {
2451 err = got_error_prefix_errno("got_object_id_dup");
2455 if (err && *new_te) {
2456 got_object_tree_entry_close(*new_te);
2462 static const struct got_error *
2463 insert_tree_entry(struct got_tree_entry *new_te,
2464 struct got_pathlist_head *paths)
2466 const struct got_error *err = NULL;
2467 struct got_pathlist_entry *new_pe;
2469 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2473 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2477 static const struct got_error *
2478 report_ct_status(struct commitable *ct,
2479 got_worktree_status_cb status_cb, void *status_arg)
2481 const char *ct_path = ct->path;
2482 while (ct_path[0] == '/')
2484 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id);
2487 static const struct got_error *
2488 match_modified_subtree(int *modified, struct got_tree_entry *te,
2489 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2491 const struct got_error *err = NULL;
2492 struct got_pathlist_entry *pe;
2497 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2498 got_path_is_root_dir(base_tree_path) ? "" : "/",
2500 return got_error_prefix_errno("asprintf");
2502 TAILQ_FOREACH(pe, commitable_paths, entry) {
2503 struct commitable *ct = pe->data;
2504 *modified = got_path_is_child(ct->in_repo_path, te_path,
2514 static const struct got_error *
2515 match_deleted_or_modified_ct(struct commitable **ctp,
2516 struct got_tree_entry *te, const char *base_tree_path,
2517 struct got_pathlist_head *commitable_paths)
2519 const struct got_error *err = NULL;
2520 struct got_pathlist_entry *pe;
2524 TAILQ_FOREACH(pe, commitable_paths, entry) {
2525 struct commitable *ct = pe->data;
2526 char *ct_name = NULL;
2529 if (ct->status != GOT_STATUS_MODIFY &&
2530 ct->status != GOT_STATUS_DELETE)
2533 if (got_object_id_cmp(ct->base_id, te->id) != 0)
2536 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2542 ct_name = basename(pe->path);
2543 if (ct_name == NULL)
2544 return got_error_prefix_errno2("basename", pe->path);
2546 if (strcmp(te->name, ct_name) != 0)
2556 static const struct got_error *
2557 write_tree(struct got_object_id **new_tree_id,
2558 struct got_tree_object *base_tree, const char *path_base_tree,
2559 struct got_pathlist_head *commitable_paths,
2560 got_worktree_status_cb status_cb, void *status_arg,
2561 struct got_repository *repo)
2563 const struct got_error *err = NULL;
2564 const struct got_tree_entries *base_entries = NULL;
2565 struct got_pathlist_head paths;
2566 struct got_tree_entries new_tree_entries;
2567 struct got_tree_entry *te, *new_te = NULL;
2568 struct got_pathlist_entry *pe;
2571 new_tree_entries.nentries = 0;
2572 SIMPLEQ_INIT(&new_tree_entries.head);
2574 /* Insert, and recurse into, newly added entries first. */
2575 TAILQ_FOREACH(pe, commitable_paths, entry) {
2576 struct commitable *ct = pe->data;
2577 char *child_path = NULL, *slash;
2579 if (ct->status != GOT_STATUS_ADD)
2582 if (!got_path_is_child(pe->path, path_base_tree,
2583 strlen(path_base_tree)))
2586 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2591 slash = strchr(child_path, '/');
2592 if (slash == NULL) {
2593 err = alloc_added_blob_tree_entry(&new_te, ct);
2596 err = report_ct_status(ct, status_cb, status_arg);
2601 struct got_pathlist_entry *pe2;
2604 *slash = '\0'; /* trim trailing path components */
2605 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2606 got_path_is_root_dir(path_base_tree) ? "" : "/",
2607 child_path) == -1) {
2608 err = got_error_prefix_errno("asprintf");
2611 TAILQ_FOREACH(pe2, &paths, entry) {
2612 if (got_path_cmp(subtree_path, pe2->path) != 0)
2622 new_te = calloc(1, sizeof(*new_te));
2623 new_te->mode = S_IFDIR;
2624 new_te->name = strdup(child_path);
2625 if (new_te->name == NULL) {
2626 err = got_error_prefix_errno("strdup");
2627 got_object_tree_entry_close(new_te);
2631 err = write_tree(&new_te->id, NULL, subtree_path,
2632 commitable_paths, status_cb, status_arg, repo);
2635 got_object_tree_entry_close(new_te);
2640 err = insert_tree_entry(new_te, &paths);
2646 /* Handle modified and deleted entries. */
2647 base_entries = got_object_tree_get_entries(base_tree);
2648 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2649 struct commitable *ct = NULL;
2651 if (S_ISDIR(te->mode)) {
2653 err = got_object_tree_entry_dup(&new_te, te);
2656 err = match_modified_subtree(&modified, te,
2657 path_base_tree, commitable_paths);
2660 /* Avoid recursion into unmodified subtrees. */
2663 err = write_subtree(&new_te->id, te,
2664 path_base_tree, commitable_paths,
2665 status_cb, status_arg, repo);
2669 err = insert_tree_entry(new_te, &paths);
2675 err = match_deleted_or_modified_ct(&ct, te,
2676 path_base_tree, commitable_paths);
2678 /* NB: Deleted entries get dropped here. */
2679 if (ct->status == GOT_STATUS_MODIFY) {
2680 err = alloc_modified_blob_tree_entry(
2684 err = insert_tree_entry(new_te, &paths);
2688 err = report_ct_status(ct, status_cb, status_arg);
2692 /* Entry is unchanged; just copy it. */
2693 err = got_object_tree_entry_dup(&new_te, te);
2696 err = insert_tree_entry(new_te, &paths);
2703 /* Write new list of entries; deleted entries have been dropped. */
2704 TAILQ_FOREACH(pe, &paths, entry) {
2705 struct got_tree_entry *te = pe->data;
2706 new_tree_entries.nentries++;
2707 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2709 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2711 got_object_tree_entries_close(&new_tree_entries);
2712 got_pathlist_free(&paths);
2716 static const struct got_error *
2717 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2718 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2720 const struct got_error *err = NULL;
2721 char *fileindex_path = NULL, *new_fileindex_path = NULL;
2722 struct got_fileindex *fileindex = NULL;
2723 FILE *new_index = NULL;
2724 struct got_pathlist_entry *pe;
2726 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2730 err = got_opentemp_named(&new_fileindex_path, &new_index,
2735 TAILQ_FOREACH(pe, commitable_paths, entry) {
2736 struct got_fileindex_entry *ie;
2737 struct commitable *ct = pe->data;
2739 ie = got_fileindex_entry_get(fileindex, pe->path);
2741 if (ct->status == GOT_STATUS_DELETE) {
2742 got_fileindex_entry_remove(fileindex, ie);
2743 got_fileindex_entry_free(ie);
2745 err = got_fileindex_entry_update(ie,
2746 ct->ondisk_path, ct->blob_id->sha1,
2747 new_base_commit_id->sha1, 1);
2749 err = got_fileindex_entry_alloc(&ie,
2750 ct->ondisk_path, pe->path, ct->blob_id->sha1,
2751 new_base_commit_id->sha1);
2754 err = got_fileindex_entry_add(fileindex, ie);
2760 err = got_fileindex_write(fileindex, new_index);
2764 if (rename(new_fileindex_path, fileindex_path) != 0) {
2765 err = got_error_prefix_errno3("rename", new_fileindex_path,
2767 unlink(new_fileindex_path);
2771 free(new_fileindex_path);
2772 new_fileindex_path = NULL;
2775 if (new_fileindex_path)
2776 unlink(new_fileindex_path);
2779 free(new_fileindex_path);
2780 free(fileindex_path);
2781 got_fileindex_free(fileindex);
2785 static const struct got_error *
2786 check_ct_out_of_date(struct commitable *ct, struct got_repository *repo,
2787 struct got_object_id *head_commit_id)
2789 const struct got_error *err = NULL;
2790 struct got_object_id *id_in_head;
2793 * XXX This should probably be checking each commit from
2794 * worktree's base_commit -> head and verify that the
2795 * same blob exists in each of these commits.
2796 * Removals+additions within this line of history could mean
2797 * that renames have occured in which case we should really
2798 * be forcing the user to run an update...
2800 err = got_object_id_by_path(&id_in_head, repo,
2801 head_commit_id, ct->in_repo_path);
2803 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2805 if (ct->status != GOT_STATUS_ADD)
2806 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2811 if (id_in_head && got_object_id_cmp(id_in_head, ct->base_id) != 0)
2812 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2818 const struct got_error *
2819 got_worktree_commit(struct got_object_id **new_commit_id,
2820 struct got_worktree *worktree, const char *ondisk_path,
2821 const char *author, const char *committer, const char *logmsg,
2822 got_worktree_status_cb status_cb, void *status_arg,
2823 struct got_repository *repo)
2825 const struct got_error *err = NULL, *unlockerr = NULL;
2826 struct collect_commitables_arg cc_arg;
2827 struct got_pathlist_head commitable_paths;
2828 struct got_pathlist_entry *pe;
2829 char *relpath = NULL;
2830 const char *head_ref_name = NULL;
2831 struct got_reference *head_ref = NULL;
2832 struct got_commit_object *head_commit = NULL;
2833 struct got_object_id *head_commit_id = NULL;
2834 struct got_reference *head_ref2 = NULL;
2835 struct got_object_id *head_commit_id2 = NULL;
2836 struct got_tree_object *head_tree = NULL;
2837 struct got_object_id *new_tree_id = NULL;
2838 struct got_object_id_queue parent_ids;
2839 struct got_object_qid *pid = NULL;
2841 *new_commit_id = NULL;
2843 TAILQ_INIT(&commitable_paths);
2844 SIMPLEQ_INIT(&parent_ids);
2847 err = got_path_skip_common_ancestor(&relpath,
2848 worktree->root_path, ondisk_path);
2853 err = lock_worktree(worktree, LOCK_EX);
2857 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
2860 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2864 cc_arg.commitable_paths = &commitable_paths;
2865 cc_arg.worktree = worktree;
2867 err = got_worktree_status(worktree, relpath ? relpath : "",
2868 repo, collect_commitables, &cc_arg, NULL, NULL);
2872 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2876 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2877 struct commitable *ct = pe->data;
2878 err = check_ct_out_of_date(ct, repo, head_commit_id);
2883 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2887 /* TODO: collect commit message if not specified */
2889 /* Create blobs from added and modified files and record their IDs. */
2890 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2891 struct commitable *ct = pe->data;
2894 if (ct->status != GOT_STATUS_ADD &&
2895 ct->status != GOT_STATUS_MODIFY)
2898 if (asprintf(&ondisk_path, "%s/%s",
2899 worktree->root_path, pe->path) == -1) {
2900 err = got_error_prefix_errno("asprintf");
2903 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2909 /* Recursively write new tree objects. */
2910 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
2911 status_cb, status_arg, repo);
2915 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
2918 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
2919 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2920 1, author, time(NULL), committer, time(NULL), logmsg, repo);
2921 got_object_qid_free(pid);
2925 /* Check if a concurrent commit to our branch has occurred. */
2926 head_ref_name = got_worktree_get_head_ref_name(worktree);
2927 if (head_ref_name == NULL) {
2928 err = got_error_prefix_errno("got_worktree_get_head_ref_name");
2931 /* Lock the reference here to prevent concurrent modification. */
2932 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
2935 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
2938 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
2939 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
2942 /* Update branch head in repository. */
2943 err = got_ref_change_ref(head_ref2, *new_commit_id);
2946 err = got_ref_write(head_ref2, repo);
2950 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
2954 err = ref_base_commit(worktree, repo);
2958 err = update_fileindex_after_commit(&commitable_paths,
2959 *new_commit_id, worktree);
2963 unlockerr = lock_worktree(worktree, LOCK_SH);
2964 if (unlockerr && err == NULL)
2966 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2967 struct commitable *ct = pe->data;
2968 free_commitable(ct);
2970 got_pathlist_free(&commitable_paths);
2972 got_object_tree_close(head_tree);
2974 got_object_commit_close(head_commit);
2976 free(head_commit_id);
2977 free(head_commit_id2);
2979 got_ref_close(head_ref);
2981 unlockerr = got_ref_unlock(head_ref2);
2982 if (unlockerr && err == NULL)
2984 got_ref_close(head_ref2);