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 const struct got_object_id *
445 got_worktree_get_base_commit_id(struct got_worktree *worktree)
447 return worktree->base_commit_id;
450 const struct got_error *
451 got_worktree_set_base_commit_id(struct got_worktree *worktree,
452 struct got_repository *repo, struct got_object_id *commit_id)
454 const struct got_error *err;
455 struct got_object *obj = NULL;
457 char *path_got = NULL;
459 if (asprintf(&path_got, "%s/%s", worktree->root_path,
460 GOT_WORKTREE_GOT_DIR) == -1) {
461 err = got_error_from_errno();
466 err = got_object_open(&obj, repo, commit_id);
470 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
471 err = got_error(GOT_ERR_OBJ_TYPE);
475 /* Record our base commit. */
476 err = got_object_id_str(&id_str, commit_id);
479 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
483 free(worktree->base_commit_id);
484 worktree->base_commit_id = got_object_id_dup(commit_id);
485 if (worktree->base_commit_id == NULL) {
486 err = got_error_from_errno();
491 got_object_close(obj);
497 static const struct got_error *
498 lock_worktree(struct got_worktree *worktree, int operation)
500 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
501 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
502 : got_error_from_errno());
507 apply_path_prefix(struct got_worktree *worktree, const char *path)
509 const char *p = path;
510 p += strlen(worktree->path_prefix);
516 static const struct got_error *
517 blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
518 struct got_fileindex_entry *entry, const char *path,
519 struct got_blob_object *blob, struct got_repository *repo,
520 got_worktree_checkout_cb progress_cb, void *progress_arg,
521 const char *progress_path)
523 const struct got_error *err = NULL;
528 char *tmppath = NULL;
530 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
531 apply_path_prefix(worktree, path)) == -1)
532 return got_error_from_errno();
534 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
535 GOT_DEFAULT_FILE_MODE);
537 err = got_error_from_errno();
538 if (errno == EEXIST) {
540 if (lstat(ondisk_path, &sb) == -1) {
541 err = got_error_from_errno();
543 } else if (!S_ISREG(sb.st_mode)) {
544 /* TODO file is obstructed; do something */
545 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
548 err = got_opentemp_named_fd(&tmppath, &fd,
558 (*progress_cb)(progress_arg,
559 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
561 hdrlen = got_object_blob_get_hdrlen(blob);
563 const uint8_t *buf = got_object_blob_get_read_buf(blob);
564 err = got_object_blob_read_block(&len, blob);
568 /* Skip blob object header first time around. */
569 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
571 err = got_error_from_errno();
573 } else if (outlen != len - hdrlen) {
574 err = got_error(GOT_ERR_IO);
584 if (rename(tmppath, ondisk_path) != 0) {
585 err = got_error_from_errno();
591 err = got_fileindex_entry_update(entry, ondisk_path,
592 blob->id.sha1, worktree->base_commit_id->sha1);
594 err = got_fileindex_entry_alloc(&entry, ondisk_path,
595 apply_path_prefix(worktree, path), blob->id.sha1,
596 worktree->base_commit_id->sha1);
599 err = got_fileindex_entry_add(fileindex, entry);
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path,
618 apply_path_prefix(worktree, path)) == -1)
619 return got_error_from_errno();
621 /* XXX queue work rather than editing disk directly? */
622 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
625 if (errno != EEXIST) {
626 err = got_error_from_errno();
630 if (lstat(abspath, &sb) == -1) {
631 err = got_error_from_errno();
635 if (!S_ISDIR(sb.st_mode)) {
636 /* TODO directory is obstructed; do something */
637 return got_error(GOT_ERR_FILE_OBSTRUCTED);
646 static const struct got_error *
647 tree_checkout(struct got_worktree *, struct got_fileindex *,
648 struct got_tree_object *, const char *, struct got_repository *,
649 got_worktree_checkout_cb progress_cb, void *progress_arg,
650 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
652 static const struct got_error *
653 tree_checkout_entry(struct got_worktree *worktree,
654 struct got_fileindex *fileindex, struct got_tree_entry *te,
655 const char *parent, struct got_repository *repo,
656 got_worktree_checkout_cb progress_cb, void *progress_arg,
657 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
659 const struct got_error *err = NULL;
660 struct got_object *obj = NULL;
661 struct got_blob_object *blob = NULL;
662 struct got_fileindex_entry *entry = NULL;
663 struct got_tree_object *tree = NULL;
665 char *progress_path = NULL;
668 if (parent[0] == '/' && parent[1] == '\0')
670 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
671 return got_error_from_errno();
673 /* Skip this entry if it is outside of our path prefix. */
674 len = MIN(strlen(worktree->path_prefix), strlen(path));
675 if (strncmp(path, worktree->path_prefix, len) != 0) {
680 err = got_object_open(&obj, repo, te->id);
684 progress_path = path;
685 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
686 progress_path += len;
689 case GOT_OBJ_TYPE_BLOB:
690 if (strlen(worktree->path_prefix) >= strlen(path))
692 entry = got_fileindex_entry_get(fileindex,
693 apply_path_prefix(worktree, path));
695 memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
696 SHA1_DIGEST_LENGTH) == 0) {
697 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
701 if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
702 SHA1_DIGEST_LENGTH) == 0)
704 err = got_object_blob_open(&blob, repo, obj, 8192);
707 err = blob_checkout(worktree, fileindex, entry, path, blob,
708 repo, progress_cb, progress_arg, progress_path);
710 case GOT_OBJ_TYPE_TREE:
711 if (strlen(worktree->path_prefix) < strlen(path)) {
712 err = add_dir_on_disk(worktree, path);
716 err = got_object_tree_open(&tree, repo, obj);
719 /* XXX infinite recursion possible */
720 err = tree_checkout(worktree, fileindex, tree, path, repo,
721 progress_cb, progress_arg, cancel_cb, cancel_arg);
729 got_object_blob_close(blob);
731 got_object_tree_close(tree);
733 got_object_close(obj);
738 static const struct got_error *
739 tree_checkout(struct got_worktree *worktree,
740 struct got_fileindex *fileindex, struct got_tree_object *tree,
741 const char *path, struct got_repository *repo,
742 got_worktree_checkout_cb progress_cb, void *progress_arg,
743 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
745 const struct got_error *err = NULL;
746 const struct got_tree_entries *entries;
747 struct got_tree_entry *te;
750 /* Skip this tree if it is outside of our path prefix. */
751 len = MIN(strlen(worktree->path_prefix), strlen(path));
752 if (strncmp(path, worktree->path_prefix, len) != 0)
755 entries = got_object_tree_get_entries(tree);
756 SIMPLEQ_FOREACH(te, &entries->head, entry) {
758 err = (*cancel_cb)(cancel_arg);
762 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
763 progress_cb, progress_arg, cancel_cb, cancel_arg);
771 const struct got_error *
772 got_worktree_checkout_files(struct got_worktree *worktree,
773 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
774 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
776 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
777 struct got_commit_object *commit = NULL;
778 struct got_tree_object *tree = NULL;
779 char *fileindex_path = NULL, *new_fileindex_path = NULL;
780 struct got_fileindex *fileindex = NULL;
781 FILE *index = NULL, *new_index = NULL;
783 err = lock_worktree(worktree, LOCK_EX);
787 fileindex = got_fileindex_alloc();
788 if (fileindex == NULL) {
789 err = got_error_from_errno();
793 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
794 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
795 err = got_error_from_errno();
796 fileindex_path = NULL;
801 * Read the file index.
802 * Checking out files is supposed to be an idempotent operation.
803 * If the on-disk file index is incomplete we will try to complete it.
805 index = fopen(fileindex_path, "rb");
807 err = got_error_from_errno();
810 err = got_fileindex_read(fileindex, index);
815 err = got_opentemp_named(&new_fileindex_path, &new_index,
820 err = got_object_open_as_commit(&commit, repo,
821 worktree->base_commit_id);
825 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
829 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
830 progress_cb, progress_arg, cancel_cb, cancel_arg);
832 /* Try to sync the fileindex back to disk in any case. */
833 err = got_fileindex_write(fileindex, new_index);
837 if (rename(new_fileindex_path, fileindex_path) != 0) {
838 err = got_error_from_errno();
842 free(new_fileindex_path);
843 new_fileindex_path = NULL;
847 got_object_tree_close(tree);
849 got_object_commit_close(commit);
850 if (new_fileindex_path)
851 unlink(new_fileindex_path);
854 free(new_fileindex_path);
855 free(fileindex_path);
856 got_fileindex_free(fileindex);
859 unlockerr = lock_worktree(worktree, LOCK_SH);
860 if (unlockerr && err == NULL)