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>
32 #include "got_error.h"
33 #include "got_repository.h"
34 #include "got_reference.h"
35 #include "got_object.h"
36 #include "got_worktree.h"
37 #include "got_opentemp.h"
39 #include "got_lib_worktree.h"
40 #include "got_lib_path.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_fileindex.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
48 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 static const struct got_error *
52 create_meta_file(const char *path_got, const char *name, const char *content)
54 const struct got_error *err = NULL;
58 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
59 err = got_error_from_errno();
64 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
65 GOT_DEFAULT_FILE_MODE);
67 err = got_error_from_errno();
72 int len = dprintf(fd, "%s\n", content);
73 if (len != strlen(content) + 1) {
74 err = got_error_from_errno();
80 if (fd != -1 && close(fd) == -1 && err == NULL)
81 err = got_error_from_errno();
86 static const struct got_error *
87 update_meta_file(const char *path_got, const char *name, const char *content)
89 const struct got_error *err = NULL;
94 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
95 err = got_error_from_errno();
100 err = got_opentemp_named(&tmppath, &tmpfile, path);
105 int len = fprintf(tmpfile, "%s\n", content);
106 if (len != strlen(content) + 1) {
107 err = got_error_from_errno();
112 if (rename(tmppath, path) != 0) {
113 err = got_error_from_errno();
123 static const struct got_error *
124 read_meta_file(char **content, const char *path_got, const char *name)
126 const struct got_error *err = NULL;
134 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
135 err = got_error_from_errno();
140 fd = open(path, O_RDONLY | O_NOFOLLOW);
142 err = got_error_from_errno();
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno());
152 *content = calloc(1, sb.st_size);
153 if (*content == NULL) {
154 err = got_error_from_errno();
158 n = read(fd, *content, sb.st_size);
159 if (n != sb.st_size) {
160 err = (n == -1 ? got_error_from_errno() :
161 got_error(GOT_ERR_WORKTREE_META));
164 if ((*content)[sb.st_size - 1] != '\n') {
165 err = got_error(GOT_ERR_WORKTREE_META);
168 (*content)[sb.st_size - 1] = '\0';
171 if (fd != -1 && close(fd) == -1 && err == NULL)
172 err = got_error_from_errno();
181 const struct got_error *
182 got_worktree_init(const char *path, struct got_reference *head_ref,
183 const char *prefix, struct got_repository *repo)
185 const struct got_error *err = NULL;
186 struct got_object_id *commit_id = NULL;
188 char *path_got = NULL;
190 char *repo_path = 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 repo_path = got_repo_get_path(repo);
255 if (repo_path == NULL) {
256 err = got_error_from_errno();
259 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
263 /* Store in-repository path prefix. */
264 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
265 absprefix ? absprefix : prefix);
269 /* Stamp work tree with format file. */
270 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
271 err = got_error_from_errno();
274 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
289 const struct got_error *
290 got_worktree_open(struct got_worktree **worktree, const char *path)
292 const struct got_error *err = NULL;
294 char *formatstr = NULL;
295 char *path_lock = NULL;
296 char *base_commit_id_str = NULL;
297 char *head_ref_str = NULL;
298 int version, fd = -1;
300 struct got_repository *repo = NULL;
304 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
305 err = got_error_from_errno();
310 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
311 err = got_error_from_errno();
316 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
318 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
319 : got_error_from_errno());
323 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
327 version = strtonum(formatstr, 1, INT_MAX, &errstr);
329 err = got_error(GOT_ERR_WORKTREE_META);
332 if (version != GOT_WORKTREE_FORMAT_VERSION) {
333 err = got_error(GOT_ERR_WORKTREE_VERS);
337 *worktree = calloc(1, sizeof(**worktree));
338 if (*worktree == NULL) {
339 err = got_error_from_errno();
342 (*worktree)->lockfd = -1;
344 (*worktree)->root_path = strdup(path);
345 if ((*worktree)->root_path == NULL) {
346 err = got_error_from_errno();
349 err = read_meta_file(&(*worktree)->repo_path, path_got,
350 GOT_WORKTREE_REPOSITORY);
354 err = read_meta_file(&(*worktree)->path_prefix, path_got,
355 GOT_WORKTREE_PATH_PREFIX);
359 err = read_meta_file(&base_commit_id_str, path_got,
360 GOT_WORKTREE_BASE_COMMIT);
364 err = got_repo_open(&repo, (*worktree)->repo_path);
368 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
373 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
377 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
380 got_repo_close(repo);
384 free(base_commit_id_str);
388 if (*worktree != NULL)
389 got_worktree_close(*worktree);
392 (*worktree)->lockfd = fd;
398 got_worktree_close(struct got_worktree *worktree)
400 free(worktree->root_path);
401 free(worktree->repo_path);
402 free(worktree->path_prefix);
403 free(worktree->base_commit_id);
404 if (worktree->head_ref)
405 got_ref_close(worktree->head_ref);
406 if (worktree->lockfd != -1)
407 close(worktree->lockfd);
412 got_worktree_get_repo_path(struct got_worktree *worktree)
414 return worktree->repo_path;
418 got_worktree_get_path_prefix(struct got_worktree *worktree)
420 return worktree->path_prefix;
423 const struct got_error *
424 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
425 const char *path_prefix)
427 char *absprefix = NULL;
429 if (!got_path_is_absolute(path_prefix)) {
430 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
431 return got_error_from_errno();
433 *match = (strcmp(absprefix ? absprefix : path_prefix,
434 worktree->path_prefix) == 0);
440 got_worktree_get_head_ref_name(struct got_worktree *worktree)
442 return got_ref_to_str(worktree->head_ref);
445 struct got_reference *
446 got_worktree_get_head_ref(struct got_worktree *worktree)
448 return got_ref_dup(worktree->head_ref);
451 const struct got_object_id *
452 got_worktree_get_base_commit_id(struct got_worktree *worktree)
454 return worktree->base_commit_id;
457 const struct got_error *
458 got_worktree_set_base_commit_id(struct got_worktree *worktree,
459 struct got_repository *repo, struct got_object_id *commit_id)
461 const struct got_error *err;
462 struct got_object *obj = NULL;
464 char *path_got = NULL;
466 if (asprintf(&path_got, "%s/%s", worktree->root_path,
467 GOT_WORKTREE_GOT_DIR) == -1) {
468 err = got_error_from_errno();
473 err = got_object_open(&obj, repo, commit_id);
477 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
478 err = got_error(GOT_ERR_OBJ_TYPE);
482 /* Record our base commit. */
483 err = got_object_id_str(&id_str, commit_id);
486 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
490 free(worktree->base_commit_id);
491 worktree->base_commit_id = got_object_id_dup(commit_id);
492 if (worktree->base_commit_id == NULL) {
493 err = got_error_from_errno();
498 got_object_close(obj);
504 static const struct got_error *
505 lock_worktree(struct got_worktree *worktree, int operation)
507 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
508 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
509 : got_error_from_errno());
514 apply_path_prefix(struct got_worktree *worktree, const char *path)
516 const char *p = path;
517 p += strlen(worktree->path_prefix);
523 static const struct got_error *
524 blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
525 struct got_fileindex_entry *entry, const char *path,
526 struct got_blob_object *blob, struct got_repository *repo,
527 got_worktree_checkout_cb progress_cb, void *progress_arg,
528 const char *progress_path)
530 const struct got_error *err = NULL;
535 char *tmppath = NULL;
537 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
538 apply_path_prefix(worktree, path)) == -1)
539 return got_error_from_errno();
541 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
542 GOT_DEFAULT_FILE_MODE);
544 err = got_error_from_errno();
545 if (errno == EEXIST) {
547 if (lstat(ondisk_path, &sb) == -1) {
548 err = got_error_from_errno();
550 } else if (!S_ISREG(sb.st_mode)) {
551 /* TODO file is obstructed; do something */
552 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
555 err = got_opentemp_named_fd(&tmppath, &fd,
565 (*progress_cb)(progress_arg,
566 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
568 hdrlen = got_object_blob_get_hdrlen(blob);
570 const uint8_t *buf = got_object_blob_get_read_buf(blob);
571 err = got_object_blob_read_block(&len, blob);
575 /* Skip blob object header first time around. */
576 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
578 err = got_error_from_errno();
580 } else if (outlen != len - hdrlen) {
581 err = got_error(GOT_ERR_IO);
591 if (rename(tmppath, ondisk_path) != 0) {
592 err = got_error_from_errno();
598 err = got_fileindex_entry_update(entry, ondisk_path,
599 blob->id.sha1, worktree->base_commit_id->sha1);
601 err = got_fileindex_entry_alloc(&entry, ondisk_path,
602 apply_path_prefix(worktree, path), blob->id.sha1,
603 worktree->base_commit_id->sha1);
606 err = got_fileindex_entry_add(fileindex, entry);
618 static const struct got_error *
619 add_dir_on_disk(struct got_worktree *worktree, const char *path)
621 const struct got_error *err = NULL;
624 if (asprintf(&abspath, "%s/%s", worktree->root_path,
625 apply_path_prefix(worktree, path)) == -1)
626 return got_error_from_errno();
628 /* XXX queue work rather than editing disk directly? */
629 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
632 if (errno != EEXIST) {
633 err = got_error_from_errno();
637 if (lstat(abspath, &sb) == -1) {
638 err = got_error_from_errno();
642 if (!S_ISDIR(sb.st_mode)) {
643 /* TODO directory is obstructed; do something */
644 return got_error(GOT_ERR_FILE_OBSTRUCTED);
653 static const struct got_error *
654 tree_checkout(struct got_worktree *, struct got_fileindex *,
655 struct got_tree_object *, const char *, struct got_repository *,
656 got_worktree_checkout_cb progress_cb, void *progress_arg,
657 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
659 static const struct got_error *
660 tree_checkout_entry(struct got_worktree *worktree,
661 struct got_fileindex *fileindex, struct got_tree_entry *te,
662 const char *parent, struct got_repository *repo,
663 got_worktree_checkout_cb progress_cb, void *progress_arg,
664 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
666 const struct got_error *err = NULL;
667 struct got_object *obj = NULL;
668 struct got_blob_object *blob = NULL;
669 struct got_fileindex_entry *entry = NULL;
670 struct got_tree_object *tree = NULL;
672 char *progress_path = NULL;
675 if (parent[0] == '/' && parent[1] == '\0')
677 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
678 return got_error_from_errno();
680 /* Skip this entry if it is outside of our path prefix. */
681 len = MIN(strlen(worktree->path_prefix), strlen(path));
682 if (strncmp(path, worktree->path_prefix, len) != 0) {
687 err = got_object_open(&obj, repo, te->id);
691 progress_path = path;
692 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
693 progress_path += len;
696 case GOT_OBJ_TYPE_BLOB:
697 if (strlen(worktree->path_prefix) >= strlen(path))
699 entry = got_fileindex_entry_get(fileindex,
700 apply_path_prefix(worktree, path));
702 memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
703 SHA1_DIGEST_LENGTH) == 0) {
704 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
708 if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
709 SHA1_DIGEST_LENGTH) == 0)
711 err = got_object_blob_open(&blob, repo, obj, 8192);
714 err = blob_checkout(worktree, fileindex, entry, path, blob,
715 repo, progress_cb, progress_arg, progress_path);
717 case GOT_OBJ_TYPE_TREE:
718 if (strlen(worktree->path_prefix) < strlen(path)) {
719 err = add_dir_on_disk(worktree, path);
723 err = got_object_tree_open(&tree, repo, obj);
726 /* XXX infinite recursion possible */
727 err = tree_checkout(worktree, fileindex, tree, path, repo,
728 progress_cb, progress_arg, cancel_cb, cancel_arg);
736 got_object_blob_close(blob);
738 got_object_tree_close(tree);
740 got_object_close(obj);
745 struct collect_missing_entry_args {
746 struct got_fileindex *fileindex;
747 const struct got_tree_entries *entries;
748 struct got_fileindex missing_entries;
749 const char *path_prefix;
752 static const struct got_error *
753 collect_missing_file(void *args, struct got_fileindex_entry *entry)
755 struct collect_missing_entry_args *a = args;
757 struct got_tree_entry *te;
760 if (a->path_prefix[0] == '\0' && strchr(entry->path, '/') != NULL)
762 if (a->path_prefix[0] != '\0' &&
763 strncmp(a->path_prefix, entry->path, strlen(a->path_prefix)) != 0)
766 name = basename(entry->path);
768 return got_error_from_errno();
770 SIMPLEQ_FOREACH(te, &a->entries->head, entry) {
771 if (strcmp(te->name, name) == 0) {
780 got_fileindex_entry_remove(a->fileindex, entry);
781 return got_fileindex_entry_add(&a->missing_entries, entry);
784 /* Remove files which exist in the file index but not in the tree. */
785 static const struct got_error *
786 remove_missing_files(struct got_worktree *worktree, const char *path,
787 struct got_fileindex *fileindex, const struct got_tree_entries *entries,
788 got_worktree_checkout_cb progress_cb, void *progress_arg,
789 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
791 const struct got_error *err = NULL;
792 struct collect_missing_entry_args a;
793 struct got_fileindex_entry *entry, *tmp;
795 a.fileindex = fileindex;
797 a.missing_entries.nentries = 0;
798 a.path_prefix = path;
799 while (a.path_prefix[0] == '/')
801 TAILQ_INIT(&a.missing_entries.entries);
802 err = got_fileindex_for_each_entry(fileindex, collect_missing_file, &a);
806 TAILQ_FOREACH_SAFE(entry, &a.missing_entries.entries, entry, tmp) {
807 char *ondisk_path = NULL;
810 err = (*cancel_cb)(cancel_arg);
815 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, entry->path);
817 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
818 entry->path) == -1) {
819 err = got_error_from_errno();
823 if (unlink(ondisk_path) == -1)
824 err = got_error_from_errno();
829 TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
830 got_fileindex_entry_free(entry);
834 while (!TAILQ_EMPTY(&a.missing_entries.entries)) {
835 entry = TAILQ_FIRST(&a.missing_entries.entries);
836 TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
837 got_fileindex_entry_free(entry);
844 static const struct got_error *
845 tree_checkout(struct got_worktree *worktree,
846 struct got_fileindex *fileindex, struct got_tree_object *tree,
847 const char *path, struct got_repository *repo,
848 got_worktree_checkout_cb progress_cb, void *progress_arg,
849 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
851 const struct got_error *err = NULL;
852 const struct got_tree_entries *entries;
853 struct got_tree_entry *te;
856 /* Skip this tree if it is outside of our path prefix. */
857 len = MIN(strlen(worktree->path_prefix), strlen(path));
858 if (strncmp(path, worktree->path_prefix, len) != 0)
861 entries = got_object_tree_get_entries(tree);
862 SIMPLEQ_FOREACH(te, &entries->head, entry) {
864 err = (*cancel_cb)(cancel_arg);
868 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
869 progress_cb, progress_arg, cancel_cb, cancel_arg);
874 len = strlen(worktree->path_prefix);
875 if (strncmp(worktree->path_prefix, path, len) == 0) {
876 err = remove_missing_files(worktree, path, fileindex, entries,
877 progress_cb, progress_arg, cancel_cb, cancel_arg);
885 const struct got_error *
886 got_worktree_checkout_files(struct got_worktree *worktree,
887 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
888 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
890 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
891 struct got_commit_object *commit = NULL;
892 struct got_tree_object *tree = NULL;
893 char *fileindex_path = NULL, *new_fileindex_path = NULL;
894 struct got_fileindex *fileindex = NULL;
895 FILE *index = NULL, *new_index = NULL;
897 err = lock_worktree(worktree, LOCK_EX);
901 fileindex = got_fileindex_alloc();
902 if (fileindex == NULL) {
903 err = got_error_from_errno();
907 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
908 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
909 err = got_error_from_errno();
910 fileindex_path = NULL;
915 * Read the file index.
916 * Checking out files is supposed to be an idempotent operation.
917 * If the on-disk file index is incomplete we will try to complete it.
919 index = fopen(fileindex_path, "rb");
921 err = got_error_from_errno();
924 err = got_fileindex_read(fileindex, index);
929 err = got_opentemp_named(&new_fileindex_path, &new_index,
934 err = got_object_open_as_commit(&commit, repo,
935 worktree->base_commit_id);
939 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
943 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
944 progress_cb, progress_arg, cancel_cb, cancel_arg);
946 /* Try to sync the fileindex back to disk in any case. */
947 err = got_fileindex_write(fileindex, new_index);
951 if (rename(new_fileindex_path, fileindex_path) != 0) {
952 err = got_error_from_errno();
956 free(new_fileindex_path);
957 new_fileindex_path = NULL;
961 got_object_tree_close(tree);
963 got_object_commit_close(commit);
964 if (new_fileindex_path)
965 unlink(new_fileindex_path);
968 free(new_fileindex_path);
969 free(fileindex_path);
970 got_fileindex_free(fileindex);
973 unlockerr = lock_worktree(worktree, LOCK_SH);
974 if (unlockerr && err == NULL)