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 *formatstr = NULL;
191 char *absprefix = NULL;
192 char *basestr = NULL;
194 err = got_ref_resolve(&commit_id, repo, head_ref);
197 err = got_object_get_type(&obj_type, repo, commit_id);
200 if (obj_type != GOT_OBJ_TYPE_COMMIT)
201 return got_error(GOT_ERR_OBJ_TYPE);
203 if (!got_path_is_absolute(prefix)) {
204 if (asprintf(&absprefix, "/%s", prefix) == -1)
205 return got_error_from_errno();
208 /* Create top-level directory (may already exist). */
209 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
210 err = got_error_from_errno();
214 /* Create .got directory (may already exist). */
215 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
216 err = got_error_from_errno();
219 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
220 err = got_error_from_errno();
224 /* Create an empty lock file. */
225 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
229 /* Create an empty file index. */
230 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
234 /* Write the HEAD reference. */
235 refstr = got_ref_to_str(head_ref);
236 if (refstr == NULL) {
237 err = got_error_from_errno();
240 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
244 /* Record our base commit. */
245 err = got_object_id_str(&basestr, commit_id);
248 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
252 /* Store path to repository. */
253 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
254 got_repo_get_path(repo));
258 /* Store in-repository path prefix. */
259 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
260 absprefix ? absprefix : prefix);
264 /* Stamp work tree with format file. */
265 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
266 err = got_error_from_errno();
269 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
283 const struct got_error *
284 got_worktree_open(struct got_worktree **worktree, const char *path)
286 const struct got_error *err = NULL;
288 char *formatstr = NULL;
289 char *path_lock = NULL;
290 char *base_commit_id_str = NULL;
291 char *head_ref_str = NULL;
292 int version, fd = -1;
294 struct got_repository *repo = NULL;
298 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
299 err = got_error_from_errno();
304 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
305 err = got_error_from_errno();
310 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
312 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
313 : got_error_from_errno());
317 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
321 version = strtonum(formatstr, 1, INT_MAX, &errstr);
323 err = got_error(GOT_ERR_WORKTREE_META);
326 if (version != GOT_WORKTREE_FORMAT_VERSION) {
327 err = got_error(GOT_ERR_WORKTREE_VERS);
331 *worktree = calloc(1, sizeof(**worktree));
332 if (*worktree == NULL) {
333 err = got_error_from_errno();
336 (*worktree)->lockfd = -1;
338 (*worktree)->root_path = strdup(path);
339 if ((*worktree)->root_path == NULL) {
340 err = got_error_from_errno();
343 err = read_meta_file(&(*worktree)->repo_path, path_got,
344 GOT_WORKTREE_REPOSITORY);
348 err = read_meta_file(&(*worktree)->path_prefix, path_got,
349 GOT_WORKTREE_PATH_PREFIX);
353 err = read_meta_file(&base_commit_id_str, path_got,
354 GOT_WORKTREE_BASE_COMMIT);
358 err = got_repo_open(&repo, (*worktree)->repo_path);
362 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
367 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
371 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
374 got_repo_close(repo);
378 free(base_commit_id_str);
382 if (*worktree != NULL)
383 got_worktree_close(*worktree);
386 (*worktree)->lockfd = fd;
392 got_worktree_close(struct got_worktree *worktree)
394 free(worktree->root_path);
395 free(worktree->repo_path);
396 free(worktree->path_prefix);
397 free(worktree->base_commit_id);
398 if (worktree->head_ref)
399 got_ref_close(worktree->head_ref);
400 if (worktree->lockfd != -1)
401 close(worktree->lockfd);
406 got_worktree_get_repo_path(struct got_worktree *worktree)
408 return worktree->repo_path;
412 got_worktree_get_path_prefix(struct got_worktree *worktree)
414 return worktree->path_prefix;
417 const struct got_error *
418 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
419 const char *path_prefix)
421 char *absprefix = NULL;
423 if (!got_path_is_absolute(path_prefix)) {
424 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
425 return got_error_from_errno();
427 *match = (strcmp(absprefix ? absprefix : path_prefix,
428 worktree->path_prefix) == 0);
434 got_worktree_get_head_ref_name(struct got_worktree *worktree)
436 return got_ref_to_str(worktree->head_ref);
439 struct got_reference *
440 got_worktree_get_head_ref(struct got_worktree *worktree)
442 return got_ref_dup(worktree->head_ref);
445 const struct got_object_id *
446 got_worktree_get_base_commit_id(struct got_worktree *worktree)
448 return worktree->base_commit_id;
451 const struct got_error *
452 got_worktree_set_base_commit_id(struct got_worktree *worktree,
453 struct got_repository *repo, struct got_object_id *commit_id)
455 const struct got_error *err;
456 struct got_object *obj = NULL;
458 char *path_got = NULL;
460 if (asprintf(&path_got, "%s/%s", worktree->root_path,
461 GOT_WORKTREE_GOT_DIR) == -1) {
462 err = got_error_from_errno();
467 err = got_object_open(&obj, repo, commit_id);
471 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
472 err = got_error(GOT_ERR_OBJ_TYPE);
476 /* Record our base commit. */
477 err = got_object_id_str(&id_str, commit_id);
480 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
484 free(worktree->base_commit_id);
485 worktree->base_commit_id = got_object_id_dup(commit_id);
486 if (worktree->base_commit_id == NULL) {
487 err = got_error_from_errno();
492 got_object_close(obj);
498 static const struct got_error *
499 lock_worktree(struct got_worktree *worktree, int operation)
501 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
502 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
503 : got_error_from_errno());
508 apply_path_prefix(struct got_worktree *worktree, const char *path)
510 const char *p = path;
511 p += strlen(worktree->path_prefix);
517 static const struct got_error *
518 blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
519 struct got_fileindex_entry *entry, const char *path,
520 struct got_blob_object *blob, struct got_repository *repo,
521 got_worktree_checkout_cb progress_cb, void *progress_arg,
522 const char *progress_path)
524 const struct got_error *err = NULL;
529 char *tmppath = NULL;
531 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
532 apply_path_prefix(worktree, path)) == -1)
533 return got_error_from_errno();
535 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
536 GOT_DEFAULT_FILE_MODE);
538 err = got_error_from_errno();
539 if (errno == EEXIST) {
541 if (lstat(ondisk_path, &sb) == -1) {
542 err = got_error_from_errno();
544 } else if (!S_ISREG(sb.st_mode)) {
545 /* TODO file is obstructed; do something */
546 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
549 err = got_opentemp_named_fd(&tmppath, &fd,
559 (*progress_cb)(progress_arg,
560 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
562 hdrlen = got_object_blob_get_hdrlen(blob);
564 const uint8_t *buf = got_object_blob_get_read_buf(blob);
565 err = got_object_blob_read_block(&len, blob);
569 /* Skip blob object header first time around. */
570 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
572 err = got_error_from_errno();
574 } else if (outlen != len - hdrlen) {
575 err = got_error(GOT_ERR_IO);
585 if (rename(tmppath, ondisk_path) != 0) {
586 err = got_error_from_errno();
592 err = got_fileindex_entry_update(entry, ondisk_path,
593 blob->id.sha1, worktree->base_commit_id->sha1);
595 err = got_fileindex_entry_alloc(&entry, ondisk_path,
596 apply_path_prefix(worktree, path), blob->id.sha1,
597 worktree->base_commit_id->sha1);
600 err = got_fileindex_entry_add(fileindex, entry);
612 static const struct got_error *
613 add_dir_on_disk(struct got_worktree *worktree, const char *path)
615 const struct got_error *err = NULL;
618 if (asprintf(&abspath, "%s/%s", worktree->root_path,
619 apply_path_prefix(worktree, path)) == -1)
620 return got_error_from_errno();
622 /* XXX queue work rather than editing disk directly? */
623 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
626 if (errno != EEXIST) {
627 err = got_error_from_errno();
631 if (lstat(abspath, &sb) == -1) {
632 err = got_error_from_errno();
636 if (!S_ISDIR(sb.st_mode)) {
637 /* TODO directory is obstructed; do something */
638 return got_error(GOT_ERR_FILE_OBSTRUCTED);
647 static const struct got_error *
648 tree_checkout(struct got_worktree *, struct got_fileindex *,
649 struct got_tree_object *, const char *, struct got_repository *,
650 got_worktree_checkout_cb progress_cb, void *progress_arg,
651 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
653 static const struct got_error *
654 tree_checkout_entry(struct got_worktree *worktree,
655 struct got_fileindex *fileindex, struct got_tree_entry *te,
656 const char *parent, struct got_repository *repo,
657 got_worktree_checkout_cb progress_cb, void *progress_arg,
658 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
660 const struct got_error *err = NULL;
661 struct got_object *obj = NULL;
662 struct got_blob_object *blob = NULL;
663 struct got_fileindex_entry *entry = NULL;
664 struct got_tree_object *tree = NULL;
666 char *progress_path = NULL;
669 if (parent[0] == '/' && parent[1] == '\0')
671 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
672 return got_error_from_errno();
674 /* Skip this entry if it is outside of our path prefix. */
675 len = MIN(strlen(worktree->path_prefix), strlen(path));
676 if (strncmp(path, worktree->path_prefix, len) != 0) {
681 err = got_object_open(&obj, repo, te->id);
685 progress_path = path;
686 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
687 progress_path += len;
690 case GOT_OBJ_TYPE_BLOB:
691 if (strlen(worktree->path_prefix) >= strlen(path))
693 entry = got_fileindex_entry_get(fileindex,
694 apply_path_prefix(worktree, path));
696 memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
697 SHA1_DIGEST_LENGTH) == 0) {
698 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
702 if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
703 SHA1_DIGEST_LENGTH) == 0)
705 err = got_object_blob_open(&blob, repo, obj, 8192);
708 err = blob_checkout(worktree, fileindex, entry, path, blob,
709 repo, progress_cb, progress_arg, progress_path);
711 case GOT_OBJ_TYPE_TREE:
712 if (strlen(worktree->path_prefix) < strlen(path)) {
713 err = add_dir_on_disk(worktree, path);
717 err = got_object_tree_open(&tree, repo, obj);
720 /* XXX infinite recursion possible */
721 err = tree_checkout(worktree, fileindex, tree, path, repo,
722 progress_cb, progress_arg, cancel_cb, cancel_arg);
730 got_object_blob_close(blob);
732 got_object_tree_close(tree);
734 got_object_close(obj);
739 struct collect_missing_entry_args {
740 struct got_fileindex *fileindex;
741 const struct got_tree_entries *entries;
742 struct got_fileindex missing_entries;
743 const char *path_prefix;
746 static const struct got_error *
747 collect_missing_file(void *args, struct got_fileindex_entry *entry)
749 struct collect_missing_entry_args *a = args;
751 struct got_tree_entry *te;
754 if (a->path_prefix[0] == '\0' && strchr(entry->path, '/') != NULL)
756 if (a->path_prefix[0] != '\0' &&
757 strncmp(a->path_prefix, entry->path, strlen(a->path_prefix)) != 0)
760 name = basename(entry->path);
762 return got_error_from_errno();
764 SIMPLEQ_FOREACH(te, &a->entries->head, entry) {
765 if (strcmp(te->name, name) == 0) {
774 got_fileindex_entry_remove(a->fileindex, entry);
775 return got_fileindex_entry_add(&a->missing_entries, entry);
778 /* Remove files which exist in the file index but not in the tree. */
779 static const struct got_error *
780 remove_missing_files(struct got_worktree *worktree, const char *path,
781 struct got_fileindex *fileindex, const struct got_tree_entries *entries,
782 got_worktree_checkout_cb progress_cb, void *progress_arg,
783 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
785 const struct got_error *err = NULL;
786 struct collect_missing_entry_args a;
787 struct got_fileindex_entry *entry, *tmp;
789 a.fileindex = fileindex;
791 a.missing_entries.nentries = 0;
792 a.path_prefix = path;
793 while (a.path_prefix[0] == '/')
795 TAILQ_INIT(&a.missing_entries.entries);
796 err = got_fileindex_for_each_entry(fileindex, collect_missing_file, &a);
800 TAILQ_FOREACH_SAFE(entry, &a.missing_entries.entries, entry, tmp) {
801 char *ondisk_path = NULL;
804 err = (*cancel_cb)(cancel_arg);
809 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, entry->path);
811 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
812 entry->path) == -1) {
813 err = got_error_from_errno();
817 if (unlink(ondisk_path) == -1)
818 err = got_error_from_errno();
823 TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
824 got_fileindex_entry_free(entry);
828 while (!TAILQ_EMPTY(&a.missing_entries.entries)) {
829 entry = TAILQ_FIRST(&a.missing_entries.entries);
830 TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
831 got_fileindex_entry_free(entry);
838 static const struct got_error *
839 tree_checkout(struct got_worktree *worktree,
840 struct got_fileindex *fileindex, struct got_tree_object *tree,
841 const char *path, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg,
843 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
845 const struct got_error *err = NULL;
846 const struct got_tree_entries *entries;
847 struct got_tree_entry *te;
850 /* Skip this tree if it is outside of our path prefix. */
851 len = MIN(strlen(worktree->path_prefix), strlen(path));
852 if (strncmp(path, worktree->path_prefix, len) != 0)
855 entries = got_object_tree_get_entries(tree);
856 SIMPLEQ_FOREACH(te, &entries->head, entry) {
858 err = (*cancel_cb)(cancel_arg);
862 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
863 progress_cb, progress_arg, cancel_cb, cancel_arg);
868 len = strlen(worktree->path_prefix);
869 if (strncmp(worktree->path_prefix, path, len) == 0) {
870 err = remove_missing_files(worktree, path, fileindex, entries,
871 progress_cb, progress_arg, cancel_cb, cancel_arg);
879 const struct got_error *
880 got_worktree_checkout_files(struct got_worktree *worktree,
881 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
882 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
884 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
885 struct got_commit_object *commit = NULL;
886 struct got_tree_object *tree = NULL;
887 char *fileindex_path = NULL, *new_fileindex_path = NULL;
888 struct got_fileindex *fileindex = NULL;
889 FILE *index = NULL, *new_index = NULL;
891 err = lock_worktree(worktree, LOCK_EX);
895 fileindex = got_fileindex_alloc();
896 if (fileindex == NULL) {
897 err = got_error_from_errno();
901 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
902 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
903 err = got_error_from_errno();
904 fileindex_path = NULL;
909 * Read the file index.
910 * Checking out files is supposed to be an idempotent operation.
911 * If the on-disk file index is incomplete we will try to complete it.
913 index = fopen(fileindex_path, "rb");
915 err = got_error_from_errno();
918 err = got_fileindex_read(fileindex, index);
923 err = got_opentemp_named(&new_fileindex_path, &new_index,
928 err = got_object_open_as_commit(&commit, repo,
929 worktree->base_commit_id);
933 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
937 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
938 progress_cb, progress_arg, cancel_cb, cancel_arg);
940 /* Try to sync the fileindex back to disk in any case. */
941 err = got_fileindex_write(fileindex, new_index);
945 if (rename(new_fileindex_path, fileindex_path) != 0) {
946 err = got_error_from_errno();
950 free(new_fileindex_path);
951 new_fileindex_path = NULL;
955 got_object_tree_close(tree);
957 got_object_commit_close(commit);
958 if (new_fileindex_path)
959 unlink(new_fileindex_path);
962 free(new_fileindex_path);
963 free(fileindex_path);
964 got_fileindex_free(fileindex);
967 unlockerr = lock_worktree(worktree, LOCK_SH);
968 if (unlockerr && err == NULL)