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>
26 #include <errno.h>
27 #include <libgen.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"
44 #ifndef nitems
45 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
46 #endif
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"
60 char *
61 got_repo_get_path(struct got_repository *repo)
62 {
63 return strdup(repo->path);
64 }
66 char *
67 got_repo_get_path_git_dir(struct got_repository *repo)
68 {
69 return strdup(repo->path_git_dir);
70 }
72 static char *
73 get_path_git_child(struct got_repository *repo, const char *basename)
74 {
75 char *path_child;
77 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
78 basename) == -1)
79 return NULL;
81 return path_child;
82 }
84 char *
85 got_repo_get_path_objects(struct got_repository *repo)
86 {
87 return get_path_git_child(repo, GOT_OBJECTS_DIR);
88 }
90 char *
91 got_repo_get_path_objects_pack(struct got_repository *repo)
92 {
93 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
94 }
96 char *
97 got_repo_get_path_refs(struct got_repository *repo)
98 {
99 return get_path_git_child(repo, GOT_REFS_DIR);
102 static char *
103 get_path_head(struct got_repository *repo)
105 return get_path_git_child(repo, GOT_HEAD_FILE);
108 static int
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);
115 int ret = 0;
116 struct stat sb;
117 struct got_reference *head_ref;
119 if (lstat(path_git, &sb) == -1)
120 goto done;
121 if (!S_ISDIR(sb.st_mode))
122 goto done;
124 if (lstat(path_objects, &sb) == -1)
125 goto done;
126 if (!S_ISDIR(sb.st_mode))
127 goto done;
129 if (lstat(path_refs, &sb) == -1)
130 goto done;
131 if (!S_ISDIR(sb.st_mode))
132 goto done;
134 if (lstat(path_head, &sb) == -1)
135 goto done;
136 if (!S_ISREG(sb.st_mode))
137 goto done;
139 /* Check if the HEAD reference can be opened. */
140 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
141 goto done;
142 got_ref_close(head_ref);
144 ret = 1;
145 done:
146 free(path_git);
147 free(path_objects);
148 free(path_refs);
149 free(path_head);
150 return ret;
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;
160 int nelem;
162 nelem = got_object_idcache_num_elements(cache->idcache);
163 if (nelem >= cache->size) {
164 err = got_object_idcache_remove_least_used((void **)&ce,
165 cache->idcache);
166 if (err)
167 return err;
168 switch (cache->type) {
169 case GOT_OBJECT_CACHE_TYPE_OBJ:
170 got_object_close(ce->data.obj);
171 break;
172 case GOT_OBJECT_CACHE_TYPE_TREE:
173 got_object_tree_close(ce->data.tree);
174 break;
175 case GOT_OBJECT_CACHE_TYPE_COMMIT:
176 got_object_commit_close(ce->data.commit);
177 break;
179 free(ce);
182 ce = calloc(1, sizeof(*ce));
183 if (ce == NULL)
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;
189 break;
190 case GOT_OBJECT_CACHE_TYPE_TREE:
191 ce->data.tree = (struct got_tree_object *)item;
192 break;
193 case GOT_OBJECT_CACHE_TYPE_COMMIT:
194 ce->data.commit = (struct got_commit_object *)item;
195 break;
198 err = got_object_idcache_add(cache->idcache, id, ce);
199 if (err) {
200 if (err->code == GOT_ERR_OBJ_EXISTS) {
201 free(ce);
202 err = NULL;
205 return err;
207 #endif
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);
216 if (err)
217 return err;
218 obj->refcnt++;
219 #endif
220 return NULL;
223 struct got_object *
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);
230 if (ce) {
231 repo->objcache.cache_hit++;
232 return ce->data.obj;
235 repo->objcache.cache_miss++;
236 return NULL;
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);
246 if (err)
247 return err;
248 tree->refcnt++;
249 #endif
250 return NULL;
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);
260 if (ce) {
261 repo->treecache.cache_hit++;
262 return ce->data.tree;
265 repo->treecache.cache_miss++;
266 return NULL;
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);
276 if (err)
277 return err;
279 commit->refcnt++;
280 #endif
281 return NULL;
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);
291 if (ce) {
292 repo->commitcache.cache_hit++;
293 return ce->data.commit;
296 repo->commitcache.cache_miss++;
297 return NULL;
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();
310 goto done;
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();
316 goto done;
318 return NULL;
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();
325 goto done;
327 if (is_git_repo(repo)) {
328 repo->path = strdup(path);
329 if (repo->path == NULL) {
330 err = got_error_from_errno();
331 goto done;
333 return NULL;
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);
340 if (err) {
341 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
342 err = got_error(GOT_ERR_NOT_GIT_REPO);
343 goto done;
345 repo->path_git_dir = strdup(worktree->repo_path);
346 if (repo->path_git_dir == NULL) {
347 err = got_error_from_errno();
348 goto done;
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;
358 goto done;
360 if (!is_git_repo(repo)) {
361 err = got_error(GOT_ERR_NOT_GIT_REPO);
362 goto done;
364 repo->path = strdup(worktree->repo_path);
365 if (repo->path == NULL) {
366 err = got_error_from_errno();
367 goto done;
369 } else {
370 repo->path = strdup(repo->path_git_dir);
371 if (repo->path == NULL) {
372 err = got_error_from_errno();
373 goto done;
376 done:
377 if (worktree)
378 got_worktree_close(worktree);
379 return err;
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;
388 int tried_root = 0;
390 *repop = NULL;
392 if (got_path_is_absolute(path))
393 abspath = strdup(path);
394 else
395 abspath = got_path_get_absolute(path);
396 if (abspath == NULL)
397 return got_error(GOT_ERR_BAD_PATH);
399 repo = calloc(1, sizeof(*repo));
400 if (repo == NULL) {
401 err = got_error_from_errno();
402 goto done;
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();
410 goto done;
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();
419 goto done;
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();
428 goto done;
431 normpath = got_path_normalize(abspath);
432 if (normpath == NULL) {
433 err = got_error(GOT_ERR_BAD_PATH);
434 goto done;
437 path = normpath;
438 do {
439 err = open_repo(repo, path);
440 if (err == NULL)
441 break;
442 if (err->code != GOT_ERR_NOT_GIT_REPO)
443 break;
444 if (path[0] == '/' && path[1] == '\0') {
445 if (tried_root) {
446 err = got_error(GOT_ERR_NOT_GIT_REPO);
447 break;
449 tried_root = 1;
451 path = dirname(path);
452 if (path == NULL)
453 err = got_error_from_errno();
454 } while (path);
455 done:
456 if (err)
457 got_repo_close(repo);
458 else
459 *repop = repo;
460 free(abspath);
461 free(normpath);
462 return err;
465 #if 0
466 static void
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;
481 char *id_str;
483 if (got_object_id_str(&id_str, id) != NULL)
484 return;
486 switch (cache->type) {
487 case GOT_OBJECT_CACHE_TYPE_OBJ:
488 obj = ce->data.obj;
489 if (obj->refcnt == 1)
490 break;
491 fprintf(stderr, "object %s has %d unclaimed references\n",
492 id_str, obj->refcnt - 1);
493 break;
494 case GOT_OBJECT_CACHE_TYPE_TREE:
495 tree = ce->data.tree;
496 if (tree->refcnt == 1)
497 break;
498 fprintf(stderr, "tree %s has %d unclaimed references\n",
499 id_str, tree->refcnt - 1);
500 break;
501 case GOT_OBJECT_CACHE_TYPE_COMMIT:
502 commit = ce->data.commit;
503 if (commit->refcnt == 1)
504 break;
505 fprintf(stderr, "commit %s has %d unclaimed references\n",
506 id_str, commit->refcnt);
507 break;
509 free(id_str);
511 #endif
513 void
514 got_repo_close(struct got_repository *repo)
516 int i;
518 for (i = 0; i < nitems(repo->packidx_cache); i++) {
519 if (repo->packidx_cache[i] == NULL)
520 break;
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)
526 break;
527 got_pack_close(&repo->packs[i]);
530 free(repo->path);
531 free(repo->path_git_dir);
533 #if 0
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,
538 &repo->objcache);
539 got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
540 &repo->treecache);
541 got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
542 &repo->commitcache);
543 #endif
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);
551 free(repo);