2 * Copyright (c) 2018, 2019 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>
35 #include "got_error.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_object.h"
39 #include "got_worktree.h"
40 #include "got_opentemp.h"
42 #include "got_lib_worktree.h"
43 #include "got_lib_path.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_fileindex.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
51 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 static const struct got_error *
55 create_meta_file(const char *path_got, const char *name, const char *content)
57 const struct got_error *err = NULL;
61 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
62 err = got_error_from_errno();
67 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
68 GOT_DEFAULT_FILE_MODE);
70 err = got_error_from_errno();
75 int len = dprintf(fd, "%s\n", content);
76 if (len != strlen(content) + 1) {
77 err = got_error_from_errno();
83 if (fd != -1 && close(fd) == -1 && err == NULL)
84 err = got_error_from_errno();
89 static const struct got_error *
90 update_meta_file(const char *path_got, const char *name, const char *content)
92 const struct got_error *err = NULL;
97 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 err = got_error_from_errno();
103 err = got_opentemp_named(&tmppath, &tmpfile, path);
108 int len = fprintf(tmpfile, "%s\n", content);
109 if (len != strlen(content) + 1) {
110 err = got_error_from_errno();
115 if (rename(tmppath, path) != 0) {
116 err = got_error_from_errno();
126 static const struct got_error *
127 read_meta_file(char **content, const char *path_got, const char *name)
129 const struct got_error *err = NULL;
137 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
138 err = got_error_from_errno();
143 fd = open(path, O_RDONLY | O_NOFOLLOW);
145 err = got_error_from_errno();
148 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
149 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
150 : got_error_from_errno());
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno();
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno() :
164 got_error(GOT_ERR_WORKTREE_META));
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error(GOT_ERR_WORKTREE_META);
171 (*content)[sb.st_size - 1] = '\0';
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno();
184 const struct got_error *
185 got_worktree_init(const char *path, struct got_reference *head_ref,
186 const char *prefix, struct got_repository *repo)
188 const struct got_error *err = NULL;
189 struct got_object_id *commit_id = NULL;
191 char *path_got = NULL;
193 char *formatstr = NULL;
194 char *absprefix = NULL;
195 char *basestr = NULL;
197 err = got_ref_resolve(&commit_id, repo, head_ref);
200 err = got_object_get_type(&obj_type, repo, commit_id);
203 if (obj_type != GOT_OBJ_TYPE_COMMIT)
204 return got_error(GOT_ERR_OBJ_TYPE);
206 if (!got_path_is_absolute(prefix)) {
207 if (asprintf(&absprefix, "/%s", prefix) == -1)
208 return got_error_from_errno();
211 /* Create top-level directory (may already exist). */
212 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
213 err = got_error_from_errno();
217 /* Create .got directory (may already exist). */
218 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
219 err = got_error_from_errno();
222 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
223 err = got_error_from_errno();
227 /* Create an empty lock file. */
228 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
232 /* Create an empty file index. */
233 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
237 /* Write the HEAD reference. */
238 refstr = got_ref_to_str(head_ref);
239 if (refstr == NULL) {
240 err = got_error_from_errno();
243 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
247 /* Record our base commit. */
248 err = got_object_id_str(&basestr, commit_id);
251 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
255 /* Store path to repository. */
256 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
257 got_repo_get_path(repo));
261 /* Store in-repository path prefix. */
262 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
263 absprefix ? absprefix : prefix);
267 /* Stamp work tree with format file. */
268 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
269 err = got_error_from_errno();
272 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
286 const struct got_error *
287 got_worktree_open(struct got_worktree **worktree, const char *path)
289 const struct got_error *err = NULL;
291 char *formatstr = NULL;
292 char *path_lock = NULL;
293 char *base_commit_id_str = NULL;
294 char *head_ref_str = NULL;
295 int version, fd = -1;
297 struct got_repository *repo = NULL;
301 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
302 err = got_error_from_errno();
307 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
308 err = got_error_from_errno();
313 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
315 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
316 : got_error_from_errno());
320 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
324 version = strtonum(formatstr, 1, INT_MAX, &errstr);
326 err = got_error(GOT_ERR_WORKTREE_META);
329 if (version != GOT_WORKTREE_FORMAT_VERSION) {
330 err = got_error(GOT_ERR_WORKTREE_VERS);
334 *worktree = calloc(1, sizeof(**worktree));
335 if (*worktree == NULL) {
336 err = got_error_from_errno();
339 (*worktree)->lockfd = -1;
341 (*worktree)->root_path = strdup(path);
342 if ((*worktree)->root_path == NULL) {
343 err = got_error_from_errno();
346 err = read_meta_file(&(*worktree)->repo_path, path_got,
347 GOT_WORKTREE_REPOSITORY);
351 err = read_meta_file(&(*worktree)->path_prefix, path_got,
352 GOT_WORKTREE_PATH_PREFIX);
356 err = read_meta_file(&base_commit_id_str, path_got,
357 GOT_WORKTREE_BASE_COMMIT);
361 err = got_repo_open(&repo, (*worktree)->repo_path);
365 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
370 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
374 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
377 got_repo_close(repo);
381 free(base_commit_id_str);
385 if (*worktree != NULL)
386 got_worktree_close(*worktree);
389 (*worktree)->lockfd = fd;
395 got_worktree_close(struct got_worktree *worktree)
397 free(worktree->root_path);
398 free(worktree->repo_path);
399 free(worktree->path_prefix);
400 free(worktree->base_commit_id);
401 if (worktree->head_ref)
402 got_ref_close(worktree->head_ref);
403 if (worktree->lockfd != -1)
404 close(worktree->lockfd);
409 got_worktree_get_repo_path(struct got_worktree *worktree)
411 return worktree->repo_path;
415 got_worktree_get_path_prefix(struct got_worktree *worktree)
417 return worktree->path_prefix;
420 const struct got_error *
421 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
422 const char *path_prefix)
424 char *absprefix = NULL;
426 if (!got_path_is_absolute(path_prefix)) {
427 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
428 return got_error_from_errno();
430 *match = (strcmp(absprefix ? absprefix : path_prefix,
431 worktree->path_prefix) == 0);
437 got_worktree_get_head_ref_name(struct got_worktree *worktree)
439 return got_ref_to_str(worktree->head_ref);
442 struct got_reference *
443 got_worktree_get_head_ref(struct got_worktree *worktree)
445 return got_ref_dup(worktree->head_ref);
448 const struct got_object_id *
449 got_worktree_get_base_commit_id(struct got_worktree *worktree)
451 return worktree->base_commit_id;
454 const struct got_error *
455 got_worktree_set_base_commit_id(struct got_worktree *worktree,
456 struct got_repository *repo, struct got_object_id *commit_id)
458 const struct got_error *err;
459 struct got_object *obj = NULL;
461 char *path_got = NULL;
463 if (asprintf(&path_got, "%s/%s", worktree->root_path,
464 GOT_WORKTREE_GOT_DIR) == -1) {
465 err = got_error_from_errno();
470 err = got_object_open(&obj, repo, commit_id);
474 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
475 err = got_error(GOT_ERR_OBJ_TYPE);
479 /* Record our base commit. */
480 err = got_object_id_str(&id_str, commit_id);
483 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
487 free(worktree->base_commit_id);
488 worktree->base_commit_id = got_object_id_dup(commit_id);
489 if (worktree->base_commit_id == NULL) {
490 err = got_error_from_errno();
495 got_object_close(obj);
501 static const struct got_error *
502 lock_worktree(struct got_worktree *worktree, int operation)
504 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
505 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
506 : got_error_from_errno());
510 static const struct got_error *
511 make_parent_dirs(const char *abspath)
513 const struct got_error *err = NULL;
515 char *parent = dirname(abspath);
519 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
520 if (errno == ENOENT) {
521 err = make_parent_dirs(parent);
524 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
525 return got_error_from_errno();
527 err = got_error_from_errno();
533 static const struct got_error *
534 add_dir_on_disk(struct got_worktree *worktree, const char *path)
536 const struct got_error *err = NULL;
539 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
540 return got_error_from_errno();
542 /* XXX queue work rather than editing disk directly? */
543 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
546 if (errno == EEXIST) {
547 if (lstat(abspath, &sb) == -1) {
548 err = got_error_from_errno();
552 if (!S_ISDIR(sb.st_mode)) {
553 /* TODO directory is obstructed; do something */
554 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
559 } else if (errno == ENOENT) {
560 err = make_parent_dirs(abspath);
563 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
564 err = got_error_from_errno();
566 err = got_error_from_errno();
574 static const struct got_error *
575 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
576 struct got_fileindex_entry *entry, const char *path,
577 struct got_blob_object *blob,
578 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
581 const struct got_error *err = NULL;
586 char *tmppath = NULL;
588 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
589 return got_error_from_errno();
591 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
592 GOT_DEFAULT_FILE_MODE);
594 if (errno == ENOENT) {
595 char *parent = dirname(path);
597 return got_error_from_errno();
598 err = add_dir_on_disk(worktree, parent);
601 fd = open(ondisk_path,
602 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
603 GOT_DEFAULT_FILE_MODE);
605 return got_error_from_errno();
606 } else if (errno == EEXIST) {
608 if (lstat(ondisk_path, &sb) == -1) {
609 err = got_error_from_errno();
611 } else if (!S_ISREG(sb.st_mode)) {
612 /* TODO file is obstructed; do something */
613 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
616 err = got_opentemp_named_fd(&tmppath, &fd,
623 return got_error_from_errno();
626 (*progress_cb)(progress_arg,
627 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
629 hdrlen = got_object_blob_get_hdrlen(blob);
631 const uint8_t *buf = got_object_blob_get_read_buf(blob);
632 err = got_object_blob_read_block(&len, blob);
636 /* Skip blob object header first time around. */
637 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
639 err = got_error_from_errno();
641 } else if (outlen != len - hdrlen) {
642 err = got_error(GOT_ERR_IO);
652 if (rename(tmppath, ondisk_path) != 0) {
653 err = got_error_from_errno();
659 entry = got_fileindex_entry_get(fileindex, path);
661 err = got_fileindex_entry_update(entry, ondisk_path,
662 blob->id.sha1, worktree->base_commit_id->sha1);
664 err = got_fileindex_entry_alloc(&entry, ondisk_path,
665 path, blob->id.sha1, worktree->base_commit_id->sha1);
668 err = got_fileindex_entry_add(fileindex, entry);
678 static const struct got_error *
679 update_blob(struct got_worktree *worktree,
680 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
681 struct got_tree_entry *te, const char *path,
682 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
683 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
685 const struct got_error *err = NULL;
686 struct got_blob_object *blob = NULL;
689 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
690 SHA1_DIGEST_LENGTH) == 0) {
691 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
695 if (memcmp(ie->blob_sha1,
696 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
700 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
704 err = install_blob(worktree, fileindex, ie, path, blob, repo,
705 progress_cb, progress_arg);
706 got_object_blob_close(blob);
710 static const struct got_error *
711 remove_ondisk_file(const char *root_path, const char *path)
713 const struct got_error *err = NULL;
714 char *ondisk_path = NULL;
716 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
717 return got_error_from_errno();
719 if (unlink(ondisk_path) == -1) {
721 err = got_error_from_errno();
723 char *parent = dirname(ondisk_path);
724 while (parent && strcmp(parent, root_path) != 0) {
725 if (rmdir(parent) == -1) {
726 if (errno != ENOTEMPTY)
727 err = got_error_from_errno();
730 parent = dirname(parent);
738 struct got_fileindex *fileindex;
739 struct got_worktree *worktree;
740 struct got_repository *repo;
741 got_worktree_checkout_cb progress_cb;
743 got_worktree_cancel_cb cancel_cb;
747 static const struct got_error *
748 diff_old_new(void *arg, struct got_fileindex_entry *ie,
749 struct got_tree_entry *te, const char *parent_path)
751 struct diff_cb_arg *a = arg;
753 return update_blob(a->worktree, a->fileindex, ie, te,
754 ie->path, a->repo, a->progress_cb, a->progress_arg,
755 a->cancel_cb, a->cancel_arg);
758 static const struct got_error *
759 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
761 const struct got_error *err;
762 struct diff_cb_arg *a = arg;
764 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
766 err = remove_ondisk_file(a->worktree->root_path, ie->path);
769 got_fileindex_entry_remove(a->fileindex, ie);
773 static const struct got_error *
774 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
776 struct diff_cb_arg *a = arg;
777 const struct got_error *err;
780 if (asprintf(&path, "%s%s%s", parent_path,
781 parent_path[0] ? "/" : "", te->name)
783 return got_error_from_errno();
785 if (S_ISDIR(te->mode))
786 err = add_dir_on_disk(a->worktree, path);
788 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
789 a->repo, a->progress_cb, a->progress_arg,
790 a->cancel_cb, a->cancel_arg);
796 const struct got_error *
797 got_worktree_checkout_files(struct got_worktree *worktree,
798 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
799 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
801 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
802 struct got_commit_object *commit = NULL;
803 struct got_object_id *tree_id = NULL;
804 struct got_tree_object *tree = NULL;
805 char *fileindex_path = NULL, *new_fileindex_path = NULL;
806 struct got_fileindex *fileindex = NULL;
807 FILE *index = NULL, *new_index = NULL;
808 struct got_fileindex_diff_tree_cb diff_cb;
809 struct diff_cb_arg arg;
811 err = lock_worktree(worktree, LOCK_EX);
815 fileindex = got_fileindex_alloc();
816 if (fileindex == NULL) {
817 err = got_error_from_errno();
821 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
822 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
823 err = got_error_from_errno();
824 fileindex_path = NULL;
829 * Read the file index.
830 * Checking out files is supposed to be an idempotent operation.
831 * If the on-disk file index is incomplete we will try to complete it.
833 index = fopen(fileindex_path, "rb");
835 if (errno != ENOENT) {
836 err = got_error_from_errno();
840 err = got_fileindex_read(fileindex, index);
846 err = got_opentemp_named(&new_fileindex_path, &new_index,
851 err = got_object_open_as_commit(&commit, repo,
852 worktree->base_commit_id);
856 err = got_object_id_by_path(&tree_id, repo,
857 worktree->base_commit_id, worktree->path_prefix);
861 err = got_object_open_as_tree(&tree, repo, tree_id);
865 diff_cb.diff_old_new = diff_old_new;
866 diff_cb.diff_old = diff_old;
867 diff_cb.diff_new = diff_new;
868 arg.fileindex = fileindex;
869 arg.worktree = worktree;
871 arg.progress_cb = progress_cb;
872 arg.progress_arg = progress_arg;
873 arg.cancel_cb = cancel_cb;
874 arg.cancel_arg = cancel_arg;
875 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
878 /* Try to sync the fileindex back to disk in any case. */
879 err = got_fileindex_write(fileindex, new_index);
883 if (rename(new_fileindex_path, fileindex_path) != 0) {
884 err = got_error_from_errno();
888 free(new_fileindex_path);
889 new_fileindex_path = NULL;
893 got_object_tree_close(tree);
895 got_object_commit_close(commit);
896 if (new_fileindex_path)
897 unlink(new_fileindex_path);
900 free(new_fileindex_path);
901 free(fileindex_path);
902 got_fileindex_free(fileindex);
905 unlockerr = lock_worktree(worktree, LOCK_SH);
906 if (unlockerr && err == NULL)