2 * Copyright (c) 2018 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>
33 #include "got_error.h"
34 #include "got_repository.h"
35 #include "got_reference.h"
36 #include "got_object.h"
37 #include "got_worktree.h"
38 #include "got_opentemp.h"
40 #include "got_lib_worktree.h"
41 #include "got_lib_path.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_fileindex.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 static const struct got_error *
53 create_meta_file(const char *path_got, const char *name, const char *content)
55 const struct got_error *err = NULL;
59 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
60 err = got_error_from_errno();
65 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
66 GOT_DEFAULT_FILE_MODE);
68 err = got_error_from_errno();
73 int len = dprintf(fd, "%s\n", content);
74 if (len != strlen(content) + 1) {
75 err = got_error_from_errno();
81 if (fd != -1 && close(fd) == -1 && err == NULL)
82 err = got_error_from_errno();
87 static const struct got_error *
88 update_meta_file(const char *path_got, const char *name, const char *content)
90 const struct got_error *err = NULL;
95 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
96 err = got_error_from_errno();
101 err = got_opentemp_named(&tmppath, &tmpfile, path);
106 int len = fprintf(tmpfile, "%s\n", content);
107 if (len != strlen(content) + 1) {
108 err = got_error_from_errno();
113 if (rename(tmppath, path) != 0) {
114 err = got_error_from_errno();
124 static const struct got_error *
125 read_meta_file(char **content, const char *path_got, const char *name)
127 const struct got_error *err = NULL;
135 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
136 err = got_error_from_errno();
141 fd = open(path, O_RDONLY | O_NOFOLLOW);
143 err = got_error_from_errno();
146 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
147 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
148 : got_error_from_errno());
153 *content = calloc(1, sb.st_size);
154 if (*content == NULL) {
155 err = got_error_from_errno();
159 n = read(fd, *content, sb.st_size);
160 if (n != sb.st_size) {
161 err = (n == -1 ? got_error_from_errno() :
162 got_error(GOT_ERR_WORKTREE_META));
165 if ((*content)[sb.st_size - 1] != '\n') {
166 err = got_error(GOT_ERR_WORKTREE_META);
169 (*content)[sb.st_size - 1] = '\0';
172 if (fd != -1 && close(fd) == -1 && err == NULL)
173 err = got_error_from_errno();
182 const struct got_error *
183 got_worktree_init(const char *path, struct got_reference *head_ref,
184 const char *prefix, struct got_repository *repo)
186 const struct got_error *err = NULL;
187 struct got_object_id *commit_id = NULL;
189 char *path_got = NULL;
191 char *formatstr = NULL;
192 char *absprefix = NULL;
193 char *basestr = NULL;
195 err = got_ref_resolve(&commit_id, repo, head_ref);
198 err = got_object_get_type(&obj_type, repo, commit_id);
201 if (obj_type != GOT_OBJ_TYPE_COMMIT)
202 return got_error(GOT_ERR_OBJ_TYPE);
204 if (!got_path_is_absolute(prefix)) {
205 if (asprintf(&absprefix, "/%s", prefix) == -1)
206 return got_error_from_errno();
209 /* Create top-level directory (may already exist). */
210 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
211 err = got_error_from_errno();
215 /* Create .got directory (may already exist). */
216 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
217 err = got_error_from_errno();
220 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
221 err = got_error_from_errno();
225 /* Create an empty lock file. */
226 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
230 /* Create an empty file index. */
231 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
235 /* Write the HEAD reference. */
236 refstr = got_ref_to_str(head_ref);
237 if (refstr == NULL) {
238 err = got_error_from_errno();
241 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
245 /* Record our base commit. */
246 err = got_object_id_str(&basestr, commit_id);
249 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
253 /* Store path to repository. */
254 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
255 got_repo_get_path(repo));
259 /* Store in-repository path prefix. */
260 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
261 absprefix ? absprefix : prefix);
265 /* Stamp work tree with format file. */
266 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
267 err = got_error_from_errno();
270 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
284 const struct got_error *
285 got_worktree_open(struct got_worktree **worktree, const char *path)
287 const struct got_error *err = NULL;
289 char *formatstr = NULL;
290 char *path_lock = NULL;
291 char *base_commit_id_str = NULL;
292 char *head_ref_str = NULL;
293 int version, fd = -1;
295 struct got_repository *repo = NULL;
299 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
300 err = got_error_from_errno();
305 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
306 err = got_error_from_errno();
311 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
313 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
314 : got_error_from_errno());
318 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
322 version = strtonum(formatstr, 1, INT_MAX, &errstr);
324 err = got_error(GOT_ERR_WORKTREE_META);
327 if (version != GOT_WORKTREE_FORMAT_VERSION) {
328 err = got_error(GOT_ERR_WORKTREE_VERS);
332 *worktree = calloc(1, sizeof(**worktree));
333 if (*worktree == NULL) {
334 err = got_error_from_errno();
337 (*worktree)->lockfd = -1;
339 (*worktree)->root_path = strdup(path);
340 if ((*worktree)->root_path == NULL) {
341 err = got_error_from_errno();
344 err = read_meta_file(&(*worktree)->repo_path, path_got,
345 GOT_WORKTREE_REPOSITORY);
349 err = read_meta_file(&(*worktree)->path_prefix, path_got,
350 GOT_WORKTREE_PATH_PREFIX);
354 err = read_meta_file(&base_commit_id_str, path_got,
355 GOT_WORKTREE_BASE_COMMIT);
359 err = got_repo_open(&repo, (*worktree)->repo_path);
363 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
368 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
372 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
375 got_repo_close(repo);
379 free(base_commit_id_str);
383 if (*worktree != NULL)
384 got_worktree_close(*worktree);
387 (*worktree)->lockfd = fd;
393 got_worktree_close(struct got_worktree *worktree)
395 free(worktree->root_path);
396 free(worktree->repo_path);
397 free(worktree->path_prefix);
398 free(worktree->base_commit_id);
399 if (worktree->head_ref)
400 got_ref_close(worktree->head_ref);
401 if (worktree->lockfd != -1)
402 close(worktree->lockfd);
407 got_worktree_get_repo_path(struct got_worktree *worktree)
409 return worktree->repo_path;
413 got_worktree_get_path_prefix(struct got_worktree *worktree)
415 return worktree->path_prefix;
418 const struct got_error *
419 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
420 const char *path_prefix)
422 char *absprefix = NULL;
424 if (!got_path_is_absolute(path_prefix)) {
425 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
426 return got_error_from_errno();
428 *match = (strcmp(absprefix ? absprefix : path_prefix,
429 worktree->path_prefix) == 0);
435 got_worktree_get_head_ref_name(struct got_worktree *worktree)
437 return got_ref_to_str(worktree->head_ref);
440 struct got_reference *
441 got_worktree_get_head_ref(struct got_worktree *worktree)
443 return got_ref_dup(worktree->head_ref);
446 const struct got_object_id *
447 got_worktree_get_base_commit_id(struct got_worktree *worktree)
449 return worktree->base_commit_id;
452 const struct got_error *
453 got_worktree_set_base_commit_id(struct got_worktree *worktree,
454 struct got_repository *repo, struct got_object_id *commit_id)
456 const struct got_error *err;
457 struct got_object *obj = NULL;
459 char *path_got = NULL;
461 if (asprintf(&path_got, "%s/%s", worktree->root_path,
462 GOT_WORKTREE_GOT_DIR) == -1) {
463 err = got_error_from_errno();
468 err = got_object_open(&obj, repo, commit_id);
472 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
473 err = got_error(GOT_ERR_OBJ_TYPE);
477 /* Record our base commit. */
478 err = got_object_id_str(&id_str, commit_id);
481 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
485 free(worktree->base_commit_id);
486 worktree->base_commit_id = got_object_id_dup(commit_id);
487 if (worktree->base_commit_id == NULL) {
488 err = got_error_from_errno();
493 got_object_close(obj);
499 static const struct got_error *
500 lock_worktree(struct got_worktree *worktree, int operation)
502 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
503 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
504 : got_error_from_errno());
509 apply_path_prefix(struct got_worktree *worktree, const char *path)
511 const char *p = path;
512 p += strlen(worktree->path_prefix);
518 static const struct got_error *
519 blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
520 struct got_fileindex_entry *entry, const char *path,
521 struct got_blob_object *blob, struct got_repository *repo,
522 got_worktree_checkout_cb progress_cb, void *progress_arg,
523 const char *progress_path)
525 const struct got_error *err = NULL;
530 char *tmppath = NULL;
532 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
533 apply_path_prefix(worktree, path)) == -1)
534 return got_error_from_errno();
536 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
537 GOT_DEFAULT_FILE_MODE);
539 err = got_error_from_errno();
540 if (errno == EEXIST) {
542 if (lstat(ondisk_path, &sb) == -1) {
543 err = got_error_from_errno();
545 } else if (!S_ISREG(sb.st_mode)) {
546 /* TODO file is obstructed; do something */
547 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
550 err = got_opentemp_named_fd(&tmppath, &fd,
560 (*progress_cb)(progress_arg,
561 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
563 hdrlen = got_object_blob_get_hdrlen(blob);
565 const uint8_t *buf = got_object_blob_get_read_buf(blob);
566 err = got_object_blob_read_block(&len, blob);
570 /* Skip blob object header first time around. */
571 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
573 err = got_error_from_errno();
575 } else if (outlen != len - hdrlen) {
576 err = got_error(GOT_ERR_IO);
586 if (rename(tmppath, ondisk_path) != 0) {
587 err = got_error_from_errno();
593 err = got_fileindex_entry_update(entry, ondisk_path,
594 blob->id.sha1, worktree->base_commit_id->sha1);
596 err = got_fileindex_entry_alloc(&entry, ondisk_path,
597 apply_path_prefix(worktree, path), blob->id.sha1,
598 worktree->base_commit_id->sha1);
601 err = got_fileindex_entry_add(fileindex, entry);
613 static const struct got_error *
614 add_dir_on_disk(struct got_worktree *worktree, const char *path)
616 const struct got_error *err = NULL;
619 if (asprintf(&abspath, "%s/%s", worktree->root_path,
620 apply_path_prefix(worktree, path)) == -1)
621 return got_error_from_errno();
623 /* XXX queue work rather than editing disk directly? */
624 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
627 if (errno != EEXIST) {
628 err = got_error_from_errno();
632 if (lstat(abspath, &sb) == -1) {
633 err = got_error_from_errno();
637 if (!S_ISDIR(sb.st_mode)) {
638 /* TODO directory is obstructed; do something */
639 return got_error(GOT_ERR_FILE_OBSTRUCTED);
648 static const struct got_error *
649 tree_checkout(struct got_worktree *, struct got_fileindex *,
650 struct got_tree_object *, const char *, struct got_repository *,
651 got_worktree_checkout_cb progress_cb, void *progress_arg,
652 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
654 static const struct got_error *
655 tree_checkout_entry(struct got_worktree *worktree,
656 struct got_fileindex *fileindex, struct got_tree_entry *te,
657 const char *parent, struct got_repository *repo,
658 got_worktree_checkout_cb progress_cb, void *progress_arg,
659 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
661 const struct got_error *err = NULL;
662 struct got_object *obj = NULL;
663 struct got_blob_object *blob = NULL;
664 struct got_fileindex_entry *entry = NULL;
665 struct got_tree_object *tree = NULL;
667 char *progress_path = NULL;
670 if (parent[0] == '/' && parent[1] == '\0')
672 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
673 return got_error_from_errno();
675 /* Skip this entry if it is outside of our path prefix. */
676 len = MIN(strlen(worktree->path_prefix), strlen(path));
677 if (strncmp(path, worktree->path_prefix, len) != 0) {
682 err = got_object_open(&obj, repo, te->id);
686 progress_path = path;
687 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
688 progress_path += len;
691 case GOT_OBJ_TYPE_BLOB:
692 if (strlen(worktree->path_prefix) >= strlen(path))
694 entry = got_fileindex_entry_get(fileindex,
695 apply_path_prefix(worktree, path));
697 memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
698 SHA1_DIGEST_LENGTH) == 0) {
699 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
703 if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
704 SHA1_DIGEST_LENGTH) == 0)
706 err = got_object_blob_open(&blob, repo, obj, 8192);
709 err = blob_checkout(worktree, fileindex, entry, path, blob,
710 repo, progress_cb, progress_arg, progress_path);
712 case GOT_OBJ_TYPE_TREE:
713 if (strlen(worktree->path_prefix) < strlen(path)) {
714 err = add_dir_on_disk(worktree, path);
718 err = got_object_tree_open(&tree, repo, obj);
721 /* XXX infinite recursion possible */
722 err = tree_checkout(worktree, fileindex, tree, path, repo,
723 progress_cb, progress_arg, cancel_cb, cancel_arg);
731 got_object_blob_close(blob);
733 got_object_tree_close(tree);
735 got_object_close(obj);
740 struct collect_missing_entry_args {
741 struct got_fileindex *fileindex;
742 const struct got_tree_entries *entries;
743 struct got_fileindex missing_entries;
744 const char *current_subdir;
747 static const struct got_error *
748 collect_missing_file(void *args, struct got_fileindex_entry *entry)
750 struct collect_missing_entry_args *a = args;
753 struct got_tree_entry *te;
756 if (a->current_subdir[0] != '\0' &&
757 strncmp(a->current_subdir, entry->path,
758 strlen(a->current_subdir)) != 0)
761 start = entry->path + strlen(a->current_subdir);
762 while (start[0] == '/')
764 end = strchr(start, '/');
766 end = strchr(start, '\0');
768 return got_error(GOT_ERR_BAD_PATH);
772 SIMPLEQ_FOREACH(te, &a->entries->head, entry) {
773 if (strncmp(start, te->name, len) == 0 &&
774 te->name[len] == '\0') {
783 got_fileindex_entry_remove(a->fileindex, entry);
784 return got_fileindex_entry_add(&a->missing_entries, entry);
787 /* Remove files which exist in the file index but not in the tree. */
788 static const struct got_error *
789 remove_missing_files(struct got_worktree *worktree, const char *path,
790 struct got_fileindex *fileindex, const struct got_tree_entries *entries,
791 got_worktree_checkout_cb progress_cb, void *progress_arg,
792 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
794 const struct got_error *err = NULL;
795 struct collect_missing_entry_args a;
796 struct got_fileindex_entry *entry, *tmp;
798 a.fileindex = fileindex;
800 a.missing_entries.nentries = 0;
801 a.current_subdir = apply_path_prefix(worktree, path);
802 TAILQ_INIT(&a.missing_entries.entries);
803 err = got_fileindex_for_each_entry_safe(fileindex,
804 collect_missing_file, &a);
808 TAILQ_FOREACH_SAFE(entry, &a.missing_entries.entries, entry, tmp) {
809 char *ondisk_path = NULL;
812 err = (*cancel_cb)(cancel_arg);
817 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, entry->path);
819 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
820 entry->path) == -1) {
821 err = got_error_from_errno();
825 if (unlink(ondisk_path) == -1)
826 err = got_error_from_errno();
828 char *parent = dirname(ondisk_path);
829 if (rmdir(parent) == -1 && errno != ENOTEMPTY)
830 err = got_error_from_errno();
836 TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
837 got_fileindex_entry_free(entry);
841 while (!TAILQ_EMPTY(&a.missing_entries.entries)) {
842 entry = TAILQ_FIRST(&a.missing_entries.entries);
843 TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
844 got_fileindex_entry_free(entry);
851 static const struct got_error *
852 tree_checkout(struct got_worktree *worktree,
853 struct got_fileindex *fileindex, struct got_tree_object *tree,
854 const char *path, struct got_repository *repo,
855 got_worktree_checkout_cb progress_cb, void *progress_arg,
856 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
858 const struct got_error *err = NULL;
859 const struct got_tree_entries *entries;
860 struct got_tree_entry *te;
863 /* Skip this tree if it shares no path components with the prefix. */
864 len = MIN(strlen(worktree->path_prefix), strlen(path));
865 if (strncmp(path, worktree->path_prefix, len) != 0)
868 entries = got_object_tree_get_entries(tree);
869 SIMPLEQ_FOREACH(te, &entries->head, entry) {
871 err = (*cancel_cb)(cancel_arg);
875 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
876 progress_cb, progress_arg, cancel_cb, cancel_arg);
881 len = strlen(worktree->path_prefix);
882 if (strncmp(worktree->path_prefix, path, len) == 0) {
883 err = remove_missing_files(worktree, path, fileindex, entries,
884 progress_cb, progress_arg, cancel_cb, cancel_arg);
892 const struct got_error *
893 got_worktree_checkout_files(struct got_worktree *worktree,
894 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
895 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
897 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
898 struct got_commit_object *commit = NULL;
899 struct got_tree_object *tree = NULL;
900 char *fileindex_path = NULL, *new_fileindex_path = NULL;
901 struct got_fileindex *fileindex = NULL;
902 FILE *index = NULL, *new_index = NULL;
904 err = lock_worktree(worktree, LOCK_EX);
908 fileindex = got_fileindex_alloc();
909 if (fileindex == NULL) {
910 err = got_error_from_errno();
914 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
915 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
916 err = got_error_from_errno();
917 fileindex_path = NULL;
922 * Read the file index.
923 * Checking out files is supposed to be an idempotent operation.
924 * If the on-disk file index is incomplete we will try to complete it.
926 index = fopen(fileindex_path, "rb");
928 err = got_error_from_errno();
931 err = got_fileindex_read(fileindex, index);
936 err = got_opentemp_named(&new_fileindex_path, &new_index,
941 err = got_object_open_as_commit(&commit, repo,
942 worktree->base_commit_id);
946 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
950 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
951 progress_cb, progress_arg, cancel_cb, cancel_arg);
953 /* Try to sync the fileindex back to disk in any case. */
954 err = got_fileindex_write(fileindex, new_index);
958 if (rename(new_fileindex_path, fileindex_path) != 0) {
959 err = got_error_from_errno();
963 free(new_fileindex_path);
964 new_fileindex_path = NULL;
968 got_object_tree_close(tree);
970 got_object_commit_close(commit);
971 if (new_fileindex_path)
972 unlink(new_fileindex_path);
975 free(new_fileindex_path);
976 free(fileindex_path);
977 got_fileindex_free(fileindex);
980 unlockerr = lock_worktree(worktree, LOCK_SH);
981 if (unlockerr && err == NULL)