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>
20 #include <sys/socket.h>
23 #include <sys/resource.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
60 #include "got_lib_gotconfig.h"
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
68 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
69 got_packidx_bloom_filter_cmp);
72 got_repo_get_path(struct got_repository *repo)
78 got_repo_get_path_git_dir(struct got_repository *repo)
80 return repo->path_git_dir;
84 got_repo_get_fd(struct got_repository *repo)
86 return repo->gitdir_fd;
90 got_repo_get_gitconfig_author_name(struct got_repository *repo)
92 return repo->gitconfig_author_name;
96 got_repo_get_gitconfig_author_email(struct got_repository *repo)
98 return repo->gitconfig_author_email;
102 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
104 return repo->global_gitconfig_author_name;
108 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
110 return repo->global_gitconfig_author_email;
114 got_repo_get_gitconfig_owner(struct got_repository *repo)
116 return repo->gitconfig_owner;
120 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
121 struct got_repository *repo)
123 *extensions = repo->extensions;
124 *nextensions = repo->nextensions;
128 got_repo_is_bare(struct got_repository *repo)
130 return (strcmp(repo->path, repo->path_git_dir) == 0);
134 get_path_git_child(struct got_repository *repo, const char *basename)
138 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
146 got_repo_get_path_objects(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_DIR);
152 got_repo_get_path_objects_pack(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
158 got_repo_get_path_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_REFS_DIR);
164 got_repo_get_path_packed_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
170 get_path_head(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_HEAD_FILE);
176 got_repo_get_path_gitconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GITCONFIG);
182 got_repo_get_path_gotconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
187 const struct got_gotconfig *
188 got_repo_get_gotconfig(struct got_repository *repo)
190 return repo->gotconfig;
194 got_repo_get_gitconfig_remotes(int *nremotes,
195 const struct got_remote_repo **remotes, struct got_repository *repo)
197 *nremotes = repo->ngitconfig_remotes;
198 *remotes = repo->gitconfig_remotes;
202 is_git_repo(struct got_repository *repo)
204 const char *path_git = got_repo_get_path_git_dir(repo);
205 char *path_objects = got_repo_get_path_objects(repo);
206 char *path_refs = got_repo_get_path_refs(repo);
207 char *path_head = get_path_head(repo);
210 struct got_reference *head_ref;
212 if (lstat(path_git, &sb) == -1)
214 if (!S_ISDIR(sb.st_mode))
217 if (lstat(path_objects, &sb) == -1)
219 if (!S_ISDIR(sb.st_mode))
222 if (lstat(path_refs, &sb) == -1)
224 if (!S_ISDIR(sb.st_mode))
227 if (lstat(path_head, &sb) == -1)
229 if (!S_ISREG(sb.st_mode))
232 /* Check if the HEAD reference can be opened. */
233 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
235 got_ref_close(head_ref);
246 const struct got_error *
247 got_repo_pack_fds_open(int **pack_fds)
249 const struct got_error *err = NULL;
252 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
253 if (*pack_fds == NULL)
254 return got_error_from_errno("calloc");
257 * got_repo_pack_fds_close will try to close all of the
258 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
259 * a value from got_opentempfd(), which would result in a close(0) if
260 * we do not initialize to -1 here.
262 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
265 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
266 (*pack_fds)[i] = got_opentempfd();
267 if ((*pack_fds)[i] == -1) {
268 err = got_error_from_errno("got_opentempfd");
269 got_repo_pack_fds_close(*pack_fds);
278 const struct got_error *
279 got_repo_pack_fds_close(int *pack_fds)
281 const struct got_error *err = NULL;
284 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
285 if (pack_fds[i] == -1)
287 if (close(pack_fds[i]) == -1) {
288 err = got_error_from_errno("close");
296 const struct got_error *
297 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
298 struct got_object *obj)
300 #ifndef GOT_NO_OBJ_CACHE
301 const struct got_error *err = NULL;
302 err = got_object_cache_add(&repo->objcache, id, obj);
304 if (err->code == GOT_ERR_OBJ_EXISTS ||
305 err->code == GOT_ERR_OBJ_TOO_LARGE)
315 got_repo_get_cached_object(struct got_repository *repo,
316 struct got_object_id *id)
318 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
321 const struct got_error *
322 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
323 struct got_tree_object *tree)
325 #ifndef GOT_NO_OBJ_CACHE
326 const struct got_error *err = NULL;
327 err = got_object_cache_add(&repo->treecache, id, tree);
329 if (err->code == GOT_ERR_OBJ_EXISTS ||
330 err->code == GOT_ERR_OBJ_TOO_LARGE)
339 struct got_tree_object *
340 got_repo_get_cached_tree(struct got_repository *repo,
341 struct got_object_id *id)
343 return (struct got_tree_object *)got_object_cache_get(
344 &repo->treecache, id);
347 const struct got_error *
348 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
349 struct got_commit_object *commit)
351 #ifndef GOT_NO_OBJ_CACHE
352 const struct got_error *err = NULL;
353 err = got_object_cache_add(&repo->commitcache, id, commit);
355 if (err->code == GOT_ERR_OBJ_EXISTS ||
356 err->code == GOT_ERR_OBJ_TOO_LARGE)
365 struct got_commit_object *
366 got_repo_get_cached_commit(struct got_repository *repo,
367 struct got_object_id *id)
369 return (struct got_commit_object *)got_object_cache_get(
370 &repo->commitcache, id);
373 const struct got_error *
374 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
375 struct got_tag_object *tag)
377 #ifndef GOT_NO_OBJ_CACHE
378 const struct got_error *err = NULL;
379 err = got_object_cache_add(&repo->tagcache, id, tag);
381 if (err->code == GOT_ERR_OBJ_EXISTS ||
382 err->code == GOT_ERR_OBJ_TOO_LARGE)
391 struct got_tag_object *
392 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
394 return (struct got_tag_object *)got_object_cache_get(
395 &repo->tagcache, id);
398 const struct got_error *
399 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
400 struct got_raw_object *raw)
402 #ifndef GOT_NO_OBJ_CACHE
403 const struct got_error *err = NULL;
404 err = got_object_cache_add(&repo->rawcache, id, raw);
406 if (err->code == GOT_ERR_OBJ_EXISTS ||
407 err->code == GOT_ERR_OBJ_TOO_LARGE)
417 struct got_raw_object *
418 got_repo_get_cached_raw_object(struct got_repository *repo,
419 struct got_object_id *id)
421 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
425 static const struct got_error *
426 open_repo(struct got_repository *repo, const char *path)
428 const struct got_error *err = NULL;
430 repo->gitdir_fd = -1;
432 /* bare git repository? */
433 repo->path_git_dir = strdup(path);
434 if (repo->path_git_dir == NULL)
435 return got_error_from_errno("strdup");
436 if (is_git_repo(repo)) {
437 repo->path = strdup(repo->path_git_dir);
438 if (repo->path == NULL) {
439 err = got_error_from_errno("strdup");
442 repo->gitdir_fd = open(repo->path_git_dir,
443 O_DIRECTORY | O_CLOEXEC);
444 if (repo->gitdir_fd == -1) {
445 err = got_error_from_errno2("open",
452 /* git repository with working tree? */
453 free(repo->path_git_dir);
454 repo->path_git_dir = NULL;
455 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
456 err = got_error_from_errno("asprintf");
459 if (is_git_repo(repo)) {
460 repo->path = strdup(path);
461 if (repo->path == NULL) {
462 err = got_error_from_errno("strdup");
465 repo->gitdir_fd = open(repo->path_git_dir,
466 O_DIRECTORY | O_CLOEXEC);
467 if (repo->gitdir_fd == -1) {
468 err = got_error_from_errno2("open",
475 err = got_error(GOT_ERR_NOT_GIT_REPO);
480 free(repo->path_git_dir);
481 repo->path_git_dir = NULL;
482 if (repo->gitdir_fd != -1)
483 close(repo->gitdir_fd);
484 repo->gitdir_fd = -1;
490 static const struct got_error *
491 parse_gitconfig_file(int *gitconfig_repository_format_version,
492 char **gitconfig_author_name, char **gitconfig_author_email,
493 struct got_remote_repo **remotes, int *nremotes,
494 char **gitconfig_owner, char ***extensions, int *nextensions,
495 const char *gitconfig_path)
497 const struct got_error *err = NULL, *child_err = NULL;
499 int imsg_fds[2] = { -1, -1 };
501 struct imsgbuf *ibuf;
503 *gitconfig_repository_format_version = 0;
508 *gitconfig_author_name = NULL;
509 *gitconfig_author_email = NULL;
515 *gitconfig_owner = NULL;
517 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
521 return got_error_from_errno2("open", gitconfig_path);
524 ibuf = calloc(1, sizeof(*ibuf));
526 err = got_error_from_errno("calloc");
530 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
531 err = got_error_from_errno("socketpair");
537 err = got_error_from_errno("fork");
539 } else if (pid == 0) {
540 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
545 if (close(imsg_fds[1]) == -1) {
546 err = got_error_from_errno("close");
550 imsg_init(ibuf, imsg_fds[0]);
552 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
557 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
561 err = got_privsep_recv_gitconfig_int(
562 gitconfig_repository_format_version, ibuf);
566 if (extensions && nextensions) {
567 err = got_privsep_send_gitconfig_repository_extensions_req(
571 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
574 if (*nextensions > 0) {
576 *extensions = calloc(*nextensions, sizeof(char *));
577 if (*extensions == NULL) {
578 err = got_error_from_errno("calloc");
581 for (i = 0; i < *nextensions; i++) {
583 err = got_privsep_recv_gitconfig_str(&ext,
587 (*extensions)[i] = ext;
592 err = got_privsep_send_gitconfig_author_name_req(ibuf);
596 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
600 err = got_privsep_send_gitconfig_author_email_req(ibuf);
604 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
608 if (remotes && nremotes) {
609 err = got_privsep_send_gitconfig_remotes_req(ibuf);
613 err = got_privsep_recv_gitconfig_remotes(remotes,
619 if (gitconfig_owner) {
620 err = got_privsep_send_gitconfig_owner_req(ibuf);
623 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
628 err = got_privsep_send_stop(imsg_fds[0]);
629 child_err = got_privsep_wait_for_child(pid);
630 if (child_err && err == NULL)
633 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
634 err = got_error_from_errno("close");
635 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
636 err = got_error_from_errno("close");
637 if (fd != -1 && close(fd) == -1 && err == NULL)
638 err = got_error_from_errno2("close", gitconfig_path);
643 static const struct got_error *
644 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
646 const struct got_error *err = NULL;
647 char *repo_gitconfig_path = NULL;
649 if (global_gitconfig_path) {
650 /* Read settings from ~/.gitconfig. */
651 int dummy_repo_version;
652 err = parse_gitconfig_file(&dummy_repo_version,
653 &repo->global_gitconfig_author_name,
654 &repo->global_gitconfig_author_email,
655 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
660 /* Read repository's .git/config file. */
661 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
662 if (repo_gitconfig_path == NULL)
663 return got_error_from_errno("got_repo_get_path_gitconfig");
665 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
666 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
667 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
668 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
669 repo_gitconfig_path);
673 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
676 for (i = 0; i < repo->ngitconfig_remotes; i++) {
677 got_repo_free_remote_repo_data(
678 &repo->gitconfig_remotes[i]);
680 free(repo->gitconfig_remotes);
681 repo->gitconfig_remotes = NULL;
682 repo->ngitconfig_remotes = 0;
684 free(repo->gitconfig_author_name);
685 repo->gitconfig_author_name = NULL;
686 free(repo->gitconfig_author_email);
687 repo->gitconfig_author_email = NULL;
689 free(repo->global_gitconfig_author_name);
690 repo->global_gitconfig_author_name = NULL;
691 free(repo->global_gitconfig_author_email);
692 repo->global_gitconfig_author_email = NULL;
696 free(repo_gitconfig_path);
700 static const struct got_error *
701 read_gotconfig(struct got_repository *repo)
703 const struct got_error *err = NULL;
704 char *gotconfig_path;
706 gotconfig_path = got_repo_get_path_gotconfig(repo);
707 if (gotconfig_path == NULL)
708 return got_error_from_errno("got_repo_get_path_gotconfig");
710 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
711 free(gotconfig_path);
715 /* Supported repository format extensions. */
716 static const char *const repo_extensions[] = {
717 "noop", /* Got supports repository format version 1. */
718 "preciousObjects", /* Supported by gotadmin cleanup. */
719 "worktreeConfig", /* Got does not care about Git work trees. */
722 const struct got_error *
723 got_repo_open(struct got_repository **repop, const char *path,
724 const char *global_gitconfig_path, int *pack_fds)
726 struct got_repository *repo = NULL;
727 const struct got_error *err = NULL;
728 char *repo_path = NULL;
734 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
735 return got_error_from_errno("getrlimit");
737 repo = calloc(1, sizeof(*repo));
739 return got_error_from_errno("calloc");
741 RB_INIT(&repo->packidx_bloom_filters);
742 TAILQ_INIT(&repo->packidx_paths);
744 for (i = 0; i < nitems(repo->privsep_children); i++) {
745 memset(&repo->privsep_children[i], 0,
746 sizeof(repo->privsep_children[0]));
747 repo->privsep_children[i].imsg_fd = -1;
750 err = got_object_cache_init(&repo->objcache,
751 GOT_OBJECT_CACHE_TYPE_OBJ);
754 err = got_object_cache_init(&repo->treecache,
755 GOT_OBJECT_CACHE_TYPE_TREE);
758 err = got_object_cache_init(&repo->commitcache,
759 GOT_OBJECT_CACHE_TYPE_COMMIT);
762 err = got_object_cache_init(&repo->tagcache,
763 GOT_OBJECT_CACHE_TYPE_TAG);
766 err = got_object_cache_init(&repo->rawcache,
767 GOT_OBJECT_CACHE_TYPE_RAW);
771 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
772 if (repo->pack_cache_size > rl.rlim_cur / 8)
773 repo->pack_cache_size = rl.rlim_cur / 8;
774 for (i = 0; i < nitems(repo->packs); i++) {
775 if (i < repo->pack_cache_size) {
776 repo->packs[i].basefd = pack_fds[j++];
777 repo->packs[i].accumfd = pack_fds[j++];
779 repo->packs[i].basefd = -1;
780 repo->packs[i].accumfd = -1;
783 repo->pinned_pack = -1;
784 repo->pinned_packidx = -1;
785 repo->pinned_pid = 0;
787 repo_path = realpath(path, NULL);
788 if (repo_path == NULL) {
789 err = got_error_from_errno2("realpath", path);
796 err = open_repo(repo, repo_path);
799 if (err->code != GOT_ERR_NOT_GIT_REPO)
801 if (repo_path[0] == '/' && repo_path[1] == '\0') {
802 err = got_error(GOT_ERR_NOT_GIT_REPO);
805 err = got_path_dirname(&parent_path, repo_path);
809 repo_path = parent_path;
812 err = read_gotconfig(repo);
816 err = read_gitconfig(repo, global_gitconfig_path);
819 if (repo->gitconfig_repository_format_version != 0) {
820 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
823 for (i = 0; i < repo->nextensions; i++) {
824 char *ext = repo->extensions[i];
825 int j, supported = 0;
826 for (j = 0; j < nitems(repo_extensions); j++) {
827 if (strcmp(ext, repo_extensions[j]) == 0) {
833 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
838 err = got_repo_list_packidx(&repo->packidx_paths, repo);
841 got_repo_close(repo);
848 const struct got_error *
849 got_repo_close(struct got_repository *repo)
851 const struct got_error *err = NULL, *child_err;
852 struct got_packidx_bloom_filter *bf;
853 struct got_pathlist_entry *pe;
856 for (i = 0; i < repo->pack_cache_size; i++) {
857 if (repo->packidx_cache[i] == NULL)
859 got_packidx_close(repo->packidx_cache[i]);
862 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
863 &repo->packidx_bloom_filters))) {
864 RB_REMOVE(got_packidx_bloom_filter_tree,
865 &repo->packidx_bloom_filters, bf);
866 bloom_free(bf->bloom);
871 for (i = 0; i < repo->pack_cache_size; i++)
872 if (repo->packs[i].path_packfile)
873 if (repo->packs[i].path_packfile)
874 got_pack_close(&repo->packs[i]);
877 free(repo->path_git_dir);
879 got_object_cache_close(&repo->objcache);
880 got_object_cache_close(&repo->treecache);
881 got_object_cache_close(&repo->commitcache);
882 got_object_cache_close(&repo->tagcache);
883 got_object_cache_close(&repo->rawcache);
885 for (i = 0; i < nitems(repo->privsep_children); i++) {
886 if (repo->privsep_children[i].imsg_fd == -1)
888 imsg_clear(repo->privsep_children[i].ibuf);
889 free(repo->privsep_children[i].ibuf);
890 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
891 child_err = got_privsep_wait_for_child(
892 repo->privsep_children[i].pid);
893 if (child_err && err == NULL)
895 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
897 err = got_error_from_errno("close");
900 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
902 err = got_error_from_errno("close");
905 got_gotconfig_free(repo->gotconfig);
906 free(repo->gitconfig_author_name);
907 free(repo->gitconfig_author_email);
908 for (i = 0; i < repo->ngitconfig_remotes; i++)
909 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
910 free(repo->gitconfig_remotes);
911 for (i = 0; i < repo->nextensions; i++)
912 free(repo->extensions[i]);
913 free(repo->extensions);
915 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
916 free((void *)pe->path);
917 got_pathlist_free(&repo->packidx_paths);
924 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
930 free(repo->fetch_url);
931 repo->fetch_url = NULL;
932 free(repo->send_url);
933 repo->send_url = NULL;
934 for (i = 0; i < repo->nfetch_branches; i++)
935 free(repo->fetch_branches[i]);
936 free(repo->fetch_branches);
937 repo->fetch_branches = NULL;
938 repo->nfetch_branches = 0;
939 for (i = 0; i < repo->nsend_branches; i++)
940 free(repo->send_branches[i]);
941 free(repo->send_branches);
942 repo->send_branches = NULL;
943 repo->nsend_branches = 0;
946 const struct got_error *
947 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
948 const char *input_path)
950 const struct got_error *err = NULL;
951 const char *repo_abspath = NULL;
953 char *canonpath, *path = NULL;
955 *in_repo_path = NULL;
957 canonpath = strdup(input_path);
958 if (canonpath == NULL) {
959 err = got_error_from_errno("strdup");
962 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
966 repo_abspath = got_repo_get_path(repo);
968 if (canonpath[0] == '\0') {
969 path = strdup(canonpath);
971 err = got_error_from_errno("strdup");
975 path = realpath(canonpath, NULL);
977 if (errno != ENOENT) {
978 err = got_error_from_errno2("realpath",
983 * Path is not on disk.
984 * Assume it is already relative to repository root.
986 path = strdup(canonpath);
988 err = got_error_from_errno("strdup");
993 repolen = strlen(repo_abspath);
997 if (strcmp(path, repo_abspath) == 0) {
1001 err = got_error_from_errno("strdup");
1004 } else if (len > repolen &&
1005 got_path_is_child(path, repo_abspath, repolen)) {
1006 /* Matched an on-disk path inside repository. */
1007 if (got_repo_is_bare(repo)) {
1009 * Matched an on-disk path inside repository
1010 * database. Treat input as repository-relative.
1017 /* Strip common prefix with repository path. */
1018 err = got_path_skip_common_ancestor(&child,
1019 repo_abspath, path);
1027 * Matched unrelated on-disk path.
1028 * Treat input as repository-relative.
1036 /* Make in-repository path absolute */
1037 if (path[0] != '/') {
1039 if (asprintf(&abspath, "/%s", path) == -1) {
1040 err = got_error_from_errno("asprintf");
1052 *in_repo_path = path;
1056 static const struct got_error *
1057 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1058 const char *path_packidx)
1060 const struct got_error *err = NULL;
1063 for (i = 0; i < repo->pack_cache_size; i++) {
1064 if (repo->packidx_cache[i] == NULL)
1066 if (strcmp(repo->packidx_cache[i]->path_packidx,
1067 path_packidx) == 0) {
1068 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1071 if (i == repo->pack_cache_size) {
1074 } while (i > 0 && repo->pinned_packidx >= 0 &&
1075 i == repo->pinned_packidx);
1076 err = got_packidx_close(repo->packidx_cache[i]);
1081 repo->packidx_cache[i] = packidx;
1087 got_repo_is_packidx_filename(const char *name, size_t len)
1089 if (len != GOT_PACKIDX_NAMELEN)
1092 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1095 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1096 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1102 static struct got_packidx_bloom_filter *
1103 get_packidx_bloom_filter(struct got_repository *repo,
1104 const char *path, size_t path_len)
1106 struct got_packidx_bloom_filter key;
1108 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1109 return NULL; /* XXX */
1110 key.path_len = path_len;
1112 return RB_FIND(got_packidx_bloom_filter_tree,
1113 &repo->packidx_bloom_filters, &key);
1117 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1118 const char *path_packidx, struct got_object_id *id)
1120 struct got_packidx_bloom_filter *bf;
1122 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1124 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1126 /* No bloom filter means this pack index must be searched. */
1130 static const struct got_error *
1131 add_packidx_bloom_filter(struct got_repository *repo,
1132 struct got_packidx *packidx, const char *path_packidx)
1134 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1135 struct got_packidx_bloom_filter *bf;
1139 * Don't use bloom filters for very large pack index files.
1140 * Large pack files will contain a relatively large fraction
1141 * of our objects so we will likely need to visit them anyway.
1142 * The more objects a pack file contains the higher the probability
1143 * of a false-positive match from the bloom filter. And reading
1144 * all object IDs from a large pack index file can be expensive.
1146 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1149 /* Do we already have a filter for this pack index? */
1150 if (get_packidx_bloom_filter(repo, path_packidx,
1151 strlen(path_packidx)) != NULL)
1154 bf = calloc(1, sizeof(*bf));
1156 return got_error_from_errno("calloc");
1157 bf->bloom = calloc(1, sizeof(*bf->bloom));
1158 if (bf->bloom == NULL) {
1160 return got_error_from_errno("calloc");
1163 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1164 if (len >= sizeof(bf->path)) {
1167 return got_error(GOT_ERR_NO_SPACE);
1171 /* Minimum size supported by our bloom filter is 1000 entries. */
1172 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1173 for (i = 0; i < nobjects; i++) {
1174 struct got_packidx_object_id *id;
1175 id = &packidx->hdr.sorted_ids[i];
1176 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1179 RB_INSERT(got_packidx_bloom_filter_tree,
1180 &repo->packidx_bloom_filters, bf);
1184 const struct got_error *
1185 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1186 struct got_repository *repo, struct got_object_id *id)
1188 const struct got_error *err;
1189 struct got_pathlist_entry *pe;
1192 /* Search pack index cache. */
1193 for (i = 0; i < repo->pack_cache_size; i++) {
1194 if (repo->packidx_cache[i] == NULL)
1196 if (!got_repo_check_packidx_bloom_filter(repo,
1197 repo->packidx_cache[i]->path_packidx, id))
1198 continue; /* object will not be found in this index */
1199 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1201 *packidx = repo->packidx_cache[i];
1203 * Move this cache entry to the front. Repeatedly
1204 * searching a wrong pack index can be expensive.
1207 memmove(&repo->packidx_cache[1],
1208 &repo->packidx_cache[0],
1209 i * sizeof(repo->packidx_cache[0]));
1210 repo->packidx_cache[0] = *packidx;
1211 if (repo->pinned_packidx >= 0 &&
1212 repo->pinned_packidx < i)
1213 repo->pinned_packidx++;
1214 else if (repo->pinned_packidx == i)
1215 repo->pinned_packidx = 0;
1220 /* No luck. Search the filesystem. */
1222 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1223 const char *path_packidx = pe->path;
1226 if (!got_repo_check_packidx_bloom_filter(repo,
1228 continue; /* object will not be found in this index */
1230 for (i = 0; i < repo->pack_cache_size; i++) {
1231 if (repo->packidx_cache[i] == NULL)
1233 if (strcmp(repo->packidx_cache[i]->path_packidx,
1234 path_packidx) == 0) {
1240 continue; /* already searched */
1242 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1247 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1251 err = cache_packidx(repo, *packidx, path_packidx);
1255 *idx = got_packidx_get_object_idx(*packidx, id);
1257 err = NULL; /* found the object */
1262 err = got_error_no_obj(id);
1267 const struct got_error *
1268 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1269 struct got_repository *repo)
1271 const struct got_error *err = NULL;
1272 DIR *packdir = NULL;
1273 struct dirent *dent;
1274 char *path_packidx = NULL;
1278 packdir_fd = openat(got_repo_get_fd(repo),
1279 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1280 if (packdir_fd == -1) {
1281 return got_error_from_errno_fmt("openat: %s/%s",
1282 got_repo_get_path_git_dir(repo),
1283 GOT_OBJECTS_PACK_DIR);
1286 packdir = fdopendir(packdir_fd);
1287 if (packdir == NULL) {
1288 err = got_error_from_errno("fdopendir");
1292 if (fstat(packdir_fd, &sb) == -1) {
1293 err = got_error_from_errno("fstat");
1296 repo->pack_path_mtime = sb.st_mtime;
1298 while ((dent = readdir(packdir)) != NULL) {
1299 if (!got_repo_is_packidx_filename(dent->d_name,
1300 strlen(dent->d_name)))
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);
1611 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1613 struct got_pathlist_entry *pe;
1615 while (!TAILQ_EMPTY(packidx_paths)) {
1616 pe = TAILQ_FIRST(packidx_paths);
1617 TAILQ_REMOVE(packidx_paths, pe, entry);
1618 free((char *)pe->path);
1623 static const struct got_error *
1624 match_packed_object(struct got_object_id **unique_id,
1625 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1627 const struct got_error *err = NULL;
1628 char *objects_pack_dir = NULL;
1629 struct got_object_id_queue matched_ids;
1630 struct got_pathlist_entry *pe;
1633 STAILQ_INIT(&matched_ids);
1635 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1636 if (objects_pack_dir == NULL)
1637 return got_error_from_errno("got_repo_get_path_objects_pack");
1639 if (stat(objects_pack_dir, &sb) == -1) {
1640 if (errno != ENOENT) {
1641 err = got_error_from_errno2("stat", objects_pack_dir);
1644 } else if (sb.st_mtime != repo->pack_path_mtime) {
1645 purge_packidx_paths(&repo->packidx_paths);
1646 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1651 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1652 const char *path_packidx = pe->path;
1653 struct got_packidx *packidx;
1654 struct got_object_qid *qid;
1656 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1661 err = got_packidx_match_id_str_prefix(&matched_ids,
1662 packidx, id_str_prefix);
1664 got_packidx_close(packidx);
1667 err = got_packidx_close(packidx);
1671 STAILQ_FOREACH(qid, &matched_ids, entry) {
1672 if (obj_type != GOT_OBJ_TYPE_ANY) {
1674 err = got_object_get_type(&matched_type, repo,
1678 if (matched_type != obj_type)
1681 if (*unique_id == NULL) {
1682 *unique_id = got_object_id_dup(&qid->id);
1683 if (*unique_id == NULL) {
1684 err = got_error_from_errno("malloc");
1688 if (got_object_id_cmp(*unique_id,
1690 continue; /* packed multiple times */
1691 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1697 free(objects_pack_dir);
1698 got_object_id_queue_free(&matched_ids);
1706 static const struct got_error *
1707 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1708 const char *object_dir, const char *id_str_prefix, int obj_type,
1709 struct got_repository *repo)
1711 const struct got_error *err = NULL;
1712 char *path, *id_str = NULL;
1714 struct dirent *dent;
1715 struct got_object_id id;
1717 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1718 err = got_error_from_errno("asprintf");
1722 dir = opendir(path);
1724 if (errno == ENOENT) {
1728 err = got_error_from_errno2("opendir", path);
1731 while ((dent = readdir(dir)) != NULL) {
1737 if (strcmp(dent->d_name, ".") == 0 ||
1738 strcmp(dent->d_name, "..") == 0)
1741 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1742 err = got_error_from_errno("asprintf");
1746 if (!got_parse_sha1_digest(id.sha1, id_str))
1750 * Directory entries do not necessarily appear in
1751 * sorted order, so we must iterate over all of them.
1753 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1757 if (*unique_id == NULL) {
1758 if (obj_type != GOT_OBJ_TYPE_ANY) {
1760 err = got_object_get_type(&matched_type, repo,
1764 if (matched_type != obj_type)
1767 *unique_id = got_object_id_dup(&id);
1768 if (*unique_id == NULL) {
1769 err = got_error_from_errno("got_object_id_dup");
1773 if (got_object_id_cmp(*unique_id, &id) == 0)
1774 continue; /* both packed and loose */
1775 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1780 if (dir && closedir(dir) != 0 && err == NULL)
1781 err = got_error_from_errno("closedir");
1791 const struct got_error *
1792 got_repo_match_object_id_prefix(struct got_object_id **id,
1793 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1795 const struct got_error *err = NULL;
1796 char *path_objects = NULL, *object_dir = NULL;
1802 path_objects = got_repo_get_path_objects(repo);
1804 len = strlen(id_str_prefix);
1805 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1806 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1810 for (i = 0; i < len; i++) {
1811 if (isxdigit((unsigned char)id_str_prefix[i]))
1813 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1818 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1821 object_dir = strndup(id_str_prefix, 2);
1822 if (object_dir == NULL) {
1823 err = got_error_from_errno("strdup");
1826 err = match_loose_object(id, path_objects, object_dir,
1827 id_str_prefix, obj_type, repo);
1828 } else if (len == 1) {
1830 for (i = 0; i < 0xf; i++) {
1831 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1833 err = got_error_from_errno("asprintf");
1836 err = match_packed_object(id, repo, object_dir,
1840 err = match_loose_object(id, path_objects, object_dir,
1841 id_str_prefix, obj_type, repo);
1846 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1855 } else if (*id == NULL) {
1857 case GOT_OBJ_TYPE_BLOB:
1858 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1859 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1861 case GOT_OBJ_TYPE_TREE:
1862 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1863 GOT_OBJ_LABEL_TREE, id_str_prefix);
1865 case GOT_OBJ_TYPE_COMMIT:
1866 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1867 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1869 case GOT_OBJ_TYPE_TAG:
1870 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1871 GOT_OBJ_LABEL_TAG, id_str_prefix);
1874 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1882 const struct got_error *
1883 got_repo_match_object_id(struct got_object_id **id, char **label,
1884 const char *id_str, int obj_type, struct got_reflist_head *refs,
1885 struct got_repository *repo)
1887 const struct got_error *err;
1888 struct got_tag_object *tag;
1889 struct got_reference *ref = NULL;
1896 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1899 *id = got_object_id_dup(
1900 got_object_tag_get_object_id(tag));
1902 err = got_error_from_errno("got_object_id_dup");
1903 else if (label && asprintf(label, "refs/tags/%s",
1904 got_object_tag_get_name(tag)) == -1) {
1905 err = got_error_from_errno("asprintf");
1909 got_object_tag_close(tag);
1911 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1912 err->code != GOT_ERR_NO_OBJ)
1916 err = got_ref_open(&ref, repo, id_str, 0);
1918 err = got_ref_resolve(id, repo, ref);
1922 *label = strdup(got_ref_get_name(ref));
1923 if (*label == NULL) {
1924 err = got_error_from_errno("strdup");
1929 if (err->code != GOT_ERR_NOT_REF &&
1930 err->code != GOT_ERR_BAD_REF_NAME)
1932 err = got_repo_match_object_id_prefix(id, id_str,
1935 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1936 err = got_error_not_ref(id_str);
1940 err = got_object_id_str(label, *id);
1941 if (*label == NULL) {
1942 err = got_error_from_errno("strdup");
1953 const struct got_error *
1954 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1955 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1957 const struct got_error *err = NULL;
1958 struct got_reflist_entry *re;
1959 struct got_object_id *tag_id;
1960 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1964 TAILQ_FOREACH(re, refs, entry) {
1965 const char *refname;
1966 refname = got_ref_get_name(re->ref);
1967 if (got_ref_is_symbolic(re->ref))
1969 if (strncmp(refname, "refs/tags/", 10) != 0)
1971 if (!name_is_absolute)
1972 refname += strlen("refs/tags/");
1973 if (strcmp(refname, name) != 0)
1975 err = got_ref_resolve(&tag_id, repo, re->ref);
1978 err = got_object_open_as_tag(tag, repo, tag_id);
1982 if (obj_type == GOT_OBJ_TYPE_ANY ||
1983 got_object_tag_get_object_type(*tag) == obj_type)
1985 got_object_tag_close(*tag);
1989 if (err == NULL && *tag == NULL)
1990 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1991 GOT_OBJ_LABEL_TAG, name);
1995 static const struct got_error *
1996 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1997 const char *name, mode_t mode, struct got_object_id *blob_id)
1999 const struct got_error *err = NULL;
2003 *new_te = calloc(1, sizeof(**new_te));
2004 if (*new_te == NULL)
2005 return got_error_from_errno("calloc");
2007 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2008 sizeof((*new_te)->name)) {
2009 err = got_error(GOT_ERR_NO_SPACE);
2013 if (S_ISLNK(mode)) {
2014 (*new_te)->mode = S_IFLNK;
2016 (*new_te)->mode = S_IFREG;
2017 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2019 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2021 if (err && *new_te) {
2028 static const struct got_error *
2029 import_file(struct got_tree_entry **new_te, struct dirent *de,
2030 const char *path, struct got_repository *repo)
2032 const struct got_error *err;
2033 struct got_object_id *blob_id = NULL;
2037 if (asprintf(&filepath, "%s%s%s", path,
2038 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2039 return got_error_from_errno("asprintf");
2041 if (lstat(filepath, &sb) != 0) {
2042 err = got_error_from_errno2("lstat", path);
2046 err = got_object_blob_create(&blob_id, filepath, repo);
2050 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2059 static const struct got_error *
2060 insert_tree_entry(struct got_tree_entry *new_te,
2061 struct got_pathlist_head *paths)
2063 const struct got_error *err = NULL;
2064 struct got_pathlist_entry *new_pe;
2066 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2070 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2074 static const struct got_error *write_tree(struct got_object_id **,
2075 const char *, struct got_pathlist_head *, struct got_repository *,
2076 got_repo_import_cb progress_cb, void *progress_arg);
2078 static const struct got_error *
2079 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2080 const char *path, struct got_pathlist_head *ignores,
2081 struct got_repository *repo,
2082 got_repo_import_cb progress_cb, void *progress_arg)
2084 const struct got_error *err;
2085 struct got_object_id *id = NULL;
2088 if (asprintf(&subdirpath, "%s%s%s", path,
2089 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2090 return got_error_from_errno("asprintf");
2092 (*new_te) = calloc(1, sizeof(**new_te));
2093 if (*new_te == NULL)
2094 return got_error_from_errno("calloc");
2095 (*new_te)->mode = S_IFDIR;
2096 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2097 sizeof((*new_te)->name)) {
2098 err = got_error(GOT_ERR_NO_SPACE);
2101 err = write_tree(&id, subdirpath, ignores, repo,
2102 progress_cb, progress_arg);
2105 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2117 static const struct got_error *
2118 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2119 struct got_pathlist_head *ignores, struct got_repository *repo,
2120 got_repo_import_cb progress_cb, void *progress_arg)
2122 const struct got_error *err = NULL;
2126 struct got_tree_entry *new_te = NULL;
2127 struct got_pathlist_head paths;
2128 struct got_pathlist_entry *pe;
2130 *new_tree_id = NULL;
2134 dir = opendir(path_dir);
2136 err = got_error_from_errno2("opendir", path_dir);
2141 while ((de = readdir(dir)) != NULL) {
2145 if (strcmp(de->d_name, ".") == 0 ||
2146 strcmp(de->d_name, "..") == 0)
2149 TAILQ_FOREACH(pe, ignores, entry) {
2150 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2158 err = got_path_dirent_type(&type, path_dir, de);
2162 if (type == DT_DIR) {
2163 err = import_subdir(&new_te, de, path_dir,
2164 ignores, repo, progress_cb, progress_arg);
2166 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2171 } else if (type == DT_REG || type == DT_LNK) {
2172 err = import_file(&new_te, de, path_dir, repo);
2178 err = insert_tree_entry(new_te, &paths);
2184 if (TAILQ_EMPTY(&paths)) {
2185 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2186 "cannot create tree without any entries");
2190 TAILQ_FOREACH(pe, &paths, entry) {
2191 struct got_tree_entry *te = pe->data;
2193 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2195 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2196 err = got_error_from_errno("asprintf");
2199 err = (*progress_cb)(progress_arg, path);
2205 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2209 got_pathlist_free(&paths);
2213 const struct got_error *
2214 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2215 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2216 struct got_repository *repo, got_repo_import_cb progress_cb,
2219 const struct got_error *err;
2220 struct got_object_id *new_tree_id;
2222 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2223 progress_cb, progress_arg);
2227 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2228 author, time(NULL), author, time(NULL), logmsg, repo);
2233 const struct got_error *
2234 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2235 struct got_repository *repo)
2237 const struct got_error *err = NULL;
2238 char *path_objects = NULL, *path = NULL;
2240 struct got_object_id id;
2246 path_objects = got_repo_get_path_objects(repo);
2247 if (path_objects == NULL)
2248 return got_error_from_errno("got_repo_get_path_objects");
2250 for (i = 0; i <= 0xff; i++) {
2251 struct dirent *dent;
2253 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2254 err = got_error_from_errno("asprintf");
2258 dir = opendir(path);
2260 if (errno == ENOENT) {
2264 err = got_error_from_errno2("opendir", path);
2268 while ((dent = readdir(dir)) != NULL) {
2273 if (strcmp(dent->d_name, ".") == 0 ||
2274 strcmp(dent->d_name, "..") == 0)
2277 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2278 err = got_error_from_errno("asprintf");
2282 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2288 err = got_object_open_loose_fd(&fd, &id, repo);
2292 if (fstat(fd, &sb) == -1) {
2293 err = got_error_from_errno("fstat");
2298 (*ondisk_size) += sb.st_size;
2300 if (close(fd) == -1) {
2301 err = got_error_from_errno("close");
2306 if (closedir(dir) != 0) {
2307 err = got_error_from_errno("closedir");
2316 if (dir && closedir(dir) != 0 && err == NULL)
2317 err = got_error_from_errno("closedir");
2328 const struct got_error *
2329 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2330 off_t *total_packsize, struct got_repository *repo)
2332 const struct got_error *err = NULL;
2333 DIR *packdir = NULL;
2334 struct dirent *dent;
2335 struct got_packidx *packidx = NULL;
2337 char *path_packfile;
2343 *total_packsize = 0;
2345 packdir_fd = openat(got_repo_get_fd(repo),
2346 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2347 if (packdir_fd == -1) {
2348 return got_error_from_errno_fmt("openat: %s/%s",
2349 got_repo_get_path_git_dir(repo),
2350 GOT_OBJECTS_PACK_DIR);
2353 packdir = fdopendir(packdir_fd);
2354 if (packdir == NULL) {
2355 err = got_error_from_errno("fdopendir");
2359 while ((dent = readdir(packdir)) != NULL) {
2360 if (!got_repo_is_packidx_filename(dent->d_name,
2361 strlen(dent->d_name)))
2364 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2365 dent->d_name) == -1) {
2366 err = got_error_from_errno("asprintf");
2370 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2376 if (fstat(packidx->fd, &sb) == -1)
2378 *total_packsize += sb.st_size;
2380 err = got_packidx_get_packfile_path(&path_packfile,
2381 packidx->path_packidx);
2385 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2387 free(path_packfile);
2390 free(path_packfile);
2391 *total_packsize += sb.st_size;
2393 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2397 got_packidx_close(packidx);
2402 got_packidx_close(packidx);
2403 if (packdir && closedir(packdir) != 0 && err == NULL)
2404 err = got_error_from_errno("closedir");
2408 *total_packsize = 0;
2413 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2414 got_packidx_bloom_filter_cmp);