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_zbuf.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_idset.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 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;
157 int nelem;
159 nelem = got_object_idset_num_elements(cache->set);
160 if (nelem >= cache->size) {
161 err = got_object_idset_remove_random((void **)&ce,
162 cache->set);
163 if (err)
164 return err;
165 switch (cache->type) {
166 case GOT_OBJECT_CACHE_TYPE_OBJ:
167 got_object_close(ce->data.obj);
168 break;
169 case GOT_OBJECT_CACHE_TYPE_TREE:
170 got_object_tree_close(ce->data.tree);
171 break;
172 case GOT_OBJECT_CACHE_TYPE_COMMIT:
173 got_object_commit_close(ce->data.commit);
174 break;
176 free(ce);
179 ce = calloc(1, sizeof(*ce));
180 if (ce == NULL)
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;
186 break;
187 case GOT_OBJECT_CACHE_TYPE_TREE:
188 ce->data.tree = (struct got_tree_object *)item;
189 break;
190 case GOT_OBJECT_CACHE_TYPE_COMMIT:
191 ce->data.commit = (struct got_commit_object *)item;
192 break;
194 err = got_object_idset_add(NULL, cache->set, id, ce);
195 if (err) {
196 if (err->code == GOT_ERR_OBJ_EXISTS) {
197 free(ce);
198 err = NULL;
202 return err;
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);
212 if (err)
213 return err;
215 obj->refcnt++;
216 return NULL;
219 struct got_object *
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_idset_get(repo->objcache.set, id);
226 if (ce) {
227 repo->objcache.cache_hit++;
228 return ce->data.obj;
231 repo->objcache.cache_miss++;
232 return NULL;
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);
242 if (err)
243 return err;
245 tree->refcnt++;
246 return NULL;
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_idset_get(repo->treecache.set, id);
256 if (ce) {
257 repo->treecache.cache_hit++;
258 return ce->data.tree;
261 repo->treecache.cache_miss++;
262 return NULL;
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);
272 if (err)
273 return err;
275 commit->refcnt++;
276 return NULL;
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_idset_get(repo->commitcache.set, id);
286 if (ce) {
287 repo->commitcache.cache_hit++;
288 return ce->data.commit;
291 repo->commitcache.cache_miss++;
292 return NULL;
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;
300 char *abspath;
302 if (got_path_is_absolute(path))
303 abspath = strdup(path);
304 else
305 abspath = got_path_get_absolute(path);
306 if (abspath == NULL)
307 return got_error(GOT_ERR_BAD_PATH);
309 repo = calloc(1, sizeof(*repo));
310 if (repo == NULL) {
311 err = got_error_from_errno();
312 goto done;
315 repo->objcache.set = got_object_idset_alloc();
316 if (repo->objcache.set == NULL) {
317 err = got_error_from_errno();
318 goto done;
320 repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
321 repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
323 repo->treecache.set = got_object_idset_alloc();
324 if (repo->treecache.set == NULL) {
325 err = got_error_from_errno();
326 goto done;
328 repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
329 repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
331 repo->commitcache.set = got_object_idset_alloc();
332 if (repo->commitcache.set == NULL) {
333 err = got_error_from_errno();
334 goto done;
336 repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
337 repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
339 repo->path = got_path_normalize(abspath);
340 if (repo->path == NULL) {
341 err = got_error(GOT_ERR_BAD_PATH);
342 goto done;
345 repo->path_git_dir = strdup(repo->path);
346 if (repo->path_git_dir == NULL) {
347 err = got_error_from_errno();
348 goto done;
350 if (!is_git_repo(repo)) {
351 free(repo->path_git_dir);
352 if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
353 GOT_GIT_DIR) == -1) {
354 err = got_error_from_errno();
355 goto done;
357 if (!is_git_repo(repo)) {
358 struct got_worktree *worktree;
359 if (got_worktree_open(&worktree, repo->path) == NULL) {
360 free(repo->path_git_dir);
361 repo->path_git_dir =
362 strdup(worktree->repo_path);
363 if (repo->path_git_dir == NULL) {
364 err = got_error_from_errno();
365 goto done;
367 if (!is_git_repo(repo)) {
368 free(repo->path_git_dir);
369 if (asprintf(&repo->path_git_dir,
370 "%s/%s", worktree->repo_path,
371 GOT_GIT_DIR) == -1) {
372 err = got_error_from_errno();
373 goto done;
376 got_worktree_close(worktree);
379 if (!is_git_repo(repo)) {
380 err = got_error(GOT_ERR_NOT_GIT_REPO);
381 goto done;
385 *ret = repo;
386 done:
387 if (err)
388 got_repo_close(repo);
389 free(abspath);
390 return err;
393 #if 0
394 static void
395 print_cache_stats(struct got_object_cache *cache, const char *name)
397 fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
398 name, got_object_idset_num_elements(cache->set), cache->cache_hit,
399 cache->cache_miss);
402 void check_refcount(struct got_object_id *id, void *data, void *arg)
404 struct got_object_cache *cache = arg;
405 struct got_object_cache_entry *ce = data;
406 struct got_object *obj;
407 struct got_tree_object *tree;
408 struct got_commit_object *commit;
409 char *id_str;
411 if (got_object_id_str(&id_str, id) != NULL)
412 return;
414 switch (cache->type) {
415 case GOT_OBJECT_CACHE_TYPE_OBJ:
416 obj = ce->data.obj;
417 if (obj->refcnt == 1)
418 break;
419 fprintf(stderr, "object %s has %d unclaimed references\n",
420 id_str, obj->refcnt - 1);
421 break;
422 case GOT_OBJECT_CACHE_TYPE_TREE:
423 tree = ce->data.tree;
424 if (tree->refcnt == 1)
425 break;
426 fprintf(stderr, "tree %s has %d unclaimed references\n",
427 id_str, tree->refcnt - 1);
428 break;
429 case GOT_OBJECT_CACHE_TYPE_COMMIT:
430 commit = ce->data.commit;
431 if (commit->refcnt == 1)
432 break;
433 fprintf(stderr, "commit %s has %d unclaimed references\n",
434 id_str, commit->refcnt);
435 break;
437 free(id_str);
439 #endif
441 void
442 got_repo_close(struct got_repository *repo)
444 int i;
446 for (i = 0; i < nitems(repo->packidx_cache); i++) {
447 if (repo->packidx_cache[i] == NULL)
448 break;
449 got_packidx_close(repo->packidx_cache[i]);
452 for (i = 0; i < nitems(repo->packs); i++) {
453 if (repo->packs[i].path_packfile == NULL)
454 break;
455 got_pack_close(&repo->packs[i]);
458 free(repo->path);
459 free(repo->path_git_dir);
461 #if 0
462 print_cache_stats(&repo->objcache, "object");
463 print_cache_stats(&repo->treecache, "tree");
464 print_cache_stats(&repo->commitcache, "commit");
465 got_object_idset_for_each(repo->objcache.set, check_refcount,
466 &repo->objcache);
467 got_object_idset_for_each(repo->treecache.set, check_refcount,
468 &repo->treecache);
469 got_object_idset_for_each(repo->commitcache.set, check_refcount,
470 &repo->commitcache);
471 #endif
473 if (repo->objcache.set)
474 got_object_idset_free(repo->objcache.set);
475 if (repo->treecache.set)
476 got_object_idset_free(repo->treecache.set);
477 if (repo->commitcache.set)
478 got_object_idset_free(repo->commitcache.set);
479 free(repo);