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>
38 #include "got_error.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
41 #include "got_worktree.h"
42 #include "got_object.h"
44 #include "got_lib_path.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_worktree.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_repository.h"
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
58 #define GOT_GIT_DIR ".git"
60 /* Mandatory files and directories inside the git directory. */
61 #define GOT_OBJECTS_DIR "objects"
62 #define GOT_REFS_DIR "refs"
63 #define GOT_HEAD_FILE "HEAD"
65 /* Other files and directories inside the git directory. */
66 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
67 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
68 #define GOT_OBJECTS_PACK_DIR "objects/pack"
69 #define GOT_PACKED_REFS_FILE "packed-refs"
72 got_repo_get_path(struct got_repository *repo)
78 got_repo_get_path_git_dir(struct got_repository *repo)
80 return repo->path_git_dir;
84 got_repo_is_bare(struct got_repository *repo)
86 return (strcmp(repo->path, repo->path_git_dir) == 0);
90 get_path_git_child(struct got_repository *repo, const char *basename)
94 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
102 got_repo_get_path_objects(struct got_repository *repo)
104 return get_path_git_child(repo, GOT_OBJECTS_DIR);
108 got_repo_get_path_objects_pack(struct got_repository *repo)
110 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
114 got_repo_get_path_refs(struct got_repository *repo)
116 return get_path_git_child(repo, GOT_REFS_DIR);
120 got_repo_get_path_packed_refs(struct got_repository *repo)
122 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
126 get_path_head(struct got_repository *repo)
128 return get_path_git_child(repo, GOT_HEAD_FILE);
132 is_git_repo(struct got_repository *repo)
134 const char *path_git = got_repo_get_path_git_dir(repo);
135 char *path_objects = got_repo_get_path_objects(repo);
136 char *path_refs = got_repo_get_path_refs(repo);
137 char *path_head = get_path_head(repo);
140 struct got_reference *head_ref;
142 if (lstat(path_git, &sb) == -1)
144 if (!S_ISDIR(sb.st_mode))
147 if (lstat(path_objects, &sb) == -1)
149 if (!S_ISDIR(sb.st_mode))
152 if (lstat(path_refs, &sb) == -1)
154 if (!S_ISDIR(sb.st_mode))
157 if (lstat(path_head, &sb) == -1)
159 if (!S_ISREG(sb.st_mode))
162 /* Check if the HEAD reference can be opened. */
163 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
165 got_ref_close(head_ref);
176 const struct got_error *
177 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
178 struct got_object *obj)
180 #ifndef GOT_NO_OBJ_CACHE
181 const struct got_error *err = NULL;
182 err = got_object_cache_add(&repo->objcache, id, obj);
191 got_repo_get_cached_object(struct got_repository *repo,
192 struct got_object_id *id)
194 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
197 const struct got_error *
198 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
199 struct got_tree_object *tree)
201 #ifndef GOT_NO_OBJ_CACHE
202 const struct got_error *err = NULL;
203 err = got_object_cache_add(&repo->treecache, id, tree);
211 struct got_tree_object *
212 got_repo_get_cached_tree(struct got_repository *repo,
213 struct got_object_id *id)
215 return (struct got_tree_object *)got_object_cache_get(
216 &repo->treecache, id);
219 const struct got_error *
220 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
221 struct got_commit_object *commit)
223 #ifndef GOT_NO_OBJ_CACHE
224 const struct got_error *err = NULL;
225 err = got_object_cache_add(&repo->commitcache, id, commit);
233 struct got_commit_object *
234 got_repo_get_cached_commit(struct got_repository *repo,
235 struct got_object_id *id)
237 return (struct got_commit_object *)got_object_cache_get(
238 &repo->commitcache, id);
241 const struct got_error *
242 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
243 struct got_tag_object *tag)
245 #ifndef GOT_NO_OBJ_CACHE
246 const struct got_error *err = NULL;
247 err = got_object_cache_add(&repo->tagcache, id, tag);
255 struct got_tag_object *
256 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
258 return (struct got_tag_object *)got_object_cache_get(
259 &repo->tagcache, id);
262 const struct got_error *
263 open_repo(struct got_repository *repo, const char *path)
265 const struct got_error *err = NULL;
267 /* bare git repository? */
268 repo->path_git_dir = strdup(path);
269 if (repo->path_git_dir == NULL)
270 return got_error_from_errno();
271 if (is_git_repo(repo)) {
272 repo->path = strdup(repo->path_git_dir);
273 if (repo->path == NULL) {
274 err = got_error_from_errno();
280 /* git repository with working tree? */
281 free(repo->path_git_dir);
282 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
283 err = got_error_from_errno();
286 if (is_git_repo(repo)) {
287 repo->path = strdup(path);
288 if (repo->path == NULL) {
289 err = got_error_from_errno();
295 err = got_error(GOT_ERR_NOT_GIT_REPO);
300 free(repo->path_git_dir);
301 repo->path_git_dir = NULL;
306 const struct got_error *
307 got_repo_open(struct got_repository **repop, const char *path)
309 struct got_repository *repo = NULL;
310 const struct got_error *err = NULL;
311 char *abspath, *normpath = NULL;
312 int i, tried_root = 0;
316 if (got_path_is_absolute(path))
317 abspath = strdup(path);
319 abspath = got_path_get_absolute(path);
321 return got_error(GOT_ERR_BAD_PATH);
323 repo = calloc(1, sizeof(*repo));
325 err = got_error_from_errno();
329 for (i = 0; i < nitems(repo->privsep_children); i++) {
330 memset(&repo->privsep_children[i], 0,
331 sizeof(repo->privsep_children[0]));
332 repo->privsep_children[i].imsg_fd = -1;
335 err = got_object_cache_init(&repo->objcache,
336 GOT_OBJECT_CACHE_TYPE_OBJ);
339 err = got_object_cache_init(&repo->treecache,
340 GOT_OBJECT_CACHE_TYPE_TREE);
343 err = got_object_cache_init(&repo->commitcache,
344 GOT_OBJECT_CACHE_TYPE_COMMIT);
347 err = got_object_cache_init(&repo->tagcache,
348 GOT_OBJECT_CACHE_TYPE_TAG);
352 normpath = got_path_normalize(abspath);
353 if (normpath == NULL) {
354 err = got_error(GOT_ERR_BAD_PATH);
360 err = open_repo(repo, path);
363 if (err->code != GOT_ERR_NOT_GIT_REPO)
365 if (path[0] == '/' && path[1] == '\0') {
367 err = got_error(GOT_ERR_NOT_GIT_REPO);
372 path = dirname(path);
374 err = got_error_from_errno();
378 got_repo_close(repo);
386 const struct got_error *
387 got_repo_close(struct got_repository *repo)
389 const struct got_error *err = NULL, *child_err;
392 for (i = 0; i < nitems(repo->packidx_cache); i++) {
393 if (repo->packidx_cache[i] == NULL)
395 got_packidx_close(repo->packidx_cache[i]);
398 for (i = 0; i < nitems(repo->packs); i++) {
399 if (repo->packs[i].path_packfile == NULL)
401 got_pack_close(&repo->packs[i]);
405 free(repo->path_git_dir);
407 got_object_cache_close(&repo->objcache);
408 got_object_cache_close(&repo->treecache);
409 got_object_cache_close(&repo->commitcache);
410 got_object_cache_close(&repo->tagcache);
412 for (i = 0; i < nitems(repo->privsep_children); i++) {
413 if (repo->privsep_children[i].imsg_fd == -1)
415 imsg_clear(repo->privsep_children[i].ibuf);
416 free(repo->privsep_children[i].ibuf);
417 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
418 child_err = got_privsep_wait_for_child(
419 repo->privsep_children[i].pid);
420 if (child_err && err == NULL)
422 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
424 err = got_error_from_errno();
431 const struct got_error *
432 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
433 const char *input_path, int check_disk)
435 const struct got_error *err = NULL;
436 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);
458 path = strdup(canonpath);
460 err = got_error_from_errno();
464 int is_repo_child = 0, is_cwd_child = 0;
466 path = realpath(canonpath, NULL);
468 if (errno != ENOENT) {
469 err = got_error_from_errno();
473 * Path is not on disk.
474 * Assume it is already relative to repository root.
476 path = strdup(canonpath);
478 err = got_error_from_errno();
483 repolen = strlen(repo_abspath);
484 cwdlen = strlen(cwd);
487 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
489 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
492 if (strcmp(path, repo_abspath) == 0) {
496 err = got_error_from_errno();
499 } else if (is_repo_child && is_cwd_child) {
501 /* Strip common prefix with repository path. */
502 err = got_path_skip_common_ancestor(&child,
508 } else if (is_repo_child) {
509 /* Matched an on-disk path inside repository. */
510 if (got_repo_is_bare(repo)) {
512 * Matched an on-disk path inside repository
513 * database. Treat as repository-relative.
517 /* Strip common prefix with repository path. */
518 err = got_path_skip_common_ancestor(&child,
525 } else if (is_cwd_child) {
527 /* Strip common prefix with cwd. */
528 err = got_path_skip_common_ancestor(&child, cwd,
536 * Matched unrelated on-disk path.
537 * Treat it as repository-relative.
542 /* Make in-repository path absolute */
543 if (path[0] != '/') {
545 if (asprintf(&abspath, "/%s", path) == -1) {
546 err = got_error_from_errno();
559 *in_repo_path = path;
563 const struct got_error *
564 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
566 const struct got_error *err = NULL;
569 for (i = 0; i < nitems(repo->packidx_cache); i++) {
570 if (repo->packidx_cache[i] == NULL)
573 if (i == nitems(repo->packidx_cache)) {
574 err = got_packidx_close(repo->packidx_cache[i - 1]);
580 * Insert the new pack index at the front so it will
581 * be searched first in the future.
583 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
584 sizeof(repo->packidx_cache) -
585 sizeof(repo->packidx_cache[0]));
586 repo->packidx_cache[0] = packidx;
592 is_packidx_filename(const char *name, size_t len)
594 if (len != GOT_PACKIDX_NAMELEN)
597 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
600 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
601 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
607 const struct got_error *
608 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
609 struct got_repository *repo, struct got_object_id *id)
611 const struct got_error *err;
618 /* Search pack index cache. */
619 for (i = 0; i < nitems(repo->packidx_cache); i++) {
620 if (repo->packidx_cache[i] == NULL)
622 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
624 *packidx = repo->packidx_cache[i];
628 /* No luck. Search the filesystem. */
630 path_packdir = got_repo_get_path_objects_pack(repo);
631 if (path_packdir == NULL)
632 return got_error_from_errno();
634 packdir = opendir(path_packdir);
635 if (packdir == NULL) {
636 err = got_error_from_errno();
640 while ((dent = readdir(packdir)) != NULL) {
641 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
644 if (asprintf(&path_packidx, "%s/%s", path_packdir,
645 dent->d_name) == -1) {
646 err = got_error_from_errno();
650 err = got_packidx_open(packidx, path_packidx, 0);
655 *idx = got_packidx_get_object_idx(*packidx, id);
657 err = NULL; /* found the object */
658 err = got_repo_cache_packidx(repo, *packidx);
662 err = got_packidx_close(*packidx);
668 err = got_error_no_obj(id);
671 if (packdir && closedir(packdir) != 0 && err == 0)
672 err = got_error_from_errno();
676 static const struct got_error *
677 read_packfile_hdr(int fd, struct got_packidx *packidx)
679 const struct got_error *err = NULL;
680 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
681 struct got_packfile_hdr hdr;
684 n = read(fd, &hdr, sizeof(hdr));
686 return got_error_from_errno();
687 if (n != sizeof(hdr))
688 return got_error(GOT_ERR_BAD_PACKFILE);
690 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
691 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
692 betoh32(hdr.nobjects) != totobj)
693 err = got_error(GOT_ERR_BAD_PACKFILE);
698 static const struct got_error *
699 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
701 const struct got_error *err = NULL;
703 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
705 return got_error_from_errno();
708 err = read_packfile_hdr(*fd, packidx);
718 const struct got_error *
719 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
720 const char *path_packfile, struct got_packidx *packidx)
722 const struct got_error *err = NULL;
723 struct got_pack *pack = NULL;
729 for (i = 0; i < nitems(repo->packs); i++) {
730 pack = &repo->packs[i];
731 if (pack->path_packfile == NULL)
733 if (strcmp(pack->path_packfile, path_packfile) == 0)
737 if (i == nitems(repo->packs) - 1) {
738 err = got_pack_close(&repo->packs[i - 1]);
741 memmove(&repo->packs[1], &repo->packs[0],
742 sizeof(repo->packs) - sizeof(repo->packs[0]));
746 pack = &repo->packs[i];
748 pack->path_packfile = strdup(path_packfile);
749 if (pack->path_packfile == NULL) {
750 err = got_error_from_errno();
754 err = open_packfile(&pack->fd, path_packfile, packidx);
758 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
762 pack->privsep_child = NULL;
764 #ifndef GOT_PACK_NO_MMAP
765 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
767 if (pack->map == MAP_FAILED) {
768 if (errno != ENOMEM) {
769 err = got_error_from_errno();
772 pack->map = NULL; /* fall back to read(2) */
778 free(pack->path_packfile);
779 memset(pack, 0, sizeof(*pack));
787 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
789 struct got_pack *pack = NULL;
792 for (i = 0; i < nitems(repo->packs); i++) {
793 pack = &repo->packs[i];
794 if (pack->path_packfile == NULL)
796 if (strcmp(pack->path_packfile, path_packfile) == 0)