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>
34 #include "got_error.h"
35 #include "got_repository.h"
36 #include "got_reference.h"
37 #include "got_object.h"
38 #include "got_worktree.h"
39 #include "got_opentemp.h"
41 #include "got_lib_worktree.h"
42 #include "got_lib_path.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_fileindex.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 static const struct got_error *
54 create_meta_file(const char *path_got, const char *name, const char *content)
56 const struct got_error *err = NULL;
60 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
61 err = got_error_from_errno();
66 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
67 GOT_DEFAULT_FILE_MODE);
69 err = got_error_from_errno();
74 int len = dprintf(fd, "%s\n", content);
75 if (len != strlen(content) + 1) {
76 err = got_error_from_errno();
82 if (fd != -1 && close(fd) == -1 && err == NULL)
83 err = got_error_from_errno();
88 static const struct got_error *
89 update_meta_file(const char *path_got, const char *name, const char *content)
91 const struct got_error *err = NULL;
96 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
97 err = got_error_from_errno();
102 err = got_opentemp_named(&tmppath, &tmpfile, path);
107 int len = fprintf(tmpfile, "%s\n", content);
108 if (len != strlen(content) + 1) {
109 err = got_error_from_errno();
114 if (rename(tmppath, path) != 0) {
115 err = got_error_from_errno();
125 static const struct got_error *
126 read_meta_file(char **content, const char *path_got, const char *name)
128 const struct got_error *err = NULL;
136 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
137 err = got_error_from_errno();
142 fd = open(path, O_RDONLY | O_NOFOLLOW);
144 err = got_error_from_errno();
147 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
148 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
149 : got_error_from_errno());
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno();
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno() :
163 got_error(GOT_ERR_WORKTREE_META));
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error(GOT_ERR_WORKTREE_META);
170 (*content)[sb.st_size - 1] = '\0';
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno();
183 const struct got_error *
184 got_worktree_init(const char *path, struct got_reference *head_ref,
185 const char *prefix, struct got_repository *repo)
187 const struct got_error *err = NULL;
188 struct got_object_id *commit_id = NULL;
190 char *path_got = NULL;
192 char *formatstr = NULL;
193 char *absprefix = NULL;
194 char *basestr = NULL;
196 err = got_ref_resolve(&commit_id, repo, head_ref);
199 err = got_object_get_type(&obj_type, repo, commit_id);
202 if (obj_type != GOT_OBJ_TYPE_COMMIT)
203 return got_error(GOT_ERR_OBJ_TYPE);
205 if (!got_path_is_absolute(prefix)) {
206 if (asprintf(&absprefix, "/%s", prefix) == -1)
207 return got_error_from_errno();
210 /* Create top-level directory (may already exist). */
211 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
212 err = got_error_from_errno();
216 /* Create .got directory (may already exist). */
217 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
218 err = got_error_from_errno();
221 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
222 err = got_error_from_errno();
226 /* Create an empty lock file. */
227 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
231 /* Create an empty file index. */
232 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
236 /* Write the HEAD reference. */
237 refstr = got_ref_to_str(head_ref);
238 if (refstr == NULL) {
239 err = got_error_from_errno();
242 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
246 /* Record our base commit. */
247 err = got_object_id_str(&basestr, commit_id);
250 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
254 /* Store path to repository. */
255 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
256 got_repo_get_path(repo));
260 /* Store in-repository path prefix. */
261 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
262 absprefix ? absprefix : prefix);
266 /* Stamp work tree with format file. */
267 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
268 err = got_error_from_errno();
271 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
285 const struct got_error *
286 got_worktree_open(struct got_worktree **worktree, const char *path)
288 const struct got_error *err = NULL;
290 char *formatstr = NULL;
291 char *path_lock = NULL;
292 char *base_commit_id_str = NULL;
293 char *head_ref_str = NULL;
294 int version, fd = -1;
296 struct got_repository *repo = NULL;
300 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
301 err = got_error_from_errno();
306 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
307 err = got_error_from_errno();
312 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
314 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
315 : got_error_from_errno());
319 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
323 version = strtonum(formatstr, 1, INT_MAX, &errstr);
325 err = got_error(GOT_ERR_WORKTREE_META);
328 if (version != GOT_WORKTREE_FORMAT_VERSION) {
329 err = got_error(GOT_ERR_WORKTREE_VERS);
333 *worktree = calloc(1, sizeof(**worktree));
334 if (*worktree == NULL) {
335 err = got_error_from_errno();
338 (*worktree)->lockfd = -1;
340 (*worktree)->root_path = strdup(path);
341 if ((*worktree)->root_path == NULL) {
342 err = got_error_from_errno();
345 err = read_meta_file(&(*worktree)->repo_path, path_got,
346 GOT_WORKTREE_REPOSITORY);
350 err = read_meta_file(&(*worktree)->path_prefix, path_got,
351 GOT_WORKTREE_PATH_PREFIX);
355 err = read_meta_file(&base_commit_id_str, path_got,
356 GOT_WORKTREE_BASE_COMMIT);
360 err = got_repo_open(&repo, (*worktree)->repo_path);
364 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
369 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
373 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
376 got_repo_close(repo);
380 free(base_commit_id_str);
384 if (*worktree != NULL)
385 got_worktree_close(*worktree);
388 (*worktree)->lockfd = fd;
394 got_worktree_close(struct got_worktree *worktree)
396 free(worktree->root_path);
397 free(worktree->repo_path);
398 free(worktree->path_prefix);
399 free(worktree->base_commit_id);
400 if (worktree->head_ref)
401 got_ref_close(worktree->head_ref);
402 if (worktree->lockfd != -1)
403 close(worktree->lockfd);
408 got_worktree_get_repo_path(struct got_worktree *worktree)
410 return worktree->repo_path;
414 got_worktree_get_path_prefix(struct got_worktree *worktree)
416 return worktree->path_prefix;
419 const struct got_error *
420 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
421 const char *path_prefix)
423 char *absprefix = NULL;
425 if (!got_path_is_absolute(path_prefix)) {
426 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
427 return got_error_from_errno();
429 *match = (strcmp(absprefix ? absprefix : path_prefix,
430 worktree->path_prefix) == 0);
436 got_worktree_get_head_ref_name(struct got_worktree *worktree)
438 return got_ref_to_str(worktree->head_ref);
441 struct got_reference *
442 got_worktree_get_head_ref(struct got_worktree *worktree)
444 return got_ref_dup(worktree->head_ref);
447 const struct got_object_id *
448 got_worktree_get_base_commit_id(struct got_worktree *worktree)
450 return worktree->base_commit_id;
453 const struct got_error *
454 got_worktree_set_base_commit_id(struct got_worktree *worktree,
455 struct got_repository *repo, struct got_object_id *commit_id)
457 const struct got_error *err;
458 struct got_object *obj = NULL;
460 char *path_got = NULL;
462 if (asprintf(&path_got, "%s/%s", worktree->root_path,
463 GOT_WORKTREE_GOT_DIR) == -1) {
464 err = got_error_from_errno();
469 err = got_object_open(&obj, repo, commit_id);
473 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
474 err = got_error(GOT_ERR_OBJ_TYPE);
478 /* Record our base commit. */
479 err = got_object_id_str(&id_str, commit_id);
482 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
486 free(worktree->base_commit_id);
487 worktree->base_commit_id = got_object_id_dup(commit_id);
488 if (worktree->base_commit_id == NULL) {
489 err = got_error_from_errno();
494 got_object_close(obj);
500 static const struct got_error *
501 lock_worktree(struct got_worktree *worktree, int operation)
503 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
504 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
505 : got_error_from_errno());
509 static const struct got_error *
510 add_dir_on_disk(struct got_worktree *worktree, const char *path)
512 const struct got_error *err = NULL;
515 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
516 return got_error_from_errno();
518 /* XXX queue work rather than editing disk directly? */
519 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
522 if (errno != EEXIST) {
523 err = got_error_from_errno();
527 if (lstat(abspath, &sb) == -1) {
528 err = got_error_from_errno();
532 if (!S_ISDIR(sb.st_mode)) {
533 /* TODO directory is obstructed; do something */
534 return got_error(GOT_ERR_FILE_OBSTRUCTED);
543 static const struct got_error *
544 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
545 struct got_fileindex_entry *entry, const char *path,
546 struct got_blob_object *blob,
547 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
550 const struct got_error *err = NULL;
555 char *tmppath = NULL;
557 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
558 return got_error_from_errno();
560 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
561 GOT_DEFAULT_FILE_MODE);
563 if (errno == ENOENT) {
564 char *parent = dirname(path);
566 return got_error_from_errno();
567 err = add_dir_on_disk(worktree, parent);
570 fd = open(ondisk_path,
571 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
572 GOT_DEFAULT_FILE_MODE);
574 return got_error_from_errno();
575 } else if (errno == EEXIST) {
577 if (lstat(ondisk_path, &sb) == -1) {
578 err = got_error_from_errno();
580 } else if (!S_ISREG(sb.st_mode)) {
581 /* TODO file is obstructed; do something */
582 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
585 err = got_opentemp_named_fd(&tmppath, &fd,
592 return got_error_from_errno();
595 (*progress_cb)(progress_arg,
596 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
598 hdrlen = got_object_blob_get_hdrlen(blob);
600 const uint8_t *buf = got_object_blob_get_read_buf(blob);
601 err = got_object_blob_read_block(&len, blob);
605 /* Skip blob object header first time around. */
606 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
608 err = got_error_from_errno();
610 } else if (outlen != len - hdrlen) {
611 err = got_error(GOT_ERR_IO);
621 if (rename(tmppath, ondisk_path) != 0) {
622 err = got_error_from_errno();
628 err = got_fileindex_entry_update(entry, ondisk_path,
629 blob->id.sha1, worktree->base_commit_id->sha1);
631 err = got_fileindex_entry_alloc(&entry, ondisk_path,
632 path, blob->id.sha1, worktree->base_commit_id->sha1);
635 err = got_fileindex_entry_add(fileindex, entry);
647 static const struct got_error *
648 update_blob(struct got_worktree *worktree,
649 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
650 struct got_tree_entry *te, const char *path,
651 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
652 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
654 const struct got_error *err = NULL;
655 struct got_blob_object *blob = NULL;
658 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
659 SHA1_DIGEST_LENGTH) == 0) {
660 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
664 if (memcmp(ie->blob_sha1,
665 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
669 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
673 err = install_blob(worktree, fileindex, ie, path, blob, repo,
674 progress_cb, progress_arg);
675 got_object_blob_close(blob);
679 static const struct got_error *
680 remove_ondisk_file(const char *root_path, const char *path)
682 const struct got_error *err = NULL;
683 char *ondisk_path = NULL;
685 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
686 return got_error_from_errno();
688 if (unlink(ondisk_path) == -1)
689 err = got_error_from_errno();
691 char *parent = dirname(ondisk_path);
692 while (parent && strcmp(parent, root_path) != 0) {
693 if (rmdir(parent) == -1) {
694 if (errno != ENOTEMPTY)
695 err = got_error_from_errno();
698 parent = dirname(parent);
706 struct got_fileindex *fileindex;
707 struct got_worktree *worktree;
708 struct got_repository *repo;
709 got_worktree_checkout_cb progress_cb;
711 got_worktree_cancel_cb cancel_cb;
715 static const struct got_error *
716 diff_old_new(void *arg, struct got_fileindex_entry *ie,
717 struct got_tree_entry *te, const char *parent_path)
719 struct diff_cb_arg *a = arg;
721 return update_blob(a->worktree, a->fileindex, ie, te,
722 ie->path, a->repo, a->progress_cb, a->progress_arg,
723 a->cancel_cb, a->cancel_arg);
726 static const struct got_error *
727 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
729 const struct got_error *err;
730 struct diff_cb_arg *a = arg;
732 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
734 err = remove_ondisk_file(a->worktree->root_path, ie->path);
737 got_fileindex_entry_remove(a->fileindex, ie);
741 static const struct got_error *
742 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
744 struct diff_cb_arg *a = arg;
745 const struct got_error *err;
748 if (asprintf(&path, "%s%s%s", parent_path,
749 parent_path[0] ? "/" : "", te->name)
751 return got_error_from_errno();
753 if (S_ISDIR(te->mode))
754 err = add_dir_on_disk(a->worktree, path);
756 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
757 a->repo, a->progress_cb, a->progress_arg,
758 a->cancel_cb, a->cancel_arg);
764 const struct got_error *
765 got_worktree_checkout_files(struct got_worktree *worktree,
766 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
767 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
769 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
770 struct got_commit_object *commit = NULL;
771 struct got_object_id *tree_id = NULL;
772 struct got_tree_object *tree = NULL;
773 char *fileindex_path = NULL, *new_fileindex_path = NULL;
774 struct got_fileindex *fileindex = NULL;
775 FILE *index = NULL, *new_index = NULL;
776 struct got_fileindex_diff_cb diff_cb;
777 struct diff_cb_arg arg;
779 err = lock_worktree(worktree, LOCK_EX);
783 fileindex = got_fileindex_alloc();
784 if (fileindex == NULL) {
785 err = got_error_from_errno();
789 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
790 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
791 err = got_error_from_errno();
792 fileindex_path = NULL;
797 * Read the file index.
798 * Checking out files is supposed to be an idempotent operation.
799 * If the on-disk file index is incomplete we will try to complete it.
801 index = fopen(fileindex_path, "rb");
803 err = got_error_from_errno();
806 err = got_fileindex_read(fileindex, index);
811 err = got_opentemp_named(&new_fileindex_path, &new_index,
816 err = got_object_open_as_commit(&commit, repo,
817 worktree->base_commit_id);
821 err = got_object_id_by_path(&tree_id, repo,
822 worktree->base_commit_id, worktree->path_prefix);
826 err = got_object_open_as_tree(&tree, repo, tree_id);
830 diff_cb.diff_old_new = diff_old_new;
831 diff_cb.diff_old = diff_old;
832 diff_cb.diff_new = diff_new;
833 arg.fileindex = fileindex;
834 arg.worktree = worktree;
836 arg.progress_cb = progress_cb;
837 arg.progress_arg = progress_arg;
838 arg.cancel_cb = cancel_cb;
839 arg.cancel_arg = cancel_arg;
840 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
843 /* Try to sync the fileindex back to disk in any case. */
844 err = got_fileindex_write(fileindex, new_index);
848 if (rename(new_fileindex_path, fileindex_path) != 0) {
849 err = got_error_from_errno();
853 free(new_fileindex_path);
854 new_fileindex_path = NULL;
858 got_object_tree_close(tree);
860 got_object_commit_close(commit);
861 if (new_fileindex_path)
862 unlink(new_fileindex_path);
865 free(new_fileindex_path);
866 free(fileindex_path);
867 got_fileindex_free(fileindex);
870 unlockerr = lock_worktree(worktree, LOCK_SH);
871 if (unlockerr && err == NULL)