2 * Copyright (c) 2018, 2019, 2020 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>
19 #include <sys/socket.h>
22 #include <sys/resource.h>
42 #include "got_compat.h"
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_object.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_gotconfig.h"
64 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
68 got_repo_get_path(struct got_repository *repo)
74 got_repo_get_path_git_dir(struct got_repository *repo)
76 return repo->path_git_dir;
80 got_repo_get_fd(struct got_repository *repo)
82 return repo->gitdir_fd;
86 got_repo_get_gitconfig_author_name(struct got_repository *repo)
88 return repo->gitconfig_author_name;
92 got_repo_get_gitconfig_author_email(struct got_repository *repo)
94 return repo->gitconfig_author_email;
98 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
100 return repo->global_gitconfig_author_name;
104 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
106 return repo->global_gitconfig_author_email;
110 got_repo_get_gitconfig_owner(struct got_repository *repo)
112 return repo->gitconfig_owner;
116 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
117 struct got_repository *repo)
119 *extensions = repo->extensions;
120 *nextensions = repo->nextensions;
124 got_repo_is_bare(struct got_repository *repo)
126 return (strcmp(repo->path, repo->path_git_dir) == 0);
130 get_path_git_child(struct got_repository *repo, const char *basename)
134 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
142 got_repo_get_path_objects(struct got_repository *repo)
144 return get_path_git_child(repo, GOT_OBJECTS_DIR);
148 got_repo_get_path_objects_pack(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
154 got_repo_get_path_refs(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_REFS_DIR);
160 got_repo_get_path_packed_refs(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
166 get_path_head(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_HEAD_FILE);
172 got_repo_get_path_gitconfig(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_GITCONFIG);
178 got_repo_get_path_gotconfig(struct got_repository *repo)
180 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
183 const struct got_gotconfig *
184 got_repo_get_gotconfig(struct got_repository *repo)
186 return repo->gotconfig;
190 got_repo_get_gitconfig_remotes(int *nremotes,
191 const struct got_remote_repo **remotes, struct got_repository *repo)
193 *nremotes = repo->ngitconfig_remotes;
194 *remotes = repo->gitconfig_remotes;
198 is_git_repo(struct got_repository *repo)
200 const char *path_git = got_repo_get_path_git_dir(repo);
201 char *path_objects = got_repo_get_path_objects(repo);
202 char *path_refs = got_repo_get_path_refs(repo);
203 char *path_head = get_path_head(repo);
206 struct got_reference *head_ref;
208 if (lstat(path_git, &sb) == -1)
210 if (!S_ISDIR(sb.st_mode))
213 if (lstat(path_objects, &sb) == -1)
215 if (!S_ISDIR(sb.st_mode))
218 if (lstat(path_refs, &sb) == -1)
220 if (!S_ISDIR(sb.st_mode))
223 if (lstat(path_head, &sb) == -1)
225 if (!S_ISREG(sb.st_mode))
228 /* Check if the HEAD reference can be opened. */
229 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
231 got_ref_close(head_ref);
242 const struct got_error *
243 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
244 struct got_object *obj)
246 #ifndef GOT_NO_OBJ_CACHE
247 const struct got_error *err = NULL;
248 err = got_object_cache_add(&repo->objcache, id, obj);
250 if (err->code == GOT_ERR_OBJ_EXISTS ||
251 err->code == GOT_ERR_OBJ_TOO_LARGE)
261 got_repo_get_cached_object(struct got_repository *repo,
262 struct got_object_id *id)
264 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
267 const struct got_error *
268 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
269 struct got_tree_object *tree)
271 #ifndef GOT_NO_OBJ_CACHE
272 const struct got_error *err = NULL;
273 err = got_object_cache_add(&repo->treecache, id, tree);
275 if (err->code == GOT_ERR_OBJ_EXISTS ||
276 err->code == GOT_ERR_OBJ_TOO_LARGE)
285 struct got_tree_object *
286 got_repo_get_cached_tree(struct got_repository *repo,
287 struct got_object_id *id)
289 return (struct got_tree_object *)got_object_cache_get(
290 &repo->treecache, id);
293 const struct got_error *
294 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
295 struct got_commit_object *commit)
297 #ifndef GOT_NO_OBJ_CACHE
298 const struct got_error *err = NULL;
299 err = got_object_cache_add(&repo->commitcache, id, commit);
301 if (err->code == GOT_ERR_OBJ_EXISTS ||
302 err->code == GOT_ERR_OBJ_TOO_LARGE)
311 struct got_commit_object *
312 got_repo_get_cached_commit(struct got_repository *repo,
313 struct got_object_id *id)
315 return (struct got_commit_object *)got_object_cache_get(
316 &repo->commitcache, id);
319 const struct got_error *
320 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
321 struct got_tag_object *tag)
323 #ifndef GOT_NO_OBJ_CACHE
324 const struct got_error *err = NULL;
325 err = got_object_cache_add(&repo->tagcache, id, tag);
327 if (err->code == GOT_ERR_OBJ_EXISTS ||
328 err->code == GOT_ERR_OBJ_TOO_LARGE)
337 struct got_tag_object *
338 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
340 return (struct got_tag_object *)got_object_cache_get(
341 &repo->tagcache, id);
344 static const struct got_error *
345 open_repo(struct got_repository *repo, const char *path)
347 const struct got_error *err = NULL;
349 repo->gitdir_fd = -1;
351 /* bare git repository? */
352 repo->path_git_dir = strdup(path);
353 if (repo->path_git_dir == NULL)
354 return got_error_from_errno("strdup");
355 if (is_git_repo(repo)) {
356 repo->path = strdup(repo->path_git_dir);
357 if (repo->path == NULL) {
358 err = got_error_from_errno("strdup");
361 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
362 if (repo->gitdir_fd == -1) {
363 err = got_error_from_errno2("open",
370 /* git repository with working tree? */
371 free(repo->path_git_dir);
372 repo->path_git_dir = NULL;
373 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
374 err = got_error_from_errno("asprintf");
377 if (is_git_repo(repo)) {
378 repo->path = strdup(path);
379 if (repo->path == NULL) {
380 err = got_error_from_errno("strdup");
383 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
384 if (repo->gitdir_fd == -1) {
385 err = got_error_from_errno2("open",
392 err = got_error(GOT_ERR_NOT_GIT_REPO);
397 free(repo->path_git_dir);
398 repo->path_git_dir = NULL;
399 if (repo->gitdir_fd != -1)
400 close(repo->gitdir_fd);
401 repo->gitdir_fd = -1;
407 static const struct got_error *
408 parse_gitconfig_file(int *gitconfig_repository_format_version,
409 char **gitconfig_author_name, char **gitconfig_author_email,
410 struct got_remote_repo **remotes, int *nremotes,
411 char **gitconfig_owner, char ***extensions, int *nextensions,
412 const char *gitconfig_path)
414 const struct got_error *err = NULL, *child_err = NULL;
416 int imsg_fds[2] = { -1, -1 };
418 struct imsgbuf *ibuf;
420 *gitconfig_repository_format_version = 0;
425 *gitconfig_author_name = NULL;
426 *gitconfig_author_email = NULL;
432 *gitconfig_owner = NULL;
434 fd = open(gitconfig_path, O_RDONLY);
438 return got_error_from_errno2("open", gitconfig_path);
441 ibuf = calloc(1, sizeof(*ibuf));
443 err = got_error_from_errno("calloc");
447 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
448 err = got_error_from_errno("socketpair");
454 err = got_error_from_errno("fork");
456 } else if (pid == 0) {
457 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
462 if (close(imsg_fds[1]) == -1) {
463 err = got_error_from_errno("close");
467 imsg_init(ibuf, imsg_fds[0]);
469 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
474 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
478 err = got_privsep_recv_gitconfig_int(
479 gitconfig_repository_format_version, ibuf);
483 if (extensions && nextensions) {
484 err = got_privsep_send_gitconfig_repository_extensions_req(
488 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
491 if (*nextensions > 0) {
493 *extensions = calloc(*nextensions, sizeof(char *));
494 if (*extensions == NULL) {
495 err = got_error_from_errno("calloc");
498 for (i = 0; i < *nextensions; i++) {
500 err = got_privsep_recv_gitconfig_str(&ext,
504 (*extensions)[i] = ext;
509 err = got_privsep_send_gitconfig_author_name_req(ibuf);
513 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
517 err = got_privsep_send_gitconfig_author_email_req(ibuf);
521 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
525 if (remotes && nremotes) {
526 err = got_privsep_send_gitconfig_remotes_req(ibuf);
530 err = got_privsep_recv_gitconfig_remotes(remotes,
536 if (gitconfig_owner) {
537 err = got_privsep_send_gitconfig_owner_req(ibuf);
540 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
546 err = got_privsep_send_stop(imsg_fds[0]);
547 child_err = got_privsep_wait_for_child(pid);
548 if (child_err && err == NULL)
551 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
552 err = got_error_from_errno("close");
553 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
554 err = got_error_from_errno("close");
555 if (fd != -1 && close(fd) == -1 && err == NULL)
556 err = got_error_from_errno2("close", gitconfig_path);
561 static const struct got_error *
562 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
564 const struct got_error *err = NULL;
565 char *repo_gitconfig_path = NULL;
567 if (global_gitconfig_path) {
568 /* Read settings from ~/.gitconfig. */
569 int dummy_repo_version;
570 err = parse_gitconfig_file(&dummy_repo_version,
571 &repo->global_gitconfig_author_name,
572 &repo->global_gitconfig_author_email,
573 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
578 /* Read repository's .git/config file. */
579 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
580 if (repo_gitconfig_path == NULL)
581 return got_error_from_errno("got_repo_get_path_gitconfig");
583 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
584 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
585 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
586 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
587 repo_gitconfig_path);
591 free(repo_gitconfig_path);
595 static const struct got_error *
596 read_gotconfig(struct got_repository *repo)
598 const struct got_error *err = NULL;
599 char *gotconfig_path;
601 gotconfig_path = got_repo_get_path_gotconfig(repo);
602 if (gotconfig_path == NULL)
603 return got_error_from_errno("got_repo_get_path_gotconfig");
605 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
606 free(gotconfig_path);
610 /* Supported repository format extensions. */
611 static const char *repo_extensions[] = {
612 "noop", /* Got supports repository format version 1. */
613 "preciousObjects", /* Supported by gotadmin cleanup. */
614 "worktreeConfig", /* Got does not care about Git work trees. */
617 const struct got_error *
618 got_repo_open(struct got_repository **repop, const char *path,
619 const char *global_gitconfig_path)
621 struct got_repository *repo = NULL;
622 const struct got_error *err = NULL;
623 char *repo_path = NULL;
629 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
630 return got_error_from_errno("getrlimit");
632 repo = calloc(1, sizeof(*repo));
634 err = got_error_from_errno("calloc");
638 for (i = 0; i < nitems(repo->privsep_children); i++) {
639 memset(&repo->privsep_children[i], 0,
640 sizeof(repo->privsep_children[0]));
641 repo->privsep_children[i].imsg_fd = -1;
644 err = got_object_cache_init(&repo->objcache,
645 GOT_OBJECT_CACHE_TYPE_OBJ);
648 err = got_object_cache_init(&repo->treecache,
649 GOT_OBJECT_CACHE_TYPE_TREE);
652 err = got_object_cache_init(&repo->commitcache,
653 GOT_OBJECT_CACHE_TYPE_COMMIT);
656 err = got_object_cache_init(&repo->tagcache,
657 GOT_OBJECT_CACHE_TYPE_TAG);
661 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
662 if (repo->pack_cache_size > rl.rlim_cur / 8)
663 repo->pack_cache_size = rl.rlim_cur / 8;
665 repo_path = realpath(path, NULL);
666 if (repo_path == NULL) {
667 err = got_error_from_errno2("realpath", path);
674 err = open_repo(repo, repo_path);
677 if (err->code != GOT_ERR_NOT_GIT_REPO)
679 if (repo_path[0] == '/' && repo_path[1] == '\0') {
680 err = got_error(GOT_ERR_NOT_GIT_REPO);
683 err = got_path_dirname(&parent_path, repo_path);
687 repo_path = parent_path;
690 err = read_gotconfig(repo);
694 err = read_gitconfig(repo, global_gitconfig_path);
697 if (repo->gitconfig_repository_format_version != 0)
698 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
699 for (i = 0; i < repo->nextensions; i++) {
700 char *ext = repo->extensions[i];
701 int j, supported = 0;
702 for (j = 0; j < nitems(repo_extensions); j++) {
703 if (strcmp(ext, repo_extensions[j]) == 0) {
709 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
715 got_repo_close(repo);
722 const struct got_error *
723 got_repo_close(struct got_repository *repo)
725 const struct got_error *err = NULL, *child_err;
728 for (i = 0; i < repo->pack_cache_size; i++) {
729 if (repo->packidx_cache[i] == NULL)
731 got_packidx_close(repo->packidx_cache[i]);
734 for (i = 0; i < repo->pack_cache_size; i++) {
735 if (repo->packs[i].path_packfile == NULL)
737 got_pack_close(&repo->packs[i]);
741 free(repo->path_git_dir);
743 got_object_cache_close(&repo->objcache);
744 got_object_cache_close(&repo->treecache);
745 got_object_cache_close(&repo->commitcache);
746 got_object_cache_close(&repo->tagcache);
748 for (i = 0; i < nitems(repo->privsep_children); i++) {
749 if (repo->privsep_children[i].imsg_fd == -1)
751 imsg_clear(repo->privsep_children[i].ibuf);
752 free(repo->privsep_children[i].ibuf);
753 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
754 child_err = got_privsep_wait_for_child(
755 repo->privsep_children[i].pid);
756 if (child_err && err == NULL)
758 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
760 err = got_error_from_errno("close");
763 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
765 err = got_error_from_errno("close");
768 got_gotconfig_free(repo->gotconfig);
769 free(repo->gitconfig_author_name);
770 free(repo->gitconfig_author_email);
771 for (i = 0; i < repo->ngitconfig_remotes; i++)
772 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
773 free(repo->gitconfig_remotes);
774 for (i = 0; i < repo->nextensions; i++)
775 free(repo->extensions[i]);
776 free(repo->extensions);
783 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
789 free(repo->fetch_url);
790 repo->fetch_url = NULL;
791 free(repo->send_url);
792 repo->send_url = NULL;
793 for (i = 0; i < repo->nfetch_branches; i++)
794 free(repo->fetch_branches[i]);
795 free(repo->fetch_branches);
796 repo->fetch_branches = NULL;
797 repo->nfetch_branches = 0;
798 for (i = 0; i < repo->nsend_branches; i++)
799 free(repo->send_branches[i]);
800 free(repo->send_branches);
801 repo->send_branches = NULL;
802 repo->nsend_branches = 0;
805 const struct got_error *
806 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
807 const char *input_path)
809 const struct got_error *err = NULL;
810 const char *repo_abspath = NULL;
812 char *canonpath, *path = NULL;
814 *in_repo_path = NULL;
816 canonpath = strdup(input_path);
817 if (canonpath == NULL) {
818 err = got_error_from_errno("strdup");
821 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
825 repo_abspath = got_repo_get_path(repo);
827 if (canonpath[0] == '\0') {
828 path = strdup(canonpath);
830 err = got_error_from_errno("strdup");
834 path = realpath(canonpath, NULL);
836 if (errno != ENOENT) {
837 err = got_error_from_errno2("realpath",
842 * Path is not on disk.
843 * Assume it is already relative to repository root.
845 path = strdup(canonpath);
847 err = got_error_from_errno("strdup");
852 repolen = strlen(repo_abspath);
856 if (strcmp(path, repo_abspath) == 0) {
860 err = got_error_from_errno("strdup");
863 } else if (len > repolen &&
864 got_path_is_child(path, repo_abspath, repolen)) {
865 /* Matched an on-disk path inside repository. */
866 if (got_repo_is_bare(repo)) {
868 * Matched an on-disk path inside repository
869 * database. Treat input as repository-relative.
876 /* Strip common prefix with repository path. */
877 err = got_path_skip_common_ancestor(&child,
886 * Matched unrelated on-disk path.
887 * Treat input as repository-relative.
895 /* Make in-repository path absolute */
896 if (path[0] != '/') {
898 if (asprintf(&abspath, "/%s", path) == -1) {
899 err = got_error_from_errno("asprintf");
911 *in_repo_path = path;
915 static const struct got_error *
916 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
917 const char *path_packidx)
919 const struct got_error *err = NULL;
922 for (i = 0; i < repo->pack_cache_size; i++) {
923 if (repo->packidx_cache[i] == NULL)
925 if (strcmp(repo->packidx_cache[i]->path_packidx,
926 path_packidx) == 0) {
927 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
930 if (i == repo->pack_cache_size) {
931 err = got_packidx_close(repo->packidx_cache[i - 1]);
937 * Insert the new pack index at the front so it will
938 * be searched first in the future.
940 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
941 sizeof(repo->packidx_cache) -
942 sizeof(repo->packidx_cache[0]));
943 repo->packidx_cache[0] = packidx;
949 got_repo_is_packidx_filename(const char *name, size_t len)
951 if (len != GOT_PACKIDX_NAMELEN)
954 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
957 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
958 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
964 const struct got_error *
965 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
966 struct got_repository *repo, struct got_object_id *id)
968 const struct got_error *err;
975 /* Search pack index cache. */
976 for (i = 0; i < repo->pack_cache_size; i++) {
977 if (repo->packidx_cache[i] == NULL)
979 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
981 *packidx = repo->packidx_cache[i];
983 * Move this cache entry to the front. Repeatedly
984 * searching a wrong pack index can be expensive.
987 struct got_packidx *p;
988 p = repo->packidx_cache[0];
989 repo->packidx_cache[0] = *packidx;
990 repo->packidx_cache[i] = p;
995 /* No luck. Search the filesystem. */
997 packdir_fd = openat(got_repo_get_fd(repo),
998 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
999 if (packdir_fd == -1) {
1000 if (errno == ENOENT)
1001 err = got_error_no_obj(id);
1003 err = got_error_from_errno_fmt("openat: %s/%s",
1004 got_repo_get_path_git_dir(repo),
1005 GOT_OBJECTS_PACK_DIR);
1009 packdir = fdopendir(packdir_fd);
1010 if (packdir == NULL) {
1011 err = got_error_from_errno("fdopendir");
1015 while ((dent = readdir(packdir)) != NULL) {
1018 if (!got_repo_is_packidx_filename(dent->d_name,
1019 strlen(dent->d_name)))
1022 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1023 dent->d_name) == -1) {
1024 err = got_error_from_errno("asprintf");
1028 for (i = 0; i < repo->pack_cache_size; i++) {
1029 if (repo->packidx_cache[i] == NULL)
1031 if (strcmp(repo->packidx_cache[i]->path_packidx,
1032 path_packidx) == 0) {
1039 continue; /* already searched */
1042 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1049 err = cache_packidx(repo, *packidx, path_packidx);
1054 *idx = got_packidx_get_object_idx(*packidx, id);
1056 err = NULL; /* found the object */
1061 err = got_error_no_obj(id);
1063 if (packdir && closedir(packdir) != 0 && err == NULL)
1064 err = got_error_from_errno("closedir");
1068 static const struct got_error *
1069 read_packfile_hdr(int fd, struct got_packidx *packidx)
1071 const struct got_error *err = NULL;
1072 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1073 struct got_packfile_hdr hdr;
1076 n = read(fd, &hdr, sizeof(hdr));
1078 return got_error_from_errno("read");
1079 if (n != sizeof(hdr))
1080 return got_error(GOT_ERR_BAD_PACKFILE);
1082 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1083 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1084 be32toh(hdr.nobjects) != totobj)
1085 err = got_error(GOT_ERR_BAD_PACKFILE);
1090 static const struct got_error *
1091 open_packfile(int *fd, struct got_repository *repo,
1092 const char *relpath, struct got_packidx *packidx)
1094 const struct got_error *err = NULL;
1096 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1098 return got_error_from_errno_fmt("openat: %s/%s",
1099 got_repo_get_path_git_dir(repo), relpath);
1102 err = read_packfile_hdr(*fd, packidx);
1112 const struct got_error *
1113 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1114 const char *path_packfile, struct got_packidx *packidx)
1116 const struct got_error *err = NULL;
1117 struct got_pack *pack = NULL;
1124 for (i = 0; i < repo->pack_cache_size; i++) {
1125 pack = &repo->packs[i];
1126 if (pack->path_packfile == NULL)
1128 if (strcmp(pack->path_packfile, path_packfile) == 0)
1129 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1132 if (i == repo->pack_cache_size) {
1133 err = got_pack_close(&repo->packs[i - 1]);
1136 memmove(&repo->packs[1], &repo->packs[0],
1137 sizeof(repo->packs) - sizeof(repo->packs[0]));
1141 pack = &repo->packs[i];
1143 pack->path_packfile = strdup(path_packfile);
1144 if (pack->path_packfile == NULL) {
1145 err = got_error_from_errno("strdup");
1149 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1153 if (fstat(pack->fd, &sb) != 0) {
1154 err = got_error_from_errno("fstat");
1157 pack->filesize = sb.st_size;
1159 pack->privsep_child = NULL;
1161 #ifndef GOT_PACK_NO_MMAP
1162 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1164 if (pack->map == MAP_FAILED) {
1165 if (errno != ENOMEM) {
1166 err = got_error_from_errno("mmap");
1169 pack->map = NULL; /* fall back to read(2) */
1175 free(pack->path_packfile);
1176 memset(pack, 0, sizeof(*pack));
1184 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1186 struct got_pack *pack = NULL;
1189 for (i = 0; i < repo->pack_cache_size; i++) {
1190 pack = &repo->packs[i];
1191 if (pack->path_packfile == NULL)
1193 if (strcmp(pack->path_packfile, path_packfile) == 0)
1200 const struct got_error *
1201 got_repo_init(const char *repo_path)
1203 const struct got_error *err = NULL;
1204 const char *dirnames[] = {
1206 GOT_OBJECTS_PACK_DIR,
1209 const char *description_str = "Unnamed repository; "
1210 "edit this file 'description' to name the repository.";
1211 const char *headref_str = "ref: refs/heads/main";
1212 const char *gitconfig_str = "[core]\n"
1213 "\trepositoryformatversion = 0\n"
1214 "\tfilemode = true\n"
1219 if (!got_path_dir_is_empty(repo_path))
1220 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1222 for (i = 0; i < nitems(dirnames); i++) {
1223 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1224 return got_error_from_errno("asprintf");
1226 err = got_path_mkdir(path);
1232 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1233 return got_error_from_errno("asprintf");
1234 err = got_path_create_file(path, description_str);
1239 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1240 return got_error_from_errno("asprintf");
1241 err = got_path_create_file(path, headref_str);
1246 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1247 return got_error_from_errno("asprintf");
1248 err = got_path_create_file(path, gitconfig_str);
1256 static const struct got_error *
1257 match_packed_object(struct got_object_id **unique_id,
1258 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1260 const struct got_error *err = NULL;
1261 DIR *packdir = NULL;
1262 struct dirent *dent;
1264 struct got_object_id_queue matched_ids;
1267 STAILQ_INIT(&matched_ids);
1269 packdir_fd = openat(got_repo_get_fd(repo),
1270 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1271 if (packdir_fd == -1) {
1272 if (errno != ENOENT)
1273 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1277 packdir = fdopendir(packdir_fd);
1278 if (packdir == NULL) {
1279 err = got_error_from_errno("fdopendir");
1283 while ((dent = readdir(packdir)) != NULL) {
1284 struct got_packidx *packidx;
1285 struct got_object_qid *qid;
1287 if (!got_repo_is_packidx_filename(dent->d_name,
1288 strlen(dent->d_name)))
1291 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1292 dent->d_name) == -1) {
1293 err = got_error_from_errno("strdup");
1297 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1303 err = got_packidx_match_id_str_prefix(&matched_ids,
1304 packidx, id_str_prefix);
1306 got_packidx_close(packidx);
1309 err = got_packidx_close(packidx);
1313 STAILQ_FOREACH(qid, &matched_ids, entry) {
1314 if (obj_type != GOT_OBJ_TYPE_ANY) {
1316 err = got_object_get_type(&matched_type, repo,
1320 if (matched_type != obj_type)
1323 if (*unique_id == NULL) {
1324 *unique_id = got_object_id_dup(qid->id);
1325 if (*unique_id == NULL) {
1326 err = got_error_from_errno("malloc");
1330 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1331 continue; /* packed multiple times */
1332 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1338 got_object_id_queue_free(&matched_ids);
1339 if (packdir && closedir(packdir) != 0 && err == NULL)
1340 err = got_error_from_errno("closedir");
1348 static const struct got_error *
1349 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1350 const char *object_dir, const char *id_str_prefix, int obj_type,
1351 struct got_repository *repo)
1353 const struct got_error *err = NULL;
1356 struct dirent *dent;
1357 struct got_object_id id;
1359 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1360 err = got_error_from_errno("asprintf");
1364 dir = opendir(path);
1366 if (errno == ENOENT) {
1370 err = got_error_from_errno2("opendir", path);
1373 while ((dent = readdir(dir)) != NULL) {
1377 if (strcmp(dent->d_name, ".") == 0 ||
1378 strcmp(dent->d_name, "..") == 0)
1381 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1382 err = got_error_from_errno("asprintf");
1386 if (!got_parse_sha1_digest(id.sha1, id_str))
1390 * Directory entries do not necessarily appear in
1391 * sorted order, so we must iterate over all of them.
1393 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1399 if (*unique_id == NULL) {
1400 if (obj_type != GOT_OBJ_TYPE_ANY) {
1402 err = got_object_get_type(&matched_type, repo,
1406 if (matched_type != obj_type)
1409 *unique_id = got_object_id_dup(&id);
1410 if (*unique_id == NULL) {
1411 err = got_error_from_errno("got_object_id_dup");
1416 if (got_object_id_cmp(*unique_id, &id) == 0)
1417 continue; /* both packed and loose */
1418 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1424 if (dir && closedir(dir) != 0 && err == NULL)
1425 err = got_error_from_errno("closedir");
1434 const struct got_error *
1435 got_repo_match_object_id_prefix(struct got_object_id **id,
1436 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1438 const struct got_error *err = NULL;
1439 char *path_objects = got_repo_get_path_objects(repo);
1440 char *object_dir = NULL;
1446 for (i = 0; i < strlen(id_str_prefix); i++) {
1447 if (isxdigit((unsigned char)id_str_prefix[i]))
1449 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1452 len = strlen(id_str_prefix);
1454 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1457 object_dir = strndup(id_str_prefix, 2);
1458 if (object_dir == NULL) {
1459 err = got_error_from_errno("strdup");
1462 err = match_loose_object(id, path_objects, object_dir,
1463 id_str_prefix, obj_type, repo);
1464 } else if (len == 1) {
1466 for (i = 0; i < 0xf; i++) {
1467 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1469 err = got_error_from_errno("asprintf");
1472 err = match_packed_object(id, repo, object_dir,
1476 err = match_loose_object(id, path_objects, object_dir,
1477 id_str_prefix, obj_type, repo);
1482 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1490 } else if (*id == NULL)
1491 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1496 const struct got_error *
1497 got_repo_match_object_id(struct got_object_id **id, char **label,
1498 const char *id_str, int obj_type, struct got_reflist_head *refs,
1499 struct got_repository *repo)
1501 const struct got_error *err;
1502 struct got_tag_object *tag;
1503 struct got_reference *ref = NULL;
1510 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1513 *id = got_object_id_dup(
1514 got_object_tag_get_object_id(tag));
1516 err = got_error_from_errno("got_object_id_dup");
1517 else if (label && asprintf(label, "refs/tags/%s",
1518 got_object_tag_get_name(tag)) == -1) {
1519 err = got_error_from_errno("asprintf");
1523 got_object_tag_close(tag);
1525 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1526 err->code != GOT_ERR_NO_OBJ)
1530 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1532 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1534 err = got_ref_open(&ref, repo, id_str, 0);
1538 *label = strdup(got_ref_get_name(ref));
1539 if (*label == NULL) {
1540 err = got_error_from_errno("strdup");
1544 err = got_ref_resolve(id, repo, ref);
1546 err = got_object_id_str(label, *id);
1547 if (*label == NULL) {
1548 err = got_error_from_errno("strdup");
1558 const struct got_error *
1559 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1560 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1562 const struct got_error *err = NULL;
1563 struct got_reflist_entry *re;
1564 struct got_object_id *tag_id;
1565 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1569 TAILQ_FOREACH(re, refs, entry) {
1570 const char *refname;
1571 refname = got_ref_get_name(re->ref);
1572 if (got_ref_is_symbolic(re->ref))
1574 if (strncmp(refname, "refs/tags/", 10) != 0)
1576 if (!name_is_absolute)
1577 refname += strlen("refs/tags/");
1578 if (strcmp(refname, name) != 0)
1580 err = got_ref_resolve(&tag_id, repo, re->ref);
1583 err = got_object_open_as_tag(tag, repo, tag_id);
1587 if (obj_type == GOT_OBJ_TYPE_ANY ||
1588 got_object_tag_get_object_type(*tag) == obj_type)
1590 got_object_tag_close(*tag);
1594 if (err == NULL && *tag == NULL)
1595 err = got_error_path(name, GOT_ERR_NO_OBJ);
1599 static const struct got_error *
1600 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1601 const char *name, mode_t mode, struct got_object_id *blob_id)
1603 const struct got_error *err = NULL;
1607 *new_te = calloc(1, sizeof(**new_te));
1608 if (*new_te == NULL)
1609 return got_error_from_errno("calloc");
1611 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1612 sizeof((*new_te)->name)) {
1613 err = got_error(GOT_ERR_NO_SPACE);
1617 if (S_ISLNK(mode)) {
1618 (*new_te)->mode = S_IFLNK;
1620 (*new_te)->mode = S_IFREG;
1621 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1623 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1625 if (err && *new_te) {
1632 static const struct got_error *
1633 import_file(struct got_tree_entry **new_te, struct dirent *de,
1634 const char *path, struct got_repository *repo)
1636 const struct got_error *err;
1637 struct got_object_id *blob_id = NULL;
1641 if (asprintf(&filepath, "%s%s%s", path,
1642 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1643 return got_error_from_errno("asprintf");
1645 if (lstat(filepath, &sb) != 0) {
1646 err = got_error_from_errno2("lstat", path);
1650 err = got_object_blob_create(&blob_id, filepath, repo);
1654 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1663 static const struct got_error *
1664 insert_tree_entry(struct got_tree_entry *new_te,
1665 struct got_pathlist_head *paths)
1667 const struct got_error *err = NULL;
1668 struct got_pathlist_entry *new_pe;
1670 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1674 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1678 static const struct got_error *write_tree(struct got_object_id **,
1679 const char *, struct got_pathlist_head *, struct got_repository *,
1680 got_repo_import_cb progress_cb, void *progress_arg);
1682 static const struct got_error *
1683 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1684 const char *path, struct got_pathlist_head *ignores,
1685 struct got_repository *repo,
1686 got_repo_import_cb progress_cb, void *progress_arg)
1688 const struct got_error *err;
1689 struct got_object_id *id = NULL;
1692 if (asprintf(&subdirpath, "%s%s%s", path,
1693 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1694 return got_error_from_errno("asprintf");
1696 (*new_te) = calloc(1, sizeof(**new_te));
1697 if (*new_te == NULL)
1698 return got_error_from_errno("calloc");
1699 (*new_te)->mode = S_IFDIR;
1700 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1701 sizeof((*new_te)->name)) {
1702 err = got_error(GOT_ERR_NO_SPACE);
1705 err = write_tree(&id, subdirpath, ignores, repo,
1706 progress_cb, progress_arg);
1709 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1721 static const struct got_error *
1722 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1723 struct got_pathlist_head *ignores, struct got_repository *repo,
1724 got_repo_import_cb progress_cb, void *progress_arg)
1726 const struct got_error *err = NULL;
1730 struct got_tree_entry *new_te = NULL;
1731 struct got_pathlist_head paths;
1732 struct got_pathlist_entry *pe;
1734 *new_tree_id = NULL;
1738 dir = opendir(path_dir);
1740 err = got_error_from_errno2("opendir", path_dir);
1745 while ((de = readdir(dir)) != NULL) {
1749 if (strcmp(de->d_name, ".") == 0 ||
1750 strcmp(de->d_name, "..") == 0)
1753 TAILQ_FOREACH(pe, ignores, entry) {
1754 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1762 err = got_path_dirent_type(&type, path_dir, de);
1766 if (type == DT_DIR) {
1767 err = import_subdir(&new_te, de, path_dir,
1768 ignores, repo, progress_cb, progress_arg);
1770 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1775 } else if (type == DT_REG || type == DT_LNK) {
1776 err = import_file(&new_te, de, path_dir, repo);
1782 err = insert_tree_entry(new_te, &paths);
1788 if (TAILQ_EMPTY(&paths)) {
1789 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1790 "cannot create tree without any entries");
1794 TAILQ_FOREACH(pe, &paths, entry) {
1795 struct got_tree_entry *te = pe->data;
1797 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1799 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1800 err = got_error_from_errno("asprintf");
1803 err = (*progress_cb)(progress_arg, path);
1809 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1813 got_pathlist_free(&paths);
1817 const struct got_error *
1818 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1819 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1820 struct got_repository *repo, got_repo_import_cb progress_cb,
1823 const struct got_error *err;
1824 struct got_object_id *new_tree_id;
1826 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1827 progress_cb, progress_arg);
1831 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1832 author, time(NULL), author, time(NULL), logmsg, repo);
1837 const struct got_error *
1838 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1839 struct got_repository *repo)
1841 const struct got_error *err = NULL;
1842 char *path_objects = NULL, *path = NULL;
1844 struct got_object_id id;
1850 path_objects = got_repo_get_path_objects(repo);
1851 if (path_objects == NULL)
1852 return got_error_from_errno("got_repo_get_path_objects");
1854 for (i = 0; i <= 0xff; i++) {
1855 struct dirent *dent;
1857 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1858 err = got_error_from_errno("asprintf");
1862 dir = opendir(path);
1864 if (errno == ENOENT) {
1868 err = got_error_from_errno2("opendir", path);
1872 while ((dent = readdir(dir)) != NULL) {
1877 if (strcmp(dent->d_name, ".") == 0 ||
1878 strcmp(dent->d_name, "..") == 0)
1881 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1882 err = got_error_from_errno("asprintf");
1886 if (!got_parse_sha1_digest(id.sha1, id_str)) {
1892 err = got_object_open_loose_fd(&fd, &id, repo);
1896 if (fstat(fd, &sb) == -1) {
1897 err = got_error_from_errno("fstat");
1902 (*ondisk_size) += sb.st_size;
1904 if (close(fd) == -1) {
1905 err = got_error_from_errno("close");
1910 if (closedir(dir) != 0) {
1911 err = got_error_from_errno("closedir");
1920 if (dir && closedir(dir) != 0 && err == NULL)
1921 err = got_error_from_errno("closedir");
1932 const struct got_error *
1933 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
1934 off_t *total_packsize, struct got_repository *repo)
1936 const struct got_error *err = NULL;
1937 DIR *packdir = NULL;
1938 struct dirent *dent;
1939 struct got_packidx *packidx = NULL;
1941 char *path_packfile;
1947 *total_packsize = 0;
1949 packdir_fd = openat(got_repo_get_fd(repo),
1950 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1951 if (packdir_fd == -1) {
1952 return got_error_from_errno_fmt("openat: %s/%s",
1953 got_repo_get_path_git_dir(repo),
1954 GOT_OBJECTS_PACK_DIR);
1957 packdir = fdopendir(packdir_fd);
1958 if (packdir == NULL) {
1959 err = got_error_from_errno("fdopendir");
1963 while ((dent = readdir(packdir)) != NULL) {
1964 if (!got_repo_is_packidx_filename(dent->d_name,
1965 strlen(dent->d_name)))
1968 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1969 dent->d_name) == -1) {
1970 err = got_error_from_errno("asprintf");
1974 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1980 if (fstat(packidx->fd, &sb) == -1)
1982 *total_packsize += sb.st_size;
1984 err = got_packidx_get_packfile_path(&path_packfile,
1985 packidx->path_packidx);
1989 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
1991 free(path_packfile);
1994 free(path_packfile);
1995 *total_packsize += sb.st_size;
1997 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2001 got_packidx_close(packidx);
2006 got_packidx_close(packidx);
2007 if (packdir && closedir(packdir) != 0 && err == NULL)
2008 err = got_error_from_errno("closedir");
2012 *total_packsize = 0;