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>
18 #include <sys/queue.h>
21 #include <sys/socket.h>
24 #include <sys/resource.h>
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_gotconfig.h"
68 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
71 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
73 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
74 got_packidx_bloom_filter_cmp);
77 got_repo_get_path(struct got_repository *repo)
83 got_repo_get_path_git_dir(struct got_repository *repo)
85 return repo->path_git_dir;
89 got_repo_get_fd(struct got_repository *repo)
91 return repo->gitdir_fd;
95 got_repo_get_gitconfig_author_name(struct got_repository *repo)
97 return repo->gitconfig_author_name;
101 got_repo_get_gitconfig_author_email(struct got_repository *repo)
103 return repo->gitconfig_author_email;
107 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
109 return repo->global_gitconfig_author_name;
113 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
115 return repo->global_gitconfig_author_email;
119 got_repo_get_gitconfig_owner(struct got_repository *repo)
121 return repo->gitconfig_owner;
125 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
126 struct got_repository *repo)
128 *extensions = repo->extensions;
129 *nextensions = repo->nextensions;
133 got_repo_is_bare(struct got_repository *repo)
135 return (strcmp(repo->path, repo->path_git_dir) == 0);
139 get_path_git_child(struct got_repository *repo, const char *basename)
143 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
151 got_repo_get_path_objects(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_DIR);
157 got_repo_get_path_objects_pack(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
163 got_repo_get_path_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_REFS_DIR);
169 got_repo_get_path_packed_refs(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
175 get_path_head(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_HEAD_FILE);
181 got_repo_get_path_gitconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GITCONFIG);
187 got_repo_get_path_gotconfig(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
192 const struct got_gotconfig *
193 got_repo_get_gotconfig(struct got_repository *repo)
195 return repo->gotconfig;
199 got_repo_get_gitconfig_remotes(int *nremotes,
200 const struct got_remote_repo **remotes, struct got_repository *repo)
202 *nremotes = repo->ngitconfig_remotes;
203 *remotes = repo->gitconfig_remotes;
207 is_git_repo(struct got_repository *repo)
209 const char *path_git = got_repo_get_path_git_dir(repo);
210 char *path_objects = got_repo_get_path_objects(repo);
211 char *path_refs = got_repo_get_path_refs(repo);
212 char *path_head = get_path_head(repo);
215 struct got_reference *head_ref;
217 if (lstat(path_git, &sb) == -1)
219 if (!S_ISDIR(sb.st_mode))
222 if (lstat(path_objects, &sb) == -1)
224 if (!S_ISDIR(sb.st_mode))
227 if (lstat(path_refs, &sb) == -1)
229 if (!S_ISDIR(sb.st_mode))
232 if (lstat(path_head, &sb) == -1)
234 if (!S_ISREG(sb.st_mode))
237 /* Check if the HEAD reference can be opened. */
238 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
240 got_ref_close(head_ref);
251 const struct got_error *
252 got_repo_pack_fds_open(int **pack_fds)
254 const struct got_error *err = NULL;
255 int i, *pack_fds_tmp;
257 pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int));
258 if (pack_fds_tmp == NULL)
259 return got_error_from_errno("calloc");
260 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
261 if (*pack_fds == NULL) {
263 return got_error_from_errno("calloc");
267 * got_repo_pack_fds_close will try to close all of the
268 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
269 * a value from got_opentempfd(), which would result in a close(0) if
270 * we do not initialize to -1 here.
272 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
273 pack_fds_tmp[i] = -1;
275 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
276 pack_fds_tmp[i] = got_opentempfd();
277 if (pack_fds_tmp[i] == -1) {
278 err = got_error_from_errno("got_opentempfd");
279 got_repo_pack_fds_close(pack_fds_tmp);
283 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
287 const struct got_error *
288 got_repo_pack_fds_close(int *pack_fds)
290 const struct got_error *err = NULL;
293 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
294 if (pack_fds[i] == -1)
296 if (close(pack_fds[i]) == -1) {
297 err = got_error_from_errno("close");
305 const struct got_error *
306 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
307 struct got_object *obj)
309 #ifndef GOT_NO_OBJ_CACHE
310 const struct got_error *err = NULL;
311 err = got_object_cache_add(&repo->objcache, id, obj);
313 if (err->code == GOT_ERR_OBJ_EXISTS ||
314 err->code == GOT_ERR_OBJ_TOO_LARGE)
324 got_repo_get_cached_object(struct got_repository *repo,
325 struct got_object_id *id)
327 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
330 const struct got_error *
331 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
332 struct got_tree_object *tree)
334 #ifndef GOT_NO_OBJ_CACHE
335 const struct got_error *err = NULL;
336 err = got_object_cache_add(&repo->treecache, id, tree);
338 if (err->code == GOT_ERR_OBJ_EXISTS ||
339 err->code == GOT_ERR_OBJ_TOO_LARGE)
348 struct got_tree_object *
349 got_repo_get_cached_tree(struct got_repository *repo,
350 struct got_object_id *id)
352 return (struct got_tree_object *)got_object_cache_get(
353 &repo->treecache, id);
356 const struct got_error *
357 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
358 struct got_commit_object *commit)
360 #ifndef GOT_NO_OBJ_CACHE
361 const struct got_error *err = NULL;
362 err = got_object_cache_add(&repo->commitcache, id, commit);
364 if (err->code == GOT_ERR_OBJ_EXISTS ||
365 err->code == GOT_ERR_OBJ_TOO_LARGE)
374 struct got_commit_object *
375 got_repo_get_cached_commit(struct got_repository *repo,
376 struct got_object_id *id)
378 return (struct got_commit_object *)got_object_cache_get(
379 &repo->commitcache, id);
382 const struct got_error *
383 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
384 struct got_tag_object *tag)
386 #ifndef GOT_NO_OBJ_CACHE
387 const struct got_error *err = NULL;
388 err = got_object_cache_add(&repo->tagcache, id, tag);
390 if (err->code == GOT_ERR_OBJ_EXISTS ||
391 err->code == GOT_ERR_OBJ_TOO_LARGE)
400 struct got_tag_object *
401 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
403 return (struct got_tag_object *)got_object_cache_get(
404 &repo->tagcache, id);
407 const struct got_error *
408 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
409 struct got_raw_object *raw)
411 #ifndef GOT_NO_OBJ_CACHE
412 const struct got_error *err = NULL;
413 err = got_object_cache_add(&repo->rawcache, id, raw);
415 if (err->code == GOT_ERR_OBJ_EXISTS ||
416 err->code == GOT_ERR_OBJ_TOO_LARGE)
426 struct got_raw_object *
427 got_repo_get_cached_raw_object(struct got_repository *repo,
428 struct got_object_id *id)
430 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
434 static const struct got_error *
435 open_repo(struct got_repository *repo, const char *path)
437 const struct got_error *err = NULL;
439 repo->gitdir_fd = -1;
441 /* bare git repository? */
442 repo->path_git_dir = strdup(path);
443 if (repo->path_git_dir == NULL)
444 return got_error_from_errno("strdup");
445 if (is_git_repo(repo)) {
446 repo->path = strdup(repo->path_git_dir);
447 if (repo->path == NULL) {
448 err = got_error_from_errno("strdup");
451 repo->gitdir_fd = open(repo->path_git_dir,
452 O_DIRECTORY | O_CLOEXEC);
453 if (repo->gitdir_fd == -1) {
454 err = got_error_from_errno2("open",
461 /* git repository with working tree? */
462 free(repo->path_git_dir);
463 repo->path_git_dir = NULL;
464 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
465 err = got_error_from_errno("asprintf");
468 if (is_git_repo(repo)) {
469 repo->path = strdup(path);
470 if (repo->path == NULL) {
471 err = got_error_from_errno("strdup");
474 repo->gitdir_fd = open(repo->path_git_dir,
475 O_DIRECTORY | O_CLOEXEC);
476 if (repo->gitdir_fd == -1) {
477 err = got_error_from_errno2("open",
484 err = got_error(GOT_ERR_NOT_GIT_REPO);
489 free(repo->path_git_dir);
490 repo->path_git_dir = NULL;
491 if (repo->gitdir_fd != -1)
492 close(repo->gitdir_fd);
493 repo->gitdir_fd = -1;
499 static const struct got_error *
500 parse_gitconfig_file(int *gitconfig_repository_format_version,
501 char **gitconfig_author_name, char **gitconfig_author_email,
502 struct got_remote_repo **remotes, int *nremotes,
503 char **gitconfig_owner, char ***extensions, int *nextensions,
504 const char *gitconfig_path)
506 const struct got_error *err = NULL, *child_err = NULL;
508 int imsg_fds[2] = { -1, -1 };
510 struct imsgbuf *ibuf;
512 *gitconfig_repository_format_version = 0;
517 *gitconfig_author_name = NULL;
518 *gitconfig_author_email = NULL;
524 *gitconfig_owner = NULL;
526 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
530 return got_error_from_errno2("open", gitconfig_path);
533 ibuf = calloc(1, sizeof(*ibuf));
535 err = got_error_from_errno("calloc");
539 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
540 err = got_error_from_errno("socketpair");
546 err = got_error_from_errno("fork");
548 } else if (pid == 0) {
549 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
554 if (close(imsg_fds[1]) == -1) {
555 err = got_error_from_errno("close");
559 imsg_init(ibuf, imsg_fds[0]);
561 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
566 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
570 err = got_privsep_recv_gitconfig_int(
571 gitconfig_repository_format_version, ibuf);
575 if (extensions && nextensions) {
576 err = got_privsep_send_gitconfig_repository_extensions_req(
580 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
583 if (*nextensions > 0) {
585 *extensions = calloc(*nextensions, sizeof(char *));
586 if (*extensions == NULL) {
587 err = got_error_from_errno("calloc");
590 for (i = 0; i < *nextensions; i++) {
592 err = got_privsep_recv_gitconfig_str(&ext,
596 (*extensions)[i] = ext;
601 err = got_privsep_send_gitconfig_author_name_req(ibuf);
605 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
609 err = got_privsep_send_gitconfig_author_email_req(ibuf);
613 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
617 if (remotes && nremotes) {
618 err = got_privsep_send_gitconfig_remotes_req(ibuf);
622 err = got_privsep_recv_gitconfig_remotes(remotes,
628 if (gitconfig_owner) {
629 err = got_privsep_send_gitconfig_owner_req(ibuf);
632 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
637 err = got_privsep_send_stop(imsg_fds[0]);
638 child_err = got_privsep_wait_for_child(pid);
639 if (child_err && err == NULL)
642 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
643 err = got_error_from_errno("close");
644 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
645 err = got_error_from_errno("close");
646 if (fd != -1 && close(fd) == -1 && err == NULL)
647 err = got_error_from_errno2("close", gitconfig_path);
652 static const struct got_error *
653 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
655 const struct got_error *err = NULL;
656 char *repo_gitconfig_path = NULL;
658 if (global_gitconfig_path) {
659 /* Read settings from ~/.gitconfig. */
660 int dummy_repo_version;
661 err = parse_gitconfig_file(&dummy_repo_version,
662 &repo->global_gitconfig_author_name,
663 &repo->global_gitconfig_author_email,
664 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
669 /* Read repository's .git/config file. */
670 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
671 if (repo_gitconfig_path == NULL)
672 return got_error_from_errno("got_repo_get_path_gitconfig");
674 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
675 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
676 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
677 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
678 repo_gitconfig_path);
682 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
685 for (i = 0; i < repo->ngitconfig_remotes; i++) {
686 got_repo_free_remote_repo_data(
687 &repo->gitconfig_remotes[i]);
689 free(repo->gitconfig_remotes);
690 repo->ngitconfig_remotes = 0;
692 free(repo->gitconfig_author_name);
693 repo->gitconfig_author_name = NULL;
694 free(repo->gitconfig_author_email);
695 repo->gitconfig_author_email = NULL;
699 free(repo_gitconfig_path);
703 static const struct got_error *
704 read_gotconfig(struct got_repository *repo)
706 const struct got_error *err = NULL;
707 char *gotconfig_path;
709 gotconfig_path = got_repo_get_path_gotconfig(repo);
710 if (gotconfig_path == NULL)
711 return got_error_from_errno("got_repo_get_path_gotconfig");
713 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
714 free(gotconfig_path);
718 /* Supported repository format extensions. */
719 static const char *const repo_extensions[] = {
720 "noop", /* Got supports repository format version 1. */
721 "preciousObjects", /* Supported by gotadmin cleanup. */
722 "worktreeConfig", /* Got does not care about Git work trees. */
725 const struct got_error *
726 got_repo_open(struct got_repository **repop, const char *path,
727 const char *global_gitconfig_path, int *pack_fds)
729 struct got_repository *repo = NULL;
730 const struct got_error *err = NULL;
731 char *repo_path = NULL;
737 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
738 return got_error_from_errno("getrlimit");
740 repo = calloc(1, sizeof(*repo));
742 return got_error_from_errno("calloc");
744 RB_INIT(&repo->packidx_bloom_filters);
745 TAILQ_INIT(&repo->packidx_paths);
747 for (i = 0; i < nitems(repo->privsep_children); i++) {
748 memset(&repo->privsep_children[i], 0,
749 sizeof(repo->privsep_children[0]));
750 repo->privsep_children[i].imsg_fd = -1;
753 err = got_object_cache_init(&repo->objcache,
754 GOT_OBJECT_CACHE_TYPE_OBJ);
757 err = got_object_cache_init(&repo->treecache,
758 GOT_OBJECT_CACHE_TYPE_TREE);
761 err = got_object_cache_init(&repo->commitcache,
762 GOT_OBJECT_CACHE_TYPE_COMMIT);
765 err = got_object_cache_init(&repo->tagcache,
766 GOT_OBJECT_CACHE_TYPE_TAG);
769 err = got_object_cache_init(&repo->rawcache,
770 GOT_OBJECT_CACHE_TYPE_RAW);
774 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
775 if (repo->pack_cache_size > rl.rlim_cur / 8)
776 repo->pack_cache_size = rl.rlim_cur / 8;
777 for (i = 0; i < nitems(repo->packs); i++) {
778 if (i < repo->pack_cache_size) {
779 repo->packs[i].basefd = pack_fds[j++];
780 repo->packs[i].accumfd = pack_fds[j++];
782 repo->packs[i].basefd = -1;
783 repo->packs[i].accumfd = -1;
786 repo->pinned_pack = -1;
787 repo->pinned_packidx = -1;
788 repo->pinned_pid = 0;
790 repo_path = realpath(path, NULL);
791 if (repo_path == NULL) {
792 err = got_error_from_errno2("realpath", path);
799 err = open_repo(repo, repo_path);
802 if (err->code != GOT_ERR_NOT_GIT_REPO)
804 if (repo_path[0] == '/' && repo_path[1] == '\0') {
805 err = got_error(GOT_ERR_NOT_GIT_REPO);
808 err = got_path_dirname(&parent_path, repo_path);
812 repo_path = parent_path;
815 err = read_gotconfig(repo);
819 err = read_gitconfig(repo, global_gitconfig_path);
822 if (repo->gitconfig_repository_format_version != 0) {
823 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
826 for (i = 0; i < repo->nextensions; i++) {
827 char *ext = repo->extensions[i];
828 int j, supported = 0;
829 for (j = 0; j < nitems(repo_extensions); j++) {
830 if (strcmp(ext, repo_extensions[j]) == 0) {
836 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
841 err = got_repo_list_packidx(&repo->packidx_paths, repo);
844 got_repo_close(repo);
851 const struct got_error *
852 got_repo_close(struct got_repository *repo)
854 const struct got_error *err = NULL, *child_err;
855 struct got_packidx_bloom_filter *bf;
856 struct got_pathlist_entry *pe;
859 for (i = 0; i < repo->pack_cache_size; i++) {
860 if (repo->packidx_cache[i] == NULL)
862 got_packidx_close(repo->packidx_cache[i]);
865 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
866 &repo->packidx_bloom_filters))) {
867 RB_REMOVE(got_packidx_bloom_filter_tree,
868 &repo->packidx_bloom_filters, bf);
873 for (i = 0; i < repo->pack_cache_size; i++)
874 if (repo->packs[i].path_packfile)
875 if (repo->packs[i].path_packfile)
876 got_pack_close(&repo->packs[i]);
879 free(repo->path_git_dir);
881 got_object_cache_close(&repo->objcache);
882 got_object_cache_close(&repo->treecache);
883 got_object_cache_close(&repo->commitcache);
884 got_object_cache_close(&repo->tagcache);
885 got_object_cache_close(&repo->rawcache);
887 for (i = 0; i < nitems(repo->privsep_children); i++) {
888 if (repo->privsep_children[i].imsg_fd == -1)
890 imsg_clear(repo->privsep_children[i].ibuf);
891 free(repo->privsep_children[i].ibuf);
892 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
893 child_err = got_privsep_wait_for_child(
894 repo->privsep_children[i].pid);
895 if (child_err && err == NULL)
897 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
899 err = got_error_from_errno("close");
902 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
904 err = got_error_from_errno("close");
907 got_gotconfig_free(repo->gotconfig);
908 free(repo->gitconfig_author_name);
909 free(repo->gitconfig_author_email);
910 for (i = 0; i < repo->ngitconfig_remotes; i++)
911 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
912 free(repo->gitconfig_remotes);
913 for (i = 0; i < repo->nextensions; i++)
914 free(repo->extensions[i]);
915 free(repo->extensions);
917 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
918 free((void *)pe->path);
919 got_pathlist_free(&repo->packidx_paths);
926 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
932 free(repo->fetch_url);
933 repo->fetch_url = NULL;
934 free(repo->send_url);
935 repo->send_url = NULL;
936 for (i = 0; i < repo->nfetch_branches; i++)
937 free(repo->fetch_branches[i]);
938 free(repo->fetch_branches);
939 repo->fetch_branches = NULL;
940 repo->nfetch_branches = 0;
941 for (i = 0; i < repo->nsend_branches; i++)
942 free(repo->send_branches[i]);
943 free(repo->send_branches);
944 repo->send_branches = NULL;
945 repo->nsend_branches = 0;
948 const struct got_error *
949 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
950 const char *input_path)
952 const struct got_error *err = NULL;
953 const char *repo_abspath = NULL;
955 char *canonpath, *path = NULL;
957 *in_repo_path = NULL;
959 canonpath = strdup(input_path);
960 if (canonpath == NULL) {
961 err = got_error_from_errno("strdup");
964 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
968 repo_abspath = got_repo_get_path(repo);
970 if (canonpath[0] == '\0') {
971 path = strdup(canonpath);
973 err = got_error_from_errno("strdup");
977 path = realpath(canonpath, NULL);
979 if (errno != ENOENT) {
980 err = got_error_from_errno2("realpath",
985 * Path is not on disk.
986 * Assume it is already relative to repository root.
988 path = strdup(canonpath);
990 err = got_error_from_errno("strdup");
995 repolen = strlen(repo_abspath);
999 if (strcmp(path, repo_abspath) == 0) {
1003 err = got_error_from_errno("strdup");
1006 } else if (len > repolen &&
1007 got_path_is_child(path, repo_abspath, repolen)) {
1008 /* Matched an on-disk path inside repository. */
1009 if (got_repo_is_bare(repo)) {
1011 * Matched an on-disk path inside repository
1012 * database. Treat input as repository-relative.
1019 /* Strip common prefix with repository path. */
1020 err = got_path_skip_common_ancestor(&child,
1021 repo_abspath, path);
1029 * Matched unrelated on-disk path.
1030 * Treat input as repository-relative.
1038 /* Make in-repository path absolute */
1039 if (path[0] != '/') {
1041 if (asprintf(&abspath, "/%s", path) == -1) {
1042 err = got_error_from_errno("asprintf");
1054 *in_repo_path = path;
1058 static const struct got_error *
1059 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1060 const char *path_packidx)
1062 const struct got_error *err = NULL;
1065 for (i = 0; i < repo->pack_cache_size; i++) {
1066 if (repo->packidx_cache[i] == NULL)
1068 if (strcmp(repo->packidx_cache[i]->path_packidx,
1069 path_packidx) == 0) {
1070 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1073 if (i == repo->pack_cache_size) {
1076 } while (i > 0 && repo->pinned_packidx >= 0 &&
1077 i == repo->pinned_packidx);
1078 err = got_packidx_close(repo->packidx_cache[i]);
1083 repo->packidx_cache[i] = packidx;
1089 got_repo_is_packidx_filename(const char *name, size_t len)
1091 if (len != GOT_PACKIDX_NAMELEN)
1094 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1097 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1098 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1104 static struct got_packidx_bloom_filter *
1105 get_packidx_bloom_filter(struct got_repository *repo,
1106 const char *path, size_t path_len)
1108 struct got_packidx_bloom_filter key;
1110 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1111 return NULL; /* XXX */
1112 key.path_len = path_len;
1114 return RB_FIND(got_packidx_bloom_filter_tree,
1115 &repo->packidx_bloom_filters, &key);
1119 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1120 const char *path_packidx, struct got_object_id *id)
1122 struct got_packidx_bloom_filter *bf;
1124 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1126 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1128 /* No bloom filter means this pack index must be searched. */
1132 static const struct got_error *
1133 add_packidx_bloom_filter(struct got_repository *repo,
1134 struct got_packidx *packidx, const char *path_packidx)
1136 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1137 struct got_packidx_bloom_filter *bf;
1141 * Don't use bloom filters for very large pack index files.
1142 * Large pack files will contain a relatively large fraction
1143 * of our objects so we will likely need to visit them anyway.
1144 * The more objects a pack file contains the higher the probability
1145 * of a false-positive match from the bloom filter. And reading
1146 * all object IDs from a large pack index file can be expensive.
1148 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1151 /* Do we already have a filter for this pack index? */
1152 if (get_packidx_bloom_filter(repo, path_packidx,
1153 strlen(path_packidx)) != NULL)
1156 bf = calloc(1, sizeof(*bf));
1158 return got_error_from_errno("calloc");
1159 bf->bloom = calloc(1, sizeof(*bf->bloom));
1160 if (bf->bloom == NULL) {
1162 return got_error_from_errno("calloc");
1165 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1166 if (len >= sizeof(bf->path)) {
1169 return got_error(GOT_ERR_NO_SPACE);
1173 /* Minimum size supported by our bloom filter is 1000 entries. */
1174 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1175 for (i = 0; i < nobjects; i++) {
1176 struct got_packidx_object_id *id;
1177 id = &packidx->hdr.sorted_ids[i];
1178 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1181 RB_INSERT(got_packidx_bloom_filter_tree,
1182 &repo->packidx_bloom_filters, bf);
1186 const struct got_error *
1187 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1188 struct got_repository *repo, struct got_object_id *id)
1190 const struct got_error *err;
1191 struct got_pathlist_entry *pe;
1194 /* Search pack index cache. */
1195 for (i = 0; i < repo->pack_cache_size; i++) {
1196 if (repo->packidx_cache[i] == NULL)
1198 if (!got_repo_check_packidx_bloom_filter(repo,
1199 repo->packidx_cache[i]->path_packidx, id))
1200 continue; /* object will not be found in this index */
1201 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1203 *packidx = repo->packidx_cache[i];
1205 * Move this cache entry to the front. Repeatedly
1206 * searching a wrong pack index can be expensive.
1209 memmove(&repo->packidx_cache[1],
1210 &repo->packidx_cache[0],
1211 i * sizeof(repo->packidx_cache[0]));
1212 repo->packidx_cache[0] = *packidx;
1213 if (repo->pinned_packidx >= 0 &&
1214 repo->pinned_packidx < i)
1215 repo->pinned_packidx++;
1216 else if (repo->pinned_packidx == i)
1217 repo->pinned_packidx = 0;
1222 /* No luck. Search the filesystem. */
1224 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1225 const char *path_packidx = pe->path;
1228 if (!got_repo_check_packidx_bloom_filter(repo,
1230 continue; /* object will not be found in this index */
1232 for (i = 0; i < repo->pack_cache_size; i++) {
1233 if (repo->packidx_cache[i] == NULL)
1235 if (strcmp(repo->packidx_cache[i]->path_packidx,
1236 path_packidx) == 0) {
1242 continue; /* already searched */
1244 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1249 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1253 err = cache_packidx(repo, *packidx, path_packidx);
1257 *idx = got_packidx_get_object_idx(*packidx, id);
1259 err = NULL; /* found the object */
1264 err = got_error_no_obj(id);
1269 const struct got_error *
1270 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1271 struct got_repository *repo)
1273 const struct got_error *err = NULL;
1274 DIR *packdir = NULL;
1275 struct dirent *dent;
1276 char *path_packidx = NULL;
1279 packdir_fd = openat(got_repo_get_fd(repo),
1280 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1281 if (packdir_fd == -1) {
1282 return got_error_from_errno_fmt("openat: %s/%s",
1283 got_repo_get_path_git_dir(repo),
1284 GOT_OBJECTS_PACK_DIR);
1287 packdir = fdopendir(packdir_fd);
1288 if (packdir == NULL) {
1289 err = got_error_from_errno("fdopendir");
1293 while ((dent = readdir(packdir)) != NULL) {
1294 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1297 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1298 dent->d_name) == -1) {
1299 err = got_error_from_errno("asprintf");
1300 path_packidx = NULL;
1304 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1311 if (packdir && closedir(packdir) != 0 && err == NULL)
1312 err = got_error_from_errno("closedir");
1316 const struct got_error *
1317 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1318 struct got_repository *repo)
1320 const struct got_error *err;
1325 /* Search pack index cache. */
1326 for (i = 0; i < repo->pack_cache_size; i++) {
1327 if (repo->packidx_cache[i] == NULL)
1329 if (strcmp(repo->packidx_cache[i]->path_packidx,
1330 path_packidx) == 0) {
1331 *packidx = repo->packidx_cache[i];
1335 /* No luck. Search the filesystem. */
1337 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1342 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1346 err = cache_packidx(repo, *packidx, path_packidx);
1349 got_packidx_close(*packidx);
1355 static const struct got_error *
1356 read_packfile_hdr(int fd, struct got_packidx *packidx)
1358 const struct got_error *err = NULL;
1359 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1360 struct got_packfile_hdr hdr;
1363 n = read(fd, &hdr, sizeof(hdr));
1365 return got_error_from_errno("read");
1366 if (n != sizeof(hdr))
1367 return got_error(GOT_ERR_BAD_PACKFILE);
1369 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1370 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1371 be32toh(hdr.nobjects) != totobj)
1372 err = got_error(GOT_ERR_BAD_PACKFILE);
1377 static const struct got_error *
1378 open_packfile(int *fd, struct got_repository *repo,
1379 const char *relpath, struct got_packidx *packidx)
1381 const struct got_error *err = NULL;
1383 *fd = openat(got_repo_get_fd(repo), relpath,
1384 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1386 return got_error_from_errno_fmt("openat: %s/%s",
1387 got_repo_get_path_git_dir(repo), relpath);
1390 err = read_packfile_hdr(*fd, packidx);
1400 const struct got_error *
1401 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1402 const char *path_packfile, struct got_packidx *packidx)
1404 const struct got_error *err = NULL;
1405 struct got_pack *pack = NULL;
1412 for (i = 0; i < repo->pack_cache_size; i++) {
1413 pack = &repo->packs[i];
1414 if (pack->path_packfile == NULL)
1416 if (strcmp(pack->path_packfile, path_packfile) == 0)
1417 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1420 if (i == repo->pack_cache_size) {
1421 struct got_pack tmp;
1424 } while (i > 0 && repo->pinned_pack >= 0 &&
1425 i == repo->pinned_pack);
1426 err = got_pack_close(&repo->packs[i]);
1429 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1430 return got_error_from_errno("ftruncate");
1431 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1432 return got_error_from_errno("ftruncate");
1433 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1434 memcpy(&repo->packs[i], &repo->packs[0],
1435 sizeof(repo->packs[i]));
1436 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1437 if (repo->pinned_pack == 0)
1438 repo->pinned_pack = i;
1439 else if (repo->pinned_pack == i)
1440 repo->pinned_pack = 0;
1444 pack = &repo->packs[i];
1446 pack->path_packfile = strdup(path_packfile);
1447 if (pack->path_packfile == NULL) {
1448 err = got_error_from_errno("strdup");
1452 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1456 if (fstat(pack->fd, &sb) != 0) {
1457 err = got_error_from_errno("fstat");
1460 pack->filesize = sb.st_size;
1462 pack->privsep_child = NULL;
1464 #ifndef GOT_PACK_NO_MMAP
1465 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1467 if (pack->map == MAP_FAILED) {
1468 if (errno != ENOMEM) {
1469 err = got_error_from_errno("mmap");
1472 pack->map = NULL; /* fall back to read(2) */
1478 free(pack->path_packfile);
1479 memset(pack, 0, sizeof(*pack));
1487 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1489 struct got_pack *pack = NULL;
1492 for (i = 0; i < repo->pack_cache_size; i++) {
1493 pack = &repo->packs[i];
1494 if (pack->path_packfile == NULL)
1496 if (strcmp(pack->path_packfile, path_packfile) == 0)
1503 const struct got_error *
1504 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1505 struct got_pack *pack)
1508 int pinned_pack = -1, pinned_packidx = -1;
1510 for (i = 0; i < repo->pack_cache_size; i++) {
1511 if (repo->packidx_cache[i] &&
1512 strcmp(repo->packidx_cache[i]->path_packidx,
1513 packidx->path_packidx) == 0)
1515 if (repo->packs[i].path_packfile &&
1516 strcmp(repo->packs[i].path_packfile,
1517 pack->path_packfile) == 0)
1521 if (pinned_packidx == -1 || pinned_pack == -1)
1522 return got_error(GOT_ERR_PIN_PACK);
1524 repo->pinned_pack = pinned_pack;
1525 repo->pinned_packidx = pinned_packidx;
1526 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1531 got_repo_get_pinned_pack(struct got_repository *repo)
1533 if (repo->pinned_pack >= 0 &&
1534 repo->pinned_pack < repo->pack_cache_size)
1535 return &repo->packs[repo->pinned_pack];
1541 got_repo_unpin_pack(struct got_repository *repo)
1543 repo->pinned_packidx = -1;
1544 repo->pinned_pack = -1;
1545 repo->pinned_pid = 0;
1548 const struct got_error *
1549 got_repo_init(const char *repo_path)
1551 const struct got_error *err = NULL;
1552 const char *dirnames[] = {
1554 GOT_OBJECTS_PACK_DIR,
1557 const char *description_str = "Unnamed repository; "
1558 "edit this file 'description' to name the repository.";
1559 const char *headref_str = "ref: refs/heads/main";
1560 const char *gitconfig_str = "[core]\n"
1561 "\trepositoryformatversion = 0\n"
1562 "\tfilemode = true\n"
1567 if (!got_path_dir_is_empty(repo_path))
1568 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1570 for (i = 0; i < nitems(dirnames); i++) {
1571 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1572 return got_error_from_errno("asprintf");
1574 err = got_path_mkdir(path);
1580 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1581 return got_error_from_errno("asprintf");
1582 err = got_path_create_file(path, description_str);
1587 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1588 return got_error_from_errno("asprintf");
1589 err = got_path_create_file(path, headref_str);
1594 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1595 return got_error_from_errno("asprintf");
1596 err = got_path_create_file(path, gitconfig_str);
1604 static const struct got_error *
1605 match_packed_object(struct got_object_id **unique_id,
1606 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1608 const struct got_error *err = NULL;
1609 struct got_object_id_queue matched_ids;
1610 struct got_pathlist_entry *pe;
1612 STAILQ_INIT(&matched_ids);
1614 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1615 const char *path_packidx = pe->path;
1616 struct got_packidx *packidx;
1617 struct got_object_qid *qid;
1619 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1624 err = got_packidx_match_id_str_prefix(&matched_ids,
1625 packidx, id_str_prefix);
1627 got_packidx_close(packidx);
1630 err = got_packidx_close(packidx);
1634 STAILQ_FOREACH(qid, &matched_ids, entry) {
1635 if (obj_type != GOT_OBJ_TYPE_ANY) {
1637 err = got_object_get_type(&matched_type, repo,
1641 if (matched_type != obj_type)
1644 if (*unique_id == NULL) {
1645 *unique_id = got_object_id_dup(&qid->id);
1646 if (*unique_id == NULL) {
1647 err = got_error_from_errno("malloc");
1651 if (got_object_id_cmp(*unique_id,
1653 continue; /* packed multiple times */
1654 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1660 got_object_id_queue_free(&matched_ids);
1668 static const struct got_error *
1669 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1670 const char *object_dir, const char *id_str_prefix, int obj_type,
1671 struct got_repository *repo)
1673 const struct got_error *err = NULL;
1676 struct dirent *dent;
1677 struct got_object_id id;
1679 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1680 err = got_error_from_errno("asprintf");
1684 dir = opendir(path);
1686 if (errno == ENOENT) {
1690 err = got_error_from_errno2("opendir", path);
1693 while ((dent = readdir(dir)) != NULL) {
1697 if (strcmp(dent->d_name, ".") == 0 ||
1698 strcmp(dent->d_name, "..") == 0)
1701 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1702 err = got_error_from_errno("asprintf");
1706 if (!got_parse_sha1_digest(id.sha1, id_str))
1710 * Directory entries do not necessarily appear in
1711 * sorted order, so we must iterate over all of them.
1713 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1719 if (*unique_id == NULL) {
1720 if (obj_type != GOT_OBJ_TYPE_ANY) {
1722 err = got_object_get_type(&matched_type, repo,
1726 if (matched_type != obj_type)
1729 *unique_id = got_object_id_dup(&id);
1730 if (*unique_id == NULL) {
1731 err = got_error_from_errno("got_object_id_dup");
1736 if (got_object_id_cmp(*unique_id, &id) == 0)
1737 continue; /* both packed and loose */
1738 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1744 if (dir && closedir(dir) != 0 && err == NULL)
1745 err = got_error_from_errno("closedir");
1754 const struct got_error *
1755 got_repo_match_object_id_prefix(struct got_object_id **id,
1756 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1758 const struct got_error *err = NULL;
1759 char *path_objects = got_repo_get_path_objects(repo);
1760 char *object_dir = NULL;
1766 len = strlen(id_str_prefix);
1767 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1768 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1770 for (i = 0; i < len; i++) {
1771 if (isxdigit((unsigned char)id_str_prefix[i]))
1773 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1777 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1780 object_dir = strndup(id_str_prefix, 2);
1781 if (object_dir == NULL) {
1782 err = got_error_from_errno("strdup");
1785 err = match_loose_object(id, path_objects, object_dir,
1786 id_str_prefix, obj_type, repo);
1787 } else if (len == 1) {
1789 for (i = 0; i < 0xf; i++) {
1790 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1792 err = got_error_from_errno("asprintf");
1795 err = match_packed_object(id, repo, object_dir,
1799 err = match_loose_object(id, path_objects, object_dir,
1800 id_str_prefix, obj_type, repo);
1805 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1813 } else if (*id == NULL) {
1815 case GOT_OBJ_TYPE_BLOB:
1816 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1817 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1819 case GOT_OBJ_TYPE_TREE:
1820 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1821 GOT_OBJ_LABEL_TREE, id_str_prefix);
1823 case GOT_OBJ_TYPE_COMMIT:
1824 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1825 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1827 case GOT_OBJ_TYPE_TAG:
1828 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1829 GOT_OBJ_LABEL_TAG, id_str_prefix);
1832 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1840 const struct got_error *
1841 got_repo_match_object_id(struct got_object_id **id, char **label,
1842 const char *id_str, int obj_type, struct got_reflist_head *refs,
1843 struct got_repository *repo)
1845 const struct got_error *err;
1846 struct got_tag_object *tag;
1847 struct got_reference *ref = NULL;
1854 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1857 *id = got_object_id_dup(
1858 got_object_tag_get_object_id(tag));
1860 err = got_error_from_errno("got_object_id_dup");
1861 else if (label && asprintf(label, "refs/tags/%s",
1862 got_object_tag_get_name(tag)) == -1) {
1863 err = got_error_from_errno("asprintf");
1867 got_object_tag_close(tag);
1869 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1870 err->code != GOT_ERR_NO_OBJ)
1874 err = got_ref_open(&ref, repo, id_str, 0);
1876 err = got_ref_resolve(id, repo, ref);
1880 *label = strdup(got_ref_get_name(ref));
1881 if (*label == NULL) {
1882 err = got_error_from_errno("strdup");
1887 if (err->code != GOT_ERR_NOT_REF &&
1888 err->code != GOT_ERR_BAD_REF_NAME)
1890 err = got_repo_match_object_id_prefix(id, id_str,
1893 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1894 err = got_error_not_ref(id_str);
1898 err = got_object_id_str(label, *id);
1899 if (*label == NULL) {
1900 err = got_error_from_errno("strdup");
1911 const struct got_error *
1912 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1913 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1915 const struct got_error *err = NULL;
1916 struct got_reflist_entry *re;
1917 struct got_object_id *tag_id;
1918 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1922 TAILQ_FOREACH(re, refs, entry) {
1923 const char *refname;
1924 refname = got_ref_get_name(re->ref);
1925 if (got_ref_is_symbolic(re->ref))
1927 if (strncmp(refname, "refs/tags/", 10) != 0)
1929 if (!name_is_absolute)
1930 refname += strlen("refs/tags/");
1931 if (strcmp(refname, name) != 0)
1933 err = got_ref_resolve(&tag_id, repo, re->ref);
1936 err = got_object_open_as_tag(tag, repo, tag_id);
1940 if (obj_type == GOT_OBJ_TYPE_ANY ||
1941 got_object_tag_get_object_type(*tag) == obj_type)
1943 got_object_tag_close(*tag);
1947 if (err == NULL && *tag == NULL)
1948 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1949 GOT_OBJ_LABEL_TAG, name);
1953 static const struct got_error *
1954 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1955 const char *name, mode_t mode, struct got_object_id *blob_id)
1957 const struct got_error *err = NULL;
1961 *new_te = calloc(1, sizeof(**new_te));
1962 if (*new_te == NULL)
1963 return got_error_from_errno("calloc");
1965 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1966 sizeof((*new_te)->name)) {
1967 err = got_error(GOT_ERR_NO_SPACE);
1971 if (S_ISLNK(mode)) {
1972 (*new_te)->mode = S_IFLNK;
1974 (*new_te)->mode = S_IFREG;
1975 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1977 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1979 if (err && *new_te) {
1986 static const struct got_error *
1987 import_file(struct got_tree_entry **new_te, struct dirent *de,
1988 const char *path, struct got_repository *repo)
1990 const struct got_error *err;
1991 struct got_object_id *blob_id = NULL;
1995 if (asprintf(&filepath, "%s%s%s", path,
1996 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1997 return got_error_from_errno("asprintf");
1999 if (lstat(filepath, &sb) != 0) {
2000 err = got_error_from_errno2("lstat", path);
2004 err = got_object_blob_create(&blob_id, filepath, repo);
2008 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2017 static const struct got_error *
2018 insert_tree_entry(struct got_tree_entry *new_te,
2019 struct got_pathlist_head *paths)
2021 const struct got_error *err = NULL;
2022 struct got_pathlist_entry *new_pe;
2024 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2028 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2032 static const struct got_error *write_tree(struct got_object_id **,
2033 const char *, struct got_pathlist_head *, struct got_repository *,
2034 got_repo_import_cb progress_cb, void *progress_arg);
2036 static const struct got_error *
2037 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2038 const char *path, struct got_pathlist_head *ignores,
2039 struct got_repository *repo,
2040 got_repo_import_cb progress_cb, void *progress_arg)
2042 const struct got_error *err;
2043 struct got_object_id *id = NULL;
2046 if (asprintf(&subdirpath, "%s%s%s", path,
2047 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2048 return got_error_from_errno("asprintf");
2050 (*new_te) = calloc(1, sizeof(**new_te));
2051 if (*new_te == NULL)
2052 return got_error_from_errno("calloc");
2053 (*new_te)->mode = S_IFDIR;
2054 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2055 sizeof((*new_te)->name)) {
2056 err = got_error(GOT_ERR_NO_SPACE);
2059 err = write_tree(&id, subdirpath, ignores, repo,
2060 progress_cb, progress_arg);
2063 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2075 static const struct got_error *
2076 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2077 struct got_pathlist_head *ignores, struct got_repository *repo,
2078 got_repo_import_cb progress_cb, void *progress_arg)
2080 const struct got_error *err = NULL;
2084 struct got_tree_entry *new_te = NULL;
2085 struct got_pathlist_head paths;
2086 struct got_pathlist_entry *pe;
2088 *new_tree_id = NULL;
2092 dir = opendir(path_dir);
2094 err = got_error_from_errno2("opendir", path_dir);
2099 while ((de = readdir(dir)) != NULL) {
2103 if (strcmp(de->d_name, ".") == 0 ||
2104 strcmp(de->d_name, "..") == 0)
2107 TAILQ_FOREACH(pe, ignores, entry) {
2108 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2116 err = got_path_dirent_type(&type, path_dir, de);
2120 if (type == DT_DIR) {
2121 err = import_subdir(&new_te, de, path_dir,
2122 ignores, repo, progress_cb, progress_arg);
2124 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2129 } else if (type == DT_REG || type == DT_LNK) {
2130 err = import_file(&new_te, de, path_dir, repo);
2136 err = insert_tree_entry(new_te, &paths);
2142 if (TAILQ_EMPTY(&paths)) {
2143 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2144 "cannot create tree without any entries");
2148 TAILQ_FOREACH(pe, &paths, entry) {
2149 struct got_tree_entry *te = pe->data;
2151 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2153 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2154 err = got_error_from_errno("asprintf");
2157 err = (*progress_cb)(progress_arg, path);
2163 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2167 got_pathlist_free(&paths);
2171 const struct got_error *
2172 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2173 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2174 struct got_repository *repo, got_repo_import_cb progress_cb,
2177 const struct got_error *err;
2178 struct got_object_id *new_tree_id;
2180 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2181 progress_cb, progress_arg);
2185 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2186 author, time(NULL), author, time(NULL), logmsg, repo);
2191 const struct got_error *
2192 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2193 struct got_repository *repo)
2195 const struct got_error *err = NULL;
2196 char *path_objects = NULL, *path = NULL;
2198 struct got_object_id id;
2204 path_objects = got_repo_get_path_objects(repo);
2205 if (path_objects == NULL)
2206 return got_error_from_errno("got_repo_get_path_objects");
2208 for (i = 0; i <= 0xff; i++) {
2209 struct dirent *dent;
2211 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2212 err = got_error_from_errno("asprintf");
2216 dir = opendir(path);
2218 if (errno == ENOENT) {
2222 err = got_error_from_errno2("opendir", path);
2226 while ((dent = readdir(dir)) != NULL) {
2231 if (strcmp(dent->d_name, ".") == 0 ||
2232 strcmp(dent->d_name, "..") == 0)
2235 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2236 err = got_error_from_errno("asprintf");
2240 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2246 err = got_object_open_loose_fd(&fd, &id, repo);
2250 if (fstat(fd, &sb) == -1) {
2251 err = got_error_from_errno("fstat");
2256 (*ondisk_size) += sb.st_size;
2258 if (close(fd) == -1) {
2259 err = got_error_from_errno("close");
2264 if (closedir(dir) != 0) {
2265 err = got_error_from_errno("closedir");
2274 if (dir && closedir(dir) != 0 && err == NULL)
2275 err = got_error_from_errno("closedir");
2286 const struct got_error *
2287 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2288 off_t *total_packsize, struct got_repository *repo)
2290 const struct got_error *err = NULL;
2291 DIR *packdir = NULL;
2292 struct dirent *dent;
2293 struct got_packidx *packidx = NULL;
2295 char *path_packfile;
2301 *total_packsize = 0;
2303 packdir_fd = openat(got_repo_get_fd(repo),
2304 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2305 if (packdir_fd == -1) {
2306 return got_error_from_errno_fmt("openat: %s/%s",
2307 got_repo_get_path_git_dir(repo),
2308 GOT_OBJECTS_PACK_DIR);
2311 packdir = fdopendir(packdir_fd);
2312 if (packdir == NULL) {
2313 err = got_error_from_errno("fdopendir");
2317 while ((dent = readdir(packdir)) != NULL) {
2318 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2321 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2322 dent->d_name) == -1) {
2323 err = got_error_from_errno("asprintf");
2327 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2333 if (fstat(packidx->fd, &sb) == -1)
2335 *total_packsize += sb.st_size;
2337 err = got_packidx_get_packfile_path(&path_packfile,
2338 packidx->path_packidx);
2342 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2344 free(path_packfile);
2347 free(path_packfile);
2348 *total_packsize += sb.st_size;
2350 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2354 got_packidx_close(packidx);
2359 got_packidx_close(packidx);
2360 if (packdir && closedir(packdir) != 0 && err == NULL)
2361 err = got_error_from_errno("closedir");
2365 *total_packsize = 0;
2370 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2371 got_packidx_bloom_filter_cmp);