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->gitconfig_remotes = NULL;
691 repo->ngitconfig_remotes = 0;
693 free(repo->gitconfig_author_name);
694 repo->gitconfig_author_name = NULL;
695 free(repo->gitconfig_author_email);
696 repo->gitconfig_author_email = NULL;
698 free(repo->global_gitconfig_author_name);
699 repo->global_gitconfig_author_name = NULL;
700 free(repo->global_gitconfig_author_email);
701 repo->global_gitconfig_author_email = NULL;
705 free(repo_gitconfig_path);
709 static const struct got_error *
710 read_gotconfig(struct got_repository *repo)
712 const struct got_error *err = NULL;
713 char *gotconfig_path;
715 gotconfig_path = got_repo_get_path_gotconfig(repo);
716 if (gotconfig_path == NULL)
717 return got_error_from_errno("got_repo_get_path_gotconfig");
719 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
720 free(gotconfig_path);
724 /* Supported repository format extensions. */
725 static const char *const repo_extensions[] = {
726 "noop", /* Got supports repository format version 1. */
727 "preciousObjects", /* Supported by gotadmin cleanup. */
728 "worktreeConfig", /* Got does not care about Git work trees. */
731 const struct got_error *
732 got_repo_open(struct got_repository **repop, const char *path,
733 const char *global_gitconfig_path, int *pack_fds)
735 struct got_repository *repo = NULL;
736 const struct got_error *err = NULL;
737 char *repo_path = NULL;
743 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
744 return got_error_from_errno("getrlimit");
746 repo = calloc(1, sizeof(*repo));
748 return got_error_from_errno("calloc");
750 RB_INIT(&repo->packidx_bloom_filters);
751 TAILQ_INIT(&repo->packidx_paths);
753 for (i = 0; i < nitems(repo->privsep_children); i++) {
754 memset(&repo->privsep_children[i], 0,
755 sizeof(repo->privsep_children[0]));
756 repo->privsep_children[i].imsg_fd = -1;
759 err = got_object_cache_init(&repo->objcache,
760 GOT_OBJECT_CACHE_TYPE_OBJ);
763 err = got_object_cache_init(&repo->treecache,
764 GOT_OBJECT_CACHE_TYPE_TREE);
767 err = got_object_cache_init(&repo->commitcache,
768 GOT_OBJECT_CACHE_TYPE_COMMIT);
771 err = got_object_cache_init(&repo->tagcache,
772 GOT_OBJECT_CACHE_TYPE_TAG);
775 err = got_object_cache_init(&repo->rawcache,
776 GOT_OBJECT_CACHE_TYPE_RAW);
780 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
781 if (repo->pack_cache_size > rl.rlim_cur / 8)
782 repo->pack_cache_size = rl.rlim_cur / 8;
783 for (i = 0; i < nitems(repo->packs); i++) {
784 if (i < repo->pack_cache_size) {
785 repo->packs[i].basefd = pack_fds[j++];
786 repo->packs[i].accumfd = pack_fds[j++];
788 repo->packs[i].basefd = -1;
789 repo->packs[i].accumfd = -1;
792 repo->pinned_pack = -1;
793 repo->pinned_packidx = -1;
794 repo->pinned_pid = 0;
796 repo_path = realpath(path, NULL);
797 if (repo_path == NULL) {
798 err = got_error_from_errno2("realpath", path);
805 err = open_repo(repo, repo_path);
808 if (err->code != GOT_ERR_NOT_GIT_REPO)
810 if (repo_path[0] == '/' && repo_path[1] == '\0') {
811 err = got_error(GOT_ERR_NOT_GIT_REPO);
814 err = got_path_dirname(&parent_path, repo_path);
818 repo_path = parent_path;
821 err = read_gotconfig(repo);
825 err = read_gitconfig(repo, global_gitconfig_path);
828 if (repo->gitconfig_repository_format_version != 0) {
829 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
832 for (i = 0; i < repo->nextensions; i++) {
833 char *ext = repo->extensions[i];
834 int j, supported = 0;
835 for (j = 0; j < nitems(repo_extensions); j++) {
836 if (strcmp(ext, repo_extensions[j]) == 0) {
842 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
847 err = got_repo_list_packidx(&repo->packidx_paths, repo);
850 got_repo_close(repo);
857 const struct got_error *
858 got_repo_close(struct got_repository *repo)
860 const struct got_error *err = NULL, *child_err;
861 struct got_packidx_bloom_filter *bf;
862 struct got_pathlist_entry *pe;
865 for (i = 0; i < repo->pack_cache_size; i++) {
866 if (repo->packidx_cache[i] == NULL)
868 got_packidx_close(repo->packidx_cache[i]);
871 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
872 &repo->packidx_bloom_filters))) {
873 RB_REMOVE(got_packidx_bloom_filter_tree,
874 &repo->packidx_bloom_filters, bf);
879 for (i = 0; i < repo->pack_cache_size; i++)
880 if (repo->packs[i].path_packfile)
881 if (repo->packs[i].path_packfile)
882 got_pack_close(&repo->packs[i]);
885 free(repo->path_git_dir);
887 got_object_cache_close(&repo->objcache);
888 got_object_cache_close(&repo->treecache);
889 got_object_cache_close(&repo->commitcache);
890 got_object_cache_close(&repo->tagcache);
891 got_object_cache_close(&repo->rawcache);
893 for (i = 0; i < nitems(repo->privsep_children); i++) {
894 if (repo->privsep_children[i].imsg_fd == -1)
896 imsg_clear(repo->privsep_children[i].ibuf);
897 free(repo->privsep_children[i].ibuf);
898 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
899 child_err = got_privsep_wait_for_child(
900 repo->privsep_children[i].pid);
901 if (child_err && err == NULL)
903 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
905 err = got_error_from_errno("close");
908 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
910 err = got_error_from_errno("close");
913 got_gotconfig_free(repo->gotconfig);
914 free(repo->gitconfig_author_name);
915 free(repo->gitconfig_author_email);
916 for (i = 0; i < repo->ngitconfig_remotes; i++)
917 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
918 free(repo->gitconfig_remotes);
919 for (i = 0; i < repo->nextensions; i++)
920 free(repo->extensions[i]);
921 free(repo->extensions);
923 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
924 free((void *)pe->path);
925 got_pathlist_free(&repo->packidx_paths);
932 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
938 free(repo->fetch_url);
939 repo->fetch_url = NULL;
940 free(repo->send_url);
941 repo->send_url = NULL;
942 for (i = 0; i < repo->nfetch_branches; i++)
943 free(repo->fetch_branches[i]);
944 free(repo->fetch_branches);
945 repo->fetch_branches = NULL;
946 repo->nfetch_branches = 0;
947 for (i = 0; i < repo->nsend_branches; i++)
948 free(repo->send_branches[i]);
949 free(repo->send_branches);
950 repo->send_branches = NULL;
951 repo->nsend_branches = 0;
954 const struct got_error *
955 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
956 const char *input_path)
958 const struct got_error *err = NULL;
959 const char *repo_abspath = NULL;
961 char *canonpath, *path = NULL;
963 *in_repo_path = NULL;
965 canonpath = strdup(input_path);
966 if (canonpath == NULL) {
967 err = got_error_from_errno("strdup");
970 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
974 repo_abspath = got_repo_get_path(repo);
976 if (canonpath[0] == '\0') {
977 path = strdup(canonpath);
979 err = got_error_from_errno("strdup");
983 path = realpath(canonpath, NULL);
985 if (errno != ENOENT) {
986 err = got_error_from_errno2("realpath",
991 * Path is not on disk.
992 * Assume it is already relative to repository root.
994 path = strdup(canonpath);
996 err = got_error_from_errno("strdup");
1001 repolen = strlen(repo_abspath);
1005 if (strcmp(path, repo_abspath) == 0) {
1009 err = got_error_from_errno("strdup");
1012 } else if (len > repolen &&
1013 got_path_is_child(path, repo_abspath, repolen)) {
1014 /* Matched an on-disk path inside repository. */
1015 if (got_repo_is_bare(repo)) {
1017 * Matched an on-disk path inside repository
1018 * database. Treat input as repository-relative.
1025 /* Strip common prefix with repository path. */
1026 err = got_path_skip_common_ancestor(&child,
1027 repo_abspath, path);
1035 * Matched unrelated on-disk path.
1036 * Treat input as repository-relative.
1044 /* Make in-repository path absolute */
1045 if (path[0] != '/') {
1047 if (asprintf(&abspath, "/%s", path) == -1) {
1048 err = got_error_from_errno("asprintf");
1060 *in_repo_path = path;
1064 static const struct got_error *
1065 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1066 const char *path_packidx)
1068 const struct got_error *err = NULL;
1071 for (i = 0; i < repo->pack_cache_size; i++) {
1072 if (repo->packidx_cache[i] == NULL)
1074 if (strcmp(repo->packidx_cache[i]->path_packidx,
1075 path_packidx) == 0) {
1076 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1079 if (i == repo->pack_cache_size) {
1082 } while (i > 0 && repo->pinned_packidx >= 0 &&
1083 i == repo->pinned_packidx);
1084 err = got_packidx_close(repo->packidx_cache[i]);
1089 repo->packidx_cache[i] = packidx;
1095 got_repo_is_packidx_filename(const char *name, size_t len)
1097 if (len != GOT_PACKIDX_NAMELEN)
1100 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1103 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1104 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1110 static struct got_packidx_bloom_filter *
1111 get_packidx_bloom_filter(struct got_repository *repo,
1112 const char *path, size_t path_len)
1114 struct got_packidx_bloom_filter key;
1116 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1117 return NULL; /* XXX */
1118 key.path_len = path_len;
1120 return RB_FIND(got_packidx_bloom_filter_tree,
1121 &repo->packidx_bloom_filters, &key);
1125 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1126 const char *path_packidx, struct got_object_id *id)
1128 struct got_packidx_bloom_filter *bf;
1130 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1132 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1134 /* No bloom filter means this pack index must be searched. */
1138 static const struct got_error *
1139 add_packidx_bloom_filter(struct got_repository *repo,
1140 struct got_packidx *packidx, const char *path_packidx)
1142 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1143 struct got_packidx_bloom_filter *bf;
1147 * Don't use bloom filters for very large pack index files.
1148 * Large pack files will contain a relatively large fraction
1149 * of our objects so we will likely need to visit them anyway.
1150 * The more objects a pack file contains the higher the probability
1151 * of a false-positive match from the bloom filter. And reading
1152 * all object IDs from a large pack index file can be expensive.
1154 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1157 /* Do we already have a filter for this pack index? */
1158 if (get_packidx_bloom_filter(repo, path_packidx,
1159 strlen(path_packidx)) != NULL)
1162 bf = calloc(1, sizeof(*bf));
1164 return got_error_from_errno("calloc");
1165 bf->bloom = calloc(1, sizeof(*bf->bloom));
1166 if (bf->bloom == NULL) {
1168 return got_error_from_errno("calloc");
1171 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1172 if (len >= sizeof(bf->path)) {
1175 return got_error(GOT_ERR_NO_SPACE);
1179 /* Minimum size supported by our bloom filter is 1000 entries. */
1180 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1181 for (i = 0; i < nobjects; i++) {
1182 struct got_packidx_object_id *id;
1183 id = &packidx->hdr.sorted_ids[i];
1184 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1187 RB_INSERT(got_packidx_bloom_filter_tree,
1188 &repo->packidx_bloom_filters, bf);
1192 const struct got_error *
1193 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1194 struct got_repository *repo, struct got_object_id *id)
1196 const struct got_error *err;
1197 struct got_pathlist_entry *pe;
1200 /* Search pack index cache. */
1201 for (i = 0; i < repo->pack_cache_size; i++) {
1202 if (repo->packidx_cache[i] == NULL)
1204 if (!got_repo_check_packidx_bloom_filter(repo,
1205 repo->packidx_cache[i]->path_packidx, id))
1206 continue; /* object will not be found in this index */
1207 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1209 *packidx = repo->packidx_cache[i];
1211 * Move this cache entry to the front. Repeatedly
1212 * searching a wrong pack index can be expensive.
1215 memmove(&repo->packidx_cache[1],
1216 &repo->packidx_cache[0],
1217 i * sizeof(repo->packidx_cache[0]));
1218 repo->packidx_cache[0] = *packidx;
1219 if (repo->pinned_packidx >= 0 &&
1220 repo->pinned_packidx < i)
1221 repo->pinned_packidx++;
1222 else if (repo->pinned_packidx == i)
1223 repo->pinned_packidx = 0;
1228 /* No luck. Search the filesystem. */
1230 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1231 const char *path_packidx = pe->path;
1234 if (!got_repo_check_packidx_bloom_filter(repo,
1236 continue; /* object will not be found in this index */
1238 for (i = 0; i < repo->pack_cache_size; i++) {
1239 if (repo->packidx_cache[i] == NULL)
1241 if (strcmp(repo->packidx_cache[i]->path_packidx,
1242 path_packidx) == 0) {
1248 continue; /* already searched */
1250 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1255 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1259 err = cache_packidx(repo, *packidx, path_packidx);
1263 *idx = got_packidx_get_object_idx(*packidx, id);
1265 err = NULL; /* found the object */
1270 err = got_error_no_obj(id);
1275 const struct got_error *
1276 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1277 struct got_repository *repo)
1279 const struct got_error *err = NULL;
1280 DIR *packdir = NULL;
1281 struct dirent *dent;
1282 char *path_packidx = NULL;
1285 packdir_fd = openat(got_repo_get_fd(repo),
1286 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1287 if (packdir_fd == -1) {
1288 return got_error_from_errno_fmt("openat: %s/%s",
1289 got_repo_get_path_git_dir(repo),
1290 GOT_OBJECTS_PACK_DIR);
1293 packdir = fdopendir(packdir_fd);
1294 if (packdir == NULL) {
1295 err = got_error_from_errno("fdopendir");
1299 while ((dent = readdir(packdir)) != NULL) {
1300 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1303 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1304 dent->d_name) == -1) {
1305 err = got_error_from_errno("asprintf");
1306 path_packidx = NULL;
1310 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1317 if (packdir && closedir(packdir) != 0 && err == NULL)
1318 err = got_error_from_errno("closedir");
1322 const struct got_error *
1323 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1324 struct got_repository *repo)
1326 const struct got_error *err;
1331 /* Search pack index cache. */
1332 for (i = 0; i < repo->pack_cache_size; i++) {
1333 if (repo->packidx_cache[i] == NULL)
1335 if (strcmp(repo->packidx_cache[i]->path_packidx,
1336 path_packidx) == 0) {
1337 *packidx = repo->packidx_cache[i];
1341 /* No luck. Search the filesystem. */
1343 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1348 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1352 err = cache_packidx(repo, *packidx, path_packidx);
1355 got_packidx_close(*packidx);
1361 static const struct got_error *
1362 read_packfile_hdr(int fd, struct got_packidx *packidx)
1364 const struct got_error *err = NULL;
1365 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1366 struct got_packfile_hdr hdr;
1369 n = read(fd, &hdr, sizeof(hdr));
1371 return got_error_from_errno("read");
1372 if (n != sizeof(hdr))
1373 return got_error(GOT_ERR_BAD_PACKFILE);
1375 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1376 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1377 be32toh(hdr.nobjects) != totobj)
1378 err = got_error(GOT_ERR_BAD_PACKFILE);
1383 static const struct got_error *
1384 open_packfile(int *fd, struct got_repository *repo,
1385 const char *relpath, struct got_packidx *packidx)
1387 const struct got_error *err = NULL;
1389 *fd = openat(got_repo_get_fd(repo), relpath,
1390 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1392 return got_error_from_errno_fmt("openat: %s/%s",
1393 got_repo_get_path_git_dir(repo), relpath);
1396 err = read_packfile_hdr(*fd, packidx);
1406 const struct got_error *
1407 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1408 const char *path_packfile, struct got_packidx *packidx)
1410 const struct got_error *err = NULL;
1411 struct got_pack *pack = NULL;
1418 for (i = 0; i < repo->pack_cache_size; i++) {
1419 pack = &repo->packs[i];
1420 if (pack->path_packfile == NULL)
1422 if (strcmp(pack->path_packfile, path_packfile) == 0)
1423 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1426 if (i == repo->pack_cache_size) {
1427 struct got_pack tmp;
1430 } while (i > 0 && repo->pinned_pack >= 0 &&
1431 i == repo->pinned_pack);
1432 err = got_pack_close(&repo->packs[i]);
1435 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1436 return got_error_from_errno("ftruncate");
1437 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1438 return got_error_from_errno("ftruncate");
1439 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1440 memcpy(&repo->packs[i], &repo->packs[0],
1441 sizeof(repo->packs[i]));
1442 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1443 if (repo->pinned_pack == 0)
1444 repo->pinned_pack = i;
1445 else if (repo->pinned_pack == i)
1446 repo->pinned_pack = 0;
1450 pack = &repo->packs[i];
1452 pack->path_packfile = strdup(path_packfile);
1453 if (pack->path_packfile == NULL) {
1454 err = got_error_from_errno("strdup");
1458 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1462 if (fstat(pack->fd, &sb) != 0) {
1463 err = got_error_from_errno("fstat");
1466 pack->filesize = sb.st_size;
1468 pack->privsep_child = NULL;
1470 #ifndef GOT_PACK_NO_MMAP
1471 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1473 if (pack->map == MAP_FAILED) {
1474 if (errno != ENOMEM) {
1475 err = got_error_from_errno("mmap");
1478 pack->map = NULL; /* fall back to read(2) */
1484 free(pack->path_packfile);
1485 memset(pack, 0, sizeof(*pack));
1493 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1495 struct got_pack *pack = NULL;
1498 for (i = 0; i < repo->pack_cache_size; i++) {
1499 pack = &repo->packs[i];
1500 if (pack->path_packfile == NULL)
1502 if (strcmp(pack->path_packfile, path_packfile) == 0)
1509 const struct got_error *
1510 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1511 struct got_pack *pack)
1514 int pinned_pack = -1, pinned_packidx = -1;
1516 for (i = 0; i < repo->pack_cache_size; i++) {
1517 if (repo->packidx_cache[i] &&
1518 strcmp(repo->packidx_cache[i]->path_packidx,
1519 packidx->path_packidx) == 0)
1521 if (repo->packs[i].path_packfile &&
1522 strcmp(repo->packs[i].path_packfile,
1523 pack->path_packfile) == 0)
1527 if (pinned_packidx == -1 || pinned_pack == -1)
1528 return got_error(GOT_ERR_PIN_PACK);
1530 repo->pinned_pack = pinned_pack;
1531 repo->pinned_packidx = pinned_packidx;
1532 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1537 got_repo_get_pinned_pack(struct got_repository *repo)
1539 if (repo->pinned_pack >= 0 &&
1540 repo->pinned_pack < repo->pack_cache_size)
1541 return &repo->packs[repo->pinned_pack];
1547 got_repo_unpin_pack(struct got_repository *repo)
1549 repo->pinned_packidx = -1;
1550 repo->pinned_pack = -1;
1551 repo->pinned_pid = 0;
1554 const struct got_error *
1555 got_repo_init(const char *repo_path)
1557 const struct got_error *err = NULL;
1558 const char *dirnames[] = {
1560 GOT_OBJECTS_PACK_DIR,
1563 const char *description_str = "Unnamed repository; "
1564 "edit this file 'description' to name the repository.";
1565 const char *headref_str = "ref: refs/heads/main";
1566 const char *gitconfig_str = "[core]\n"
1567 "\trepositoryformatversion = 0\n"
1568 "\tfilemode = true\n"
1573 if (!got_path_dir_is_empty(repo_path))
1574 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1576 for (i = 0; i < nitems(dirnames); i++) {
1577 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1578 return got_error_from_errno("asprintf");
1580 err = got_path_mkdir(path);
1586 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1587 return got_error_from_errno("asprintf");
1588 err = got_path_create_file(path, description_str);
1593 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1594 return got_error_from_errno("asprintf");
1595 err = got_path_create_file(path, headref_str);
1600 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1601 return got_error_from_errno("asprintf");
1602 err = got_path_create_file(path, gitconfig_str);
1610 static const struct got_error *
1611 match_packed_object(struct got_object_id **unique_id,
1612 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1614 const struct got_error *err = NULL;
1615 struct got_object_id_queue matched_ids;
1616 struct got_pathlist_entry *pe;
1618 STAILQ_INIT(&matched_ids);
1620 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1621 const char *path_packidx = pe->path;
1622 struct got_packidx *packidx;
1623 struct got_object_qid *qid;
1625 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1630 err = got_packidx_match_id_str_prefix(&matched_ids,
1631 packidx, id_str_prefix);
1633 got_packidx_close(packidx);
1636 err = got_packidx_close(packidx);
1640 STAILQ_FOREACH(qid, &matched_ids, entry) {
1641 if (obj_type != GOT_OBJ_TYPE_ANY) {
1643 err = got_object_get_type(&matched_type, repo,
1647 if (matched_type != obj_type)
1650 if (*unique_id == NULL) {
1651 *unique_id = got_object_id_dup(&qid->id);
1652 if (*unique_id == NULL) {
1653 err = got_error_from_errno("malloc");
1657 if (got_object_id_cmp(*unique_id,
1659 continue; /* packed multiple times */
1660 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1666 got_object_id_queue_free(&matched_ids);
1674 static const struct got_error *
1675 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1676 const char *object_dir, const char *id_str_prefix, int obj_type,
1677 struct got_repository *repo)
1679 const struct got_error *err = NULL;
1682 struct dirent *dent;
1683 struct got_object_id id;
1685 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1686 err = got_error_from_errno("asprintf");
1690 dir = opendir(path);
1692 if (errno == ENOENT) {
1696 err = got_error_from_errno2("opendir", path);
1699 while ((dent = readdir(dir)) != NULL) {
1703 if (strcmp(dent->d_name, ".") == 0 ||
1704 strcmp(dent->d_name, "..") == 0)
1707 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1708 err = got_error_from_errno("asprintf");
1712 if (!got_parse_sha1_digest(id.sha1, id_str))
1716 * Directory entries do not necessarily appear in
1717 * sorted order, so we must iterate over all of them.
1719 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1725 if (*unique_id == NULL) {
1726 if (obj_type != GOT_OBJ_TYPE_ANY) {
1728 err = got_object_get_type(&matched_type, repo,
1732 if (matched_type != obj_type)
1735 *unique_id = got_object_id_dup(&id);
1736 if (*unique_id == NULL) {
1737 err = got_error_from_errno("got_object_id_dup");
1742 if (got_object_id_cmp(*unique_id, &id) == 0)
1743 continue; /* both packed and loose */
1744 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1750 if (dir && closedir(dir) != 0 && err == NULL)
1751 err = got_error_from_errno("closedir");
1760 const struct got_error *
1761 got_repo_match_object_id_prefix(struct got_object_id **id,
1762 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1764 const struct got_error *err = NULL;
1765 char *path_objects = got_repo_get_path_objects(repo);
1766 char *object_dir = NULL;
1772 len = strlen(id_str_prefix);
1773 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1774 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1776 for (i = 0; i < len; i++) {
1777 if (isxdigit((unsigned char)id_str_prefix[i]))
1779 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1783 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1786 object_dir = strndup(id_str_prefix, 2);
1787 if (object_dir == NULL) {
1788 err = got_error_from_errno("strdup");
1791 err = match_loose_object(id, path_objects, object_dir,
1792 id_str_prefix, obj_type, repo);
1793 } else if (len == 1) {
1795 for (i = 0; i < 0xf; i++) {
1796 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1798 err = got_error_from_errno("asprintf");
1801 err = match_packed_object(id, repo, object_dir,
1805 err = match_loose_object(id, path_objects, object_dir,
1806 id_str_prefix, obj_type, repo);
1811 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1819 } else if (*id == NULL) {
1821 case GOT_OBJ_TYPE_BLOB:
1822 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1823 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1825 case GOT_OBJ_TYPE_TREE:
1826 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1827 GOT_OBJ_LABEL_TREE, id_str_prefix);
1829 case GOT_OBJ_TYPE_COMMIT:
1830 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1831 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1833 case GOT_OBJ_TYPE_TAG:
1834 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1835 GOT_OBJ_LABEL_TAG, id_str_prefix);
1838 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1846 const struct got_error *
1847 got_repo_match_object_id(struct got_object_id **id, char **label,
1848 const char *id_str, int obj_type, struct got_reflist_head *refs,
1849 struct got_repository *repo)
1851 const struct got_error *err;
1852 struct got_tag_object *tag;
1853 struct got_reference *ref = NULL;
1860 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1863 *id = got_object_id_dup(
1864 got_object_tag_get_object_id(tag));
1866 err = got_error_from_errno("got_object_id_dup");
1867 else if (label && asprintf(label, "refs/tags/%s",
1868 got_object_tag_get_name(tag)) == -1) {
1869 err = got_error_from_errno("asprintf");
1873 got_object_tag_close(tag);
1875 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1876 err->code != GOT_ERR_NO_OBJ)
1880 err = got_ref_open(&ref, repo, id_str, 0);
1882 err = got_ref_resolve(id, repo, ref);
1886 *label = strdup(got_ref_get_name(ref));
1887 if (*label == NULL) {
1888 err = got_error_from_errno("strdup");
1893 if (err->code != GOT_ERR_NOT_REF &&
1894 err->code != GOT_ERR_BAD_REF_NAME)
1896 err = got_repo_match_object_id_prefix(id, id_str,
1899 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1900 err = got_error_not_ref(id_str);
1904 err = got_object_id_str(label, *id);
1905 if (*label == NULL) {
1906 err = got_error_from_errno("strdup");
1917 const struct got_error *
1918 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1919 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1921 const struct got_error *err = NULL;
1922 struct got_reflist_entry *re;
1923 struct got_object_id *tag_id;
1924 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1928 TAILQ_FOREACH(re, refs, entry) {
1929 const char *refname;
1930 refname = got_ref_get_name(re->ref);
1931 if (got_ref_is_symbolic(re->ref))
1933 if (strncmp(refname, "refs/tags/", 10) != 0)
1935 if (!name_is_absolute)
1936 refname += strlen("refs/tags/");
1937 if (strcmp(refname, name) != 0)
1939 err = got_ref_resolve(&tag_id, repo, re->ref);
1942 err = got_object_open_as_tag(tag, repo, tag_id);
1946 if (obj_type == GOT_OBJ_TYPE_ANY ||
1947 got_object_tag_get_object_type(*tag) == obj_type)
1949 got_object_tag_close(*tag);
1953 if (err == NULL && *tag == NULL)
1954 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1955 GOT_OBJ_LABEL_TAG, name);
1959 static const struct got_error *
1960 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1961 const char *name, mode_t mode, struct got_object_id *blob_id)
1963 const struct got_error *err = NULL;
1967 *new_te = calloc(1, sizeof(**new_te));
1968 if (*new_te == NULL)
1969 return got_error_from_errno("calloc");
1971 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1972 sizeof((*new_te)->name)) {
1973 err = got_error(GOT_ERR_NO_SPACE);
1977 if (S_ISLNK(mode)) {
1978 (*new_te)->mode = S_IFLNK;
1980 (*new_te)->mode = S_IFREG;
1981 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1983 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1985 if (err && *new_te) {
1992 static const struct got_error *
1993 import_file(struct got_tree_entry **new_te, struct dirent *de,
1994 const char *path, struct got_repository *repo)
1996 const struct got_error *err;
1997 struct got_object_id *blob_id = NULL;
2001 if (asprintf(&filepath, "%s%s%s", path,
2002 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2003 return got_error_from_errno("asprintf");
2005 if (lstat(filepath, &sb) != 0) {
2006 err = got_error_from_errno2("lstat", path);
2010 err = got_object_blob_create(&blob_id, filepath, repo);
2014 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2023 static const struct got_error *
2024 insert_tree_entry(struct got_tree_entry *new_te,
2025 struct got_pathlist_head *paths)
2027 const struct got_error *err = NULL;
2028 struct got_pathlist_entry *new_pe;
2030 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2034 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2038 static const struct got_error *write_tree(struct got_object_id **,
2039 const char *, struct got_pathlist_head *, struct got_repository *,
2040 got_repo_import_cb progress_cb, void *progress_arg);
2042 static const struct got_error *
2043 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2044 const char *path, struct got_pathlist_head *ignores,
2045 struct got_repository *repo,
2046 got_repo_import_cb progress_cb, void *progress_arg)
2048 const struct got_error *err;
2049 struct got_object_id *id = NULL;
2052 if (asprintf(&subdirpath, "%s%s%s", path,
2053 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2054 return got_error_from_errno("asprintf");
2056 (*new_te) = calloc(1, sizeof(**new_te));
2057 if (*new_te == NULL)
2058 return got_error_from_errno("calloc");
2059 (*new_te)->mode = S_IFDIR;
2060 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2061 sizeof((*new_te)->name)) {
2062 err = got_error(GOT_ERR_NO_SPACE);
2065 err = write_tree(&id, subdirpath, ignores, repo,
2066 progress_cb, progress_arg);
2069 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2081 static const struct got_error *
2082 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2083 struct got_pathlist_head *ignores, struct got_repository *repo,
2084 got_repo_import_cb progress_cb, void *progress_arg)
2086 const struct got_error *err = NULL;
2090 struct got_tree_entry *new_te = NULL;
2091 struct got_pathlist_head paths;
2092 struct got_pathlist_entry *pe;
2094 *new_tree_id = NULL;
2098 dir = opendir(path_dir);
2100 err = got_error_from_errno2("opendir", path_dir);
2105 while ((de = readdir(dir)) != NULL) {
2109 if (strcmp(de->d_name, ".") == 0 ||
2110 strcmp(de->d_name, "..") == 0)
2113 TAILQ_FOREACH(pe, ignores, entry) {
2114 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2122 err = got_path_dirent_type(&type, path_dir, de);
2126 if (type == DT_DIR) {
2127 err = import_subdir(&new_te, de, path_dir,
2128 ignores, repo, progress_cb, progress_arg);
2130 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2135 } else if (type == DT_REG || type == DT_LNK) {
2136 err = import_file(&new_te, de, path_dir, repo);
2142 err = insert_tree_entry(new_te, &paths);
2148 if (TAILQ_EMPTY(&paths)) {
2149 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2150 "cannot create tree without any entries");
2154 TAILQ_FOREACH(pe, &paths, entry) {
2155 struct got_tree_entry *te = pe->data;
2157 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2159 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2160 err = got_error_from_errno("asprintf");
2163 err = (*progress_cb)(progress_arg, path);
2169 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2173 got_pathlist_free(&paths);
2177 const struct got_error *
2178 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2179 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2180 struct got_repository *repo, got_repo_import_cb progress_cb,
2183 const struct got_error *err;
2184 struct got_object_id *new_tree_id;
2186 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2187 progress_cb, progress_arg);
2191 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2192 author, time(NULL), author, time(NULL), logmsg, repo);
2197 const struct got_error *
2198 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2199 struct got_repository *repo)
2201 const struct got_error *err = NULL;
2202 char *path_objects = NULL, *path = NULL;
2204 struct got_object_id id;
2210 path_objects = got_repo_get_path_objects(repo);
2211 if (path_objects == NULL)
2212 return got_error_from_errno("got_repo_get_path_objects");
2214 for (i = 0; i <= 0xff; i++) {
2215 struct dirent *dent;
2217 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2218 err = got_error_from_errno("asprintf");
2222 dir = opendir(path);
2224 if (errno == ENOENT) {
2228 err = got_error_from_errno2("opendir", path);
2232 while ((dent = readdir(dir)) != NULL) {
2237 if (strcmp(dent->d_name, ".") == 0 ||
2238 strcmp(dent->d_name, "..") == 0)
2241 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2242 err = got_error_from_errno("asprintf");
2246 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2252 err = got_object_open_loose_fd(&fd, &id, repo);
2256 if (fstat(fd, &sb) == -1) {
2257 err = got_error_from_errno("fstat");
2262 (*ondisk_size) += sb.st_size;
2264 if (close(fd) == -1) {
2265 err = got_error_from_errno("close");
2270 if (closedir(dir) != 0) {
2271 err = got_error_from_errno("closedir");
2280 if (dir && closedir(dir) != 0 && err == NULL)
2281 err = got_error_from_errno("closedir");
2292 const struct got_error *
2293 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2294 off_t *total_packsize, struct got_repository *repo)
2296 const struct got_error *err = NULL;
2297 DIR *packdir = NULL;
2298 struct dirent *dent;
2299 struct got_packidx *packidx = NULL;
2301 char *path_packfile;
2307 *total_packsize = 0;
2309 packdir_fd = openat(got_repo_get_fd(repo),
2310 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2311 if (packdir_fd == -1) {
2312 return got_error_from_errno_fmt("openat: %s/%s",
2313 got_repo_get_path_git_dir(repo),
2314 GOT_OBJECTS_PACK_DIR);
2317 packdir = fdopendir(packdir_fd);
2318 if (packdir == NULL) {
2319 err = got_error_from_errno("fdopendir");
2323 while ((dent = readdir(packdir)) != NULL) {
2324 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2327 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2328 dent->d_name) == -1) {
2329 err = got_error_from_errno("asprintf");
2333 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2339 if (fstat(packidx->fd, &sb) == -1)
2341 *total_packsize += sb.st_size;
2343 err = got_packidx_get_packfile_path(&path_packfile,
2344 packidx->path_packidx);
2348 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2350 free(path_packfile);
2353 free(path_packfile);
2354 *total_packsize += sb.st_size;
2356 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2360 got_packidx_close(packidx);
2365 got_packidx_close(packidx);
2366 if (packdir && closedir(packdir) != 0 && err == NULL)
2367 err = got_error_from_errno("closedir");
2371 *total_packsize = 0;
2376 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2377 got_packidx_bloom_filter_cmp);