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 int
73 got_repo_is_bare(struct got_repository *repo)
74 {
75 return (strcmp(repo->path, repo->path_git_dir) == 0);
76 }
78 static char *
79 get_path_git_child(struct got_repository *repo, const char *basename)
80 {
81 char *path_child;
83 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
84 basename) == -1)
85 return NULL;
87 return path_child;
88 }
90 char *
91 got_repo_get_path_objects(struct got_repository *repo)
92 {
93 return get_path_git_child(repo, GOT_OBJECTS_DIR);
94 }
96 char *
97 got_repo_get_path_objects_pack(struct got_repository *repo)
98 {
99 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
102 char *
103 got_repo_get_path_refs(struct got_repository *repo)
105 return get_path_git_child(repo, GOT_REFS_DIR);
108 static char *
109 get_path_head(struct got_repository *repo)
111 return get_path_git_child(repo, GOT_HEAD_FILE);
114 static int
115 is_git_repo(struct got_repository *repo)
117 char *path_git = got_repo_get_path_git_dir(repo);
118 char *path_objects = got_repo_get_path_objects(repo);
119 char *path_refs = got_repo_get_path_refs(repo);
120 char *path_head = get_path_head(repo);
121 int ret = 0;
122 struct stat sb;
123 struct got_reference *head_ref;
125 if (lstat(path_git, &sb) == -1)
126 goto done;
127 if (!S_ISDIR(sb.st_mode))
128 goto done;
130 if (lstat(path_objects, &sb) == -1)
131 goto done;
132 if (!S_ISDIR(sb.st_mode))
133 goto done;
135 if (lstat(path_refs, &sb) == -1)
136 goto done;
137 if (!S_ISDIR(sb.st_mode))
138 goto done;
140 if (lstat(path_head, &sb) == -1)
141 goto done;
142 if (!S_ISREG(sb.st_mode))
143 goto done;
145 /* Check if the HEAD reference can be opened. */
146 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
147 goto done;
148 got_ref_close(head_ref);
150 ret = 1;
151 done:
152 free(path_git);
153 free(path_objects);
154 free(path_refs);
155 free(path_head);
156 return ret;
160 #ifndef GOT_NO_OBJ_CACHE
161 static const struct got_error *
162 cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
164 const struct got_error *err = NULL;
165 struct got_object_cache_entry *ce;
166 int nelem;
168 nelem = got_object_idcache_num_elements(cache->idcache);
169 if (nelem >= cache->size) {
170 err = got_object_idcache_remove_least_used((void **)&ce,
171 cache->idcache);
172 if (err)
173 return err;
174 switch (cache->type) {
175 case GOT_OBJECT_CACHE_TYPE_OBJ:
176 got_object_close(ce->data.obj);
177 break;
178 case GOT_OBJECT_CACHE_TYPE_TREE:
179 got_object_tree_close(ce->data.tree);
180 break;
181 case GOT_OBJECT_CACHE_TYPE_COMMIT:
182 got_object_commit_close(ce->data.commit);
183 break;
185 free(ce);
188 ce = calloc(1, sizeof(*ce));
189 if (ce == NULL)
190 return got_error_from_errno();
191 memcpy(&ce->id, id, sizeof(ce->id));
192 switch (cache->type) {
193 case GOT_OBJECT_CACHE_TYPE_OBJ:
194 ce->data.obj = (struct got_object *)item;
195 break;
196 case GOT_OBJECT_CACHE_TYPE_TREE:
197 ce->data.tree = (struct got_tree_object *)item;
198 break;
199 case GOT_OBJECT_CACHE_TYPE_COMMIT:
200 ce->data.commit = (struct got_commit_object *)item;
201 break;
204 err = got_object_idcache_add(cache->idcache, id, ce);
205 if (err) {
206 if (err->code == GOT_ERR_OBJ_EXISTS) {
207 free(ce);
208 err = NULL;
211 return err;
213 #endif
215 const struct got_error *
216 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
217 struct got_object *obj)
219 #ifndef GOT_NO_OBJ_CACHE
220 const struct got_error *err = NULL;
221 err = cache_add(&repo->objcache, id, obj);
222 if (err)
223 return err;
224 obj->refcnt++;
225 #endif
226 return NULL;
229 struct got_object *
230 got_repo_get_cached_object(struct got_repository *repo,
231 struct got_object_id *id)
233 struct got_object_cache_entry *ce;
235 ce = got_object_idcache_get(repo->objcache.idcache, id);
236 if (ce) {
237 repo->objcache.cache_hit++;
238 return ce->data.obj;
241 repo->objcache.cache_miss++;
242 return NULL;
245 const struct got_error *
246 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
247 struct got_tree_object *tree)
249 #ifndef GOT_NO_OBJ_CACHE
250 const struct got_error *err = NULL;
251 err = cache_add(&repo->treecache, id, tree);
252 if (err)
253 return err;
254 tree->refcnt++;
255 #endif
256 return NULL;
259 struct got_tree_object *
260 got_repo_get_cached_tree(struct got_repository *repo,
261 struct got_object_id *id)
263 struct got_object_cache_entry *ce;
265 ce = got_object_idcache_get(repo->treecache.idcache, id);
266 if (ce) {
267 repo->treecache.cache_hit++;
268 return ce->data.tree;
271 repo->treecache.cache_miss++;
272 return NULL;
275 const struct got_error *
276 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
277 struct got_commit_object *commit)
279 #ifndef GOT_NO_OBJ_CACHE
280 const struct got_error *err = NULL;
281 err = cache_add(&repo->commitcache, id, commit);
282 if (err)
283 return err;
285 commit->refcnt++;
286 #endif
287 return NULL;
290 struct got_commit_object *
291 got_repo_get_cached_commit(struct got_repository *repo,
292 struct got_object_id *id)
294 struct got_object_cache_entry *ce;
296 ce = got_object_idcache_get(repo->commitcache.idcache, id);
297 if (ce) {
298 repo->commitcache.cache_hit++;
299 return ce->data.commit;
302 repo->commitcache.cache_miss++;
303 return NULL;
306 const struct got_error *
307 open_repo(struct got_repository *repo, const char *path)
309 const struct got_error *err = NULL;
310 struct got_worktree *worktree = NULL;
312 /* bare git repository? */
313 repo->path_git_dir = strdup(path);
314 if (repo->path_git_dir == NULL) {
315 err = got_error_from_errno();
316 goto done;
318 if (is_git_repo(repo)) {
319 repo->path = strdup(repo->path_git_dir);
320 if (repo->path == NULL) {
321 err = got_error_from_errno();
322 goto done;
324 return NULL;
327 /* git repository with working tree? */
328 free(repo->path_git_dir);
329 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
330 err = got_error_from_errno();
331 goto done;
333 if (is_git_repo(repo)) {
334 repo->path = strdup(path);
335 if (repo->path == NULL) {
336 err = got_error_from_errno();
337 goto done;
339 return NULL;
342 /* got work tree checked out from bare git repository? */
343 free(repo->path_git_dir);
344 repo->path_git_dir = NULL;
345 err = got_worktree_open(&worktree, path);
346 if (err) {
347 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
348 err = got_error(GOT_ERR_NOT_GIT_REPO);
349 goto done;
351 repo->path_git_dir = strdup(worktree->repo_path);
352 if (repo->path_git_dir == NULL) {
353 err = got_error_from_errno();
354 goto done;
357 /* got work tree checked out from git repository with working tree? */
358 if (!is_git_repo(repo)) {
359 free(repo->path_git_dir);
360 if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
361 GOT_GIT_DIR) == -1) {
362 err = got_error_from_errno();
363 repo->path_git_dir = NULL;
364 goto done;
366 if (!is_git_repo(repo)) {
367 err = got_error(GOT_ERR_NOT_GIT_REPO);
368 goto done;
370 repo->path = strdup(worktree->repo_path);
371 if (repo->path == NULL) {
372 err = got_error_from_errno();
373 goto done;
375 } else {
376 repo->path = strdup(repo->path_git_dir);
377 if (repo->path == NULL) {
378 err = got_error_from_errno();
379 goto done;
382 done:
383 if (worktree)
384 got_worktree_close(worktree);
385 return err;
388 const struct got_error *
389 got_repo_open(struct got_repository **repop, const char *path)
391 struct got_repository *repo = NULL;
392 const struct got_error *err = NULL;
393 char *abspath, *normpath = NULL;
394 int tried_root = 0;
396 *repop = NULL;
398 if (got_path_is_absolute(path))
399 abspath = strdup(path);
400 else
401 abspath = got_path_get_absolute(path);
402 if (abspath == NULL)
403 return got_error(GOT_ERR_BAD_PATH);
405 repo = calloc(1, sizeof(*repo));
406 if (repo == NULL) {
407 err = got_error_from_errno();
408 goto done;
411 repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
412 repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
413 repo->objcache.idcache = got_object_idcache_alloc(repo->objcache.size);
414 if (repo->objcache.idcache == NULL) {
415 err = got_error_from_errno();
416 goto done;
419 repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
420 repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
421 repo->treecache.idcache =
422 got_object_idcache_alloc(repo->treecache.size);
423 if (repo->treecache.idcache == NULL) {
424 err = got_error_from_errno();
425 goto done;
428 repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
429 repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
430 repo->commitcache.idcache =
431 got_object_idcache_alloc(repo->commitcache.size);
432 if (repo->commitcache.idcache == NULL) {
433 err = got_error_from_errno();
434 goto done;
437 normpath = got_path_normalize(abspath);
438 if (normpath == NULL) {
439 err = got_error(GOT_ERR_BAD_PATH);
440 goto done;
443 path = normpath;
444 do {
445 err = open_repo(repo, path);
446 if (err == NULL)
447 break;
448 if (err->code != GOT_ERR_NOT_GIT_REPO)
449 break;
450 if (path[0] == '/' && path[1] == '\0') {
451 if (tried_root) {
452 err = got_error(GOT_ERR_NOT_GIT_REPO);
453 break;
455 tried_root = 1;
457 path = dirname(path);
458 if (path == NULL)
459 err = got_error_from_errno();
460 } while (path);
461 done:
462 if (err)
463 got_repo_close(repo);
464 else
465 *repop = repo;
466 free(abspath);
467 free(normpath);
468 return err;
471 #if 0
472 static void
473 print_cache_stats(struct got_object_cache *cache, const char *name)
475 fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
476 name, got_object_idcache_num_elements(cache->idcache),
477 cache->cache_hit, cache->cache_miss);
480 void check_refcount(struct got_object_id *id, void *data, void *arg)
482 struct got_object_cache *cache = arg;
483 struct got_object_cache_entry *ce = data;
484 struct got_object *obj;
485 struct got_tree_object *tree;
486 struct got_commit_object *commit;
487 char *id_str;
489 if (got_object_id_str(&id_str, id) != NULL)
490 return;
492 switch (cache->type) {
493 case GOT_OBJECT_CACHE_TYPE_OBJ:
494 obj = ce->data.obj;
495 if (obj->refcnt == 1)
496 break;
497 fprintf(stderr, "object %s has %d unclaimed references\n",
498 id_str, obj->refcnt - 1);
499 break;
500 case GOT_OBJECT_CACHE_TYPE_TREE:
501 tree = ce->data.tree;
502 if (tree->refcnt == 1)
503 break;
504 fprintf(stderr, "tree %s has %d unclaimed references\n",
505 id_str, tree->refcnt - 1);
506 break;
507 case GOT_OBJECT_CACHE_TYPE_COMMIT:
508 commit = ce->data.commit;
509 if (commit->refcnt == 1)
510 break;
511 fprintf(stderr, "commit %s has %d unclaimed references\n",
512 id_str, commit->refcnt);
513 break;
515 free(id_str);
517 #endif
519 void
520 got_repo_close(struct got_repository *repo)
522 int i;
524 for (i = 0; i < nitems(repo->packidx_cache); i++) {
525 if (repo->packidx_cache[i] == NULL)
526 break;
527 got_packidx_close(repo->packidx_cache[i]);
530 for (i = 0; i < nitems(repo->packs); i++) {
531 if (repo->packs[i].path_packfile == NULL)
532 break;
533 got_pack_close(&repo->packs[i]);
536 free(repo->path);
537 free(repo->path_git_dir);
539 #if 0
540 print_cache_stats(&repo->objcache, "object");
541 print_cache_stats(&repo->treecache, "tree");
542 print_cache_stats(&repo->commitcache, "commit");
543 got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
544 &repo->objcache);
545 got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
546 &repo->treecache);
547 got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
548 &repo->commitcache);
549 #endif
551 if (repo->objcache.idcache)
552 got_object_idcache_free(repo->objcache.idcache);
553 if (repo->treecache.idcache)
554 got_object_idcache_free(repo->treecache.idcache);
555 if (repo->commitcache.idcache)
556 got_object_idcache_free(repo->commitcache.idcache);
557 free(repo);
560 const struct got_error *
561 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
562 const char *input_path)
564 const struct got_error *err = NULL;
565 char *repo_abspath = NULL, *cwd = NULL;
566 struct stat sb;
567 size_t repolen, cwdlen, len;
568 char *canonpath, *path;
570 *in_repo_path = NULL;
572 cwd = getcwd(NULL, 0);
573 if (cwd == NULL)
574 return got_error_from_errno();
576 canonpath = strdup(input_path);
577 if (canonpath == NULL) {
578 err = got_error_from_errno();
579 goto done;
581 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
582 if (err)
583 goto done;
585 repo_abspath = got_repo_get_path(repo);
586 if (repo_abspath == NULL) {
587 err = got_error_from_errno();
588 goto done;
591 /* TODO: Call "get in-repository path of work-tree node" API. */
593 if (lstat(canonpath, &sb) != 0) {
594 if (errno != ENOENT) {
595 err = got_error_from_errno();
596 goto done;
598 /*
599 * Path is not on disk.
600 * Assume it is already relative to repository root.
601 */
602 path = strdup(canonpath);
603 } else {
604 int is_repo_child = 0, is_cwd_child = 0;
606 path = realpath(canonpath, NULL);
607 if (path == NULL) {
608 err = got_error_from_errno();
609 goto done;
612 repolen = strlen(repo_abspath);
613 cwdlen = strlen(cwd);
614 len = strlen(path);
616 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
617 is_repo_child = 1;
618 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
619 is_cwd_child = 1;
621 if (strcmp(path, repo_abspath) == 0) {
622 free(path);
623 path = strdup("");
624 if (path == NULL) {
625 err = got_error_from_errno();
626 goto done;
628 } else if (is_repo_child && is_cwd_child) {
629 char *child;
630 /* TODO: Is path inside a got worktree? */
631 /* Strip common prefix with repository path. */
632 err = got_path_skip_common_ancestor(&child,
633 repo_abspath, path);
634 if (err)
635 goto done;
636 free(path);
637 path = child;
638 } else if (is_repo_child) {
639 /* Matched an on-disk path inside repository. */
640 if (got_repo_is_bare(repo)) {
641 /*
642 * Matched an on-disk path inside repository
643 * database. Treat as repository-relative.
644 */
645 } else {
646 char *child;
647 /* Strip common prefix with repository path. */
648 err = got_path_skip_common_ancestor(&child,
649 repo_abspath, path);
650 if (err)
651 goto done;
652 free(path);
653 path = child;
655 } else if (is_cwd_child) {
656 char *child;
657 /* TODO: Is path inside a got worktree? */
658 /* Strip common prefix with cwd. */
659 err = got_path_skip_common_ancestor(&child, cwd,
660 path);
661 if (err)
662 goto done;
663 free(path);
664 path = child;
665 } else {
666 /*
667 * Matched unrelated on-disk path.
668 * Treat it as repository-relative.
669 */
673 /* Make in-repository path absolute */
674 if (path[0] != '/') {
675 char *abspath;
676 if (asprintf(&abspath, "/%s", path) == -1) {
677 err = got_error_from_errno();
678 goto done;
680 free(path);
681 path = abspath;
684 done:
685 free(repo_abspath);
686 free(cwd);
687 free(canonpath);
688 if (err)
689 free(path);
690 else
691 *in_repo_path = path;
692 return err;