Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <sha1.h>
24 #include <string.h>
25 #include <zlib.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"
42 #ifndef nitems
43 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
44 #endif
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"
58 char *
59 got_repo_get_path(struct got_repository *repo)
60 {
61 return strdup(repo->path);
62 }
64 char *
65 got_repo_get_path_git_dir(struct got_repository *repo)
66 {
67 return strdup(repo->path_git_dir);
68 }
70 static char *
71 get_path_git_child(struct got_repository *repo, const char *basename)
72 {
73 char *path_child;
75 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
76 basename) == -1)
77 return NULL;
79 return path_child;
80 }
82 char *
83 got_repo_get_path_objects(struct got_repository *repo)
84 {
85 return get_path_git_child(repo, GOT_OBJECTS_DIR);
86 }
88 char *
89 got_repo_get_path_objects_pack(struct got_repository *repo)
90 {
91 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
92 }
94 char *
95 got_repo_get_path_refs(struct got_repository *repo)
96 {
97 return get_path_git_child(repo, GOT_REFS_DIR);
98 }
100 static char *
101 get_path_head(struct got_repository *repo)
103 return get_path_git_child(repo, GOT_HEAD_FILE);
106 static int
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);
113 int ret = 0;
114 struct stat sb;
115 struct got_reference *head_ref;
117 if (lstat(path_git, &sb) == -1)
118 goto done;
119 if (!S_ISDIR(sb.st_mode))
120 goto done;
122 if (lstat(path_objects, &sb) == -1)
123 goto done;
124 if (!S_ISDIR(sb.st_mode))
125 goto done;
127 if (lstat(path_refs, &sb) == -1)
128 goto done;
129 if (!S_ISDIR(sb.st_mode))
130 goto done;
132 if (lstat(path_head, &sb) == -1)
133 goto done;
134 if (!S_ISREG(sb.st_mode))
135 goto done;
137 /* Check if the HEAD reference can be opened. */
138 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
139 goto done;
140 got_ref_close(head_ref);
142 ret = 1;
143 done:
144 free(path_git);
145 free(path_objects);
146 free(path_refs);
147 free(path_head);
148 return ret;
152 #ifndef GOT_NO_OBJ_CACHE
153 static const struct got_error *
154 cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
156 const struct got_error *err = NULL;
157 struct got_object_cache_entry *ce;
158 int nelem;
160 nelem = got_object_idcache_num_elements(cache->idcache);
161 if (nelem >= cache->size) {
162 err = got_object_idcache_remove_least_used((void **)&ce,
163 cache->idcache);
164 if (err)
165 return err;
166 switch (cache->type) {
167 case GOT_OBJECT_CACHE_TYPE_OBJ:
168 got_object_close(ce->data.obj);
169 break;
170 case GOT_OBJECT_CACHE_TYPE_TREE:
171 got_object_tree_close(ce->data.tree);
172 break;
173 case GOT_OBJECT_CACHE_TYPE_COMMIT:
174 got_object_commit_close(ce->data.commit);
175 break;
177 free(ce);
180 ce = calloc(1, sizeof(*ce));
181 if (ce == NULL)
182 return got_error_from_errno();
183 memcpy(&ce->id, id, sizeof(ce->id));
184 switch (cache->type) {
185 case GOT_OBJECT_CACHE_TYPE_OBJ:
186 ce->data.obj = (struct got_object *)item;
187 break;
188 case GOT_OBJECT_CACHE_TYPE_TREE:
189 ce->data.tree = (struct got_tree_object *)item;
190 break;
191 case GOT_OBJECT_CACHE_TYPE_COMMIT:
192 ce->data.commit = (struct got_commit_object *)item;
193 break;
196 err = got_object_idcache_add(cache->idcache, id, ce);
197 if (err) {
198 if (err->code == GOT_ERR_OBJ_EXISTS) {
199 free(ce);
200 err = NULL;
203 return err;
205 #endif
207 const struct got_error *
208 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
209 struct got_object *obj)
211 #ifndef GOT_NO_OBJ_CACHE
212 const struct got_error *err = NULL;
213 err = cache_add(&repo->objcache, id, obj);
214 if (err)
215 return err;
216 obj->refcnt++;
217 #endif
218 return NULL;
221 struct got_object *
222 got_repo_get_cached_object(struct got_repository *repo,
223 struct got_object_id *id)
225 struct got_object_cache_entry *ce;
227 ce = got_object_idcache_get(repo->objcache.idcache, id);
228 if (ce) {
229 repo->objcache.cache_hit++;
230 return ce->data.obj;
233 repo->objcache.cache_miss++;
234 return NULL;
237 const struct got_error *
238 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
239 struct got_tree_object *tree)
241 #ifndef GOT_NO_OBJ_CACHE
242 const struct got_error *err = NULL;
243 err = cache_add(&repo->treecache, id, tree);
244 if (err)
245 return err;
246 tree->refcnt++;
247 #endif
248 return NULL;
251 struct got_tree_object *
252 got_repo_get_cached_tree(struct got_repository *repo,
253 struct got_object_id *id)
255 struct got_object_cache_entry *ce;
257 ce = got_object_idcache_get(repo->treecache.idcache, id);
258 if (ce) {
259 repo->treecache.cache_hit++;
260 return ce->data.tree;
263 repo->treecache.cache_miss++;
264 return NULL;
267 const struct got_error *
268 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
269 struct got_commit_object *commit)
271 #ifndef GOT_NO_OBJ_CACHE
272 const struct got_error *err = NULL;
273 err = cache_add(&repo->commitcache, id, commit);
274 if (err)
275 return err;
277 commit->refcnt++;
278 #endif
279 return NULL;
282 struct got_commit_object *
283 got_repo_get_cached_commit(struct got_repository *repo,
284 struct got_object_id *id)
286 struct got_object_cache_entry *ce;
288 ce = got_object_idcache_get(repo->commitcache.idcache, id);
289 if (ce) {
290 repo->commitcache.cache_hit++;
291 return ce->data.commit;
294 repo->commitcache.cache_miss++;
295 return NULL;
298 const struct got_error *
299 got_repo_open(struct got_repository **ret, const char *path)
301 struct got_repository *repo = NULL;
302 const struct got_error *err = NULL;
303 char *abspath;
305 if (got_path_is_absolute(path))
306 abspath = strdup(path);
307 else
308 abspath = got_path_get_absolute(path);
309 if (abspath == NULL)
310 return got_error(GOT_ERR_BAD_PATH);
312 repo = calloc(1, sizeof(*repo));
313 if (repo == NULL) {
314 err = got_error_from_errno();
315 goto done;
318 repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
319 repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
320 repo->objcache.idcache = got_object_idcache_alloc(repo->objcache.size);
321 if (repo->objcache.idcache == NULL) {
322 err = got_error_from_errno();
323 goto done;
326 repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
327 repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
328 repo->treecache.idcache =
329 got_object_idcache_alloc(repo->treecache.size);
330 if (repo->treecache.idcache == NULL) {
331 err = got_error_from_errno();
332 goto done;
335 repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
336 repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
337 repo->commitcache.idcache =
338 got_object_idcache_alloc(repo->commitcache.size);
339 if (repo->commitcache.idcache == NULL) {
340 err = got_error_from_errno();
341 goto done;
344 repo->path = got_path_normalize(abspath);
345 if (repo->path == NULL) {
346 err = got_error(GOT_ERR_BAD_PATH);
347 goto done;
350 repo->path_git_dir = strdup(repo->path);
351 if (repo->path_git_dir == NULL) {
352 err = got_error_from_errno();
353 goto done;
355 if (!is_git_repo(repo)) {
356 free(repo->path_git_dir);
357 if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
358 GOT_GIT_DIR) == -1) {
359 err = got_error_from_errno();
360 goto done;
362 if (!is_git_repo(repo)) {
363 struct got_worktree *worktree;
364 if (got_worktree_open(&worktree, repo->path) == NULL) {
365 free(repo->path_git_dir);
366 repo->path_git_dir =
367 strdup(worktree->repo_path);
368 if (repo->path_git_dir == NULL) {
369 err = got_error_from_errno();
370 goto done;
372 if (!is_git_repo(repo)) {
373 free(repo->path_git_dir);
374 if (asprintf(&repo->path_git_dir,
375 "%s/%s", worktree->repo_path,
376 GOT_GIT_DIR) == -1) {
377 err = got_error_from_errno();
378 goto done;
381 got_worktree_close(worktree);
384 if (!is_git_repo(repo)) {
385 err = got_error(GOT_ERR_NOT_GIT_REPO);
386 goto done;
390 *ret = repo;
391 done:
392 if (err)
393 got_repo_close(repo);
394 free(abspath);
395 return err;
398 #if 0
399 static void
400 print_cache_stats(struct got_object_cache *cache, const char *name)
402 fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
403 name, got_object_idcache_num_elements(cache->idcache),
404 cache->cache_hit, cache->cache_miss);
407 void check_refcount(struct got_object_id *id, void *data, void *arg)
409 struct got_object_cache *cache = arg;
410 struct got_object_cache_entry *ce = data;
411 struct got_object *obj;
412 struct got_tree_object *tree;
413 struct got_commit_object *commit;
414 char *id_str;
416 if (got_object_id_str(&id_str, id) != NULL)
417 return;
419 switch (cache->type) {
420 case GOT_OBJECT_CACHE_TYPE_OBJ:
421 obj = ce->data.obj;
422 if (obj->refcnt == 1)
423 break;
424 fprintf(stderr, "object %s has %d unclaimed references\n",
425 id_str, obj->refcnt - 1);
426 break;
427 case GOT_OBJECT_CACHE_TYPE_TREE:
428 tree = ce->data.tree;
429 if (tree->refcnt == 1)
430 break;
431 fprintf(stderr, "tree %s has %d unclaimed references\n",
432 id_str, tree->refcnt - 1);
433 break;
434 case GOT_OBJECT_CACHE_TYPE_COMMIT:
435 commit = ce->data.commit;
436 if (commit->refcnt == 1)
437 break;
438 fprintf(stderr, "commit %s has %d unclaimed references\n",
439 id_str, commit->refcnt);
440 break;
442 free(id_str);
444 #endif
446 void
447 got_repo_close(struct got_repository *repo)
449 int i;
451 for (i = 0; i < nitems(repo->packidx_cache); i++) {
452 if (repo->packidx_cache[i] == NULL)
453 break;
454 got_packidx_close(repo->packidx_cache[i]);
457 for (i = 0; i < nitems(repo->packs); i++) {
458 if (repo->packs[i].path_packfile == NULL)
459 break;
460 got_pack_close(&repo->packs[i]);
463 free(repo->path);
464 free(repo->path_git_dir);
466 #if 0
467 print_cache_stats(&repo->objcache, "object");
468 print_cache_stats(&repo->treecache, "tree");
469 print_cache_stats(&repo->commitcache, "commit");
470 got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
471 &repo->objcache);
472 got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
473 &repo->treecache);
474 got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
475 &repo->commitcache);
476 #endif
478 if (repo->objcache.idcache)
479 got_object_idcache_free(repo->objcache.idcache);
480 if (repo->treecache.idcache)
481 got_object_idcache_free(repo->treecache.idcache);
482 if (repo->commitcache.idcache)
483 got_object_idcache_free(repo->commitcache.idcache);
484 free(repo);