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)
76 got_repo_get_path_git_dir(struct got_repository *repo)
78 return 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 const 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);
168 const struct got_error *
169 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
170 struct got_object *obj)
172 #ifndef GOT_NO_OBJ_CACHE
173 const struct got_error *err = NULL;
174 err = got_object_cache_add(&repo->objcache, id, obj);
183 got_repo_get_cached_object(struct got_repository *repo,
184 struct got_object_id *id)
186 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
189 const struct got_error *
190 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
191 struct got_tree_object *tree)
193 #ifndef GOT_NO_OBJ_CACHE
194 const struct got_error *err = NULL;
195 err = got_object_cache_add(&repo->treecache, id, tree);
203 struct got_tree_object *
204 got_repo_get_cached_tree(struct got_repository *repo,
205 struct got_object_id *id)
207 return (struct got_tree_object *)got_object_cache_get(
208 &repo->treecache, id);
211 const struct got_error *
212 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
213 struct got_commit_object *commit)
215 #ifndef GOT_NO_OBJ_CACHE
216 const struct got_error *err = NULL;
217 err = got_object_cache_add(&repo->commitcache, id, commit);
225 struct got_commit_object *
226 got_repo_get_cached_commit(struct got_repository *repo,
227 struct got_object_id *id)
229 return (struct got_commit_object *)got_object_cache_get(
230 &repo->commitcache, id);
233 const struct got_error *
234 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
235 struct got_tag_object *tag)
237 #ifndef GOT_NO_OBJ_CACHE
238 const struct got_error *err = NULL;
239 err = got_object_cache_add(&repo->tagcache, id, tag);
247 struct got_tag_object *
248 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
250 return (struct got_tag_object *)got_object_cache_get(
251 &repo->tagcache, id);
254 const struct got_error *
255 open_repo(struct got_repository *repo, const char *path)
257 const struct got_error *err = NULL;
258 struct got_worktree *worktree = NULL;
260 /* bare git repository? */
261 repo->path_git_dir = strdup(path);
262 if (repo->path_git_dir == NULL) {
263 err = got_error_from_errno();
266 if (is_git_repo(repo)) {
267 repo->path = strdup(repo->path_git_dir);
268 if (repo->path == NULL) {
269 err = got_error_from_errno();
275 /* git repository with working tree? */
276 free(repo->path_git_dir);
277 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
278 err = got_error_from_errno();
281 if (is_git_repo(repo)) {
282 repo->path = strdup(path);
283 if (repo->path == NULL) {
284 err = got_error_from_errno();
290 /* got work tree checked out from bare git repository? */
291 free(repo->path_git_dir);
292 repo->path_git_dir = NULL;
293 err = got_worktree_open(&worktree, path);
295 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
296 err = got_error(GOT_ERR_NOT_GIT_REPO);
299 repo->path_git_dir = strdup(worktree->repo_path);
300 if (repo->path_git_dir == NULL) {
301 err = got_error_from_errno();
305 /* got work tree checked out from git repository with working tree? */
306 if (!is_git_repo(repo)) {
307 free(repo->path_git_dir);
308 if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
309 GOT_GIT_DIR) == -1) {
310 err = got_error_from_errno();
311 repo->path_git_dir = NULL;
314 if (!is_git_repo(repo)) {
315 err = got_error(GOT_ERR_NOT_GIT_REPO);
318 repo->path = strdup(worktree->repo_path);
319 if (repo->path == NULL) {
320 err = got_error_from_errno();
324 repo->path = strdup(repo->path_git_dir);
325 if (repo->path == NULL) {
326 err = got_error_from_errno();
332 got_worktree_close(worktree);
336 const struct got_error *
337 got_repo_open(struct got_repository **repop, const char *path)
339 struct got_repository *repo = NULL;
340 const struct got_error *err = NULL;
341 char *abspath, *normpath = NULL;
342 int i, tried_root = 0;
346 if (got_path_is_absolute(path))
347 abspath = strdup(path);
349 abspath = got_path_get_absolute(path);
351 return got_error(GOT_ERR_BAD_PATH);
353 repo = calloc(1, sizeof(*repo));
355 err = got_error_from_errno();
359 for (i = 0; i < nitems(repo->privsep_children); i++) {
360 memset(&repo->privsep_children[i], 0,
361 sizeof(repo->privsep_children[0]));
362 repo->privsep_children[i].imsg_fd = -1;
365 err = got_object_cache_init(&repo->objcache,
366 GOT_OBJECT_CACHE_TYPE_OBJ);
369 err = got_object_cache_init(&repo->treecache,
370 GOT_OBJECT_CACHE_TYPE_TREE);
373 err = got_object_cache_init(&repo->commitcache,
374 GOT_OBJECT_CACHE_TYPE_COMMIT);
377 err = got_object_cache_init(&repo->tagcache,
378 GOT_OBJECT_CACHE_TYPE_TAG);
382 normpath = got_path_normalize(abspath);
383 if (normpath == NULL) {
384 err = got_error(GOT_ERR_BAD_PATH);
390 err = open_repo(repo, path);
393 if (err->code != GOT_ERR_NOT_GIT_REPO)
395 if (path[0] == '/' && path[1] == '\0') {
397 err = got_error(GOT_ERR_NOT_GIT_REPO);
402 path = dirname(path);
404 err = got_error_from_errno();
408 got_repo_close(repo);
416 const struct got_error *
417 got_repo_close(struct got_repository *repo)
419 const struct got_error *err = NULL, *child_err;
422 for (i = 0; i < nitems(repo->packidx_cache); i++) {
423 if (repo->packidx_cache[i] == NULL)
425 got_packidx_close(repo->packidx_cache[i]);
428 for (i = 0; i < nitems(repo->packs); i++) {
429 if (repo->packs[i].path_packfile == NULL)
431 got_pack_close(&repo->packs[i]);
435 free(repo->path_git_dir);
437 got_object_cache_close(&repo->objcache);
438 got_object_cache_close(&repo->treecache);
439 got_object_cache_close(&repo->commitcache);
440 got_object_cache_close(&repo->tagcache);
442 for (i = 0; i < nitems(repo->privsep_children); i++) {
443 if (repo->privsep_children[i].imsg_fd == -1)
445 imsg_clear(repo->privsep_children[i].ibuf);
446 free(repo->privsep_children[i].ibuf);
447 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
448 child_err = got_privsep_wait_for_child(
449 repo->privsep_children[i].pid);
450 if (child_err && err == NULL)
452 close(repo->privsep_children[i].imsg_fd);
459 const struct got_error *
460 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
461 const char *input_path, int check_disk)
463 const struct got_error *err = NULL;
464 const char *repo_abspath = NULL;
466 size_t repolen, cwdlen, len;
467 char *cwd, *canonpath, *path = NULL;
469 *in_repo_path = NULL;
471 cwd = getcwd(NULL, 0);
473 return got_error_from_errno();
475 canonpath = strdup(input_path);
476 if (canonpath == NULL) {
477 err = got_error_from_errno();
480 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
484 repo_abspath = got_repo_get_path(repo);
486 /* TODO: Call "get in-repository path of work-tree node" API. */
489 path = strdup(canonpath);
490 else if (lstat(canonpath, &sb) != 0) {
491 if (errno != ENOENT) {
492 err = got_error_from_errno();
496 * Path is not on disk.
497 * Assume it is already relative to repository root.
499 path = strdup(canonpath);
501 int is_repo_child = 0, is_cwd_child = 0;
503 path = realpath(canonpath, NULL);
505 err = got_error_from_errno();
509 repolen = strlen(repo_abspath);
510 cwdlen = strlen(cwd);
513 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
515 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
518 if (strcmp(path, repo_abspath) == 0) {
522 err = got_error_from_errno();
525 } else if (is_repo_child && is_cwd_child) {
527 /* TODO: Is path inside a got worktree? */
528 /* Strip common prefix with repository path. */
529 err = got_path_skip_common_ancestor(&child,
535 } else if (is_repo_child) {
536 /* Matched an on-disk path inside repository. */
537 if (got_repo_is_bare(repo)) {
539 * Matched an on-disk path inside repository
540 * database. Treat as repository-relative.
544 /* Strip common prefix with repository path. */
545 err = got_path_skip_common_ancestor(&child,
552 } else if (is_cwd_child) {
554 /* TODO: Is path inside a got worktree? */
555 /* Strip common prefix with cwd. */
556 err = got_path_skip_common_ancestor(&child, cwd,
564 * Matched unrelated on-disk path.
565 * Treat it as repository-relative.
570 /* Make in-repository path absolute */
571 if (path[0] != '/') {
573 if (asprintf(&abspath, "/%s", path) == -1) {
574 err = got_error_from_errno();
587 *in_repo_path = path;
591 const struct got_error *
592 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
594 const struct got_error *err = NULL;
597 for (i = 0; i < nitems(repo->packidx_cache); i++) {
598 if (repo->packidx_cache[i] == NULL)
601 if (i == nitems(repo->packidx_cache)) {
602 err = got_packidx_close(repo->packidx_cache[i - 1]);
608 * Insert the new pack index at the front so it will
609 * be searched first in the future.
611 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
612 sizeof(repo->packidx_cache) -
613 sizeof(repo->packidx_cache[0]));
614 repo->packidx_cache[0] = packidx;
620 is_packidx_filename(const char *name, size_t len)
622 if (len != GOT_PACKIDX_NAMELEN)
625 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
628 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
629 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
635 const struct got_error *
636 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
637 struct got_repository *repo, struct got_object_id *id)
639 const struct got_error *err;
646 /* Search pack index cache. */
647 for (i = 0; i < nitems(repo->packidx_cache); i++) {
648 if (repo->packidx_cache[i] == NULL)
650 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
652 *packidx = repo->packidx_cache[i];
656 /* No luck. Search the filesystem. */
658 path_packdir = got_repo_get_path_objects_pack(repo);
659 if (path_packdir == NULL)
660 return got_error_from_errno();
662 packdir = opendir(path_packdir);
663 if (packdir == NULL) {
664 err = got_error_from_errno();
668 while ((dent = readdir(packdir)) != NULL) {
669 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
672 if (asprintf(&path_packidx, "%s/%s", path_packdir,
673 dent->d_name) == -1) {
674 err = got_error_from_errno();
678 err = got_packidx_open(packidx, path_packidx, 0);
683 *idx = got_packidx_get_object_idx(*packidx, id);
685 err = NULL; /* found the object */
686 err = got_repo_cache_packidx(repo, *packidx);
690 err = got_packidx_close(*packidx);
696 err = got_error_no_obj(id);
699 if (packdir && closedir(packdir) != 0 && err == 0)
700 err = got_error_from_errno();
704 static const struct got_error *
705 read_packfile_hdr(int fd, struct got_packidx *packidx)
707 const struct got_error *err = NULL;
708 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
709 struct got_packfile_hdr hdr;
712 n = read(fd, &hdr, sizeof(hdr));
714 return got_error_from_errno();
715 if (n != sizeof(hdr))
716 return got_error(GOT_ERR_BAD_PACKFILE);
718 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
719 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
720 betoh32(hdr.nobjects) != totobj)
721 err = got_error(GOT_ERR_BAD_PACKFILE);
726 static const struct got_error *
727 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
729 const struct got_error *err = NULL;
731 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
733 return got_error_from_errno();
736 err = read_packfile_hdr(*fd, packidx);
746 const struct got_error *
747 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
748 const char *path_packfile, struct got_packidx *packidx)
750 const struct got_error *err = NULL;
751 struct got_pack *pack = NULL;
757 for (i = 0; i < nitems(repo->packs); i++) {
758 pack = &repo->packs[i];
759 if (pack->path_packfile == NULL)
761 if (strcmp(pack->path_packfile, path_packfile) == 0)
765 if (i == nitems(repo->packs) - 1) {
766 err = got_pack_close(&repo->packs[i - 1]);
769 memmove(&repo->packs[1], &repo->packs[0],
770 sizeof(repo->packs) - sizeof(repo->packs[0]));
774 pack = &repo->packs[i];
776 pack->path_packfile = strdup(path_packfile);
777 if (pack->path_packfile == NULL) {
778 err = got_error_from_errno();
782 err = open_packfile(&pack->fd, path_packfile, packidx);
786 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
790 pack->privsep_child = NULL;
792 #ifndef GOT_PACK_NO_MMAP
793 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
795 if (pack->map == MAP_FAILED)
796 pack->map = NULL; /* fall back to read(2) */
801 free(pack->path_packfile);
802 memset(pack, 0, sizeof(*pack));
810 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
812 struct got_pack *pack = NULL;
815 for (i = 0; i < nitems(repo->packs); i++) {
816 pack = &repo->packs[i];
817 if (pack->path_packfile == NULL)
819 if (strcmp(pack->path_packfile, path_packfile) == 0)