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 get_path_git_child(struct got_repository *repo, const char *basename)
77 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
85 got_repo_get_path_objects(struct got_repository *repo)
87 return get_path_git_child(repo, GOT_OBJECTS_DIR);
91 got_repo_get_path_objects_pack(struct got_repository *repo)
93 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
97 got_repo_get_path_refs(struct got_repository *repo)
99 return get_path_git_child(repo, GOT_REFS_DIR);
103 get_path_head(struct got_repository *repo)
105 return get_path_git_child(repo, GOT_HEAD_FILE);
109 is_git_repo(struct got_repository *repo)
111 char *path_git = got_repo_get_path_git_dir(repo);
112 char *path_objects = got_repo_get_path_objects(repo);
113 char *path_refs = got_repo_get_path_refs(repo);
114 char *path_head = get_path_head(repo);
117 struct got_reference *head_ref;
119 if (lstat(path_git, &sb) == -1)
121 if (!S_ISDIR(sb.st_mode))
124 if (lstat(path_objects, &sb) == -1)
126 if (!S_ISDIR(sb.st_mode))
129 if (lstat(path_refs, &sb) == -1)
131 if (!S_ISDIR(sb.st_mode))
134 if (lstat(path_head, &sb) == -1)
136 if (!S_ISREG(sb.st_mode))
139 /* Check if the HEAD reference can be opened. */
140 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
142 got_ref_close(head_ref);
154 #ifndef GOT_NO_OBJ_CACHE
155 static const struct got_error *
156 cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
158 const struct got_error *err = NULL;
159 struct got_object_cache_entry *ce;
162 nelem = got_object_idcache_num_elements(cache->idcache);
163 if (nelem >= cache->size) {
164 err = got_object_idcache_remove_least_used((void **)&ce,
168 switch (cache->type) {
169 case GOT_OBJECT_CACHE_TYPE_OBJ:
170 got_object_close(ce->data.obj);
172 case GOT_OBJECT_CACHE_TYPE_TREE:
173 got_object_tree_close(ce->data.tree);
175 case GOT_OBJECT_CACHE_TYPE_COMMIT:
176 got_object_commit_close(ce->data.commit);
182 ce = calloc(1, sizeof(*ce));
184 return got_error_from_errno();
185 memcpy(&ce->id, id, sizeof(ce->id));
186 switch (cache->type) {
187 case GOT_OBJECT_CACHE_TYPE_OBJ:
188 ce->data.obj = (struct got_object *)item;
190 case GOT_OBJECT_CACHE_TYPE_TREE:
191 ce->data.tree = (struct got_tree_object *)item;
193 case GOT_OBJECT_CACHE_TYPE_COMMIT:
194 ce->data.commit = (struct got_commit_object *)item;
198 err = got_object_idcache_add(cache->idcache, id, ce);
200 if (err->code == GOT_ERR_OBJ_EXISTS) {
209 const struct got_error *
210 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
211 struct got_object *obj)
213 #ifndef GOT_NO_OBJ_CACHE
214 const struct got_error *err = NULL;
215 err = cache_add(&repo->objcache, id, obj);
224 got_repo_get_cached_object(struct got_repository *repo,
225 struct got_object_id *id)
227 struct got_object_cache_entry *ce;
229 ce = got_object_idcache_get(repo->objcache.idcache, id);
231 repo->objcache.cache_hit++;
235 repo->objcache.cache_miss++;
239 const struct got_error *
240 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
241 struct got_tree_object *tree)
243 #ifndef GOT_NO_OBJ_CACHE
244 const struct got_error *err = NULL;
245 err = cache_add(&repo->treecache, id, tree);
253 struct got_tree_object *
254 got_repo_get_cached_tree(struct got_repository *repo,
255 struct got_object_id *id)
257 struct got_object_cache_entry *ce;
259 ce = got_object_idcache_get(repo->treecache.idcache, id);
261 repo->treecache.cache_hit++;
262 return ce->data.tree;
265 repo->treecache.cache_miss++;
269 const struct got_error *
270 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
271 struct got_commit_object *commit)
273 #ifndef GOT_NO_OBJ_CACHE
274 const struct got_error *err = NULL;
275 err = cache_add(&repo->commitcache, id, commit);
284 struct got_commit_object *
285 got_repo_get_cached_commit(struct got_repository *repo,
286 struct got_object_id *id)
288 struct got_object_cache_entry *ce;
290 ce = got_object_idcache_get(repo->commitcache.idcache, id);
292 repo->commitcache.cache_hit++;
293 return ce->data.commit;
296 repo->commitcache.cache_miss++;
300 const struct got_error *
301 open_repo(struct got_repository *repo, const char *path)
303 const struct got_error *err = NULL;
304 struct got_worktree *worktree = NULL;
306 /* bare git repository? */
307 repo->path_git_dir = strdup(path);
308 if (repo->path_git_dir == NULL) {
309 err = got_error_from_errno();
312 if (is_git_repo(repo)) {
313 repo->path = strdup(repo->path_git_dir);
314 if (repo->path == NULL) {
315 err = got_error_from_errno();
321 /* git repository with working tree? */
322 free(repo->path_git_dir);
323 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
324 err = got_error_from_errno();
327 if (is_git_repo(repo)) {
328 repo->path = strdup(path);
329 if (repo->path == NULL) {
330 err = got_error_from_errno();
336 /* got work tree checked out from bare git repository? */
337 free(repo->path_git_dir);
338 repo->path_git_dir = NULL;
339 err = got_worktree_open(&worktree, path);
341 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
342 err = got_error(GOT_ERR_NOT_GIT_REPO);
345 repo->path_git_dir = strdup(worktree->repo_path);
346 if (repo->path_git_dir == NULL) {
347 err = got_error_from_errno();
351 /* got work tree checked out from git repository with working tree? */
352 if (!is_git_repo(repo)) {
353 free(repo->path_git_dir);
354 if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
355 GOT_GIT_DIR) == -1) {
356 err = got_error_from_errno();
357 repo->path_git_dir = NULL;
360 if (!is_git_repo(repo)) {
361 err = got_error(GOT_ERR_NOT_GIT_REPO);
364 repo->path = strdup(worktree->repo_path);
365 if (repo->path == NULL) {
366 err = got_error_from_errno();
370 repo->path = strdup(repo->path_git_dir);
371 if (repo->path == NULL) {
372 err = got_error_from_errno();
378 got_worktree_close(worktree);
382 const struct got_error *
383 got_repo_open(struct got_repository **repop, const char *path)
385 struct got_repository *repo = NULL;
386 const struct got_error *err = NULL;
387 char *abspath, *normpath = NULL;
392 if (got_path_is_absolute(path))
393 abspath = strdup(path);
395 abspath = got_path_get_absolute(path);
397 return got_error(GOT_ERR_BAD_PATH);
399 repo = calloc(1, sizeof(*repo));
401 err = got_error_from_errno();
405 repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
406 repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
407 repo->objcache.idcache = got_object_idcache_alloc(repo->objcache.size);
408 if (repo->objcache.idcache == NULL) {
409 err = got_error_from_errno();
413 repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
414 repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
415 repo->treecache.idcache =
416 got_object_idcache_alloc(repo->treecache.size);
417 if (repo->treecache.idcache == NULL) {
418 err = got_error_from_errno();
422 repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
423 repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
424 repo->commitcache.idcache =
425 got_object_idcache_alloc(repo->commitcache.size);
426 if (repo->commitcache.idcache == NULL) {
427 err = got_error_from_errno();
431 normpath = got_path_normalize(abspath);
432 if (normpath == NULL) {
433 err = got_error(GOT_ERR_BAD_PATH);
439 err = open_repo(repo, path);
442 if (err->code != GOT_ERR_NOT_GIT_REPO)
444 if (path[0] == '/' && path[1] == '\0') {
446 err = got_error(GOT_ERR_NOT_GIT_REPO);
451 path = dirname(path);
453 err = got_error_from_errno();
457 got_repo_close(repo);
467 print_cache_stats(struct got_object_cache *cache, const char *name)
469 fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
470 name, got_object_idcache_num_elements(cache->idcache),
471 cache->cache_hit, cache->cache_miss);
474 void check_refcount(struct got_object_id *id, void *data, void *arg)
476 struct got_object_cache *cache = arg;
477 struct got_object_cache_entry *ce = data;
478 struct got_object *obj;
479 struct got_tree_object *tree;
480 struct got_commit_object *commit;
483 if (got_object_id_str(&id_str, id) != NULL)
486 switch (cache->type) {
487 case GOT_OBJECT_CACHE_TYPE_OBJ:
489 if (obj->refcnt == 1)
491 fprintf(stderr, "object %s has %d unclaimed references\n",
492 id_str, obj->refcnt - 1);
494 case GOT_OBJECT_CACHE_TYPE_TREE:
495 tree = ce->data.tree;
496 if (tree->refcnt == 1)
498 fprintf(stderr, "tree %s has %d unclaimed references\n",
499 id_str, tree->refcnt - 1);
501 case GOT_OBJECT_CACHE_TYPE_COMMIT:
502 commit = ce->data.commit;
503 if (commit->refcnt == 1)
505 fprintf(stderr, "commit %s has %d unclaimed references\n",
506 id_str, commit->refcnt);
514 got_repo_close(struct got_repository *repo)
518 for (i = 0; i < nitems(repo->packidx_cache); i++) {
519 if (repo->packidx_cache[i] == NULL)
521 got_packidx_close(repo->packidx_cache[i]);
524 for (i = 0; i < nitems(repo->packs); i++) {
525 if (repo->packs[i].path_packfile == NULL)
527 got_pack_close(&repo->packs[i]);
531 free(repo->path_git_dir);
534 print_cache_stats(&repo->objcache, "object");
535 print_cache_stats(&repo->treecache, "tree");
536 print_cache_stats(&repo->commitcache, "commit");
537 got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
539 got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
541 got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
545 if (repo->objcache.idcache)
546 got_object_idcache_free(repo->objcache.idcache);
547 if (repo->treecache.idcache)
548 got_object_idcache_free(repo->treecache.idcache);
549 if (repo->commitcache.idcache)
550 got_object_idcache_free(repo->commitcache.idcache);