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/queue.h>
29 #include "got_error.h"
30 #include "got_reference.h"
31 #include "got_repository.h"
32 #include "got_worktree.h"
33 #include "got_object.h"
35 #include "got_lib_path.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
39 #include "got_lib_pack.h"
40 #include "got_lib_repository.h"
41 #include "got_lib_worktree.h"
42 #include "got_lib_object_idcache.h"
45 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 #define GOT_GIT_DIR ".git"
50 /* Mandatory files and directories inside the git directory. */
51 #define GOT_OBJECTS_DIR "objects"
52 #define GOT_REFS_DIR "refs"
53 #define GOT_HEAD_FILE "HEAD"
55 /* Other files and directories inside the git directory. */
56 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
57 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
58 #define GOT_OBJECTS_PACK_DIR "objects/pack"
61 got_repo_get_path(struct got_repository *repo)
63 return strdup(repo->path);
67 got_repo_get_path_git_dir(struct got_repository *repo)
69 return strdup(repo->path_git_dir);
73 got_repo_is_bare(struct got_repository *repo)
75 return (strcmp(repo->path, repo->path_git_dir) == 0);
79 get_path_git_child(struct got_repository *repo, const char *basename)
83 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
91 got_repo_get_path_objects(struct got_repository *repo)
93 return get_path_git_child(repo, GOT_OBJECTS_DIR);
97 got_repo_get_path_objects_pack(struct got_repository *repo)
99 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
103 got_repo_get_path_refs(struct got_repository *repo)
105 return get_path_git_child(repo, GOT_REFS_DIR);
109 get_path_head(struct got_repository *repo)
111 return get_path_git_child(repo, GOT_HEAD_FILE);
115 is_git_repo(struct got_repository *repo)
117 char *path_git = got_repo_get_path_git_dir(repo);
118 char *path_objects = got_repo_get_path_objects(repo);
119 char *path_refs = got_repo_get_path_refs(repo);
120 char *path_head = get_path_head(repo);
123 struct got_reference *head_ref;
125 if (lstat(path_git, &sb) == -1)
127 if (!S_ISDIR(sb.st_mode))
130 if (lstat(path_objects, &sb) == -1)
132 if (!S_ISDIR(sb.st_mode))
135 if (lstat(path_refs, &sb) == -1)
137 if (!S_ISDIR(sb.st_mode))
140 if (lstat(path_head, &sb) == -1)
142 if (!S_ISREG(sb.st_mode))
145 /* Check if the HEAD reference can be opened. */
146 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
148 got_ref_close(head_ref);
160 #ifndef GOT_NO_OBJ_CACHE
161 static const struct got_error *
162 cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
164 const struct got_error *err = NULL;
165 struct got_object_cache_entry *ce;
168 nelem = got_object_idcache_num_elements(cache->idcache);
169 if (nelem >= cache->size) {
170 err = got_object_idcache_remove_least_used((void **)&ce,
174 switch (cache->type) {
175 case GOT_OBJECT_CACHE_TYPE_OBJ:
176 got_object_close(ce->data.obj);
178 case GOT_OBJECT_CACHE_TYPE_TREE:
179 got_object_tree_close(ce->data.tree);
181 case GOT_OBJECT_CACHE_TYPE_COMMIT:
182 got_object_commit_close(ce->data.commit);
188 ce = calloc(1, sizeof(*ce));
190 return got_error_from_errno();
191 memcpy(&ce->id, id, sizeof(ce->id));
192 switch (cache->type) {
193 case GOT_OBJECT_CACHE_TYPE_OBJ:
194 ce->data.obj = (struct got_object *)item;
196 case GOT_OBJECT_CACHE_TYPE_TREE:
197 ce->data.tree = (struct got_tree_object *)item;
199 case GOT_OBJECT_CACHE_TYPE_COMMIT:
200 ce->data.commit = (struct got_commit_object *)item;
204 err = got_object_idcache_add(cache->idcache, id, ce);
206 if (err->code == GOT_ERR_OBJ_EXISTS) {
215 const struct got_error *
216 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
217 struct got_object *obj)
219 #ifndef GOT_NO_OBJ_CACHE
220 const struct got_error *err = NULL;
221 err = cache_add(&repo->objcache, id, obj);
230 got_repo_get_cached_object(struct got_repository *repo,
231 struct got_object_id *id)
233 struct got_object_cache_entry *ce;
235 ce = got_object_idcache_get(repo->objcache.idcache, id);
237 repo->objcache.cache_hit++;
241 repo->objcache.cache_miss++;
245 const struct got_error *
246 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
247 struct got_tree_object *tree)
249 #ifndef GOT_NO_OBJ_CACHE
250 const struct got_error *err = NULL;
251 err = cache_add(&repo->treecache, id, tree);
259 struct got_tree_object *
260 got_repo_get_cached_tree(struct got_repository *repo,
261 struct got_object_id *id)
263 struct got_object_cache_entry *ce;
265 ce = got_object_idcache_get(repo->treecache.idcache, id);
267 repo->treecache.cache_hit++;
268 return ce->data.tree;
271 repo->treecache.cache_miss++;
275 const struct got_error *
276 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
277 struct got_commit_object *commit)
279 #ifndef GOT_NO_OBJ_CACHE
280 const struct got_error *err = NULL;
281 err = cache_add(&repo->commitcache, id, commit);
290 struct got_commit_object *
291 got_repo_get_cached_commit(struct got_repository *repo,
292 struct got_object_id *id)
294 struct got_object_cache_entry *ce;
296 ce = got_object_idcache_get(repo->commitcache.idcache, id);
298 repo->commitcache.cache_hit++;
299 return ce->data.commit;
302 repo->commitcache.cache_miss++;
306 const struct got_error *
307 open_repo(struct got_repository *repo, const char *path)
309 const struct got_error *err = NULL;
310 struct got_worktree *worktree = NULL;
312 /* bare git repository? */
313 repo->path_git_dir = strdup(path);
314 if (repo->path_git_dir == NULL) {
315 err = got_error_from_errno();
318 if (is_git_repo(repo)) {
319 repo->path = strdup(repo->path_git_dir);
320 if (repo->path == NULL) {
321 err = got_error_from_errno();
327 /* git repository with working tree? */
328 free(repo->path_git_dir);
329 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
330 err = got_error_from_errno();
333 if (is_git_repo(repo)) {
334 repo->path = strdup(path);
335 if (repo->path == NULL) {
336 err = got_error_from_errno();
342 /* got work tree checked out from bare git repository? */
343 free(repo->path_git_dir);
344 repo->path_git_dir = NULL;
345 err = got_worktree_open(&worktree, path);
347 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
348 err = got_error(GOT_ERR_NOT_GIT_REPO);
351 repo->path_git_dir = strdup(worktree->repo_path);
352 if (repo->path_git_dir == NULL) {
353 err = got_error_from_errno();
357 /* got work tree checked out from git repository with working tree? */
358 if (!is_git_repo(repo)) {
359 free(repo->path_git_dir);
360 if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
361 GOT_GIT_DIR) == -1) {
362 err = got_error_from_errno();
363 repo->path_git_dir = NULL;
366 if (!is_git_repo(repo)) {
367 err = got_error(GOT_ERR_NOT_GIT_REPO);
370 repo->path = strdup(worktree->repo_path);
371 if (repo->path == NULL) {
372 err = got_error_from_errno();
376 repo->path = strdup(repo->path_git_dir);
377 if (repo->path == NULL) {
378 err = got_error_from_errno();
384 got_worktree_close(worktree);
388 const struct got_error *
389 got_repo_open(struct got_repository **repop, const char *path)
391 struct got_repository *repo = NULL;
392 const struct got_error *err = NULL;
393 char *abspath, *normpath = NULL;
398 if (got_path_is_absolute(path))
399 abspath = strdup(path);
401 abspath = got_path_get_absolute(path);
403 return got_error(GOT_ERR_BAD_PATH);
405 repo = calloc(1, sizeof(*repo));
407 err = got_error_from_errno();
411 repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
412 repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
413 repo->objcache.idcache = got_object_idcache_alloc(repo->objcache.size);
414 if (repo->objcache.idcache == NULL) {
415 err = got_error_from_errno();
419 repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
420 repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
421 repo->treecache.idcache =
422 got_object_idcache_alloc(repo->treecache.size);
423 if (repo->treecache.idcache == NULL) {
424 err = got_error_from_errno();
428 repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
429 repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
430 repo->commitcache.idcache =
431 got_object_idcache_alloc(repo->commitcache.size);
432 if (repo->commitcache.idcache == NULL) {
433 err = got_error_from_errno();
437 normpath = got_path_normalize(abspath);
438 if (normpath == NULL) {
439 err = got_error(GOT_ERR_BAD_PATH);
445 err = open_repo(repo, path);
448 if (err->code != GOT_ERR_NOT_GIT_REPO)
450 if (path[0] == '/' && path[1] == '\0') {
452 err = got_error(GOT_ERR_NOT_GIT_REPO);
457 path = dirname(path);
459 err = got_error_from_errno();
463 got_repo_close(repo);
473 print_cache_stats(struct got_object_cache *cache, const char *name)
475 fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
476 name, got_object_idcache_num_elements(cache->idcache),
477 cache->cache_hit, cache->cache_miss);
480 void check_refcount(struct got_object_id *id, void *data, void *arg)
482 struct got_object_cache *cache = arg;
483 struct got_object_cache_entry *ce = data;
484 struct got_object *obj;
485 struct got_tree_object *tree;
486 struct got_commit_object *commit;
489 if (got_object_id_str(&id_str, id) != NULL)
492 switch (cache->type) {
493 case GOT_OBJECT_CACHE_TYPE_OBJ:
495 if (obj->refcnt == 1)
497 fprintf(stderr, "object %s has %d unclaimed references\n",
498 id_str, obj->refcnt - 1);
500 case GOT_OBJECT_CACHE_TYPE_TREE:
501 tree = ce->data.tree;
502 if (tree->refcnt == 1)
504 fprintf(stderr, "tree %s has %d unclaimed references\n",
505 id_str, tree->refcnt - 1);
507 case GOT_OBJECT_CACHE_TYPE_COMMIT:
508 commit = ce->data.commit;
509 if (commit->refcnt == 1)
511 fprintf(stderr, "commit %s has %d unclaimed references\n",
512 id_str, commit->refcnt);
520 got_repo_close(struct got_repository *repo)
524 for (i = 0; i < nitems(repo->packidx_cache); i++) {
525 if (repo->packidx_cache[i] == NULL)
527 got_packidx_close(repo->packidx_cache[i]);
530 for (i = 0; i < nitems(repo->packs); i++) {
531 if (repo->packs[i].path_packfile == NULL)
533 got_pack_close(&repo->packs[i]);
537 free(repo->path_git_dir);
540 print_cache_stats(&repo->objcache, "object");
541 print_cache_stats(&repo->treecache, "tree");
542 print_cache_stats(&repo->commitcache, "commit");
543 got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
545 got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
547 got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
551 if (repo->objcache.idcache)
552 got_object_idcache_free(repo->objcache.idcache);
553 if (repo->treecache.idcache)
554 got_object_idcache_free(repo->treecache.idcache);
555 if (repo->commitcache.idcache)
556 got_object_idcache_free(repo->commitcache.idcache);
560 const struct got_error *
561 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
562 const char *input_path)
564 const struct got_error *err = NULL;
565 char *repo_abspath = NULL, *cwd = NULL;
567 size_t repolen, cwdlen, len;
568 char *canonpath, *path;
570 *in_repo_path = NULL;
572 cwd = getcwd(NULL, 0);
574 return got_error_from_errno();
576 canonpath = strdup(input_path);
577 if (canonpath == NULL) {
578 err = got_error_from_errno();
581 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
585 repo_abspath = got_repo_get_path(repo);
586 if (repo_abspath == NULL) {
587 err = got_error_from_errno();
591 /* TODO: Call "get in-repository path of work-tree node" API. */
593 if (lstat(canonpath, &sb) != 0) {
594 if (errno != ENOENT) {
595 err = got_error_from_errno();
599 * Path is not on disk.
600 * Assume it is already relative to repository root.
602 path = strdup(canonpath);
604 int is_repo_child = 0, is_cwd_child = 0;
606 path = realpath(canonpath, NULL);
608 err = got_error_from_errno();
612 repolen = strlen(repo_abspath);
613 cwdlen = strlen(cwd);
616 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
618 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
621 if (strcmp(path, repo_abspath) == 0) {
625 err = got_error_from_errno();
628 } else if (is_repo_child && is_cwd_child) {
630 /* TODO: Is path inside a got worktree? */
631 /* Strip common prefix with repository path. */
632 err = got_path_skip_common_ancestor(&child,
638 } else if (is_repo_child) {
639 /* Matched an on-disk path inside repository. */
640 if (got_repo_is_bare(repo)) {
642 * Matched an on-disk path inside repository
643 * database. Treat as repository-relative.
647 /* Strip common prefix with repository path. */
648 err = got_path_skip_common_ancestor(&child,
655 } else if (is_cwd_child) {
657 /* TODO: Is path inside a got worktree? */
658 /* Strip common prefix with cwd. */
659 err = got_path_skip_common_ancestor(&child, cwd,
667 * Matched unrelated on-disk path.
668 * Treat it as repository-relative.
673 /* Make in-repository path absolute */
674 if (path[0] != '/') {
676 if (asprintf(&abspath, "/%s", path) == -1) {
677 err = got_error_from_errno();
691 *in_repo_path = path;