2 * Copyright (c) 2018, 2019 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"
68 #define GOT_PACKED_REFS_FILE "packed-refs"
71 got_repo_get_path(struct got_repository *repo)
77 got_repo_get_path_git_dir(struct got_repository *repo)
79 return repo->path_git_dir;
83 got_repo_is_bare(struct got_repository *repo)
85 return (strcmp(repo->path, repo->path_git_dir) == 0);
89 get_path_git_child(struct got_repository *repo, const char *basename)
93 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
101 got_repo_get_path_objects(struct got_repository *repo)
103 return get_path_git_child(repo, GOT_OBJECTS_DIR);
107 got_repo_get_path_objects_pack(struct got_repository *repo)
109 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
113 got_repo_get_path_refs(struct got_repository *repo)
115 return get_path_git_child(repo, GOT_REFS_DIR);
119 got_repo_get_path_packed_refs(struct got_repository *repo)
121 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
125 get_path_head(struct got_repository *repo)
127 return get_path_git_child(repo, GOT_HEAD_FILE);
131 is_git_repo(struct got_repository *repo)
133 const char *path_git = got_repo_get_path_git_dir(repo);
134 char *path_objects = got_repo_get_path_objects(repo);
135 char *path_refs = got_repo_get_path_refs(repo);
136 char *path_head = get_path_head(repo);
139 struct got_reference *head_ref;
141 if (lstat(path_git, &sb) == -1)
143 if (!S_ISDIR(sb.st_mode))
146 if (lstat(path_objects, &sb) == -1)
148 if (!S_ISDIR(sb.st_mode))
151 if (lstat(path_refs, &sb) == -1)
153 if (!S_ISDIR(sb.st_mode))
156 if (lstat(path_head, &sb) == -1)
158 if (!S_ISREG(sb.st_mode))
161 /* Check if the HEAD reference can be opened. */
162 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
164 got_ref_close(head_ref);
175 const struct got_error *
176 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
177 struct got_object *obj)
179 #ifndef GOT_NO_OBJ_CACHE
180 const struct got_error *err = NULL;
181 err = got_object_cache_add(&repo->objcache, id, obj);
190 got_repo_get_cached_object(struct got_repository *repo,
191 struct got_object_id *id)
193 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
196 const struct got_error *
197 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
198 struct got_tree_object *tree)
200 #ifndef GOT_NO_OBJ_CACHE
201 const struct got_error *err = NULL;
202 err = got_object_cache_add(&repo->treecache, id, tree);
210 struct got_tree_object *
211 got_repo_get_cached_tree(struct got_repository *repo,
212 struct got_object_id *id)
214 return (struct got_tree_object *)got_object_cache_get(
215 &repo->treecache, id);
218 const struct got_error *
219 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
220 struct got_commit_object *commit)
222 #ifndef GOT_NO_OBJ_CACHE
223 const struct got_error *err = NULL;
224 err = got_object_cache_add(&repo->commitcache, id, commit);
232 struct got_commit_object *
233 got_repo_get_cached_commit(struct got_repository *repo,
234 struct got_object_id *id)
236 return (struct got_commit_object *)got_object_cache_get(
237 &repo->commitcache, id);
240 const struct got_error *
241 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
242 struct got_tag_object *tag)
244 #ifndef GOT_NO_OBJ_CACHE
245 const struct got_error *err = NULL;
246 err = got_object_cache_add(&repo->tagcache, id, tag);
254 struct got_tag_object *
255 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
257 return (struct got_tag_object *)got_object_cache_get(
258 &repo->tagcache, id);
261 const struct got_error *
262 open_repo(struct got_repository *repo, const char *path)
264 const struct got_error *err = NULL;
266 /* bare git repository? */
267 repo->path_git_dir = strdup(path);
268 if (repo->path_git_dir == NULL)
269 return got_error_from_errno();
270 if (is_git_repo(repo)) {
271 repo->path = strdup(repo->path_git_dir);
272 if (repo->path == NULL) {
273 err = got_error_from_errno();
279 /* git repository with working tree? */
280 free(repo->path_git_dir);
281 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
282 err = got_error_from_errno();
285 if (is_git_repo(repo)) {
286 repo->path = strdup(path);
287 if (repo->path == NULL) {
288 err = got_error_from_errno();
294 err = got_error(GOT_ERR_NOT_GIT_REPO);
299 free(repo->path_git_dir);
300 repo->path_git_dir = NULL;
305 const struct got_error *
306 got_repo_open(struct got_repository **repop, const char *path)
308 struct got_repository *repo = NULL;
309 const struct got_error *err = NULL;
310 char *abspath, *normpath = NULL;
311 int i, tried_root = 0;
315 if (got_path_is_absolute(path))
316 abspath = strdup(path);
318 abspath = got_path_get_absolute(path);
320 return got_error(GOT_ERR_BAD_PATH);
322 repo = calloc(1, sizeof(*repo));
324 err = got_error_from_errno();
328 for (i = 0; i < nitems(repo->privsep_children); i++) {
329 memset(&repo->privsep_children[i], 0,
330 sizeof(repo->privsep_children[0]));
331 repo->privsep_children[i].imsg_fd = -1;
334 err = got_object_cache_init(&repo->objcache,
335 GOT_OBJECT_CACHE_TYPE_OBJ);
338 err = got_object_cache_init(&repo->treecache,
339 GOT_OBJECT_CACHE_TYPE_TREE);
342 err = got_object_cache_init(&repo->commitcache,
343 GOT_OBJECT_CACHE_TYPE_COMMIT);
346 err = got_object_cache_init(&repo->tagcache,
347 GOT_OBJECT_CACHE_TYPE_TAG);
351 normpath = got_path_normalize(abspath);
352 if (normpath == NULL) {
353 err = got_error(GOT_ERR_BAD_PATH);
359 err = open_repo(repo, path);
362 if (err->code != GOT_ERR_NOT_GIT_REPO)
364 if (path[0] == '/' && path[1] == '\0') {
366 err = got_error(GOT_ERR_NOT_GIT_REPO);
371 path = dirname(path);
373 err = got_error_from_errno();
377 got_repo_close(repo);
385 const struct got_error *
386 got_repo_close(struct got_repository *repo)
388 const struct got_error *err = NULL, *child_err;
391 for (i = 0; i < nitems(repo->packidx_cache); i++) {
392 if (repo->packidx_cache[i] == NULL)
394 got_packidx_close(repo->packidx_cache[i]);
397 for (i = 0; i < nitems(repo->packs); i++) {
398 if (repo->packs[i].path_packfile == NULL)
400 got_pack_close(&repo->packs[i]);
404 free(repo->path_git_dir);
406 got_object_cache_close(&repo->objcache);
407 got_object_cache_close(&repo->treecache);
408 got_object_cache_close(&repo->commitcache);
409 got_object_cache_close(&repo->tagcache);
411 for (i = 0; i < nitems(repo->privsep_children); i++) {
412 if (repo->privsep_children[i].imsg_fd == -1)
414 imsg_clear(repo->privsep_children[i].ibuf);
415 free(repo->privsep_children[i].ibuf);
416 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
417 child_err = got_privsep_wait_for_child(
418 repo->privsep_children[i].pid);
419 if (child_err && err == NULL)
421 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
423 err = got_error_from_errno();
430 const struct got_error *
431 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
432 const char *input_path, int check_disk)
434 const struct got_error *err = NULL;
435 const char *repo_abspath = NULL;
437 size_t repolen, cwdlen, len;
438 char *cwd, *canonpath, *path = NULL;
440 *in_repo_path = NULL;
442 cwd = getcwd(NULL, 0);
444 return got_error_from_errno();
446 canonpath = strdup(input_path);
447 if (canonpath == NULL) {
448 err = got_error_from_errno();
451 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
455 repo_abspath = got_repo_get_path(repo);
457 /* TODO: Call "get in-repository path of work-tree node" API. */
460 path = strdup(canonpath);
461 else if (lstat(canonpath, &sb) != 0) {
462 if (errno != ENOENT) {
463 err = got_error_from_errno();
467 * Path is not on disk.
468 * Assume it is already relative to repository root.
470 path = strdup(canonpath);
472 int is_repo_child = 0, is_cwd_child = 0;
474 path = realpath(canonpath, NULL);
476 err = got_error_from_errno();
480 repolen = strlen(repo_abspath);
481 cwdlen = strlen(cwd);
484 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
486 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
489 if (strcmp(path, repo_abspath) == 0) {
493 err = got_error_from_errno();
496 } else if (is_repo_child && is_cwd_child) {
498 /* TODO: Is path inside a got worktree? */
499 /* Strip common prefix with repository path. */
500 err = got_path_skip_common_ancestor(&child,
506 } else if (is_repo_child) {
507 /* Matched an on-disk path inside repository. */
508 if (got_repo_is_bare(repo)) {
510 * Matched an on-disk path inside repository
511 * database. Treat as repository-relative.
515 /* Strip common prefix with repository path. */
516 err = got_path_skip_common_ancestor(&child,
523 } else if (is_cwd_child) {
525 /* TODO: Is path inside a got worktree? */
526 /* Strip common prefix with cwd. */
527 err = got_path_skip_common_ancestor(&child, cwd,
535 * Matched unrelated on-disk path.
536 * Treat it as repository-relative.
541 /* Make in-repository path absolute */
542 if (path[0] != '/') {
544 if (asprintf(&abspath, "/%s", path) == -1) {
545 err = got_error_from_errno();
558 *in_repo_path = path;
562 const struct got_error *
563 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
565 const struct got_error *err = NULL;
568 for (i = 0; i < nitems(repo->packidx_cache); i++) {
569 if (repo->packidx_cache[i] == NULL)
572 if (i == nitems(repo->packidx_cache)) {
573 err = got_packidx_close(repo->packidx_cache[i - 1]);
579 * Insert the new pack index at the front so it will
580 * be searched first in the future.
582 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
583 sizeof(repo->packidx_cache) -
584 sizeof(repo->packidx_cache[0]));
585 repo->packidx_cache[0] = packidx;
591 is_packidx_filename(const char *name, size_t len)
593 if (len != GOT_PACKIDX_NAMELEN)
596 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
599 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
600 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
606 const struct got_error *
607 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
608 struct got_repository *repo, struct got_object_id *id)
610 const struct got_error *err;
617 /* Search pack index cache. */
618 for (i = 0; i < nitems(repo->packidx_cache); i++) {
619 if (repo->packidx_cache[i] == NULL)
621 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
623 *packidx = repo->packidx_cache[i];
627 /* No luck. Search the filesystem. */
629 path_packdir = got_repo_get_path_objects_pack(repo);
630 if (path_packdir == NULL)
631 return got_error_from_errno();
633 packdir = opendir(path_packdir);
634 if (packdir == NULL) {
635 err = got_error_from_errno();
639 while ((dent = readdir(packdir)) != NULL) {
640 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
643 if (asprintf(&path_packidx, "%s/%s", path_packdir,
644 dent->d_name) == -1) {
645 err = got_error_from_errno();
649 err = got_packidx_open(packidx, path_packidx, 0);
654 *idx = got_packidx_get_object_idx(*packidx, id);
656 err = NULL; /* found the object */
657 err = got_repo_cache_packidx(repo, *packidx);
661 err = got_packidx_close(*packidx);
667 err = got_error_no_obj(id);
670 if (packdir && closedir(packdir) != 0 && err == 0)
671 err = got_error_from_errno();
675 static const struct got_error *
676 read_packfile_hdr(int fd, struct got_packidx *packidx)
678 const struct got_error *err = NULL;
679 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
680 struct got_packfile_hdr hdr;
683 n = read(fd, &hdr, sizeof(hdr));
685 return got_error_from_errno();
686 if (n != sizeof(hdr))
687 return got_error(GOT_ERR_BAD_PACKFILE);
689 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
690 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
691 betoh32(hdr.nobjects) != totobj)
692 err = got_error(GOT_ERR_BAD_PACKFILE);
697 static const struct got_error *
698 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
700 const struct got_error *err = NULL;
702 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
704 return got_error_from_errno();
707 err = read_packfile_hdr(*fd, packidx);
717 const struct got_error *
718 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
719 const char *path_packfile, struct got_packidx *packidx)
721 const struct got_error *err = NULL;
722 struct got_pack *pack = NULL;
728 for (i = 0; i < nitems(repo->packs); i++) {
729 pack = &repo->packs[i];
730 if (pack->path_packfile == NULL)
732 if (strcmp(pack->path_packfile, path_packfile) == 0)
736 if (i == nitems(repo->packs) - 1) {
737 err = got_pack_close(&repo->packs[i - 1]);
740 memmove(&repo->packs[1], &repo->packs[0],
741 sizeof(repo->packs) - sizeof(repo->packs[0]));
745 pack = &repo->packs[i];
747 pack->path_packfile = strdup(path_packfile);
748 if (pack->path_packfile == NULL) {
749 err = got_error_from_errno();
753 err = open_packfile(&pack->fd, path_packfile, packidx);
757 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
761 pack->privsep_child = NULL;
763 #ifndef GOT_PACK_NO_MMAP
764 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
766 if (pack->map == MAP_FAILED)
767 pack->map = NULL; /* fall back to read(2) */
772 free(pack->path_packfile);
773 memset(pack, 0, sizeof(*pack));
781 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
783 struct got_pack *pack = NULL;
786 for (i = 0; i < nitems(repo->packs); i++) {
787 pack = &repo->packs[i];
788 if (pack->path_packfile == NULL)
790 if (strcmp(pack->path_packfile, path_packfile) == 0)