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 close(repo->privsep_children[i].imsg_fd);
428 const struct got_error *
429 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
430 const char *input_path, int check_disk)
432 const struct got_error *err = NULL;
433 const char *repo_abspath = NULL;
435 size_t repolen, cwdlen, len;
436 char *cwd, *canonpath, *path = NULL;
438 *in_repo_path = NULL;
440 cwd = getcwd(NULL, 0);
442 return got_error_from_errno();
444 canonpath = strdup(input_path);
445 if (canonpath == NULL) {
446 err = got_error_from_errno();
449 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
453 repo_abspath = got_repo_get_path(repo);
455 /* TODO: Call "get in-repository path of work-tree node" API. */
458 path = strdup(canonpath);
459 else if (lstat(canonpath, &sb) != 0) {
460 if (errno != ENOENT) {
461 err = got_error_from_errno();
465 * Path is not on disk.
466 * Assume it is already relative to repository root.
468 path = strdup(canonpath);
470 int is_repo_child = 0, is_cwd_child = 0;
472 path = realpath(canonpath, NULL);
474 err = got_error_from_errno();
478 repolen = strlen(repo_abspath);
479 cwdlen = strlen(cwd);
482 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
484 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
487 if (strcmp(path, repo_abspath) == 0) {
491 err = got_error_from_errno();
494 } else if (is_repo_child && is_cwd_child) {
496 /* TODO: Is path inside a got worktree? */
497 /* Strip common prefix with repository path. */
498 err = got_path_skip_common_ancestor(&child,
504 } else if (is_repo_child) {
505 /* Matched an on-disk path inside repository. */
506 if (got_repo_is_bare(repo)) {
508 * Matched an on-disk path inside repository
509 * database. Treat as repository-relative.
513 /* Strip common prefix with repository path. */
514 err = got_path_skip_common_ancestor(&child,
521 } else if (is_cwd_child) {
523 /* TODO: Is path inside a got worktree? */
524 /* Strip common prefix with cwd. */
525 err = got_path_skip_common_ancestor(&child, cwd,
533 * Matched unrelated on-disk path.
534 * Treat it as repository-relative.
539 /* Make in-repository path absolute */
540 if (path[0] != '/') {
542 if (asprintf(&abspath, "/%s", path) == -1) {
543 err = got_error_from_errno();
556 *in_repo_path = path;
560 const struct got_error *
561 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
563 const struct got_error *err = NULL;
566 for (i = 0; i < nitems(repo->packidx_cache); i++) {
567 if (repo->packidx_cache[i] == NULL)
570 if (i == nitems(repo->packidx_cache)) {
571 err = got_packidx_close(repo->packidx_cache[i - 1]);
577 * Insert the new pack index at the front so it will
578 * be searched first in the future.
580 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
581 sizeof(repo->packidx_cache) -
582 sizeof(repo->packidx_cache[0]));
583 repo->packidx_cache[0] = packidx;
589 is_packidx_filename(const char *name, size_t len)
591 if (len != GOT_PACKIDX_NAMELEN)
594 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
597 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
598 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
604 const struct got_error *
605 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
606 struct got_repository *repo, struct got_object_id *id)
608 const struct got_error *err;
615 /* Search pack index cache. */
616 for (i = 0; i < nitems(repo->packidx_cache); i++) {
617 if (repo->packidx_cache[i] == NULL)
619 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
621 *packidx = repo->packidx_cache[i];
625 /* No luck. Search the filesystem. */
627 path_packdir = got_repo_get_path_objects_pack(repo);
628 if (path_packdir == NULL)
629 return got_error_from_errno();
631 packdir = opendir(path_packdir);
632 if (packdir == NULL) {
633 err = got_error_from_errno();
637 while ((dent = readdir(packdir)) != NULL) {
638 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
641 if (asprintf(&path_packidx, "%s/%s", path_packdir,
642 dent->d_name) == -1) {
643 err = got_error_from_errno();
647 err = got_packidx_open(packidx, path_packidx, 0);
652 *idx = got_packidx_get_object_idx(*packidx, id);
654 err = NULL; /* found the object */
655 err = got_repo_cache_packidx(repo, *packidx);
659 err = got_packidx_close(*packidx);
665 err = got_error_no_obj(id);
668 if (packdir && closedir(packdir) != 0 && err == 0)
669 err = got_error_from_errno();
673 static const struct got_error *
674 read_packfile_hdr(int fd, struct got_packidx *packidx)
676 const struct got_error *err = NULL;
677 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
678 struct got_packfile_hdr hdr;
681 n = read(fd, &hdr, sizeof(hdr));
683 return got_error_from_errno();
684 if (n != sizeof(hdr))
685 return got_error(GOT_ERR_BAD_PACKFILE);
687 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
688 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
689 betoh32(hdr.nobjects) != totobj)
690 err = got_error(GOT_ERR_BAD_PACKFILE);
695 static const struct got_error *
696 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
698 const struct got_error *err = NULL;
700 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
702 return got_error_from_errno();
705 err = read_packfile_hdr(*fd, packidx);
715 const struct got_error *
716 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
717 const char *path_packfile, struct got_packidx *packidx)
719 const struct got_error *err = NULL;
720 struct got_pack *pack = NULL;
726 for (i = 0; i < nitems(repo->packs); i++) {
727 pack = &repo->packs[i];
728 if (pack->path_packfile == NULL)
730 if (strcmp(pack->path_packfile, path_packfile) == 0)
734 if (i == nitems(repo->packs) - 1) {
735 err = got_pack_close(&repo->packs[i - 1]);
738 memmove(&repo->packs[1], &repo->packs[0],
739 sizeof(repo->packs) - sizeof(repo->packs[0]));
743 pack = &repo->packs[i];
745 pack->path_packfile = strdup(path_packfile);
746 if (pack->path_packfile == NULL) {
747 err = got_error_from_errno();
751 err = open_packfile(&pack->fd, path_packfile, packidx);
755 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
759 pack->privsep_child = NULL;
761 #ifndef GOT_PACK_NO_MMAP
762 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
764 if (pack->map == MAP_FAILED)
765 pack->map = NULL; /* fall back to read(2) */
770 free(pack->path_packfile);
771 memset(pack, 0, sizeof(*pack));
779 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
781 struct got_pack *pack = NULL;
784 for (i = 0; i < nitems(repo->packs); i++) {
785 pack = &repo->packs[i];
786 if (pack->path_packfile == NULL)
788 if (strcmp(pack->path_packfile, path_packfile) == 0)