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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
22 #include <sys/syslimits.h>
37 #include "got_error.h"
38 #include "got_reference.h"
39 #include "got_repository.h"
40 #include "got_worktree.h"
41 #include "got_object.h"
43 #include "got_lib_path.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_pack.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_repository.h"
50 #include "got_lib_worktree.h"
51 #include "got_lib_object_idcache.h"
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 #define GOT_GIT_DIR ".git"
59 /* Mandatory files and directories inside the git directory. */
60 #define GOT_OBJECTS_DIR "objects"
61 #define GOT_REFS_DIR "refs"
62 #define GOT_HEAD_FILE "HEAD"
64 /* Other files and directories inside the git directory. */
65 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
66 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
67 #define GOT_OBJECTS_PACK_DIR "objects/pack"
70 got_repo_get_path(struct got_repository *repo)
72 return strdup(repo->path);
76 got_repo_get_path_git_dir(struct got_repository *repo)
78 return strdup(repo->path_git_dir);
82 got_repo_is_bare(struct got_repository *repo)
84 return (strcmp(repo->path, repo->path_git_dir) == 0);
88 get_path_git_child(struct got_repository *repo, const char *basename)
92 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
100 got_repo_get_path_objects(struct got_repository *repo)
102 return get_path_git_child(repo, GOT_OBJECTS_DIR);
106 got_repo_get_path_objects_pack(struct got_repository *repo)
108 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
112 got_repo_get_path_refs(struct got_repository *repo)
114 return get_path_git_child(repo, GOT_REFS_DIR);
118 get_path_head(struct got_repository *repo)
120 return get_path_git_child(repo, GOT_HEAD_FILE);
124 is_git_repo(struct got_repository *repo)
126 char *path_git = got_repo_get_path_git_dir(repo);
127 char *path_objects = got_repo_get_path_objects(repo);
128 char *path_refs = got_repo_get_path_refs(repo);
129 char *path_head = get_path_head(repo);
132 struct got_reference *head_ref;
134 if (lstat(path_git, &sb) == -1)
136 if (!S_ISDIR(sb.st_mode))
139 if (lstat(path_objects, &sb) == -1)
141 if (!S_ISDIR(sb.st_mode))
144 if (lstat(path_refs, &sb) == -1)
146 if (!S_ISDIR(sb.st_mode))
149 if (lstat(path_head, &sb) == -1)
151 if (!S_ISREG(sb.st_mode))
154 /* Check if the HEAD reference can be opened. */
155 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
157 got_ref_close(head_ref);
169 #ifndef GOT_NO_OBJ_CACHE
170 static const struct got_error *
171 cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
173 const struct got_error *err = NULL;
174 struct got_object_cache_entry *ce;
177 nelem = got_object_idcache_num_elements(cache->idcache);
178 if (nelem >= cache->size) {
179 err = got_object_idcache_remove_least_used((void **)&ce,
183 switch (cache->type) {
184 case GOT_OBJECT_CACHE_TYPE_OBJ:
185 got_object_close(ce->data.obj);
187 case GOT_OBJECT_CACHE_TYPE_TREE:
188 got_object_tree_close(ce->data.tree);
190 case GOT_OBJECT_CACHE_TYPE_COMMIT:
191 got_object_commit_close(ce->data.commit);
197 ce = calloc(1, sizeof(*ce));
199 return got_error_from_errno();
200 memcpy(&ce->id, id, sizeof(ce->id));
201 switch (cache->type) {
202 case GOT_OBJECT_CACHE_TYPE_OBJ:
203 ce->data.obj = (struct got_object *)item;
205 case GOT_OBJECT_CACHE_TYPE_TREE:
206 ce->data.tree = (struct got_tree_object *)item;
208 case GOT_OBJECT_CACHE_TYPE_COMMIT:
209 ce->data.commit = (struct got_commit_object *)item;
213 err = got_object_idcache_add(cache->idcache, id, ce);
215 if (err->code == GOT_ERR_OBJ_EXISTS) {
224 const struct got_error *
225 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
226 struct got_object *obj)
228 #ifndef GOT_NO_OBJ_CACHE
229 const struct got_error *err = NULL;
230 err = cache_add(&repo->objcache, id, obj);
239 got_repo_get_cached_object(struct got_repository *repo,
240 struct got_object_id *id)
242 struct got_object_cache_entry *ce;
244 ce = got_object_idcache_get(repo->objcache.idcache, id);
246 repo->objcache.cache_hit++;
250 repo->objcache.cache_miss++;
254 const struct got_error *
255 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
256 struct got_tree_object *tree)
258 #ifndef GOT_NO_OBJ_CACHE
259 const struct got_error *err = NULL;
260 err = cache_add(&repo->treecache, id, tree);
268 struct got_tree_object *
269 got_repo_get_cached_tree(struct got_repository *repo,
270 struct got_object_id *id)
272 struct got_object_cache_entry *ce;
274 ce = got_object_idcache_get(repo->treecache.idcache, id);
276 repo->treecache.cache_hit++;
277 return ce->data.tree;
280 repo->treecache.cache_miss++;
284 const struct got_error *
285 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
286 struct got_commit_object *commit)
288 #ifndef GOT_NO_OBJ_CACHE
289 const struct got_error *err = NULL;
290 err = cache_add(&repo->commitcache, id, commit);
299 struct got_commit_object *
300 got_repo_get_cached_commit(struct got_repository *repo,
301 struct got_object_id *id)
303 struct got_object_cache_entry *ce;
305 ce = got_object_idcache_get(repo->commitcache.idcache, id);
307 repo->commitcache.cache_hit++;
308 return ce->data.commit;
311 repo->commitcache.cache_miss++;
315 const struct got_error *
316 open_repo(struct got_repository *repo, const char *path)
318 const struct got_error *err = NULL;
319 struct got_worktree *worktree = NULL;
321 /* bare git repository? */
322 repo->path_git_dir = strdup(path);
323 if (repo->path_git_dir == NULL) {
324 err = got_error_from_errno();
327 if (is_git_repo(repo)) {
328 repo->path = strdup(repo->path_git_dir);
329 if (repo->path == NULL) {
330 err = got_error_from_errno();
336 /* git repository with working tree? */
337 free(repo->path_git_dir);
338 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
339 err = got_error_from_errno();
342 if (is_git_repo(repo)) {
343 repo->path = strdup(path);
344 if (repo->path == NULL) {
345 err = got_error_from_errno();
351 /* got work tree checked out from bare git repository? */
352 free(repo->path_git_dir);
353 repo->path_git_dir = NULL;
354 err = got_worktree_open(&worktree, path);
356 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
357 err = got_error(GOT_ERR_NOT_GIT_REPO);
360 repo->path_git_dir = strdup(worktree->repo_path);
361 if (repo->path_git_dir == NULL) {
362 err = got_error_from_errno();
366 /* got work tree checked out from git repository with working tree? */
367 if (!is_git_repo(repo)) {
368 free(repo->path_git_dir);
369 if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
370 GOT_GIT_DIR) == -1) {
371 err = got_error_from_errno();
372 repo->path_git_dir = NULL;
375 if (!is_git_repo(repo)) {
376 err = got_error(GOT_ERR_NOT_GIT_REPO);
379 repo->path = strdup(worktree->repo_path);
380 if (repo->path == NULL) {
381 err = got_error_from_errno();
385 repo->path = strdup(repo->path_git_dir);
386 if (repo->path == NULL) {
387 err = got_error_from_errno();
393 got_worktree_close(worktree);
397 const struct got_error *
398 got_repo_open(struct got_repository **repop, const char *path)
400 struct got_repository *repo = NULL;
401 const struct got_error *err = NULL;
402 char *abspath, *normpath = NULL;
403 int i, tried_root = 0;
407 if (got_path_is_absolute(path))
408 abspath = strdup(path);
410 abspath = got_path_get_absolute(path);
412 return got_error(GOT_ERR_BAD_PATH);
414 repo = calloc(1, sizeof(*repo));
416 err = got_error_from_errno();
420 for (i = 0; i < nitems(repo->privsep_children); i++) {
421 memset(&repo->privsep_children[i], 0,
422 sizeof(repo->privsep_children[0]));
423 repo->privsep_children[i].imsg_fd = -1;
426 repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
427 repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
428 repo->objcache.idcache = got_object_idcache_alloc(repo->objcache.size);
429 if (repo->objcache.idcache == NULL) {
430 err = got_error_from_errno();
434 repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
435 repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
436 repo->treecache.idcache =
437 got_object_idcache_alloc(repo->treecache.size);
438 if (repo->treecache.idcache == NULL) {
439 err = got_error_from_errno();
443 repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
444 repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
445 repo->commitcache.idcache =
446 got_object_idcache_alloc(repo->commitcache.size);
447 if (repo->commitcache.idcache == NULL) {
448 err = got_error_from_errno();
452 normpath = got_path_normalize(abspath);
453 if (normpath == NULL) {
454 err = got_error(GOT_ERR_BAD_PATH);
460 err = open_repo(repo, path);
463 if (err->code != GOT_ERR_NOT_GIT_REPO)
465 if (path[0] == '/' && path[1] == '\0') {
467 err = got_error(GOT_ERR_NOT_GIT_REPO);
472 path = dirname(path);
474 err = got_error_from_errno();
478 err = got_repo_close(repo);
488 print_cache_stats(struct got_object_cache *cache, const char *name)
490 fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
491 name, got_object_idcache_num_elements(cache->idcache),
492 cache->cache_hit, cache->cache_miss);
495 void check_refcount(struct got_object_id *id, void *data, void *arg)
497 struct got_object_cache *cache = arg;
498 struct got_object_cache_entry *ce = data;
499 struct got_object *obj;
500 struct got_tree_object *tree;
501 struct got_commit_object *commit;
504 if (got_object_id_str(&id_str, id) != NULL)
507 switch (cache->type) {
508 case GOT_OBJECT_CACHE_TYPE_OBJ:
510 if (obj->refcnt == 1)
512 fprintf(stderr, "object %s has %d unclaimed references\n",
513 id_str, obj->refcnt - 1);
515 case GOT_OBJECT_CACHE_TYPE_TREE:
516 tree = ce->data.tree;
517 if (tree->refcnt == 1)
519 fprintf(stderr, "tree %s has %d unclaimed references\n",
520 id_str, tree->refcnt - 1);
522 case GOT_OBJECT_CACHE_TYPE_COMMIT:
523 commit = ce->data.commit;
524 if (commit->refcnt == 1)
526 fprintf(stderr, "commit %s has %d unclaimed references\n",
527 id_str, commit->refcnt);
534 const struct got_error *
535 got_repo_close(struct got_repository *repo)
537 const struct got_error *err = NULL, *child_err;
540 for (i = 0; i < nitems(repo->packidx_cache); i++) {
541 if (repo->packidx_cache[i] == NULL)
543 got_packidx_close(repo->packidx_cache[i]);
546 for (i = 0; i < nitems(repo->packs); i++) {
547 if (repo->packs[i].path_packfile == NULL)
549 got_pack_close(&repo->packs[i]);
553 free(repo->path_git_dir);
556 print_cache_stats(&repo->objcache, "object");
557 print_cache_stats(&repo->treecache, "tree");
558 print_cache_stats(&repo->commitcache, "commit");
559 got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
561 got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
563 got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
567 if (repo->objcache.idcache)
568 got_object_idcache_free(repo->objcache.idcache);
569 if (repo->treecache.idcache)
570 got_object_idcache_free(repo->treecache.idcache);
571 if (repo->commitcache.idcache)
572 got_object_idcache_free(repo->commitcache.idcache);
574 for (i = 0; i < nitems(repo->privsep_children); i++) {
575 if (repo->privsep_children[i].imsg_fd == -1)
577 imsg_clear(repo->privsep_children[i].ibuf);
578 free(repo->privsep_children[i].ibuf);
579 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
580 child_err = got_privsep_wait_for_child(
581 repo->privsep_children[i].pid);
582 if (child_err && err == NULL)
584 close(repo->privsep_children[i].imsg_fd);
591 const struct got_error *
592 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
593 const char *input_path)
595 const struct got_error *err = NULL;
596 char *repo_abspath = NULL, *cwd = NULL;
598 size_t repolen, cwdlen, len;
599 char *canonpath, *path;
601 *in_repo_path = NULL;
603 cwd = getcwd(NULL, 0);
605 return got_error_from_errno();
607 canonpath = strdup(input_path);
608 if (canonpath == NULL) {
609 err = got_error_from_errno();
612 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
616 repo_abspath = got_repo_get_path(repo);
617 if (repo_abspath == NULL) {
618 err = got_error_from_errno();
622 /* TODO: Call "get in-repository path of work-tree node" API. */
624 if (lstat(canonpath, &sb) != 0) {
625 if (errno != ENOENT) {
626 err = got_error_from_errno();
630 * Path is not on disk.
631 * Assume it is already relative to repository root.
633 path = strdup(canonpath);
635 int is_repo_child = 0, is_cwd_child = 0;
637 path = realpath(canonpath, NULL);
639 err = got_error_from_errno();
643 repolen = strlen(repo_abspath);
644 cwdlen = strlen(cwd);
647 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
649 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
652 if (strcmp(path, repo_abspath) == 0) {
656 err = got_error_from_errno();
659 } else if (is_repo_child && is_cwd_child) {
661 /* TODO: Is path inside a got worktree? */
662 /* Strip common prefix with repository path. */
663 err = got_path_skip_common_ancestor(&child,
669 } else if (is_repo_child) {
670 /* Matched an on-disk path inside repository. */
671 if (got_repo_is_bare(repo)) {
673 * Matched an on-disk path inside repository
674 * database. Treat as repository-relative.
678 /* Strip common prefix with repository path. */
679 err = got_path_skip_common_ancestor(&child,
686 } else if (is_cwd_child) {
688 /* TODO: Is path inside a got worktree? */
689 /* Strip common prefix with cwd. */
690 err = got_path_skip_common_ancestor(&child, cwd,
698 * Matched unrelated on-disk path.
699 * Treat it as repository-relative.
704 /* Make in-repository path absolute */
705 if (path[0] != '/') {
707 if (asprintf(&abspath, "/%s", path) == -1) {
708 err = got_error_from_errno();
722 *in_repo_path = path;
726 const struct got_error *
727 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
729 const struct got_error *err = NULL;
732 for (i = 0; i < nitems(repo->packidx_cache); i++) {
733 if (repo->packidx_cache[i] == NULL)
737 if (i == nitems(repo->packidx_cache)) {
738 err = got_packidx_close(repo->packidx_cache[i - 1]);
741 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
742 sizeof(repo->packidx_cache) -
743 sizeof(repo->packidx_cache[0]));
747 repo->packidx_cache[i] = packidx;
752 is_packidx_filename(const char *name, size_t len)
754 if (len != GOT_PACKIDX_NAMELEN)
757 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
760 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
761 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
767 const struct got_error *
768 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
769 struct got_repository *repo, struct got_object_id *id)
771 const struct got_error *err;
778 /* Search pack index cache. */
779 for (i = 0; i < nitems(repo->packidx_cache); i++) {
780 if (repo->packidx_cache[i] == NULL)
782 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
784 *packidx = repo->packidx_cache[i];
788 /* No luck. Search the filesystem. */
790 path_packdir = got_repo_get_path_objects_pack(repo);
791 if (path_packdir == NULL)
792 return got_error_from_errno();
794 packdir = opendir(path_packdir);
795 if (packdir == NULL) {
796 err = got_error_from_errno();
800 while ((dent = readdir(packdir)) != NULL) {
801 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
804 if (asprintf(&path_packidx, "%s/%s", path_packdir,
805 dent->d_name) == -1) {
806 err = got_error_from_errno();
810 err = got_packidx_open(packidx, path_packidx, 0);
815 *idx = got_packidx_get_object_idx(*packidx, id);
817 err = NULL; /* found the object */
818 err = got_repo_cache_packidx(repo, *packidx);
822 err = got_packidx_close(*packidx);
828 err = got_error(GOT_ERR_NO_OBJ);
831 if (packdir && closedir(packdir) != 0 && err == 0)
832 err = got_error_from_errno();
836 static const struct got_error *
837 read_packfile_hdr(int fd, struct got_packidx *packidx)
839 const struct got_error *err = NULL;
840 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
841 struct got_packfile_hdr hdr;
844 n = read(fd, &hdr, sizeof(hdr));
846 return got_error_from_errno();
847 if (n != sizeof(hdr))
848 return got_error(GOT_ERR_BAD_PACKFILE);
850 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
851 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
852 betoh32(hdr.nobjects) != totobj)
853 err = got_error(GOT_ERR_BAD_PACKFILE);
858 static const struct got_error *
859 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
861 const struct got_error *err = NULL;
863 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
865 return got_error_from_errno();
868 err = read_packfile_hdr(*fd, packidx);
878 const struct got_error *
879 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
880 const char *path_packfile, struct got_packidx *packidx)
882 const struct got_error *err = NULL;
883 struct got_pack *pack = NULL;
889 for (i = 0; i < nitems(repo->packs); i++) {
890 pack = &repo->packs[i];
891 if (pack->path_packfile == NULL)
893 if (strcmp(pack->path_packfile, path_packfile) == 0)
897 if (i == nitems(repo->packs) - 1) {
898 err = got_pack_close(&repo->packs[i - 1]);
901 memmove(&repo->packs[1], &repo->packs[0],
902 sizeof(repo->packs) - sizeof(repo->packs[0]));
906 pack = &repo->packs[i];
908 pack->path_packfile = strdup(path_packfile);
909 if (pack->path_packfile == NULL) {
910 err = got_error_from_errno();
914 err = open_packfile(&pack->fd, path_packfile, packidx);
918 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
922 #ifndef GOT_PACK_NO_MMAP
923 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
925 if (pack->map == MAP_FAILED)
926 pack->map = NULL; /* fall back to read(2) */
931 free(pack->path_packfile);
932 memset(pack, 0, sizeof(*pack));
940 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
942 struct got_pack *pack = NULL;
945 for (i = 0; i < nitems(repo->packs); i++) {
946 pack = &repo->packs[i];
947 if (pack->path_packfile == NULL)
949 if (strcmp(pack->path_packfile, path_packfile) == 0)