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_delta_cache.h"
57 #include "got_lib_inflate.h"
58 #include "got_lib_object.h"
59 #include "got_lib_object_parse.h"
60 #include "got_lib_object_create.h"
61 #include "got_lib_pack.h"
62 #include "got_lib_privsep.h"
63 #include "got_lib_sha1.h"
64 #include "got_lib_object_cache.h"
65 #include "got_lib_repository.h"
66 #include "got_lib_gotconfig.h"
69 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
72 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
74 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
75 got_packidx_bloom_filter_cmp);
78 is_boolean_val(const char *val)
80 return (strcasecmp(val, "true") == 0 ||
81 strcasecmp(val, "false") == 0 ||
82 strcasecmp(val, "on") == 0 ||
83 strcasecmp(val, "off") == 0 ||
84 strcasecmp(val, "yes") == 0 ||
85 strcasecmp(val, "no") == 0 ||
86 strcasecmp(val, "1") == 0 ||
87 strcasecmp(val, "0") == 0);
91 get_boolean_val(const char *val)
93 return (strcasecmp(val, "true") == 0 ||
94 strcasecmp(val, "on") == 0 ||
95 strcasecmp(val, "yes") == 0 ||
96 strcasecmp(val, "1") == 0);
100 got_repo_get_path(struct got_repository *repo)
106 got_repo_get_path_git_dir(struct got_repository *repo)
108 return repo->path_git_dir;
112 got_repo_get_fd(struct got_repository *repo)
114 return repo->gitdir_fd;
118 got_repo_get_gitconfig_author_name(struct got_repository *repo)
120 return repo->gitconfig_author_name;
124 got_repo_get_gitconfig_author_email(struct got_repository *repo)
126 return repo->gitconfig_author_email;
130 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
132 return repo->global_gitconfig_author_name;
136 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
138 return repo->global_gitconfig_author_email;
142 got_repo_get_gitconfig_owner(struct got_repository *repo)
144 return repo->gitconfig_owner;
148 got_repo_has_extension(struct got_repository *repo, const char *ext)
152 for (i = 0; i < repo->nextensions; ++i) {
153 if (!strcasecmp(ext, repo->extnames[i]))
154 return get_boolean_val(repo->extvals[i]);
161 got_repo_is_bare(struct got_repository *repo)
163 return (strcmp(repo->path, repo->path_git_dir) == 0);
167 get_path_git_child(struct got_repository *repo, const char *basename)
171 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
179 got_repo_get_path_objects(struct got_repository *repo)
181 return get_path_git_child(repo, GOT_OBJECTS_DIR);
185 got_repo_get_path_objects_pack(struct got_repository *repo)
187 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
191 got_repo_get_path_refs(struct got_repository *repo)
193 return get_path_git_child(repo, GOT_REFS_DIR);
197 got_repo_get_path_packed_refs(struct got_repository *repo)
199 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
203 get_path_head(struct got_repository *repo)
205 return get_path_git_child(repo, GOT_HEAD_FILE);
209 got_repo_get_path_gitconfig(struct got_repository *repo)
211 return get_path_git_child(repo, GOT_GITCONFIG);
215 got_repo_get_path_gotconfig(struct got_repository *repo)
217 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
220 const struct got_gotconfig *
221 got_repo_get_gotconfig(struct got_repository *repo)
223 return repo->gotconfig;
227 got_repo_get_gitconfig_remotes(int *nremotes,
228 const struct got_remote_repo **remotes, struct got_repository *repo)
230 *nremotes = repo->ngitconfig_remotes;
231 *remotes = repo->gitconfig_remotes;
235 is_git_repo(struct got_repository *repo)
237 const char *path_git = got_repo_get_path_git_dir(repo);
238 char *path_objects = got_repo_get_path_objects(repo);
239 char *path_refs = got_repo_get_path_refs(repo);
240 char *path_head = get_path_head(repo);
243 struct got_reference *head_ref;
245 if (lstat(path_git, &sb) == -1)
247 if (!S_ISDIR(sb.st_mode))
250 if (lstat(path_objects, &sb) == -1)
252 if (!S_ISDIR(sb.st_mode))
255 if (lstat(path_refs, &sb) == -1)
257 if (!S_ISDIR(sb.st_mode))
260 if (lstat(path_head, &sb) == -1)
262 if (!S_ISREG(sb.st_mode))
265 /* Check if the HEAD reference can be opened. */
266 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
268 got_ref_close(head_ref);
279 static const struct got_error *
280 close_tempfiles(int *fds, size_t nfds)
282 const struct got_error *err = NULL;
285 for (i = 0; i < nfds; i++) {
288 if (close(fds[i]) == -1) {
289 err = got_error_from_errno("close");
297 static const struct got_error *
298 open_tempfiles(int **fds, size_t array_size, size_t nfds)
300 const struct got_error *err = NULL;
303 *fds = calloc(array_size, sizeof(**fds));
305 return got_error_from_errno("calloc");
307 for (i = 0; i < array_size; i++)
310 for (i = 0; i < nfds; i++) {
311 (*fds)[i] = got_opentempfd();
312 if ((*fds)[i] == -1) {
313 err = got_error_from_errno("got_opentempfd");
314 close_tempfiles(*fds, nfds);
323 static const struct got_error *
324 get_pack_cache_size(int *pack_cache_size)
328 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
329 return got_error_from_errno("getrlimit");
331 *pack_cache_size = GOT_PACK_CACHE_SIZE;
332 if (*pack_cache_size > rl.rlim_cur / 8)
333 *pack_cache_size = rl.rlim_cur / 8;
338 const struct got_error *
339 got_repo_pack_fds_open(int **pack_fds)
341 const struct got_error *err;
344 err = get_pack_cache_size(&nfds);
349 * We need one basefd and one accumfd per cached pack.
350 * Our constants should be set up in a way such that
351 * this error never triggers.
353 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
354 return got_error(GOT_ERR_NO_SPACE);
356 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
359 const struct got_error *
360 got_repo_pack_fds_close(int *pack_fds)
362 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
365 const struct got_error *
366 got_repo_temp_fds_open(int **temp_fds)
368 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
369 GOT_REPO_NUM_TEMPFILES);
373 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
377 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
378 repo->tempfiles[i] = temp_fds[i];
381 const struct got_error *
382 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
389 for (i = 0; i < nitems(repo->tempfiles); i++) {
390 if (repo->tempfile_use_mask & (1 << i))
392 if (repo->tempfiles[i] != -1) {
393 if (ftruncate(repo->tempfiles[i], 0L) == -1)
394 return got_error_from_errno("ftruncate");
395 *fd = repo->tempfiles[i];
397 repo->tempfile_use_mask |= (1 << i);
402 return got_error(GOT_ERR_REPO_TEMPFILE);
406 got_repo_temp_fds_put(int idx, struct got_repository *repo)
408 repo->tempfile_use_mask &= ~(1 << idx);
411 const struct got_error *
412 got_repo_temp_fds_close(int *temp_fds)
414 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
417 const struct got_error *
418 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
419 struct got_object *obj)
421 #ifndef GOT_NO_OBJ_CACHE
422 const struct got_error *err = NULL;
423 err = got_object_cache_add(&repo->objcache, id, obj);
425 if (err->code == GOT_ERR_OBJ_EXISTS ||
426 err->code == GOT_ERR_OBJ_TOO_LARGE)
436 got_repo_get_cached_object(struct got_repository *repo,
437 struct got_object_id *id)
439 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
442 const struct got_error *
443 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
444 struct got_tree_object *tree)
446 #ifndef GOT_NO_OBJ_CACHE
447 const struct got_error *err = NULL;
448 err = got_object_cache_add(&repo->treecache, id, tree);
450 if (err->code == GOT_ERR_OBJ_EXISTS ||
451 err->code == GOT_ERR_OBJ_TOO_LARGE)
460 struct got_tree_object *
461 got_repo_get_cached_tree(struct got_repository *repo,
462 struct got_object_id *id)
464 return (struct got_tree_object *)got_object_cache_get(
465 &repo->treecache, id);
468 const struct got_error *
469 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
470 struct got_commit_object *commit)
472 #ifndef GOT_NO_OBJ_CACHE
473 const struct got_error *err = NULL;
474 err = got_object_cache_add(&repo->commitcache, id, commit);
476 if (err->code == GOT_ERR_OBJ_EXISTS ||
477 err->code == GOT_ERR_OBJ_TOO_LARGE)
486 struct got_commit_object *
487 got_repo_get_cached_commit(struct got_repository *repo,
488 struct got_object_id *id)
490 return (struct got_commit_object *)got_object_cache_get(
491 &repo->commitcache, id);
494 const struct got_error *
495 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
496 struct got_tag_object *tag)
498 #ifndef GOT_NO_OBJ_CACHE
499 const struct got_error *err = NULL;
500 err = got_object_cache_add(&repo->tagcache, id, tag);
502 if (err->code == GOT_ERR_OBJ_EXISTS ||
503 err->code == GOT_ERR_OBJ_TOO_LARGE)
512 struct got_tag_object *
513 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
515 return (struct got_tag_object *)got_object_cache_get(
516 &repo->tagcache, id);
519 const struct got_error *
520 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
521 struct got_raw_object *raw)
523 #ifndef GOT_NO_OBJ_CACHE
524 const struct got_error *err = NULL;
525 err = got_object_cache_add(&repo->rawcache, id, raw);
527 if (err->code == GOT_ERR_OBJ_EXISTS ||
528 err->code == GOT_ERR_OBJ_TOO_LARGE)
538 struct got_raw_object *
539 got_repo_get_cached_raw_object(struct got_repository *repo,
540 struct got_object_id *id)
542 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
546 static const struct got_error *
547 open_repo(struct got_repository *repo, const char *path)
549 const struct got_error *err = NULL;
551 repo->gitdir_fd = -1;
553 /* bare git repository? */
554 repo->path_git_dir = strdup(path);
555 if (repo->path_git_dir == NULL)
556 return got_error_from_errno("strdup");
557 if (is_git_repo(repo)) {
558 repo->path = strdup(repo->path_git_dir);
559 if (repo->path == NULL) {
560 err = got_error_from_errno("strdup");
563 repo->gitdir_fd = open(repo->path_git_dir,
564 O_DIRECTORY | O_CLOEXEC);
565 if (repo->gitdir_fd == -1) {
566 err = got_error_from_errno2("open",
573 /* git repository with working tree? */
574 free(repo->path_git_dir);
575 repo->path_git_dir = NULL;
576 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
577 err = got_error_from_errno("asprintf");
580 if (is_git_repo(repo)) {
581 repo->path = strdup(path);
582 if (repo->path == NULL) {
583 err = got_error_from_errno("strdup");
586 repo->gitdir_fd = open(repo->path_git_dir,
587 O_DIRECTORY | O_CLOEXEC);
588 if (repo->gitdir_fd == -1) {
589 err = got_error_from_errno2("open",
596 err = got_error(GOT_ERR_NOT_GIT_REPO);
601 free(repo->path_git_dir);
602 repo->path_git_dir = NULL;
603 if (repo->gitdir_fd != -1)
604 close(repo->gitdir_fd);
605 repo->gitdir_fd = -1;
611 static const struct got_error *
612 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
614 const struct got_error *err = NULL;
615 char *repo_gitconfig_path = NULL;
617 if (global_gitconfig_path) {
618 /* Read settings from ~/.gitconfig. */
619 int dummy_repo_version;
620 err = got_repo_read_gitconfig(&dummy_repo_version,
621 &repo->global_gitconfig_author_name,
622 &repo->global_gitconfig_author_email,
623 NULL, NULL, NULL, NULL, NULL, NULL,
624 global_gitconfig_path);
629 /* Read repository's .git/config file. */
630 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
631 if (repo_gitconfig_path == NULL)
632 return got_error_from_errno("got_repo_get_path_gitconfig");
634 err = got_repo_read_gitconfig(
635 &repo->gitconfig_repository_format_version,
636 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
637 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
638 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
639 &repo->nextensions, repo_gitconfig_path);
643 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
646 for (i = 0; i < repo->ngitconfig_remotes; i++) {
647 got_repo_free_remote_repo_data(
648 &repo->gitconfig_remotes[i]);
650 free(repo->gitconfig_remotes);
651 repo->gitconfig_remotes = NULL;
652 repo->ngitconfig_remotes = 0;
654 free(repo->gitconfig_author_name);
655 repo->gitconfig_author_name = NULL;
656 free(repo->gitconfig_author_email);
657 repo->gitconfig_author_email = NULL;
659 free(repo->global_gitconfig_author_name);
660 repo->global_gitconfig_author_name = NULL;
661 free(repo->global_gitconfig_author_email);
662 repo->global_gitconfig_author_email = NULL;
666 free(repo_gitconfig_path);
670 static const struct got_error *
671 read_gotconfig(struct got_repository *repo)
673 const struct got_error *err = NULL;
674 char *gotconfig_path;
676 gotconfig_path = got_repo_get_path_gotconfig(repo);
677 if (gotconfig_path == NULL)
678 return got_error_from_errno("got_repo_get_path_gotconfig");
680 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
681 free(gotconfig_path);
685 /* Supported repository format extensions. */
686 static const char *const repo_extensions[] = {
687 "noop", /* Got supports repository format version 1. */
688 "preciousObjects", /* Supported by gotadmin cleanup. */
689 "worktreeConfig", /* Got does not care about Git work trees. */
692 const struct got_error *
693 got_repo_open(struct got_repository **repop, const char *path,
694 const char *global_gitconfig_path, int *pack_fds)
696 struct got_repository *repo = NULL;
697 const struct got_error *err = NULL;
698 char *repo_path = NULL;
703 repo = calloc(1, sizeof(*repo));
705 return got_error_from_errno("calloc");
707 RB_INIT(&repo->packidx_bloom_filters);
708 TAILQ_INIT(&repo->packidx_paths);
710 for (i = 0; i < nitems(repo->privsep_children); i++) {
711 memset(&repo->privsep_children[i], 0,
712 sizeof(repo->privsep_children[0]));
713 repo->privsep_children[i].imsg_fd = -1;
716 err = got_object_cache_init(&repo->objcache,
717 GOT_OBJECT_CACHE_TYPE_OBJ);
720 err = got_object_cache_init(&repo->treecache,
721 GOT_OBJECT_CACHE_TYPE_TREE);
724 err = got_object_cache_init(&repo->commitcache,
725 GOT_OBJECT_CACHE_TYPE_COMMIT);
728 err = got_object_cache_init(&repo->tagcache,
729 GOT_OBJECT_CACHE_TYPE_TAG);
732 err = got_object_cache_init(&repo->rawcache,
733 GOT_OBJECT_CACHE_TYPE_RAW);
737 err = get_pack_cache_size(&repo->pack_cache_size);
740 for (i = 0; i < nitems(repo->packs); i++) {
741 if (pack_fds != NULL && i < repo->pack_cache_size) {
742 repo->packs[i].basefd = pack_fds[j++];
743 repo->packs[i].accumfd = pack_fds[j++];
745 repo->packs[i].basefd = -1;
746 repo->packs[i].accumfd = -1;
749 for (i = 0; i < nitems(repo->tempfiles); i++)
750 repo->tempfiles[i] = -1;
751 repo->pinned_pack = -1;
752 repo->pinned_packidx = -1;
753 repo->pinned_pid = 0;
755 repo_path = realpath(path, NULL);
756 if (repo_path == NULL) {
757 err = got_error_from_errno2("realpath", path);
764 err = open_repo(repo, repo_path);
767 if (err->code != GOT_ERR_NOT_GIT_REPO)
769 if (repo_path[0] == '/' && repo_path[1] == '\0') {
770 err = got_error(GOT_ERR_NOT_GIT_REPO);
773 err = got_path_dirname(&parent_path, repo_path);
777 repo_path = parent_path;
780 err = read_gotconfig(repo);
784 err = read_gitconfig(repo, global_gitconfig_path);
787 if (repo->gitconfig_repository_format_version != 0) {
788 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
791 for (i = 0; i < repo->nextensions; i++) {
792 char *ext = repo->extnames[i];
793 char *val = repo->extvals[i];
794 int j, supported = 0;
796 if (!is_boolean_val(val)) {
797 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
801 if (!get_boolean_val(val))
804 for (j = 0; j < nitems(repo_extensions); j++) {
805 if (strcmp(ext, repo_extensions[j]) == 0) {
811 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
816 err = got_repo_list_packidx(&repo->packidx_paths, repo);
819 got_repo_close(repo);
826 const struct got_error *
827 got_repo_close(struct got_repository *repo)
829 const struct got_error *err = NULL, *child_err;
830 struct got_packidx_bloom_filter *bf;
833 for (i = 0; i < repo->pack_cache_size; i++) {
834 if (repo->packidx_cache[i] == NULL)
836 got_packidx_close(repo->packidx_cache[i]);
839 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
840 &repo->packidx_bloom_filters))) {
841 RB_REMOVE(got_packidx_bloom_filter_tree,
842 &repo->packidx_bloom_filters, bf);
843 bloom_free(bf->bloom);
848 for (i = 0; i < repo->pack_cache_size; i++)
849 if (repo->packs[i].path_packfile)
850 if (repo->packs[i].path_packfile)
851 got_pack_close(&repo->packs[i]);
854 free(repo->path_git_dir);
856 got_object_cache_close(&repo->objcache);
857 got_object_cache_close(&repo->treecache);
858 got_object_cache_close(&repo->commitcache);
859 got_object_cache_close(&repo->tagcache);
860 got_object_cache_close(&repo->rawcache);
862 for (i = 0; i < nitems(repo->privsep_children); i++) {
863 if (repo->privsep_children[i].imsg_fd == -1)
865 imsg_clear(repo->privsep_children[i].ibuf);
866 free(repo->privsep_children[i].ibuf);
867 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
868 child_err = got_privsep_wait_for_child(
869 repo->privsep_children[i].pid);
870 if (child_err && err == NULL)
872 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
874 err = got_error_from_errno("close");
877 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
879 err = got_error_from_errno("close");
882 got_gotconfig_free(repo->gotconfig);
883 free(repo->gitconfig_author_name);
884 free(repo->gitconfig_author_email);
885 for (i = 0; i < repo->ngitconfig_remotes; i++)
886 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
887 free(repo->gitconfig_remotes);
888 for (i = 0; i < repo->nextensions; i++) {
889 free(repo->extnames[i]);
890 free(repo->extvals[i]);
892 free(repo->extnames);
895 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
902 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
908 free(repo->fetch_url);
909 repo->fetch_url = NULL;
910 free(repo->send_url);
911 repo->send_url = NULL;
912 for (i = 0; i < repo->nfetch_branches; i++)
913 free(repo->fetch_branches[i]);
914 free(repo->fetch_branches);
915 repo->fetch_branches = NULL;
916 repo->nfetch_branches = 0;
917 for (i = 0; i < repo->nsend_branches; i++)
918 free(repo->send_branches[i]);
919 free(repo->send_branches);
920 repo->send_branches = NULL;
921 repo->nsend_branches = 0;
924 const struct got_error *
925 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
926 const char *input_path)
928 const struct got_error *err = NULL;
929 const char *repo_abspath = NULL;
931 char *canonpath, *path = NULL;
933 *in_repo_path = NULL;
935 canonpath = strdup(input_path);
936 if (canonpath == NULL) {
937 err = got_error_from_errno("strdup");
940 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
944 repo_abspath = got_repo_get_path(repo);
946 if (canonpath[0] == '\0') {
947 path = strdup(canonpath);
949 err = got_error_from_errno("strdup");
953 path = realpath(canonpath, NULL);
955 if (errno != ENOENT) {
956 err = got_error_from_errno2("realpath",
961 * Path is not on disk.
962 * Assume it is already relative to repository root.
964 path = strdup(canonpath);
966 err = got_error_from_errno("strdup");
971 repolen = strlen(repo_abspath);
975 if (strcmp(path, repo_abspath) == 0) {
979 err = got_error_from_errno("strdup");
982 } else if (len > repolen &&
983 got_path_is_child(path, repo_abspath, repolen)) {
984 /* Matched an on-disk path inside repository. */
985 if (got_repo_is_bare(repo)) {
987 * Matched an on-disk path inside repository
988 * database. Treat input as repository-relative.
995 /* Strip common prefix with repository path. */
996 err = got_path_skip_common_ancestor(&child,
1005 * Matched unrelated on-disk path.
1006 * Treat input as repository-relative.
1014 /* Make in-repository path absolute */
1015 if (path[0] != '/') {
1017 if (asprintf(&abspath, "/%s", path) == -1) {
1018 err = got_error_from_errno("asprintf");
1030 *in_repo_path = path;
1034 static const struct got_error *
1035 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1036 const char *path_packidx)
1038 const struct got_error *err = NULL;
1041 for (i = 0; i < repo->pack_cache_size; i++) {
1042 if (repo->packidx_cache[i] == NULL)
1044 if (strcmp(repo->packidx_cache[i]->path_packidx,
1045 path_packidx) == 0) {
1046 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1049 if (i == repo->pack_cache_size) {
1052 } while (i > 0 && repo->pinned_packidx >= 0 &&
1053 i == repo->pinned_packidx);
1054 err = got_packidx_close(repo->packidx_cache[i]);
1059 repo->packidx_cache[i] = packidx;
1065 got_repo_is_packidx_filename(const char *name, size_t len)
1067 if (len != GOT_PACKIDX_NAMELEN)
1070 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1073 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1074 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1080 static struct got_packidx_bloom_filter *
1081 get_packidx_bloom_filter(struct got_repository *repo,
1082 const char *path, size_t path_len)
1084 struct got_packidx_bloom_filter key;
1086 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1087 return NULL; /* XXX */
1088 key.path_len = path_len;
1090 return RB_FIND(got_packidx_bloom_filter_tree,
1091 &repo->packidx_bloom_filters, &key);
1095 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1096 const char *path_packidx, struct got_object_id *id)
1098 struct got_packidx_bloom_filter *bf;
1100 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1102 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1104 /* No bloom filter means this pack index must be searched. */
1108 static const struct got_error *
1109 add_packidx_bloom_filter(struct got_repository *repo,
1110 struct got_packidx *packidx, const char *path_packidx)
1112 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1113 struct got_packidx_bloom_filter *bf;
1117 * Don't use bloom filters for very large pack index files.
1118 * Large pack files will contain a relatively large fraction
1119 * of our objects so we will likely need to visit them anyway.
1120 * The more objects a pack file contains the higher the probability
1121 * of a false-positive match from the bloom filter. And reading
1122 * all object IDs from a large pack index file can be expensive.
1124 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1127 /* Do we already have a filter for this pack index? */
1128 if (get_packidx_bloom_filter(repo, path_packidx,
1129 strlen(path_packidx)) != NULL)
1132 bf = calloc(1, sizeof(*bf));
1134 return got_error_from_errno("calloc");
1135 bf->bloom = calloc(1, sizeof(*bf->bloom));
1136 if (bf->bloom == NULL) {
1138 return got_error_from_errno("calloc");
1141 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1142 if (len >= sizeof(bf->path)) {
1145 return got_error(GOT_ERR_NO_SPACE);
1149 /* Minimum size supported by our bloom filter is 1000 entries. */
1150 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1151 for (i = 0; i < nobjects; i++) {
1152 struct got_packidx_object_id *id;
1153 id = &packidx->hdr.sorted_ids[i];
1154 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1157 RB_INSERT(got_packidx_bloom_filter_tree,
1158 &repo->packidx_bloom_filters, bf);
1163 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1165 struct got_pathlist_entry *pe;
1167 while (!TAILQ_EMPTY(packidx_paths)) {
1168 pe = TAILQ_FIRST(packidx_paths);
1169 TAILQ_REMOVE(packidx_paths, pe, entry);
1170 free((char *)pe->path);
1175 static const struct got_error *
1176 refresh_packidx_paths(struct got_repository *repo)
1178 const struct got_error *err = NULL;
1179 char *objects_pack_dir = NULL;
1182 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1183 if (objects_pack_dir == NULL)
1184 return got_error_from_errno("got_repo_get_path_objects_pack");
1186 if (stat(objects_pack_dir, &sb) == -1) {
1187 if (errno != ENOENT) {
1188 err = got_error_from_errno2("stat", objects_pack_dir);
1191 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1192 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1193 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1194 purge_packidx_paths(&repo->packidx_paths);
1195 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1200 free(objects_pack_dir);
1204 const struct got_error *
1205 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1206 struct got_repository *repo, struct got_object_id *id)
1208 const struct got_error *err;
1209 struct got_pathlist_entry *pe;
1212 /* Search pack index cache. */
1213 for (i = 0; i < repo->pack_cache_size; i++) {
1214 if (repo->packidx_cache[i] == NULL)
1216 if (!got_repo_check_packidx_bloom_filter(repo,
1217 repo->packidx_cache[i]->path_packidx, id))
1218 continue; /* object will not be found in this index */
1219 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1221 *packidx = repo->packidx_cache[i];
1223 * Move this cache entry to the front. Repeatedly
1224 * searching a wrong pack index can be expensive.
1227 memmove(&repo->packidx_cache[1],
1228 &repo->packidx_cache[0],
1229 i * sizeof(repo->packidx_cache[0]));
1230 repo->packidx_cache[0] = *packidx;
1231 if (repo->pinned_packidx >= 0 &&
1232 repo->pinned_packidx < i)
1233 repo->pinned_packidx++;
1234 else if (repo->pinned_packidx == i)
1235 repo->pinned_packidx = 0;
1240 /* No luck. Search the filesystem. */
1242 err = refresh_packidx_paths(repo);
1246 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1247 const char *path_packidx = pe->path;
1250 if (!got_repo_check_packidx_bloom_filter(repo,
1252 continue; /* object will not be found in this index */
1254 for (i = 0; i < repo->pack_cache_size; i++) {
1255 if (repo->packidx_cache[i] == NULL)
1257 if (strcmp(repo->packidx_cache[i]->path_packidx,
1258 path_packidx) == 0) {
1264 continue; /* already searched */
1266 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1271 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1275 err = cache_packidx(repo, *packidx, path_packidx);
1279 *idx = got_packidx_get_object_idx(*packidx, id);
1281 err = NULL; /* found the object */
1286 err = got_error_no_obj(id);
1291 const struct got_error *
1292 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1293 struct got_repository *repo)
1295 const struct got_error *err = NULL;
1296 DIR *packdir = NULL;
1297 struct dirent *dent;
1298 char *path_packidx = NULL;
1302 packdir_fd = openat(got_repo_get_fd(repo),
1303 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1304 if (packdir_fd == -1) {
1305 return got_error_from_errno_fmt("openat: %s/%s",
1306 got_repo_get_path_git_dir(repo),
1307 GOT_OBJECTS_PACK_DIR);
1310 packdir = fdopendir(packdir_fd);
1311 if (packdir == NULL) {
1312 err = got_error_from_errno("fdopendir");
1316 if (fstat(packdir_fd, &sb) == -1) {
1317 err = got_error_from_errno("fstat");
1320 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1321 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1323 while ((dent = readdir(packdir)) != NULL) {
1324 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1327 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1328 dent->d_name) == -1) {
1329 err = got_error_from_errno("asprintf");
1330 path_packidx = NULL;
1334 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1341 if (packdir && closedir(packdir) != 0 && err == NULL)
1342 err = got_error_from_errno("closedir");
1346 const struct got_error *
1347 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1348 struct got_repository *repo)
1350 const struct got_error *err;
1355 /* Search pack index cache. */
1356 for (i = 0; i < repo->pack_cache_size; i++) {
1357 if (repo->packidx_cache[i] == NULL)
1359 if (strcmp(repo->packidx_cache[i]->path_packidx,
1360 path_packidx) == 0) {
1361 *packidx = repo->packidx_cache[i];
1365 /* No luck. Search the filesystem. */
1367 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1372 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1376 err = cache_packidx(repo, *packidx, path_packidx);
1379 got_packidx_close(*packidx);
1385 static const struct got_error *
1386 read_packfile_hdr(int fd, struct got_packidx *packidx)
1388 const struct got_error *err = NULL;
1389 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1390 struct got_packfile_hdr hdr;
1393 n = read(fd, &hdr, sizeof(hdr));
1395 return got_error_from_errno("read");
1396 if (n != sizeof(hdr))
1397 return got_error(GOT_ERR_BAD_PACKFILE);
1399 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1400 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1401 be32toh(hdr.nobjects) != totobj)
1402 err = got_error(GOT_ERR_BAD_PACKFILE);
1407 static const struct got_error *
1408 open_packfile(int *fd, struct got_repository *repo,
1409 const char *relpath, struct got_packidx *packidx)
1411 const struct got_error *err = NULL;
1413 *fd = openat(got_repo_get_fd(repo), relpath,
1414 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1416 return got_error_from_errno_fmt("openat: %s/%s",
1417 got_repo_get_path_git_dir(repo), relpath);
1420 err = read_packfile_hdr(*fd, packidx);
1430 const struct got_error *
1431 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1432 const char *path_packfile, struct got_packidx *packidx)
1434 const struct got_error *err = NULL;
1435 struct got_pack *pack = NULL;
1442 for (i = 0; i < repo->pack_cache_size; i++) {
1443 pack = &repo->packs[i];
1444 if (pack->path_packfile == NULL)
1446 if (strcmp(pack->path_packfile, path_packfile) == 0)
1447 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1450 if (i == repo->pack_cache_size) {
1451 struct got_pack tmp;
1454 } while (i > 0 && repo->pinned_pack >= 0 &&
1455 i == repo->pinned_pack);
1456 err = got_pack_close(&repo->packs[i]);
1459 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1460 return got_error_from_errno("ftruncate");
1461 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1462 return got_error_from_errno("ftruncate");
1463 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1464 memcpy(&repo->packs[i], &repo->packs[0],
1465 sizeof(repo->packs[i]));
1466 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1467 if (repo->pinned_pack == 0)
1468 repo->pinned_pack = i;
1469 else if (repo->pinned_pack == i)
1470 repo->pinned_pack = 0;
1474 pack = &repo->packs[i];
1476 pack->path_packfile = strdup(path_packfile);
1477 if (pack->path_packfile == NULL) {
1478 err = got_error_from_errno("strdup");
1482 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1486 if (fstat(pack->fd, &sb) != 0) {
1487 err = got_error_from_errno("fstat");
1490 pack->filesize = sb.st_size;
1492 pack->privsep_child = NULL;
1494 err = got_delta_cache_alloc(&pack->delta_cache);
1498 #ifndef GOT_PACK_NO_MMAP
1499 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1500 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1502 if (pack->map == MAP_FAILED) {
1503 if (errno != ENOMEM) {
1504 err = got_error_from_errno("mmap");
1507 pack->map = NULL; /* fall back to read(2) */
1514 got_pack_close(pack);
1521 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1523 struct got_pack *pack = NULL;
1526 for (i = 0; i < repo->pack_cache_size; i++) {
1527 pack = &repo->packs[i];
1528 if (pack->path_packfile == NULL)
1530 if (strcmp(pack->path_packfile, path_packfile) == 0)
1537 const struct got_error *
1538 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1539 struct got_pack *pack)
1542 int pinned_pack = -1, pinned_packidx = -1;
1544 for (i = 0; i < repo->pack_cache_size; i++) {
1545 if (repo->packidx_cache[i] &&
1546 strcmp(repo->packidx_cache[i]->path_packidx,
1547 packidx->path_packidx) == 0)
1549 if (repo->packs[i].path_packfile &&
1550 strcmp(repo->packs[i].path_packfile,
1551 pack->path_packfile) == 0)
1555 if (pinned_packidx == -1 || pinned_pack == -1)
1556 return got_error(GOT_ERR_PIN_PACK);
1558 repo->pinned_pack = pinned_pack;
1559 repo->pinned_packidx = pinned_packidx;
1560 if (repo->packs[pinned_pack].privsep_child)
1561 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1566 got_repo_get_pinned_pack(struct got_repository *repo)
1568 if (repo->pinned_pack >= 0 &&
1569 repo->pinned_pack < repo->pack_cache_size)
1570 return &repo->packs[repo->pinned_pack];
1576 got_repo_unpin_pack(struct got_repository *repo)
1578 repo->pinned_packidx = -1;
1579 repo->pinned_pack = -1;
1580 repo->pinned_pid = 0;
1583 const struct got_error *
1584 got_repo_init(const char *repo_path, const char *head_name)
1586 const struct got_error *err = NULL;
1587 const char *dirnames[] = {
1589 GOT_OBJECTS_PACK_DIR,
1592 const char *description_str = "Unnamed repository; "
1593 "edit this file 'description' to name the repository.";
1594 const char *headref = "ref: refs/heads/";
1595 const char *gitconfig_str = "[core]\n"
1596 "\trepositoryformatversion = 0\n"
1597 "\tfilemode = true\n"
1599 char *headref_str, *path;
1602 if (!got_path_dir_is_empty(repo_path))
1603 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1605 for (i = 0; i < nitems(dirnames); i++) {
1606 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1607 return got_error_from_errno("asprintf");
1609 err = got_path_mkdir(path);
1615 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1616 return got_error_from_errno("asprintf");
1617 err = got_path_create_file(path, description_str);
1622 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1623 return got_error_from_errno("asprintf");
1624 if (asprintf(&headref_str, "%s%s", headref,
1625 head_name ? head_name : "main") == -1) {
1627 return got_error_from_errno("asprintf");
1629 err = got_path_create_file(path, headref_str);
1635 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1636 return got_error_from_errno("asprintf");
1637 err = got_path_create_file(path, gitconfig_str);
1645 static const struct got_error *
1646 match_packed_object(struct got_object_id **unique_id,
1647 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1649 const struct got_error *err = NULL;
1650 struct got_object_id_queue matched_ids;
1651 struct got_pathlist_entry *pe;
1653 STAILQ_INIT(&matched_ids);
1655 err = refresh_packidx_paths(repo);
1659 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1660 const char *path_packidx = pe->path;
1661 struct got_packidx *packidx;
1662 struct got_object_qid *qid;
1664 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1669 err = got_packidx_match_id_str_prefix(&matched_ids,
1670 packidx, id_str_prefix);
1672 got_packidx_close(packidx);
1675 err = got_packidx_close(packidx);
1679 STAILQ_FOREACH(qid, &matched_ids, entry) {
1680 if (obj_type != GOT_OBJ_TYPE_ANY) {
1682 err = got_object_get_type(&matched_type, repo,
1686 if (matched_type != obj_type)
1689 if (*unique_id == NULL) {
1690 *unique_id = got_object_id_dup(&qid->id);
1691 if (*unique_id == NULL) {
1692 err = got_error_from_errno("malloc");
1696 if (got_object_id_cmp(*unique_id,
1698 continue; /* packed multiple times */
1699 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1705 got_object_id_queue_free(&matched_ids);
1713 static const struct got_error *
1714 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1715 const char *object_dir, const char *id_str_prefix, int obj_type,
1716 struct got_repository *repo)
1718 const struct got_error *err = NULL;
1719 char *path, *id_str = NULL;
1721 struct dirent *dent;
1722 struct got_object_id id;
1724 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1725 err = got_error_from_errno("asprintf");
1729 dir = opendir(path);
1731 if (errno == ENOENT) {
1735 err = got_error_from_errno2("opendir", path);
1738 while ((dent = readdir(dir)) != NULL) {
1744 if (strcmp(dent->d_name, ".") == 0 ||
1745 strcmp(dent->d_name, "..") == 0)
1748 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1749 err = got_error_from_errno("asprintf");
1753 if (!got_parse_sha1_digest(id.sha1, id_str))
1757 * Directory entries do not necessarily appear in
1758 * sorted order, so we must iterate over all of them.
1760 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1764 if (*unique_id == NULL) {
1765 if (obj_type != GOT_OBJ_TYPE_ANY) {
1767 err = got_object_get_type(&matched_type, repo,
1771 if (matched_type != obj_type)
1774 *unique_id = got_object_id_dup(&id);
1775 if (*unique_id == NULL) {
1776 err = got_error_from_errno("got_object_id_dup");
1780 if (got_object_id_cmp(*unique_id, &id) == 0)
1781 continue; /* both packed and loose */
1782 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1787 if (dir && closedir(dir) != 0 && err == NULL)
1788 err = got_error_from_errno("closedir");
1798 const struct got_error *
1799 got_repo_match_object_id_prefix(struct got_object_id **id,
1800 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1802 const struct got_error *err = NULL;
1803 char *path_objects = NULL, *object_dir = NULL;
1809 path_objects = got_repo_get_path_objects(repo);
1811 len = strlen(id_str_prefix);
1812 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1813 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1817 for (i = 0; i < len; i++) {
1818 if (isxdigit((unsigned char)id_str_prefix[i]))
1820 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1825 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1828 object_dir = strndup(id_str_prefix, 2);
1829 if (object_dir == NULL) {
1830 err = got_error_from_errno("strdup");
1833 err = match_loose_object(id, path_objects, object_dir,
1834 id_str_prefix, obj_type, repo);
1835 } else if (len == 1) {
1837 for (i = 0; i < 0xf; i++) {
1838 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1840 err = got_error_from_errno("asprintf");
1843 err = match_packed_object(id, repo, object_dir,
1847 err = match_loose_object(id, path_objects, object_dir,
1848 id_str_prefix, obj_type, repo);
1853 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1862 } else if (*id == NULL) {
1864 case GOT_OBJ_TYPE_BLOB:
1865 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1866 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1868 case GOT_OBJ_TYPE_TREE:
1869 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1870 GOT_OBJ_LABEL_TREE, id_str_prefix);
1872 case GOT_OBJ_TYPE_COMMIT:
1873 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1874 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1876 case GOT_OBJ_TYPE_TAG:
1877 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1878 GOT_OBJ_LABEL_TAG, id_str_prefix);
1881 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1889 const struct got_error *
1890 got_repo_match_object_id(struct got_object_id **id, char **label,
1891 const char *id_str, int obj_type, struct got_reflist_head *refs,
1892 struct got_repository *repo)
1894 const struct got_error *err;
1895 struct got_tag_object *tag;
1896 struct got_reference *ref = NULL;
1903 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1906 *id = got_object_id_dup(
1907 got_object_tag_get_object_id(tag));
1909 err = got_error_from_errno("got_object_id_dup");
1910 else if (label && asprintf(label, "refs/tags/%s",
1911 got_object_tag_get_name(tag)) == -1) {
1912 err = got_error_from_errno("asprintf");
1916 got_object_tag_close(tag);
1918 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1919 err->code != GOT_ERR_NO_OBJ)
1923 err = got_ref_open(&ref, repo, id_str, 0);
1925 err = got_ref_resolve(id, repo, ref);
1929 *label = strdup(got_ref_get_name(ref));
1930 if (*label == NULL) {
1931 err = got_error_from_errno("strdup");
1936 if (err->code != GOT_ERR_NOT_REF &&
1937 err->code != GOT_ERR_BAD_REF_NAME)
1939 err = got_repo_match_object_id_prefix(id, id_str,
1942 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1943 err = got_error_not_ref(id_str);
1947 err = got_object_id_str(label, *id);
1948 if (*label == NULL) {
1949 err = got_error_from_errno("strdup");
1960 const struct got_error *
1961 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1962 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1964 const struct got_error *err = NULL;
1965 struct got_reflist_entry *re;
1966 struct got_object_id *tag_id;
1967 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1971 TAILQ_FOREACH(re, refs, entry) {
1972 const char *refname;
1973 refname = got_ref_get_name(re->ref);
1974 if (got_ref_is_symbolic(re->ref))
1976 if (strncmp(refname, "refs/tags/", 10) != 0)
1978 if (!name_is_absolute)
1979 refname += strlen("refs/tags/");
1980 if (strcmp(refname, name) != 0)
1982 err = got_ref_resolve(&tag_id, repo, re->ref);
1985 err = got_object_open_as_tag(tag, repo, tag_id);
1989 if (obj_type == GOT_OBJ_TYPE_ANY ||
1990 got_object_tag_get_object_type(*tag) == obj_type)
1992 got_object_tag_close(*tag);
1996 if (err == NULL && *tag == NULL)
1997 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1998 GOT_OBJ_LABEL_TAG, name);
2002 static const struct got_error *
2003 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2004 const char *name, mode_t mode, struct got_object_id *blob_id)
2006 const struct got_error *err = NULL;
2010 *new_te = calloc(1, sizeof(**new_te));
2011 if (*new_te == NULL)
2012 return got_error_from_errno("calloc");
2014 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2015 sizeof((*new_te)->name)) {
2016 err = got_error(GOT_ERR_NO_SPACE);
2020 if (S_ISLNK(mode)) {
2021 (*new_te)->mode = S_IFLNK;
2023 (*new_te)->mode = S_IFREG;
2024 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2026 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2028 if (err && *new_te) {
2035 static const struct got_error *
2036 import_file(struct got_tree_entry **new_te, struct dirent *de,
2037 const char *path, struct got_repository *repo)
2039 const struct got_error *err;
2040 struct got_object_id *blob_id = NULL;
2044 if (asprintf(&filepath, "%s%s%s", path,
2045 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2046 return got_error_from_errno("asprintf");
2048 if (lstat(filepath, &sb) != 0) {
2049 err = got_error_from_errno2("lstat", path);
2053 err = got_object_blob_create(&blob_id, filepath, repo);
2057 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2066 static const struct got_error *
2067 insert_tree_entry(struct got_tree_entry *new_te,
2068 struct got_pathlist_head *paths)
2070 const struct got_error *err = NULL;
2071 struct got_pathlist_entry *new_pe;
2073 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2077 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2081 static const struct got_error *write_tree(struct got_object_id **,
2082 const char *, struct got_pathlist_head *, struct got_repository *,
2083 got_repo_import_cb progress_cb, void *progress_arg);
2085 static const struct got_error *
2086 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2087 const char *path, struct got_pathlist_head *ignores,
2088 struct got_repository *repo,
2089 got_repo_import_cb progress_cb, void *progress_arg)
2091 const struct got_error *err;
2092 struct got_object_id *id = NULL;
2095 if (asprintf(&subdirpath, "%s%s%s", path,
2096 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2097 return got_error_from_errno("asprintf");
2099 (*new_te) = calloc(1, sizeof(**new_te));
2100 if (*new_te == NULL)
2101 return got_error_from_errno("calloc");
2102 (*new_te)->mode = S_IFDIR;
2103 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2104 sizeof((*new_te)->name)) {
2105 err = got_error(GOT_ERR_NO_SPACE);
2108 err = write_tree(&id, subdirpath, ignores, repo,
2109 progress_cb, progress_arg);
2112 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2124 static const struct got_error *
2125 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2126 struct got_pathlist_head *ignores, struct got_repository *repo,
2127 got_repo_import_cb progress_cb, void *progress_arg)
2129 const struct got_error *err = NULL;
2133 struct got_tree_entry *new_te = NULL;
2134 struct got_pathlist_head paths;
2135 struct got_pathlist_entry *pe;
2137 *new_tree_id = NULL;
2141 dir = opendir(path_dir);
2143 err = got_error_from_errno2("opendir", path_dir);
2148 while ((de = readdir(dir)) != NULL) {
2152 if (strcmp(de->d_name, ".") == 0 ||
2153 strcmp(de->d_name, "..") == 0)
2156 err = got_path_dirent_type(&type, path_dir, de);
2160 TAILQ_FOREACH(pe, ignores, entry) {
2161 if (type == DT_DIR && pe->path_len > 0 &&
2162 pe->path[pe->path_len - 1] == '/') {
2163 char stripped[PATH_MAX];
2165 if (strlcpy(stripped, pe->path,
2166 sizeof(stripped)) >= sizeof(stripped)) {
2167 err = got_error(GOT_ERR_NO_SPACE);
2170 got_path_strip_trailing_slashes(stripped);
2171 if (fnmatch(stripped, de->d_name, 0) == 0) {
2175 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2183 if (type == DT_DIR) {
2184 err = import_subdir(&new_te, de, path_dir,
2185 ignores, repo, progress_cb, progress_arg);
2187 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2192 } else if (type == DT_REG || type == DT_LNK) {
2193 err = import_file(&new_te, de, path_dir, repo);
2199 err = insert_tree_entry(new_te, &paths);
2205 if (TAILQ_EMPTY(&paths)) {
2206 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2207 "cannot create tree without any entries");
2211 TAILQ_FOREACH(pe, &paths, entry) {
2212 struct got_tree_entry *te = pe->data;
2214 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2216 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2217 err = got_error_from_errno("asprintf");
2220 err = (*progress_cb)(progress_arg, path);
2226 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2230 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2234 const struct got_error *
2235 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2236 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2237 struct got_repository *repo, got_repo_import_cb progress_cb,
2240 const struct got_error *err;
2241 struct got_object_id *new_tree_id;
2243 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2244 progress_cb, progress_arg);
2248 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2249 author, time(NULL), author, time(NULL), logmsg, repo);
2254 const struct got_error *
2255 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2256 struct got_repository *repo)
2258 const struct got_error *err = NULL;
2259 char *path_objects = NULL, *path = NULL;
2261 struct got_object_id id;
2267 path_objects = got_repo_get_path_objects(repo);
2268 if (path_objects == NULL)
2269 return got_error_from_errno("got_repo_get_path_objects");
2271 for (i = 0; i <= 0xff; i++) {
2272 struct dirent *dent;
2274 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2275 err = got_error_from_errno("asprintf");
2279 dir = opendir(path);
2281 if (errno == ENOENT) {
2285 err = got_error_from_errno2("opendir", path);
2289 while ((dent = readdir(dir)) != NULL) {
2294 if (strcmp(dent->d_name, ".") == 0 ||
2295 strcmp(dent->d_name, "..") == 0)
2298 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2299 err = got_error_from_errno("asprintf");
2303 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2309 err = got_object_open_loose_fd(&fd, &id, repo);
2313 if (fstat(fd, &sb) == -1) {
2314 err = got_error_from_errno("fstat");
2319 (*ondisk_size) += sb.st_size;
2321 if (close(fd) == -1) {
2322 err = got_error_from_errno("close");
2327 if (closedir(dir) != 0) {
2328 err = got_error_from_errno("closedir");
2337 if (dir && closedir(dir) != 0 && err == NULL)
2338 err = got_error_from_errno("closedir");
2349 const struct got_error *
2350 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2351 off_t *total_packsize, struct got_repository *repo)
2353 const struct got_error *err = NULL;
2354 DIR *packdir = NULL;
2355 struct dirent *dent;
2356 struct got_packidx *packidx = NULL;
2358 char *path_packfile;
2364 *total_packsize = 0;
2366 packdir_fd = openat(got_repo_get_fd(repo),
2367 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2368 if (packdir_fd == -1) {
2369 return got_error_from_errno_fmt("openat: %s/%s",
2370 got_repo_get_path_git_dir(repo),
2371 GOT_OBJECTS_PACK_DIR);
2374 packdir = fdopendir(packdir_fd);
2375 if (packdir == NULL) {
2376 err = got_error_from_errno("fdopendir");
2380 while ((dent = readdir(packdir)) != NULL) {
2381 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2384 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2385 dent->d_name) == -1) {
2386 err = got_error_from_errno("asprintf");
2390 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2396 if (fstat(packidx->fd, &sb) == -1)
2398 *total_packsize += sb.st_size;
2400 err = got_packidx_get_packfile_path(&path_packfile,
2401 packidx->path_packidx);
2405 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2407 free(path_packfile);
2410 free(path_packfile);
2411 *total_packsize += sb.st_size;
2413 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2417 got_packidx_close(packidx);
2422 got_packidx_close(packidx);
2423 if (packdir && closedir(packdir) != 0 && err == NULL)
2424 err = got_error_from_errno("closedir");
2428 *total_packsize = 0;
2433 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2434 got_packidx_bloom_filter_cmp);