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>
31 #include "got_error.h"
32 #include "got_repository.h"
33 #include "got_reference.h"
34 #include "got_object.h"
35 #include "got_worktree.h"
36 #include "got_opentemp.h"
38 #include "got_lib_worktree.h"
39 #include "got_lib_path.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_fileindex.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_object.h"
47 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 static const struct got_error *
51 create_meta_file(const char *path_got, const char *name, const char *content)
53 const struct got_error *err = NULL;
57 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
58 err = got_error_from_errno();
63 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
64 GOT_DEFAULT_FILE_MODE);
66 err = got_error_from_errno();
71 int len = dprintf(fd, "%s\n", content);
72 if (len != strlen(content) + 1) {
73 err = got_error_from_errno();
79 if (fd != -1 && close(fd) == -1 && err == NULL)
80 err = got_error_from_errno();
85 static const struct got_error *
86 update_meta_file(const char *path_got, const char *name, const char *content)
88 const struct got_error *err = NULL;
93 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
94 err = got_error_from_errno();
99 err = got_opentemp_named(&tmppath, &tmpfile, path);
104 int len = fprintf(tmpfile, "%s\n", content);
105 if (len != strlen(content) + 1) {
106 err = got_error_from_errno();
111 if (rename(tmppath, path) != 0) {
112 err = got_error_from_errno();
122 static const struct got_error *
123 read_meta_file(char **content, const char *path_got, const char *name)
125 const struct got_error *err = NULL;
133 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
134 err = got_error_from_errno();
139 fd = open(path, O_RDONLY | O_NOFOLLOW);
141 err = got_error_from_errno();
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno());
151 *content = calloc(1, sb.st_size);
152 if (*content == NULL) {
153 err = got_error_from_errno();
157 n = read(fd, *content, sb.st_size);
158 if (n != sb.st_size) {
159 err = (n == -1 ? got_error_from_errno() :
160 got_error(GOT_ERR_WORKTREE_META));
163 if ((*content)[sb.st_size - 1] != '\n') {
164 err = got_error(GOT_ERR_WORKTREE_META);
167 (*content)[sb.st_size - 1] = '\0';
170 if (fd != -1 && close(fd) == -1 && err == NULL)
171 err = got_error_from_errno();
180 const struct got_error *
181 got_worktree_init(const char *path, struct got_reference *head_ref,
182 const char *prefix, struct got_repository *repo)
184 const struct got_error *err = NULL;
185 struct got_object_id *commit_id = NULL;
187 char *path_got = NULL;
189 char *repo_path = 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 repo_path = got_repo_get_path(repo);
254 if (repo_path == NULL) {
255 err = got_error_from_errno();
258 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
262 /* Store in-repository path prefix. */
263 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
264 absprefix ? absprefix : prefix);
268 /* Stamp work tree with format file. */
269 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
270 err = got_error_from_errno();
273 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
288 const struct got_error *
289 got_worktree_open(struct got_worktree **worktree, const char *path)
291 const struct got_error *err = NULL;
293 char *formatstr = NULL;
294 char *path_lock = NULL;
295 char *base_commit_id_str = NULL;
296 char *head_ref_str = NULL;
297 int version, fd = -1;
299 struct got_repository *repo = NULL;
303 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
304 err = got_error_from_errno();
309 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
310 err = got_error_from_errno();
315 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
317 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
318 : got_error_from_errno());
322 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
326 version = strtonum(formatstr, 1, INT_MAX, &errstr);
328 err = got_error(GOT_ERR_WORKTREE_META);
331 if (version != GOT_WORKTREE_FORMAT_VERSION) {
332 err = got_error(GOT_ERR_WORKTREE_VERS);
336 *worktree = calloc(1, sizeof(**worktree));
337 if (*worktree == NULL) {
338 err = got_error_from_errno();
341 (*worktree)->lockfd = -1;
343 (*worktree)->root_path = strdup(path);
344 if ((*worktree)->root_path == NULL) {
345 err = got_error_from_errno();
348 err = read_meta_file(&(*worktree)->repo_path, path_got,
349 GOT_WORKTREE_REPOSITORY);
353 err = read_meta_file(&(*worktree)->path_prefix, path_got,
354 GOT_WORKTREE_PATH_PREFIX);
358 err = read_meta_file(&base_commit_id_str, path_got,
359 GOT_WORKTREE_BASE_COMMIT);
363 err = got_repo_open(&repo, (*worktree)->repo_path);
367 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
372 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
376 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
379 got_repo_close(repo);
383 free(base_commit_id_str);
387 if (*worktree != NULL)
388 got_worktree_close(*worktree);
391 (*worktree)->lockfd = fd;
397 got_worktree_close(struct got_worktree *worktree)
399 free(worktree->root_path);
400 free(worktree->repo_path);
401 free(worktree->path_prefix);
402 free(worktree->base_commit_id);
403 if (worktree->head_ref)
404 got_ref_close(worktree->head_ref);
405 if (worktree->lockfd != -1)
406 close(worktree->lockfd);
411 got_worktree_get_repo_path(struct got_worktree *worktree)
413 return worktree->repo_path;
417 got_worktree_get_path_prefix(struct got_worktree *worktree)
419 return worktree->path_prefix;
422 const struct got_error *
423 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
424 const char *path_prefix)
426 char *absprefix = NULL;
428 if (!got_path_is_absolute(path_prefix)) {
429 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
430 return got_error_from_errno();
432 *match = (strcmp(absprefix ? absprefix : path_prefix,
433 worktree->path_prefix) == 0);
439 got_worktree_get_head_ref_name(struct got_worktree *worktree)
441 return got_ref_to_str(worktree->head_ref);
444 struct got_reference *
445 got_worktree_get_head_ref(struct got_worktree *worktree)
447 return got_ref_dup(worktree->head_ref);
450 const struct got_object_id *
451 got_worktree_get_base_commit_id(struct got_worktree *worktree)
453 return worktree->base_commit_id;
456 const struct got_error *
457 got_worktree_set_base_commit_id(struct got_worktree *worktree,
458 struct got_repository *repo, struct got_object_id *commit_id)
460 const struct got_error *err;
461 struct got_object *obj = NULL;
463 char *path_got = NULL;
465 if (asprintf(&path_got, "%s/%s", worktree->root_path,
466 GOT_WORKTREE_GOT_DIR) == -1) {
467 err = got_error_from_errno();
472 err = got_object_open(&obj, repo, commit_id);
476 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
477 err = got_error(GOT_ERR_OBJ_TYPE);
481 /* Record our base commit. */
482 err = got_object_id_str(&id_str, commit_id);
485 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
489 free(worktree->base_commit_id);
490 worktree->base_commit_id = got_object_id_dup(commit_id);
491 if (worktree->base_commit_id == NULL) {
492 err = got_error_from_errno();
497 got_object_close(obj);
503 static const struct got_error *
504 lock_worktree(struct got_worktree *worktree, int operation)
506 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
507 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
508 : got_error_from_errno());
513 apply_path_prefix(struct got_worktree *worktree, const char *path)
515 const char *p = path;
516 p += strlen(worktree->path_prefix);
522 static const struct got_error *
523 blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
524 struct got_fileindex_entry *entry, const char *path,
525 struct got_blob_object *blob, struct got_repository *repo,
526 got_worktree_checkout_cb progress_cb, void *progress_arg,
527 const char *progress_path)
529 const struct got_error *err = NULL;
534 char *tmppath = NULL;
536 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
537 apply_path_prefix(worktree, path)) == -1)
538 return got_error_from_errno();
540 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
541 GOT_DEFAULT_FILE_MODE);
543 err = got_error_from_errno();
544 if (errno == EEXIST) {
546 if (lstat(ondisk_path, &sb) == -1) {
547 err = got_error_from_errno();
549 } else if (!S_ISREG(sb.st_mode)) {
550 /* TODO file is obstructed; do something */
551 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
554 err = got_opentemp_named_fd(&tmppath, &fd,
564 (*progress_cb)(progress_arg,
565 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
567 hdrlen = got_object_blob_get_hdrlen(blob);
569 const uint8_t *buf = got_object_blob_get_read_buf(blob);
570 err = got_object_blob_read_block(&len, blob);
574 /* Skip blob object header first time around. */
575 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
577 err = got_error_from_errno();
579 } else if (outlen != len - hdrlen) {
580 err = got_error(GOT_ERR_IO);
590 if (rename(tmppath, ondisk_path) != 0) {
591 err = got_error_from_errno();
597 err = got_fileindex_entry_update(entry, ondisk_path,
598 blob->id.sha1, worktree->base_commit_id->sha1);
600 err = got_fileindex_entry_alloc(&entry, ondisk_path,
601 apply_path_prefix(worktree, path), blob->id.sha1,
602 worktree->base_commit_id->sha1);
605 err = got_fileindex_entry_add(fileindex, entry);
617 static const struct got_error *
618 add_dir_on_disk(struct got_worktree *worktree, const char *path)
620 const struct got_error *err = NULL;
623 if (asprintf(&abspath, "%s/%s", worktree->root_path,
624 apply_path_prefix(worktree, path)) == -1)
625 return got_error_from_errno();
627 /* XXX queue work rather than editing disk directly? */
628 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
631 if (errno != EEXIST) {
632 err = got_error_from_errno();
636 if (lstat(abspath, &sb) == -1) {
637 err = got_error_from_errno();
641 if (!S_ISDIR(sb.st_mode)) {
642 /* TODO directory is obstructed; do something */
643 return got_error(GOT_ERR_FILE_OBSTRUCTED);
652 static const struct got_error *
653 tree_checkout(struct got_worktree *, struct got_fileindex *,
654 struct got_tree_object *, const char *, struct got_repository *,
655 got_worktree_checkout_cb progress_cb, void *progress_arg,
656 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
658 static const struct got_error *
659 tree_checkout_entry(struct got_worktree *worktree,
660 struct got_fileindex *fileindex, struct got_tree_entry *te,
661 const char *parent, struct got_repository *repo,
662 got_worktree_checkout_cb progress_cb, void *progress_arg,
663 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
665 const struct got_error *err = NULL;
666 struct got_object *obj = NULL;
667 struct got_blob_object *blob = NULL;
668 struct got_fileindex_entry *entry = NULL;
669 struct got_tree_object *tree = NULL;
671 char *progress_path = NULL;
674 if (parent[0] == '/' && parent[1] == '\0')
676 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
677 return got_error_from_errno();
679 /* Skip this entry if it is outside of our path prefix. */
680 len = MIN(strlen(worktree->path_prefix), strlen(path));
681 if (strncmp(path, worktree->path_prefix, len) != 0) {
686 err = got_object_open(&obj, repo, te->id);
690 progress_path = path;
691 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
692 progress_path += len;
695 case GOT_OBJ_TYPE_BLOB:
696 if (strlen(worktree->path_prefix) >= strlen(path))
698 entry = got_fileindex_entry_get(fileindex,
699 apply_path_prefix(worktree, path));
701 memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
702 SHA1_DIGEST_LENGTH) == 0) {
703 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
707 if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
708 SHA1_DIGEST_LENGTH) == 0)
710 err = got_object_blob_open(&blob, repo, obj, 8192);
713 err = blob_checkout(worktree, fileindex, entry, path, blob,
714 repo, progress_cb, progress_arg, progress_path);
716 case GOT_OBJ_TYPE_TREE:
717 if (strlen(worktree->path_prefix) < strlen(path)) {
718 err = add_dir_on_disk(worktree, path);
722 err = got_object_tree_open(&tree, repo, obj);
725 /* XXX infinite recursion possible */
726 err = tree_checkout(worktree, fileindex, tree, path, repo,
727 progress_cb, progress_arg, cancel_cb, cancel_arg);
735 got_object_blob_close(blob);
737 got_object_tree_close(tree);
739 got_object_close(obj);
744 static const struct got_error *
745 tree_checkout(struct got_worktree *worktree,
746 struct got_fileindex *fileindex, struct got_tree_object *tree,
747 const char *path, struct got_repository *repo,
748 got_worktree_checkout_cb progress_cb, void *progress_arg,
749 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
751 const struct got_error *err = NULL;
752 const struct got_tree_entries *entries;
753 struct got_tree_entry *te;
756 /* Skip this tree if it is outside of our path prefix. */
757 len = MIN(strlen(worktree->path_prefix), strlen(path));
758 if (strncmp(path, worktree->path_prefix, len) != 0)
761 entries = got_object_tree_get_entries(tree);
762 SIMPLEQ_FOREACH(te, &entries->head, entry) {
764 err = (*cancel_cb)(cancel_arg);
768 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
769 progress_cb, progress_arg, cancel_cb, cancel_arg);
777 const struct got_error *
778 got_worktree_checkout_files(struct got_worktree *worktree,
779 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
780 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
782 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
783 struct got_commit_object *commit = NULL;
784 struct got_tree_object *tree = NULL;
785 char *fileindex_path = NULL, *new_fileindex_path = NULL;
786 struct got_fileindex *fileindex = NULL;
787 FILE *index = NULL, *new_index = NULL;
789 err = lock_worktree(worktree, LOCK_EX);
793 fileindex = got_fileindex_alloc();
794 if (fileindex == NULL) {
795 err = got_error_from_errno();
799 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
800 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
801 err = got_error_from_errno();
802 fileindex_path = NULL;
807 * Read the file index.
808 * Checking out files is supposed to be an idempotent operation.
809 * If the on-disk file index is incomplete we will try to complete it.
811 index = fopen(fileindex_path, "rb");
813 err = got_error_from_errno();
816 err = got_fileindex_read(fileindex, index);
821 err = got_opentemp_named(&new_fileindex_path, &new_index,
826 err = got_object_open_as_commit(&commit, repo,
827 worktree->base_commit_id);
831 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
835 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
836 progress_cb, progress_arg, cancel_cb, cancel_arg);
838 /* Try to sync the fileindex back to disk in any case. */
839 err = got_fileindex_write(fileindex, new_index);
843 if (rename(new_fileindex_path, fileindex_path) != 0) {
844 err = got_error_from_errno();
848 free(new_fileindex_path);
849 new_fileindex_path = NULL;
853 got_object_tree_close(tree);
855 got_object_commit_close(commit);
856 if (new_fileindex_path)
857 unlink(new_fileindex_path);
860 free(new_fileindex_path);
861 free(fileindex_path);
862 got_fileindex_free(fileindex);
865 unlockerr = lock_worktree(worktree, LOCK_SH);
866 if (unlockerr && err == NULL)