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/types.h>
18 #include <sys/queue.h>
22 #include <sys/syslimits.h>
37 #include "got_error.h"
38 #include "got_reference.h"
39 #include "got_repository.h"
40 #include "got_worktree.h"
41 #include "got_object.h"
43 #include "got_lib_path.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_pack.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_repository.h"
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 #define GOT_GIT_DIR ".git"
59 /* Mandatory files and directories inside the git directory. */
60 #define GOT_OBJECTS_DIR "objects"
61 #define GOT_REFS_DIR "refs"
62 #define GOT_HEAD_FILE "HEAD"
64 /* Other files and directories inside the git directory. */
65 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
66 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
67 #define GOT_OBJECTS_PACK_DIR "objects/pack"
70 got_repo_get_path(struct got_repository *repo)
72 return strdup(repo->path);
76 got_repo_get_path_git_dir(struct got_repository *repo)
78 return strdup(repo->path_git_dir);
82 got_repo_is_bare(struct got_repository *repo)
84 return (strcmp(repo->path, repo->path_git_dir) == 0);
88 get_path_git_child(struct got_repository *repo, const char *basename)
92 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
100 got_repo_get_path_objects(struct got_repository *repo)
102 return get_path_git_child(repo, GOT_OBJECTS_DIR);
106 got_repo_get_path_objects_pack(struct got_repository *repo)
108 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
112 got_repo_get_path_refs(struct got_repository *repo)
114 return get_path_git_child(repo, GOT_REFS_DIR);
118 get_path_head(struct got_repository *repo)
120 return get_path_git_child(repo, GOT_HEAD_FILE);
124 is_git_repo(struct got_repository *repo)
126 char *path_git = got_repo_get_path_git_dir(repo);
127 char *path_objects = got_repo_get_path_objects(repo);
128 char *path_refs = got_repo_get_path_refs(repo);
129 char *path_head = get_path_head(repo);
132 struct got_reference *head_ref;
134 if (lstat(path_git, &sb) == -1)
136 if (!S_ISDIR(sb.st_mode))
139 if (lstat(path_objects, &sb) == -1)
141 if (!S_ISDIR(sb.st_mode))
144 if (lstat(path_refs, &sb) == -1)
146 if (!S_ISDIR(sb.st_mode))
149 if (lstat(path_head, &sb) == -1)
151 if (!S_ISREG(sb.st_mode))
154 /* Check if the HEAD reference can be opened. */
155 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
157 got_ref_close(head_ref);
169 const struct got_error *
170 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
171 struct got_object *obj)
173 #ifndef GOT_NO_OBJ_CACHE
174 const struct got_error *err = NULL;
175 err = got_object_cache_add(&repo->objcache, id, obj);
184 got_repo_get_cached_object(struct got_repository *repo,
185 struct got_object_id *id)
187 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
190 const struct got_error *
191 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
192 struct got_tree_object *tree)
194 #ifndef GOT_NO_OBJ_CACHE
195 const struct got_error *err = NULL;
196 err = got_object_cache_add(&repo->treecache, id, tree);
204 struct got_tree_object *
205 got_repo_get_cached_tree(struct got_repository *repo,
206 struct got_object_id *id)
208 return (struct got_tree_object *)got_object_cache_get(
209 &repo->treecache, id);
212 const struct got_error *
213 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
214 struct got_commit_object *commit)
216 #ifndef GOT_NO_OBJ_CACHE
217 const struct got_error *err = NULL;
218 err = got_object_cache_add(&repo->commitcache, id, commit);
226 struct got_commit_object *
227 got_repo_get_cached_commit(struct got_repository *repo,
228 struct got_object_id *id)
230 return (struct got_commit_object *)got_object_cache_get(
231 &repo->commitcache, id);
234 const struct got_error *
235 got_repo_cache_mini_commit(struct got_repository *repo,
236 struct got_object_id *id, struct got_commit_object_mini *commit)
238 #ifndef GOT_NO_OBJ_CACHE
239 const struct got_error *err = NULL;
240 err = got_object_cache_add(&repo->minicommitcache, id, commit);
248 struct got_commit_object_mini *
249 got_repo_get_cached_mini_commit(struct got_repository *repo,
250 struct got_object_id *id)
252 return (struct got_commit_object_mini *)got_object_cache_get(
253 &repo->minicommitcache, id);
256 const struct got_error *
257 open_repo(struct got_repository *repo, const char *path)
259 const struct got_error *err = NULL;
260 struct got_worktree *worktree = NULL;
262 /* bare git repository? */
263 repo->path_git_dir = strdup(path);
264 if (repo->path_git_dir == NULL) {
265 err = got_error_from_errno();
268 if (is_git_repo(repo)) {
269 repo->path = strdup(repo->path_git_dir);
270 if (repo->path == NULL) {
271 err = got_error_from_errno();
277 /* git repository with working tree? */
278 free(repo->path_git_dir);
279 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
280 err = got_error_from_errno();
283 if (is_git_repo(repo)) {
284 repo->path = strdup(path);
285 if (repo->path == NULL) {
286 err = got_error_from_errno();
292 /* got work tree checked out from bare git repository? */
293 free(repo->path_git_dir);
294 repo->path_git_dir = NULL;
295 err = got_worktree_open(&worktree, path);
297 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
298 err = got_error(GOT_ERR_NOT_GIT_REPO);
301 repo->path_git_dir = strdup(worktree->repo_path);
302 if (repo->path_git_dir == NULL) {
303 err = got_error_from_errno();
307 /* got work tree checked out from git repository with working tree? */
308 if (!is_git_repo(repo)) {
309 free(repo->path_git_dir);
310 if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
311 GOT_GIT_DIR) == -1) {
312 err = got_error_from_errno();
313 repo->path_git_dir = NULL;
316 if (!is_git_repo(repo)) {
317 err = got_error(GOT_ERR_NOT_GIT_REPO);
320 repo->path = strdup(worktree->repo_path);
321 if (repo->path == NULL) {
322 err = got_error_from_errno();
326 repo->path = strdup(repo->path_git_dir);
327 if (repo->path == NULL) {
328 err = got_error_from_errno();
334 got_worktree_close(worktree);
338 const struct got_error *
339 got_repo_open(struct got_repository **repop, const char *path)
341 struct got_repository *repo = NULL;
342 const struct got_error *err = NULL;
343 char *abspath, *normpath = NULL;
344 int i, tried_root = 0;
348 if (got_path_is_absolute(path))
349 abspath = strdup(path);
351 abspath = got_path_get_absolute(path);
353 return got_error(GOT_ERR_BAD_PATH);
355 repo = calloc(1, sizeof(*repo));
357 err = got_error_from_errno();
361 for (i = 0; i < nitems(repo->privsep_children); i++) {
362 memset(&repo->privsep_children[i], 0,
363 sizeof(repo->privsep_children[0]));
364 repo->privsep_children[i].imsg_fd = -1;
367 err = got_object_cache_init(&repo->objcache,
368 GOT_OBJECT_CACHE_TYPE_OBJ);
371 err = got_object_cache_init(&repo->treecache,
372 GOT_OBJECT_CACHE_TYPE_TREE);
375 err = got_object_cache_init(&repo->commitcache,
376 GOT_OBJECT_CACHE_TYPE_COMMIT);
379 err = got_object_cache_init(&repo->minicommitcache,
380 GOT_OBJECT_CACHE_TYPE_MINI_COMMIT);
384 normpath = got_path_normalize(abspath);
385 if (normpath == NULL) {
386 err = got_error(GOT_ERR_BAD_PATH);
392 err = open_repo(repo, path);
395 if (err->code != GOT_ERR_NOT_GIT_REPO)
397 if (path[0] == '/' && path[1] == '\0') {
399 err = got_error(GOT_ERR_NOT_GIT_REPO);
404 path = dirname(path);
406 err = got_error_from_errno();
410 got_repo_close(repo);
418 const struct got_error *
419 got_repo_close(struct got_repository *repo)
421 const struct got_error *err = NULL, *child_err;
424 for (i = 0; i < nitems(repo->packidx_cache); i++) {
425 if (repo->packidx_cache[i] == NULL)
427 got_packidx_close(repo->packidx_cache[i]);
430 for (i = 0; i < nitems(repo->packs); i++) {
431 if (repo->packs[i].path_packfile == NULL)
433 got_pack_close(&repo->packs[i]);
437 free(repo->path_git_dir);
439 got_object_cache_close(&repo->objcache);
440 got_object_cache_close(&repo->treecache);
441 got_object_cache_close(&repo->commitcache);
442 got_object_cache_close(&repo->minicommitcache);
444 for (i = 0; i < nitems(repo->privsep_children); i++) {
445 if (repo->privsep_children[i].imsg_fd == -1)
447 imsg_clear(repo->privsep_children[i].ibuf);
448 free(repo->privsep_children[i].ibuf);
449 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
450 child_err = got_privsep_wait_for_child(
451 repo->privsep_children[i].pid);
452 if (child_err && err == NULL)
454 close(repo->privsep_children[i].imsg_fd);
461 const struct got_error *
462 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
463 const char *input_path, int check_disk)
465 const struct got_error *err = NULL;
466 char *repo_abspath = NULL, *cwd = NULL;
468 size_t repolen, cwdlen, len;
469 char *canonpath, *path = NULL;
471 *in_repo_path = NULL;
473 cwd = getcwd(NULL, 0);
475 return got_error_from_errno();
477 canonpath = strdup(input_path);
478 if (canonpath == NULL) {
479 err = got_error_from_errno();
482 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
486 repo_abspath = got_repo_get_path(repo);
487 if (repo_abspath == NULL) {
488 err = got_error_from_errno();
492 /* TODO: Call "get in-repository path of work-tree node" API. */
495 path = strdup(canonpath);
496 else if (lstat(canonpath, &sb) != 0) {
497 if (errno != ENOENT) {
498 err = got_error_from_errno();
502 * Path is not on disk.
503 * Assume it is already relative to repository root.
505 path = strdup(canonpath);
507 int is_repo_child = 0, is_cwd_child = 0;
509 path = realpath(canonpath, NULL);
511 err = got_error_from_errno();
515 repolen = strlen(repo_abspath);
516 cwdlen = strlen(cwd);
519 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
521 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
524 if (strcmp(path, repo_abspath) == 0) {
528 err = got_error_from_errno();
531 } else if (is_repo_child && is_cwd_child) {
533 /* TODO: Is path inside a got worktree? */
534 /* Strip common prefix with repository path. */
535 err = got_path_skip_common_ancestor(&child,
541 } else if (is_repo_child) {
542 /* Matched an on-disk path inside repository. */
543 if (got_repo_is_bare(repo)) {
545 * Matched an on-disk path inside repository
546 * database. Treat as repository-relative.
550 /* Strip common prefix with repository path. */
551 err = got_path_skip_common_ancestor(&child,
558 } else if (is_cwd_child) {
560 /* TODO: Is path inside a got worktree? */
561 /* Strip common prefix with cwd. */
562 err = got_path_skip_common_ancestor(&child, cwd,
570 * Matched unrelated on-disk path.
571 * Treat it as repository-relative.
576 /* Make in-repository path absolute */
577 if (path[0] != '/') {
579 if (asprintf(&abspath, "/%s", path) == -1) {
580 err = got_error_from_errno();
594 *in_repo_path = path;
598 const struct got_error *
599 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
601 const struct got_error *err = NULL;
604 for (i = 0; i < nitems(repo->packidx_cache); i++) {
605 if (repo->packidx_cache[i] == NULL)
609 if (i == nitems(repo->packidx_cache)) {
610 err = got_packidx_close(repo->packidx_cache[i - 1]);
613 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
614 sizeof(repo->packidx_cache) -
615 sizeof(repo->packidx_cache[0]));
619 repo->packidx_cache[i] = packidx;
624 is_packidx_filename(const char *name, size_t len)
626 if (len != GOT_PACKIDX_NAMELEN)
629 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
632 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
633 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
639 const struct got_error *
640 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
641 struct got_repository *repo, struct got_object_id *id)
643 const struct got_error *err;
650 /* Search pack index cache. */
651 for (i = 0; i < nitems(repo->packidx_cache); i++) {
652 if (repo->packidx_cache[i] == NULL)
654 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
656 *packidx = repo->packidx_cache[i];
660 /* No luck. Search the filesystem. */
662 path_packdir = got_repo_get_path_objects_pack(repo);
663 if (path_packdir == NULL)
664 return got_error_from_errno();
666 packdir = opendir(path_packdir);
667 if (packdir == NULL) {
668 err = got_error_from_errno();
672 while ((dent = readdir(packdir)) != NULL) {
673 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
676 if (asprintf(&path_packidx, "%s/%s", path_packdir,
677 dent->d_name) == -1) {
678 err = got_error_from_errno();
682 err = got_packidx_open(packidx, path_packidx, 0);
687 *idx = got_packidx_get_object_idx(*packidx, id);
689 err = NULL; /* found the object */
690 err = got_repo_cache_packidx(repo, *packidx);
694 err = got_packidx_close(*packidx);
700 err = got_error(GOT_ERR_NO_OBJ);
703 if (packdir && closedir(packdir) != 0 && err == 0)
704 err = got_error_from_errno();
708 static const struct got_error *
709 read_packfile_hdr(int fd, struct got_packidx *packidx)
711 const struct got_error *err = NULL;
712 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
713 struct got_packfile_hdr hdr;
716 n = read(fd, &hdr, sizeof(hdr));
718 return got_error_from_errno();
719 if (n != sizeof(hdr))
720 return got_error(GOT_ERR_BAD_PACKFILE);
722 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
723 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
724 betoh32(hdr.nobjects) != totobj)
725 err = got_error(GOT_ERR_BAD_PACKFILE);
730 static const struct got_error *
731 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
733 const struct got_error *err = NULL;
735 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
737 return got_error_from_errno();
740 err = read_packfile_hdr(*fd, packidx);
750 const struct got_error *
751 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
752 const char *path_packfile, struct got_packidx *packidx)
754 const struct got_error *err = NULL;
755 struct got_pack *pack = NULL;
761 for (i = 0; i < nitems(repo->packs); i++) {
762 pack = &repo->packs[i];
763 if (pack->path_packfile == NULL)
765 if (strcmp(pack->path_packfile, path_packfile) == 0)
769 if (i == nitems(repo->packs) - 1) {
770 err = got_pack_close(&repo->packs[i - 1]);
773 memmove(&repo->packs[1], &repo->packs[0],
774 sizeof(repo->packs) - sizeof(repo->packs[0]));
778 pack = &repo->packs[i];
780 pack->path_packfile = strdup(path_packfile);
781 if (pack->path_packfile == NULL) {
782 err = got_error_from_errno();
786 err = open_packfile(&pack->fd, path_packfile, packidx);
790 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
794 pack->privsep_child = NULL;
796 #ifndef GOT_PACK_NO_MMAP
797 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
799 if (pack->map == MAP_FAILED)
800 pack->map = NULL; /* fall back to read(2) */
805 free(pack->path_packfile);
806 memset(pack, 0, sizeof(*pack));
814 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
816 struct got_pack *pack = NULL;
819 for (i = 0; i < nitems(repo->packs); i++) {
820 pack = &repo->packs[i];
821 if (pack->path_packfile == NULL)
823 if (strcmp(pack->path_packfile, path_packfile) == 0)