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);
875 bloom_free(bf->bloom);
880 for (i = 0; i < repo->pack_cache_size; i++)
881 if (repo->packs[i].path_packfile)
882 if (repo->packs[i].path_packfile)
883 got_pack_close(&repo->packs[i]);
886 free(repo->path_git_dir);
888 got_object_cache_close(&repo->objcache);
889 got_object_cache_close(&repo->treecache);
890 got_object_cache_close(&repo->commitcache);
891 got_object_cache_close(&repo->tagcache);
892 got_object_cache_close(&repo->rawcache);
894 for (i = 0; i < nitems(repo->privsep_children); i++) {
895 if (repo->privsep_children[i].imsg_fd == -1)
897 imsg_clear(repo->privsep_children[i].ibuf);
898 free(repo->privsep_children[i].ibuf);
899 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
900 child_err = got_privsep_wait_for_child(
901 repo->privsep_children[i].pid);
902 if (child_err && err == NULL)
904 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
906 err = got_error_from_errno("close");
909 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
911 err = got_error_from_errno("close");
914 got_gotconfig_free(repo->gotconfig);
915 free(repo->gitconfig_author_name);
916 free(repo->gitconfig_author_email);
917 for (i = 0; i < repo->ngitconfig_remotes; i++)
918 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
919 free(repo->gitconfig_remotes);
920 for (i = 0; i < repo->nextensions; i++)
921 free(repo->extensions[i]);
922 free(repo->extensions);
924 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
925 free((void *)pe->path);
926 got_pathlist_free(&repo->packidx_paths);
933 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
939 free(repo->fetch_url);
940 repo->fetch_url = NULL;
941 free(repo->send_url);
942 repo->send_url = NULL;
943 for (i = 0; i < repo->nfetch_branches; i++)
944 free(repo->fetch_branches[i]);
945 free(repo->fetch_branches);
946 repo->fetch_branches = NULL;
947 repo->nfetch_branches = 0;
948 for (i = 0; i < repo->nsend_branches; i++)
949 free(repo->send_branches[i]);
950 free(repo->send_branches);
951 repo->send_branches = NULL;
952 repo->nsend_branches = 0;
955 const struct got_error *
956 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
957 const char *input_path)
959 const struct got_error *err = NULL;
960 const char *repo_abspath = NULL;
962 char *canonpath, *path = NULL;
964 *in_repo_path = NULL;
966 canonpath = strdup(input_path);
967 if (canonpath == NULL) {
968 err = got_error_from_errno("strdup");
971 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
975 repo_abspath = got_repo_get_path(repo);
977 if (canonpath[0] == '\0') {
978 path = strdup(canonpath);
980 err = got_error_from_errno("strdup");
984 path = realpath(canonpath, NULL);
986 if (errno != ENOENT) {
987 err = got_error_from_errno2("realpath",
992 * Path is not on disk.
993 * Assume it is already relative to repository root.
995 path = strdup(canonpath);
997 err = got_error_from_errno("strdup");
1002 repolen = strlen(repo_abspath);
1006 if (strcmp(path, repo_abspath) == 0) {
1010 err = got_error_from_errno("strdup");
1013 } else if (len > repolen &&
1014 got_path_is_child(path, repo_abspath, repolen)) {
1015 /* Matched an on-disk path inside repository. */
1016 if (got_repo_is_bare(repo)) {
1018 * Matched an on-disk path inside repository
1019 * database. Treat input as repository-relative.
1026 /* Strip common prefix with repository path. */
1027 err = got_path_skip_common_ancestor(&child,
1028 repo_abspath, path);
1036 * Matched unrelated on-disk path.
1037 * Treat input as repository-relative.
1045 /* Make in-repository path absolute */
1046 if (path[0] != '/') {
1048 if (asprintf(&abspath, "/%s", path) == -1) {
1049 err = got_error_from_errno("asprintf");
1061 *in_repo_path = path;
1065 static const struct got_error *
1066 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1067 const char *path_packidx)
1069 const struct got_error *err = NULL;
1072 for (i = 0; i < repo->pack_cache_size; i++) {
1073 if (repo->packidx_cache[i] == NULL)
1075 if (strcmp(repo->packidx_cache[i]->path_packidx,
1076 path_packidx) == 0) {
1077 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1080 if (i == repo->pack_cache_size) {
1083 } while (i > 0 && repo->pinned_packidx >= 0 &&
1084 i == repo->pinned_packidx);
1085 err = got_packidx_close(repo->packidx_cache[i]);
1090 repo->packidx_cache[i] = packidx;
1096 got_repo_is_packidx_filename(const char *name, size_t len)
1098 if (len != GOT_PACKIDX_NAMELEN)
1101 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1104 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1105 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1111 static struct got_packidx_bloom_filter *
1112 get_packidx_bloom_filter(struct got_repository *repo,
1113 const char *path, size_t path_len)
1115 struct got_packidx_bloom_filter key;
1117 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1118 return NULL; /* XXX */
1119 key.path_len = path_len;
1121 return RB_FIND(got_packidx_bloom_filter_tree,
1122 &repo->packidx_bloom_filters, &key);
1126 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1127 const char *path_packidx, struct got_object_id *id)
1129 struct got_packidx_bloom_filter *bf;
1131 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1133 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1135 /* No bloom filter means this pack index must be searched. */
1139 static const struct got_error *
1140 add_packidx_bloom_filter(struct got_repository *repo,
1141 struct got_packidx *packidx, const char *path_packidx)
1143 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1144 struct got_packidx_bloom_filter *bf;
1148 * Don't use bloom filters for very large pack index files.
1149 * Large pack files will contain a relatively large fraction
1150 * of our objects so we will likely need to visit them anyway.
1151 * The more objects a pack file contains the higher the probability
1152 * of a false-positive match from the bloom filter. And reading
1153 * all object IDs from a large pack index file can be expensive.
1155 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1158 /* Do we already have a filter for this pack index? */
1159 if (get_packidx_bloom_filter(repo, path_packidx,
1160 strlen(path_packidx)) != NULL)
1163 bf = calloc(1, sizeof(*bf));
1165 return got_error_from_errno("calloc");
1166 bf->bloom = calloc(1, sizeof(*bf->bloom));
1167 if (bf->bloom == NULL) {
1169 return got_error_from_errno("calloc");
1172 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1173 if (len >= sizeof(bf->path)) {
1176 return got_error(GOT_ERR_NO_SPACE);
1180 /* Minimum size supported by our bloom filter is 1000 entries. */
1181 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1182 for (i = 0; i < nobjects; i++) {
1183 struct got_packidx_object_id *id;
1184 id = &packidx->hdr.sorted_ids[i];
1185 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1188 RB_INSERT(got_packidx_bloom_filter_tree,
1189 &repo->packidx_bloom_filters, bf);
1193 const struct got_error *
1194 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1195 struct got_repository *repo, struct got_object_id *id)
1197 const struct got_error *err;
1198 struct got_pathlist_entry *pe;
1201 /* Search pack index cache. */
1202 for (i = 0; i < repo->pack_cache_size; i++) {
1203 if (repo->packidx_cache[i] == NULL)
1205 if (!got_repo_check_packidx_bloom_filter(repo,
1206 repo->packidx_cache[i]->path_packidx, id))
1207 continue; /* object will not be found in this index */
1208 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1210 *packidx = repo->packidx_cache[i];
1212 * Move this cache entry to the front. Repeatedly
1213 * searching a wrong pack index can be expensive.
1216 memmove(&repo->packidx_cache[1],
1217 &repo->packidx_cache[0],
1218 i * sizeof(repo->packidx_cache[0]));
1219 repo->packidx_cache[0] = *packidx;
1220 if (repo->pinned_packidx >= 0 &&
1221 repo->pinned_packidx < i)
1222 repo->pinned_packidx++;
1223 else if (repo->pinned_packidx == i)
1224 repo->pinned_packidx = 0;
1229 /* No luck. Search the filesystem. */
1231 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1232 const char *path_packidx = pe->path;
1235 if (!got_repo_check_packidx_bloom_filter(repo,
1237 continue; /* object will not be found in this index */
1239 for (i = 0; i < repo->pack_cache_size; i++) {
1240 if (repo->packidx_cache[i] == NULL)
1242 if (strcmp(repo->packidx_cache[i]->path_packidx,
1243 path_packidx) == 0) {
1249 continue; /* already searched */
1251 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1256 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1260 err = cache_packidx(repo, *packidx, path_packidx);
1264 *idx = got_packidx_get_object_idx(*packidx, id);
1266 err = NULL; /* found the object */
1271 err = got_error_no_obj(id);
1276 const struct got_error *
1277 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1278 struct got_repository *repo)
1280 const struct got_error *err = NULL;
1281 DIR *packdir = NULL;
1282 struct dirent *dent;
1283 char *path_packidx = NULL;
1287 packdir_fd = openat(got_repo_get_fd(repo),
1288 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1289 if (packdir_fd == -1) {
1290 return got_error_from_errno_fmt("openat: %s/%s",
1291 got_repo_get_path_git_dir(repo),
1292 GOT_OBJECTS_PACK_DIR);
1295 packdir = fdopendir(packdir_fd);
1296 if (packdir == NULL) {
1297 err = got_error_from_errno("fdopendir");
1301 if (fstat(packdir_fd, &sb) == -1) {
1302 err = got_error_from_errno("fstat");
1305 repo->pack_path_mtime = sb.st_mtime;
1307 while ((dent = readdir(packdir)) != NULL) {
1308 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1311 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1312 dent->d_name) == -1) {
1313 err = got_error_from_errno("asprintf");
1314 path_packidx = NULL;
1318 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1325 if (packdir && closedir(packdir) != 0 && err == NULL)
1326 err = got_error_from_errno("closedir");
1330 const struct got_error *
1331 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1332 struct got_repository *repo)
1334 const struct got_error *err;
1339 /* Search pack index cache. */
1340 for (i = 0; i < repo->pack_cache_size; i++) {
1341 if (repo->packidx_cache[i] == NULL)
1343 if (strcmp(repo->packidx_cache[i]->path_packidx,
1344 path_packidx) == 0) {
1345 *packidx = repo->packidx_cache[i];
1349 /* No luck. Search the filesystem. */
1351 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1356 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1360 err = cache_packidx(repo, *packidx, path_packidx);
1363 got_packidx_close(*packidx);
1369 static const struct got_error *
1370 read_packfile_hdr(int fd, struct got_packidx *packidx)
1372 const struct got_error *err = NULL;
1373 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1374 struct got_packfile_hdr hdr;
1377 n = read(fd, &hdr, sizeof(hdr));
1379 return got_error_from_errno("read");
1380 if (n != sizeof(hdr))
1381 return got_error(GOT_ERR_BAD_PACKFILE);
1383 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1384 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1385 be32toh(hdr.nobjects) != totobj)
1386 err = got_error(GOT_ERR_BAD_PACKFILE);
1391 static const struct got_error *
1392 open_packfile(int *fd, struct got_repository *repo,
1393 const char *relpath, struct got_packidx *packidx)
1395 const struct got_error *err = NULL;
1397 *fd = openat(got_repo_get_fd(repo), relpath,
1398 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1400 return got_error_from_errno_fmt("openat: %s/%s",
1401 got_repo_get_path_git_dir(repo), relpath);
1404 err = read_packfile_hdr(*fd, packidx);
1414 const struct got_error *
1415 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1416 const char *path_packfile, struct got_packidx *packidx)
1418 const struct got_error *err = NULL;
1419 struct got_pack *pack = NULL;
1426 for (i = 0; i < repo->pack_cache_size; i++) {
1427 pack = &repo->packs[i];
1428 if (pack->path_packfile == NULL)
1430 if (strcmp(pack->path_packfile, path_packfile) == 0)
1431 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1434 if (i == repo->pack_cache_size) {
1435 struct got_pack tmp;
1438 } while (i > 0 && repo->pinned_pack >= 0 &&
1439 i == repo->pinned_pack);
1440 err = got_pack_close(&repo->packs[i]);
1443 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1444 return got_error_from_errno("ftruncate");
1445 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1446 return got_error_from_errno("ftruncate");
1447 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1448 memcpy(&repo->packs[i], &repo->packs[0],
1449 sizeof(repo->packs[i]));
1450 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1451 if (repo->pinned_pack == 0)
1452 repo->pinned_pack = i;
1453 else if (repo->pinned_pack == i)
1454 repo->pinned_pack = 0;
1458 pack = &repo->packs[i];
1460 pack->path_packfile = strdup(path_packfile);
1461 if (pack->path_packfile == NULL) {
1462 err = got_error_from_errno("strdup");
1466 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1470 if (fstat(pack->fd, &sb) != 0) {
1471 err = got_error_from_errno("fstat");
1474 pack->filesize = sb.st_size;
1476 pack->privsep_child = NULL;
1478 #ifndef GOT_PACK_NO_MMAP
1479 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1481 if (pack->map == MAP_FAILED) {
1482 if (errno != ENOMEM) {
1483 err = got_error_from_errno("mmap");
1486 pack->map = NULL; /* fall back to read(2) */
1492 free(pack->path_packfile);
1493 memset(pack, 0, sizeof(*pack));
1501 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1503 struct got_pack *pack = NULL;
1506 for (i = 0; i < repo->pack_cache_size; i++) {
1507 pack = &repo->packs[i];
1508 if (pack->path_packfile == NULL)
1510 if (strcmp(pack->path_packfile, path_packfile) == 0)
1517 const struct got_error *
1518 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1519 struct got_pack *pack)
1522 int pinned_pack = -1, pinned_packidx = -1;
1524 for (i = 0; i < repo->pack_cache_size; i++) {
1525 if (repo->packidx_cache[i] &&
1526 strcmp(repo->packidx_cache[i]->path_packidx,
1527 packidx->path_packidx) == 0)
1529 if (repo->packs[i].path_packfile &&
1530 strcmp(repo->packs[i].path_packfile,
1531 pack->path_packfile) == 0)
1535 if (pinned_packidx == -1 || pinned_pack == -1)
1536 return got_error(GOT_ERR_PIN_PACK);
1538 repo->pinned_pack = pinned_pack;
1539 repo->pinned_packidx = pinned_packidx;
1540 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1545 got_repo_get_pinned_pack(struct got_repository *repo)
1547 if (repo->pinned_pack >= 0 &&
1548 repo->pinned_pack < repo->pack_cache_size)
1549 return &repo->packs[repo->pinned_pack];
1555 got_repo_unpin_pack(struct got_repository *repo)
1557 repo->pinned_packidx = -1;
1558 repo->pinned_pack = -1;
1559 repo->pinned_pid = 0;
1562 const struct got_error *
1563 got_repo_init(const char *repo_path)
1565 const struct got_error *err = NULL;
1566 const char *dirnames[] = {
1568 GOT_OBJECTS_PACK_DIR,
1571 const char *description_str = "Unnamed repository; "
1572 "edit this file 'description' to name the repository.";
1573 const char *headref_str = "ref: refs/heads/main";
1574 const char *gitconfig_str = "[core]\n"
1575 "\trepositoryformatversion = 0\n"
1576 "\tfilemode = true\n"
1581 if (!got_path_dir_is_empty(repo_path))
1582 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1584 for (i = 0; i < nitems(dirnames); i++) {
1585 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1586 return got_error_from_errno("asprintf");
1588 err = got_path_mkdir(path);
1594 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1595 return got_error_from_errno("asprintf");
1596 err = got_path_create_file(path, description_str);
1601 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1602 return got_error_from_errno("asprintf");
1603 err = got_path_create_file(path, headref_str);
1608 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1609 return got_error_from_errno("asprintf");
1610 err = got_path_create_file(path, gitconfig_str);
1619 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1621 struct got_pathlist_entry *pe;
1623 while (!TAILQ_EMPTY(packidx_paths)) {
1624 pe = TAILQ_FIRST(packidx_paths);
1625 TAILQ_REMOVE(packidx_paths, pe, entry);
1626 free((char *)pe->path);
1631 static const struct got_error *
1632 match_packed_object(struct got_object_id **unique_id,
1633 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1635 const struct got_error *err = NULL;
1636 struct got_object_id_queue matched_ids;
1637 struct got_pathlist_entry *pe;
1640 STAILQ_INIT(&matched_ids);
1642 if (stat(got_repo_get_path_objects_pack(repo), &sb) == -1) {
1643 if (errno != ENOENT)
1644 return got_error_from_errno2("stat",
1645 got_repo_get_path_objects_pack(repo));
1646 } else if (sb.st_mtime != repo->pack_path_mtime) {
1647 purge_packidx_paths(&repo->packidx_paths);
1648 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1653 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1654 const char *path_packidx = pe->path;
1655 struct got_packidx *packidx;
1656 struct got_object_qid *qid;
1658 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1663 err = got_packidx_match_id_str_prefix(&matched_ids,
1664 packidx, id_str_prefix);
1666 got_packidx_close(packidx);
1669 err = got_packidx_close(packidx);
1673 STAILQ_FOREACH(qid, &matched_ids, entry) {
1674 if (obj_type != GOT_OBJ_TYPE_ANY) {
1676 err = got_object_get_type(&matched_type, repo,
1680 if (matched_type != obj_type)
1683 if (*unique_id == NULL) {
1684 *unique_id = got_object_id_dup(&qid->id);
1685 if (*unique_id == NULL) {
1686 err = got_error_from_errno("malloc");
1690 if (got_object_id_cmp(*unique_id,
1692 continue; /* packed multiple times */
1693 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1699 got_object_id_queue_free(&matched_ids);
1707 static const struct got_error *
1708 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1709 const char *object_dir, const char *id_str_prefix, int obj_type,
1710 struct got_repository *repo)
1712 const struct got_error *err = NULL;
1713 char *path, *id_str = NULL;
1715 struct dirent *dent;
1716 struct got_object_id id;
1718 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1719 err = got_error_from_errno("asprintf");
1723 dir = opendir(path);
1725 if (errno == ENOENT) {
1729 err = got_error_from_errno2("opendir", path);
1732 while ((dent = readdir(dir)) != NULL) {
1738 if (strcmp(dent->d_name, ".") == 0 ||
1739 strcmp(dent->d_name, "..") == 0)
1742 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1743 err = got_error_from_errno("asprintf");
1747 if (!got_parse_sha1_digest(id.sha1, id_str))
1751 * Directory entries do not necessarily appear in
1752 * sorted order, so we must iterate over all of them.
1754 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1758 if (*unique_id == NULL) {
1759 if (obj_type != GOT_OBJ_TYPE_ANY) {
1761 err = got_object_get_type(&matched_type, repo,
1765 if (matched_type != obj_type)
1768 *unique_id = got_object_id_dup(&id);
1769 if (*unique_id == NULL) {
1770 err = got_error_from_errno("got_object_id_dup");
1774 if (got_object_id_cmp(*unique_id, &id) == 0)
1775 continue; /* both packed and loose */
1776 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1781 if (dir && closedir(dir) != 0 && err == NULL)
1782 err = got_error_from_errno("closedir");
1792 const struct got_error *
1793 got_repo_match_object_id_prefix(struct got_object_id **id,
1794 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1796 const struct got_error *err = NULL;
1797 char *path_objects = got_repo_get_path_objects(repo);
1798 char *object_dir = NULL;
1804 len = strlen(id_str_prefix);
1805 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1806 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1808 for (i = 0; i < len; i++) {
1809 if (isxdigit((unsigned char)id_str_prefix[i]))
1811 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1815 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1818 object_dir = strndup(id_str_prefix, 2);
1819 if (object_dir == NULL) {
1820 err = got_error_from_errno("strdup");
1823 err = match_loose_object(id, path_objects, object_dir,
1824 id_str_prefix, obj_type, repo);
1825 } else if (len == 1) {
1827 for (i = 0; i < 0xf; i++) {
1828 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1830 err = got_error_from_errno("asprintf");
1833 err = match_packed_object(id, repo, object_dir,
1837 err = match_loose_object(id, path_objects, object_dir,
1838 id_str_prefix, obj_type, repo);
1843 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1851 } else if (*id == NULL) {
1853 case GOT_OBJ_TYPE_BLOB:
1854 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1855 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1857 case GOT_OBJ_TYPE_TREE:
1858 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1859 GOT_OBJ_LABEL_TREE, id_str_prefix);
1861 case GOT_OBJ_TYPE_COMMIT:
1862 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1863 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1865 case GOT_OBJ_TYPE_TAG:
1866 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1867 GOT_OBJ_LABEL_TAG, id_str_prefix);
1870 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1878 const struct got_error *
1879 got_repo_match_object_id(struct got_object_id **id, char **label,
1880 const char *id_str, int obj_type, struct got_reflist_head *refs,
1881 struct got_repository *repo)
1883 const struct got_error *err;
1884 struct got_tag_object *tag;
1885 struct got_reference *ref = NULL;
1892 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1895 *id = got_object_id_dup(
1896 got_object_tag_get_object_id(tag));
1898 err = got_error_from_errno("got_object_id_dup");
1899 else if (label && asprintf(label, "refs/tags/%s",
1900 got_object_tag_get_name(tag)) == -1) {
1901 err = got_error_from_errno("asprintf");
1905 got_object_tag_close(tag);
1907 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1908 err->code != GOT_ERR_NO_OBJ)
1912 err = got_ref_open(&ref, repo, id_str, 0);
1914 err = got_ref_resolve(id, repo, ref);
1918 *label = strdup(got_ref_get_name(ref));
1919 if (*label == NULL) {
1920 err = got_error_from_errno("strdup");
1925 if (err->code != GOT_ERR_NOT_REF &&
1926 err->code != GOT_ERR_BAD_REF_NAME)
1928 err = got_repo_match_object_id_prefix(id, id_str,
1931 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1932 err = got_error_not_ref(id_str);
1936 err = got_object_id_str(label, *id);
1937 if (*label == NULL) {
1938 err = got_error_from_errno("strdup");
1949 const struct got_error *
1950 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1951 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1953 const struct got_error *err = NULL;
1954 struct got_reflist_entry *re;
1955 struct got_object_id *tag_id;
1956 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1960 TAILQ_FOREACH(re, refs, entry) {
1961 const char *refname;
1962 refname = got_ref_get_name(re->ref);
1963 if (got_ref_is_symbolic(re->ref))
1965 if (strncmp(refname, "refs/tags/", 10) != 0)
1967 if (!name_is_absolute)
1968 refname += strlen("refs/tags/");
1969 if (strcmp(refname, name) != 0)
1971 err = got_ref_resolve(&tag_id, repo, re->ref);
1974 err = got_object_open_as_tag(tag, repo, tag_id);
1978 if (obj_type == GOT_OBJ_TYPE_ANY ||
1979 got_object_tag_get_object_type(*tag) == obj_type)
1981 got_object_tag_close(*tag);
1985 if (err == NULL && *tag == NULL)
1986 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1987 GOT_OBJ_LABEL_TAG, name);
1991 static const struct got_error *
1992 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1993 const char *name, mode_t mode, struct got_object_id *blob_id)
1995 const struct got_error *err = NULL;
1999 *new_te = calloc(1, sizeof(**new_te));
2000 if (*new_te == NULL)
2001 return got_error_from_errno("calloc");
2003 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2004 sizeof((*new_te)->name)) {
2005 err = got_error(GOT_ERR_NO_SPACE);
2009 if (S_ISLNK(mode)) {
2010 (*new_te)->mode = S_IFLNK;
2012 (*new_te)->mode = S_IFREG;
2013 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2015 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2017 if (err && *new_te) {
2024 static const struct got_error *
2025 import_file(struct got_tree_entry **new_te, struct dirent *de,
2026 const char *path, struct got_repository *repo)
2028 const struct got_error *err;
2029 struct got_object_id *blob_id = NULL;
2033 if (asprintf(&filepath, "%s%s%s", path,
2034 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2035 return got_error_from_errno("asprintf");
2037 if (lstat(filepath, &sb) != 0) {
2038 err = got_error_from_errno2("lstat", path);
2042 err = got_object_blob_create(&blob_id, filepath, repo);
2046 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2055 static const struct got_error *
2056 insert_tree_entry(struct got_tree_entry *new_te,
2057 struct got_pathlist_head *paths)
2059 const struct got_error *err = NULL;
2060 struct got_pathlist_entry *new_pe;
2062 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2066 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2070 static const struct got_error *write_tree(struct got_object_id **,
2071 const char *, struct got_pathlist_head *, struct got_repository *,
2072 got_repo_import_cb progress_cb, void *progress_arg);
2074 static const struct got_error *
2075 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2076 const char *path, struct got_pathlist_head *ignores,
2077 struct got_repository *repo,
2078 got_repo_import_cb progress_cb, void *progress_arg)
2080 const struct got_error *err;
2081 struct got_object_id *id = NULL;
2084 if (asprintf(&subdirpath, "%s%s%s", path,
2085 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2086 return got_error_from_errno("asprintf");
2088 (*new_te) = calloc(1, sizeof(**new_te));
2089 if (*new_te == NULL)
2090 return got_error_from_errno("calloc");
2091 (*new_te)->mode = S_IFDIR;
2092 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2093 sizeof((*new_te)->name)) {
2094 err = got_error(GOT_ERR_NO_SPACE);
2097 err = write_tree(&id, subdirpath, ignores, repo,
2098 progress_cb, progress_arg);
2101 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2113 static const struct got_error *
2114 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2115 struct got_pathlist_head *ignores, struct got_repository *repo,
2116 got_repo_import_cb progress_cb, void *progress_arg)
2118 const struct got_error *err = NULL;
2122 struct got_tree_entry *new_te = NULL;
2123 struct got_pathlist_head paths;
2124 struct got_pathlist_entry *pe;
2126 *new_tree_id = NULL;
2130 dir = opendir(path_dir);
2132 err = got_error_from_errno2("opendir", path_dir);
2137 while ((de = readdir(dir)) != NULL) {
2141 if (strcmp(de->d_name, ".") == 0 ||
2142 strcmp(de->d_name, "..") == 0)
2145 TAILQ_FOREACH(pe, ignores, entry) {
2146 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2154 err = got_path_dirent_type(&type, path_dir, de);
2158 if (type == DT_DIR) {
2159 err = import_subdir(&new_te, de, path_dir,
2160 ignores, repo, progress_cb, progress_arg);
2162 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2167 } else if (type == DT_REG || type == DT_LNK) {
2168 err = import_file(&new_te, de, path_dir, repo);
2174 err = insert_tree_entry(new_te, &paths);
2180 if (TAILQ_EMPTY(&paths)) {
2181 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2182 "cannot create tree without any entries");
2186 TAILQ_FOREACH(pe, &paths, entry) {
2187 struct got_tree_entry *te = pe->data;
2189 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2191 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2192 err = got_error_from_errno("asprintf");
2195 err = (*progress_cb)(progress_arg, path);
2201 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2205 got_pathlist_free(&paths);
2209 const struct got_error *
2210 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2211 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2212 struct got_repository *repo, got_repo_import_cb progress_cb,
2215 const struct got_error *err;
2216 struct got_object_id *new_tree_id;
2218 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2219 progress_cb, progress_arg);
2223 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2224 author, time(NULL), author, time(NULL), logmsg, repo);
2229 const struct got_error *
2230 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2231 struct got_repository *repo)
2233 const struct got_error *err = NULL;
2234 char *path_objects = NULL, *path = NULL;
2236 struct got_object_id id;
2242 path_objects = got_repo_get_path_objects(repo);
2243 if (path_objects == NULL)
2244 return got_error_from_errno("got_repo_get_path_objects");
2246 for (i = 0; i <= 0xff; i++) {
2247 struct dirent *dent;
2249 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2250 err = got_error_from_errno("asprintf");
2254 dir = opendir(path);
2256 if (errno == ENOENT) {
2260 err = got_error_from_errno2("opendir", path);
2264 while ((dent = readdir(dir)) != NULL) {
2269 if (strcmp(dent->d_name, ".") == 0 ||
2270 strcmp(dent->d_name, "..") == 0)
2273 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2274 err = got_error_from_errno("asprintf");
2278 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2284 err = got_object_open_loose_fd(&fd, &id, repo);
2288 if (fstat(fd, &sb) == -1) {
2289 err = got_error_from_errno("fstat");
2294 (*ondisk_size) += sb.st_size;
2296 if (close(fd) == -1) {
2297 err = got_error_from_errno("close");
2302 if (closedir(dir) != 0) {
2303 err = got_error_from_errno("closedir");
2312 if (dir && closedir(dir) != 0 && err == NULL)
2313 err = got_error_from_errno("closedir");
2324 const struct got_error *
2325 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2326 off_t *total_packsize, struct got_repository *repo)
2328 const struct got_error *err = NULL;
2329 DIR *packdir = NULL;
2330 struct dirent *dent;
2331 struct got_packidx *packidx = NULL;
2333 char *path_packfile;
2339 *total_packsize = 0;
2341 packdir_fd = openat(got_repo_get_fd(repo),
2342 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2343 if (packdir_fd == -1) {
2344 return got_error_from_errno_fmt("openat: %s/%s",
2345 got_repo_get_path_git_dir(repo),
2346 GOT_OBJECTS_PACK_DIR);
2349 packdir = fdopendir(packdir_fd);
2350 if (packdir == NULL) {
2351 err = got_error_from_errno("fdopendir");
2355 while ((dent = readdir(packdir)) != NULL) {
2356 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2359 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2360 dent->d_name) == -1) {
2361 err = got_error_from_errno("asprintf");
2365 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2371 if (fstat(packidx->fd, &sb) == -1)
2373 *total_packsize += sb.st_size;
2375 err = got_packidx_get_packfile_path(&path_packfile,
2376 packidx->path_packidx);
2380 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2382 free(path_packfile);
2385 free(path_packfile);
2386 *total_packsize += sb.st_size;
2388 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2392 got_packidx_close(packidx);
2397 got_packidx_close(packidx);
2398 if (packdir && closedir(packdir) != 0 && err == NULL)
2399 err = got_error_from_errno("closedir");
2403 *total_packsize = 0;
2408 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2409 got_packidx_bloom_filter_cmp);