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>
27 #include "got_error.h"
28 #include "got_reference.h"
29 #include "got_repository.h"
30 #include "got_worktree.h"
31 #include "got_object.h"
33 #include "got_lib_path.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_inflate.h"
36 #include "got_lib_object.h"
37 #include "got_lib_pack.h"
38 #include "got_lib_repository.h"
39 #include "got_lib_worktree.h"
40 #include "got_lib_object_idcache.h"
43 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
46 #define GOT_GIT_DIR ".git"
48 /* Mandatory files and directories inside the git directory. */
49 #define GOT_OBJECTS_DIR "objects"
50 #define GOT_REFS_DIR "refs"
51 #define GOT_HEAD_FILE "HEAD"
53 /* Other files and directories inside the git directory. */
54 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
55 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
56 #define GOT_OBJECTS_PACK_DIR "objects/pack"
59 got_repo_get_path(struct got_repository *repo)
61 return strdup(repo->path);
65 got_repo_get_path_git_dir(struct got_repository *repo)
67 return strdup(repo->path_git_dir);
71 get_path_git_child(struct got_repository *repo, const char *basename)
75 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
83 got_repo_get_path_objects(struct got_repository *repo)
85 return get_path_git_child(repo, GOT_OBJECTS_DIR);
89 got_repo_get_path_objects_pack(struct got_repository *repo)
91 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
95 got_repo_get_path_refs(struct got_repository *repo)
97 return get_path_git_child(repo, GOT_REFS_DIR);
101 get_path_head(struct got_repository *repo)
103 return get_path_git_child(repo, GOT_HEAD_FILE);
107 is_git_repo(struct got_repository *repo)
109 char *path_git = got_repo_get_path_git_dir(repo);
110 char *path_objects = got_repo_get_path_objects(repo);
111 char *path_refs = got_repo_get_path_refs(repo);
112 char *path_head = get_path_head(repo);
115 struct got_reference *head_ref;
117 if (lstat(path_git, &sb) == -1)
119 if (!S_ISDIR(sb.st_mode))
122 if (lstat(path_objects, &sb) == -1)
124 if (!S_ISDIR(sb.st_mode))
127 if (lstat(path_refs, &sb) == -1)
129 if (!S_ISDIR(sb.st_mode))
132 if (lstat(path_head, &sb) == -1)
134 if (!S_ISREG(sb.st_mode))
137 /* Check if the HEAD reference can be opened. */
138 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
140 got_ref_close(head_ref);
152 static const struct got_error *
153 cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
155 const struct got_error *err = NULL;
156 struct got_object_cache_entry *ce;
159 nelem = got_object_idcache_num_elements(cache->idcache);
160 if (nelem >= cache->size) {
161 err = got_object_idcache_remove_least_used((void **)&ce,
165 switch (cache->type) {
166 case GOT_OBJECT_CACHE_TYPE_OBJ:
167 got_object_close(ce->data.obj);
169 case GOT_OBJECT_CACHE_TYPE_TREE:
170 got_object_tree_close(ce->data.tree);
172 case GOT_OBJECT_CACHE_TYPE_COMMIT:
173 got_object_commit_close(ce->data.commit);
179 ce = calloc(1, sizeof(*ce));
181 return got_error_from_errno();
182 memcpy(&ce->id, id, sizeof(ce->id));
183 switch (cache->type) {
184 case GOT_OBJECT_CACHE_TYPE_OBJ:
185 ce->data.obj = (struct got_object *)item;
187 case GOT_OBJECT_CACHE_TYPE_TREE:
188 ce->data.tree = (struct got_tree_object *)item;
190 case GOT_OBJECT_CACHE_TYPE_COMMIT:
191 ce->data.commit = (struct got_commit_object *)item;
194 err = got_object_idcache_add(cache->idcache, id, ce);
196 if (err->code == GOT_ERR_OBJ_EXISTS) {
205 const struct got_error *
206 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
207 struct got_object *obj)
209 const struct got_error *err = NULL;
211 err = cache_add(&repo->objcache, id, obj);
220 got_repo_get_cached_object(struct got_repository *repo,
221 struct got_object_id *id)
223 struct got_object_cache_entry *ce;
225 ce = got_object_idcache_get(repo->objcache.idcache, id);
227 repo->objcache.cache_hit++;
231 repo->objcache.cache_miss++;
235 const struct got_error *
236 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
237 struct got_tree_object *tree)
239 const struct got_error *err = NULL;
241 err = cache_add(&repo->treecache, id, tree);
249 struct got_tree_object *
250 got_repo_get_cached_tree(struct got_repository *repo,
251 struct got_object_id *id)
253 struct got_object_cache_entry *ce;
255 ce = got_object_idcache_get(repo->treecache.idcache, id);
257 repo->treecache.cache_hit++;
258 return ce->data.tree;
261 repo->treecache.cache_miss++;
265 const struct got_error *
266 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
267 struct got_commit_object *commit)
269 const struct got_error *err = NULL;
271 err = cache_add(&repo->commitcache, id, commit);
279 struct got_commit_object *
280 got_repo_get_cached_commit(struct got_repository *repo,
281 struct got_object_id *id)
283 struct got_object_cache_entry *ce;
285 ce = got_object_idcache_get(repo->commitcache.idcache, id);
287 repo->commitcache.cache_hit++;
288 return ce->data.commit;
291 repo->commitcache.cache_miss++;
295 const struct got_error *
296 got_repo_open(struct got_repository **ret, const char *path)
298 struct got_repository *repo = NULL;
299 const struct got_error *err = NULL;
302 if (got_path_is_absolute(path))
303 abspath = strdup(path);
305 abspath = got_path_get_absolute(path);
307 return got_error(GOT_ERR_BAD_PATH);
309 repo = calloc(1, sizeof(*repo));
311 err = got_error_from_errno();
315 repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
316 repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
317 repo->objcache.idcache = got_object_idcache_alloc(repo->objcache.size);
318 if (repo->objcache.idcache == NULL) {
319 err = got_error_from_errno();
323 repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
324 repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
325 repo->treecache.idcache =
326 got_object_idcache_alloc(repo->treecache.size);
327 if (repo->treecache.idcache == NULL) {
328 err = got_error_from_errno();
332 repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
333 repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
334 repo->commitcache.idcache =
335 got_object_idcache_alloc(repo->commitcache.size);
336 if (repo->commitcache.idcache == NULL) {
337 err = got_error_from_errno();
341 repo->path = got_path_normalize(abspath);
342 if (repo->path == NULL) {
343 err = got_error(GOT_ERR_BAD_PATH);
347 repo->path_git_dir = strdup(repo->path);
348 if (repo->path_git_dir == NULL) {
349 err = got_error_from_errno();
352 if (!is_git_repo(repo)) {
353 free(repo->path_git_dir);
354 if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
355 GOT_GIT_DIR) == -1) {
356 err = got_error_from_errno();
359 if (!is_git_repo(repo)) {
360 struct got_worktree *worktree;
361 if (got_worktree_open(&worktree, repo->path) == NULL) {
362 free(repo->path_git_dir);
364 strdup(worktree->repo_path);
365 if (repo->path_git_dir == NULL) {
366 err = got_error_from_errno();
369 if (!is_git_repo(repo)) {
370 free(repo->path_git_dir);
371 if (asprintf(&repo->path_git_dir,
372 "%s/%s", worktree->repo_path,
373 GOT_GIT_DIR) == -1) {
374 err = got_error_from_errno();
378 got_worktree_close(worktree);
381 if (!is_git_repo(repo)) {
382 err = got_error(GOT_ERR_NOT_GIT_REPO);
390 got_repo_close(repo);
397 print_cache_stats(struct got_object_cache *cache, const char *name)
399 fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
400 name, got_object_idcache_num_elements(cache->idcache),
401 cache->cache_hit, cache->cache_miss);
404 void check_refcount(struct got_object_id *id, void *data, void *arg)
406 struct got_object_cache *cache = arg;
407 struct got_object_cache_entry *ce = data;
408 struct got_object *obj;
409 struct got_tree_object *tree;
410 struct got_commit_object *commit;
413 if (got_object_id_str(&id_str, id) != NULL)
416 switch (cache->type) {
417 case GOT_OBJECT_CACHE_TYPE_OBJ:
419 if (obj->refcnt == 1)
421 fprintf(stderr, "object %s has %d unclaimed references\n",
422 id_str, obj->refcnt - 1);
424 case GOT_OBJECT_CACHE_TYPE_TREE:
425 tree = ce->data.tree;
426 if (tree->refcnt == 1)
428 fprintf(stderr, "tree %s has %d unclaimed references\n",
429 id_str, tree->refcnt - 1);
431 case GOT_OBJECT_CACHE_TYPE_COMMIT:
432 commit = ce->data.commit;
433 if (commit->refcnt == 1)
435 fprintf(stderr, "commit %s has %d unclaimed references\n",
436 id_str, commit->refcnt);
444 got_repo_close(struct got_repository *repo)
448 for (i = 0; i < nitems(repo->packidx_cache); i++) {
449 if (repo->packidx_cache[i] == NULL)
451 got_packidx_close(repo->packidx_cache[i]);
454 for (i = 0; i < nitems(repo->packs); i++) {
455 if (repo->packs[i].path_packfile == NULL)
457 got_pack_close(&repo->packs[i]);
461 free(repo->path_git_dir);
464 print_cache_stats(&repo->objcache, "object");
465 print_cache_stats(&repo->treecache, "tree");
466 print_cache_stats(&repo->commitcache, "commit");
467 got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
469 got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
471 got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
475 if (repo->objcache.idcache)
476 got_object_idcache_free(repo->objcache.idcache);
477 if (repo->treecache.idcache)
478 got_object_idcache_free(repo->treecache.idcache);
479 if (repo->commitcache.idcache)
480 got_object_idcache_free(repo->commitcache.idcache);