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>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_object.h"
50 #include "got_opentemp.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_delta_cache.h"
54 #include "got_lib_inflate.h"
55 #include "got_lib_object.h"
56 #include "got_lib_object_parse.h"
57 #include "got_lib_object_create.h"
58 #include "got_lib_pack.h"
59 #include "got_lib_privsep.h"
60 #include "got_lib_sha1.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
63 #include "got_lib_gotconfig.h"
66 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
69 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
71 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
72 got_packidx_bloom_filter_cmp);
75 is_boolean_val(const char *val)
77 return (strcasecmp(val, "true") == 0 ||
78 strcasecmp(val, "false") == 0 ||
79 strcasecmp(val, "on") == 0 ||
80 strcasecmp(val, "off") == 0 ||
81 strcasecmp(val, "yes") == 0 ||
82 strcasecmp(val, "no") == 0 ||
83 strcasecmp(val, "1") == 0 ||
84 strcasecmp(val, "0") == 0);
88 get_boolean_val(const char *val)
90 return (strcasecmp(val, "true") == 0 ||
91 strcasecmp(val, "on") == 0 ||
92 strcasecmp(val, "yes") == 0 ||
93 strcasecmp(val, "1") == 0);
97 got_repo_get_path(struct got_repository *repo)
103 got_repo_get_path_git_dir(struct got_repository *repo)
105 return repo->path_git_dir;
109 got_repo_get_fd(struct got_repository *repo)
111 return repo->gitdir_fd;
115 got_repo_get_gitconfig_author_name(struct got_repository *repo)
117 return repo->gitconfig_author_name;
121 got_repo_get_gitconfig_author_email(struct got_repository *repo)
123 return repo->gitconfig_author_email;
127 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
129 return repo->global_gitconfig_author_name;
133 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
135 return repo->global_gitconfig_author_email;
139 got_repo_get_gitconfig_owner(struct got_repository *repo)
141 return repo->gitconfig_owner;
145 got_repo_has_extension(struct got_repository *repo, const char *ext)
149 for (i = 0; i < repo->nextensions; ++i) {
150 if (!strcasecmp(ext, repo->extnames[i]))
151 return get_boolean_val(repo->extvals[i]);
158 got_repo_is_bare(struct got_repository *repo)
160 return (strcmp(repo->path, repo->path_git_dir) == 0);
164 get_path_git_child(struct got_repository *repo, const char *basename)
168 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
176 got_repo_get_path_objects(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_OBJECTS_DIR);
182 got_repo_get_path_objects_pack(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
188 got_repo_get_path_refs(struct got_repository *repo)
190 return get_path_git_child(repo, GOT_REFS_DIR);
194 got_repo_get_path_packed_refs(struct got_repository *repo)
196 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
200 get_path_head(struct got_repository *repo)
202 return get_path_git_child(repo, GOT_HEAD_FILE);
206 got_repo_get_path_gitconfig(struct got_repository *repo)
208 return get_path_git_child(repo, GOT_GITCONFIG);
212 got_repo_get_path_gotconfig(struct got_repository *repo)
214 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
217 const struct got_gotconfig *
218 got_repo_get_gotconfig(struct got_repository *repo)
220 return repo->gotconfig;
224 got_repo_get_gitconfig_remotes(int *nremotes,
225 const struct got_remote_repo **remotes, struct got_repository *repo)
227 *nremotes = repo->ngitconfig_remotes;
228 *remotes = repo->gitconfig_remotes;
232 is_git_repo(struct got_repository *repo)
234 const char *path_git = got_repo_get_path_git_dir(repo);
235 char *path_objects = got_repo_get_path_objects(repo);
236 char *path_refs = got_repo_get_path_refs(repo);
237 char *path_head = get_path_head(repo);
240 struct got_reference *head_ref;
242 if (lstat(path_git, &sb) == -1)
244 if (!S_ISDIR(sb.st_mode))
247 if (lstat(path_objects, &sb) == -1)
249 if (!S_ISDIR(sb.st_mode))
252 if (lstat(path_refs, &sb) == -1)
254 if (!S_ISDIR(sb.st_mode))
257 if (lstat(path_head, &sb) == -1)
259 if (!S_ISREG(sb.st_mode))
262 /* Check if the HEAD reference can be opened. */
263 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
265 got_ref_close(head_ref);
276 static const struct got_error *
277 close_tempfiles(int *fds, size_t nfds)
279 const struct got_error *err = NULL;
282 for (i = 0; i < nfds; i++) {
285 if (close(fds[i]) == -1) {
286 err = got_error_from_errno("close");
294 static const struct got_error *
295 open_tempfiles(int **fds, size_t array_size, size_t nfds)
297 const struct got_error *err = NULL;
300 *fds = calloc(array_size, sizeof(**fds));
302 return got_error_from_errno("calloc");
304 for (i = 0; i < array_size; i++)
307 for (i = 0; i < nfds; i++) {
308 (*fds)[i] = got_opentempfd();
309 if ((*fds)[i] == -1) {
310 err = got_error_from_errno("got_opentempfd");
311 close_tempfiles(*fds, nfds);
320 static const struct got_error *
321 get_pack_cache_size(int *pack_cache_size)
325 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
326 return got_error_from_errno("getrlimit");
328 *pack_cache_size = GOT_PACK_CACHE_SIZE;
329 if (*pack_cache_size > rl.rlim_cur / 8)
330 *pack_cache_size = rl.rlim_cur / 8;
335 const struct got_error *
336 got_repo_pack_fds_open(int **pack_fds)
338 const struct got_error *err;
341 err = get_pack_cache_size(&nfds);
346 * We need one basefd and one accumfd per cached pack.
347 * Our constants should be set up in a way such that
348 * this error never triggers.
350 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
351 return got_error(GOT_ERR_NO_SPACE);
353 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
356 const struct got_error *
357 got_repo_pack_fds_close(int *pack_fds)
359 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
362 const struct got_error *
363 got_repo_temp_fds_open(int **temp_fds)
365 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
366 GOT_REPO_NUM_TEMPFILES);
370 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
374 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
375 repo->tempfiles[i] = temp_fds[i];
378 const struct got_error *
379 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
386 for (i = 0; i < nitems(repo->tempfiles); i++) {
387 if (repo->tempfile_use_mask & (1 << i))
389 if (repo->tempfiles[i] != -1) {
390 if (ftruncate(repo->tempfiles[i], 0L) == -1)
391 return got_error_from_errno("ftruncate");
392 *fd = repo->tempfiles[i];
394 repo->tempfile_use_mask |= (1 << i);
399 return got_error(GOT_ERR_REPO_TEMPFILE);
403 got_repo_temp_fds_put(int idx, struct got_repository *repo)
405 repo->tempfile_use_mask &= ~(1 << idx);
408 const struct got_error *
409 got_repo_temp_fds_close(int *temp_fds)
411 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
414 const struct got_error *
415 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
416 struct got_object *obj)
418 #ifndef GOT_NO_OBJ_CACHE
419 const struct got_error *err = NULL;
420 err = got_object_cache_add(&repo->objcache, id, obj);
422 if (err->code == GOT_ERR_OBJ_EXISTS ||
423 err->code == GOT_ERR_OBJ_TOO_LARGE)
433 got_repo_get_cached_object(struct got_repository *repo,
434 struct got_object_id *id)
436 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
439 const struct got_error *
440 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
441 struct got_tree_object *tree)
443 #ifndef GOT_NO_OBJ_CACHE
444 const struct got_error *err = NULL;
445 err = got_object_cache_add(&repo->treecache, id, tree);
447 if (err->code == GOT_ERR_OBJ_EXISTS ||
448 err->code == GOT_ERR_OBJ_TOO_LARGE)
457 struct got_tree_object *
458 got_repo_get_cached_tree(struct got_repository *repo,
459 struct got_object_id *id)
461 return (struct got_tree_object *)got_object_cache_get(
462 &repo->treecache, id);
465 const struct got_error *
466 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
467 struct got_commit_object *commit)
469 #ifndef GOT_NO_OBJ_CACHE
470 const struct got_error *err = NULL;
471 err = got_object_cache_add(&repo->commitcache, id, commit);
473 if (err->code == GOT_ERR_OBJ_EXISTS ||
474 err->code == GOT_ERR_OBJ_TOO_LARGE)
483 struct got_commit_object *
484 got_repo_get_cached_commit(struct got_repository *repo,
485 struct got_object_id *id)
487 return (struct got_commit_object *)got_object_cache_get(
488 &repo->commitcache, id);
491 const struct got_error *
492 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
493 struct got_tag_object *tag)
495 #ifndef GOT_NO_OBJ_CACHE
496 const struct got_error *err = NULL;
497 err = got_object_cache_add(&repo->tagcache, id, tag);
499 if (err->code == GOT_ERR_OBJ_EXISTS ||
500 err->code == GOT_ERR_OBJ_TOO_LARGE)
509 struct got_tag_object *
510 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
512 return (struct got_tag_object *)got_object_cache_get(
513 &repo->tagcache, id);
516 const struct got_error *
517 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
518 struct got_raw_object *raw)
520 #ifndef GOT_NO_OBJ_CACHE
521 const struct got_error *err = NULL;
522 err = got_object_cache_add(&repo->rawcache, id, raw);
524 if (err->code == GOT_ERR_OBJ_EXISTS ||
525 err->code == GOT_ERR_OBJ_TOO_LARGE)
535 struct got_raw_object *
536 got_repo_get_cached_raw_object(struct got_repository *repo,
537 struct got_object_id *id)
539 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
543 static const struct got_error *
544 open_repo(struct got_repository *repo, const char *path)
546 const struct got_error *err = NULL;
548 repo->gitdir_fd = -1;
550 /* bare git repository? */
551 repo->path_git_dir = strdup(path);
552 if (repo->path_git_dir == NULL)
553 return got_error_from_errno("strdup");
554 if (is_git_repo(repo)) {
555 repo->path = strdup(repo->path_git_dir);
556 if (repo->path == NULL) {
557 err = got_error_from_errno("strdup");
560 repo->gitdir_fd = open(repo->path_git_dir,
561 O_DIRECTORY | O_CLOEXEC);
562 if (repo->gitdir_fd == -1) {
563 err = got_error_from_errno2("open",
570 /* git repository with working tree? */
571 free(repo->path_git_dir);
572 repo->path_git_dir = NULL;
573 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
574 err = got_error_from_errno("asprintf");
577 if (is_git_repo(repo)) {
578 repo->path = strdup(path);
579 if (repo->path == NULL) {
580 err = got_error_from_errno("strdup");
583 repo->gitdir_fd = open(repo->path_git_dir,
584 O_DIRECTORY | O_CLOEXEC);
585 if (repo->gitdir_fd == -1) {
586 err = got_error_from_errno2("open",
593 err = got_error(GOT_ERR_NOT_GIT_REPO);
598 free(repo->path_git_dir);
599 repo->path_git_dir = NULL;
600 if (repo->gitdir_fd != -1)
601 close(repo->gitdir_fd);
602 repo->gitdir_fd = -1;
608 static const struct got_error *
609 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
611 const struct got_error *err = NULL;
612 char *repo_gitconfig_path = NULL;
614 if (global_gitconfig_path) {
615 /* Read settings from ~/.gitconfig. */
616 int dummy_repo_version;
617 err = got_repo_read_gitconfig(&dummy_repo_version,
618 &repo->global_gitconfig_author_name,
619 &repo->global_gitconfig_author_email,
620 NULL, NULL, NULL, NULL, NULL, NULL,
621 global_gitconfig_path);
626 /* Read repository's .git/config file. */
627 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
628 if (repo_gitconfig_path == NULL)
629 return got_error_from_errno("got_repo_get_path_gitconfig");
631 err = got_repo_read_gitconfig(
632 &repo->gitconfig_repository_format_version,
633 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
634 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
635 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
636 &repo->nextensions, repo_gitconfig_path);
640 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
643 for (i = 0; i < repo->ngitconfig_remotes; i++) {
644 got_repo_free_remote_repo_data(
645 &repo->gitconfig_remotes[i]);
647 free(repo->gitconfig_remotes);
648 repo->gitconfig_remotes = NULL;
649 repo->ngitconfig_remotes = 0;
651 free(repo->gitconfig_author_name);
652 repo->gitconfig_author_name = NULL;
653 free(repo->gitconfig_author_email);
654 repo->gitconfig_author_email = NULL;
656 free(repo->global_gitconfig_author_name);
657 repo->global_gitconfig_author_name = NULL;
658 free(repo->global_gitconfig_author_email);
659 repo->global_gitconfig_author_email = NULL;
663 free(repo_gitconfig_path);
667 static const struct got_error *
668 read_gotconfig(struct got_repository *repo)
670 const struct got_error *err = NULL;
671 char *gotconfig_path;
673 gotconfig_path = got_repo_get_path_gotconfig(repo);
674 if (gotconfig_path == NULL)
675 return got_error_from_errno("got_repo_get_path_gotconfig");
677 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
678 free(gotconfig_path);
682 /* Supported repository format extensions. */
683 static const char *const repo_extensions[] = {
684 "noop", /* Got supports repository format version 1. */
685 "preciousObjects", /* Supported by gotadmin cleanup. */
686 "worktreeConfig", /* Got does not care about Git work trees. */
689 const struct got_error *
690 got_repo_open(struct got_repository **repop, const char *path,
691 const char *global_gitconfig_path, int *pack_fds)
693 struct got_repository *repo = NULL;
694 const struct got_error *err = NULL;
695 char *repo_path = NULL;
700 repo = calloc(1, sizeof(*repo));
702 return got_error_from_errno("calloc");
704 RB_INIT(&repo->packidx_bloom_filters);
705 TAILQ_INIT(&repo->packidx_paths);
707 for (i = 0; i < nitems(repo->privsep_children); i++) {
708 memset(&repo->privsep_children[i], 0,
709 sizeof(repo->privsep_children[0]));
710 repo->privsep_children[i].imsg_fd = -1;
713 err = got_object_cache_init(&repo->objcache,
714 GOT_OBJECT_CACHE_TYPE_OBJ);
717 err = got_object_cache_init(&repo->treecache,
718 GOT_OBJECT_CACHE_TYPE_TREE);
721 err = got_object_cache_init(&repo->commitcache,
722 GOT_OBJECT_CACHE_TYPE_COMMIT);
725 err = got_object_cache_init(&repo->tagcache,
726 GOT_OBJECT_CACHE_TYPE_TAG);
729 err = got_object_cache_init(&repo->rawcache,
730 GOT_OBJECT_CACHE_TYPE_RAW);
734 err = get_pack_cache_size(&repo->pack_cache_size);
737 for (i = 0; i < nitems(repo->packs); i++) {
738 if (pack_fds != NULL && i < repo->pack_cache_size) {
739 repo->packs[i].basefd = pack_fds[j++];
740 repo->packs[i].accumfd = pack_fds[j++];
742 repo->packs[i].basefd = -1;
743 repo->packs[i].accumfd = -1;
746 for (i = 0; i < nitems(repo->tempfiles); i++)
747 repo->tempfiles[i] = -1;
748 repo->pinned_pack = -1;
749 repo->pinned_packidx = -1;
750 repo->pinned_pid = 0;
752 repo_path = realpath(path, NULL);
753 if (repo_path == NULL) {
754 err = got_error_from_errno2("realpath", path);
761 err = open_repo(repo, repo_path);
764 if (err->code != GOT_ERR_NOT_GIT_REPO)
766 if (repo_path[0] == '/' && repo_path[1] == '\0') {
767 err = got_error(GOT_ERR_NOT_GIT_REPO);
770 err = got_path_dirname(&parent_path, repo_path);
774 repo_path = parent_path;
777 err = read_gotconfig(repo);
781 err = read_gitconfig(repo, global_gitconfig_path);
784 if (repo->gitconfig_repository_format_version != 0) {
785 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
788 for (i = 0; i < repo->nextensions; i++) {
789 char *ext = repo->extnames[i];
790 char *val = repo->extvals[i];
791 int j, supported = 0;
793 if (!is_boolean_val(val)) {
794 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
798 if (!get_boolean_val(val))
801 for (j = 0; j < nitems(repo_extensions); j++) {
802 if (strcmp(ext, repo_extensions[j]) == 0) {
808 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
813 err = got_repo_list_packidx(&repo->packidx_paths, repo);
816 got_repo_close(repo);
823 const struct got_error *
824 got_repo_close(struct got_repository *repo)
826 const struct got_error *err = NULL, *child_err;
827 struct got_packidx_bloom_filter *bf;
830 for (i = 0; i < repo->pack_cache_size; i++) {
831 if (repo->packidx_cache[i] == NULL)
833 got_packidx_close(repo->packidx_cache[i]);
836 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
837 &repo->packidx_bloom_filters))) {
838 RB_REMOVE(got_packidx_bloom_filter_tree,
839 &repo->packidx_bloom_filters, bf);
840 bloom_free(bf->bloom);
845 for (i = 0; i < repo->pack_cache_size; i++)
846 if (repo->packs[i].path_packfile)
847 if (repo->packs[i].path_packfile)
848 got_pack_close(&repo->packs[i]);
851 free(repo->path_git_dir);
853 got_object_cache_close(&repo->objcache);
854 got_object_cache_close(&repo->treecache);
855 got_object_cache_close(&repo->commitcache);
856 got_object_cache_close(&repo->tagcache);
857 got_object_cache_close(&repo->rawcache);
859 for (i = 0; i < nitems(repo->privsep_children); i++) {
860 if (repo->privsep_children[i].imsg_fd == -1)
862 imsg_clear(repo->privsep_children[i].ibuf);
863 free(repo->privsep_children[i].ibuf);
864 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
865 child_err = got_privsep_wait_for_child(
866 repo->privsep_children[i].pid);
867 if (child_err && err == NULL)
869 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
871 err = got_error_from_errno("close");
874 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
876 err = got_error_from_errno("close");
879 got_gotconfig_free(repo->gotconfig);
880 free(repo->gitconfig_author_name);
881 free(repo->gitconfig_author_email);
882 for (i = 0; i < repo->ngitconfig_remotes; i++)
883 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
884 free(repo->gitconfig_remotes);
885 for (i = 0; i < repo->nextensions; i++) {
886 free(repo->extnames[i]);
887 free(repo->extvals[i]);
889 free(repo->extnames);
892 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
899 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
905 free(repo->fetch_url);
906 repo->fetch_url = NULL;
907 free(repo->send_url);
908 repo->send_url = NULL;
909 for (i = 0; i < repo->nfetch_branches; i++)
910 free(repo->fetch_branches[i]);
911 free(repo->fetch_branches);
912 repo->fetch_branches = NULL;
913 repo->nfetch_branches = 0;
914 for (i = 0; i < repo->nsend_branches; i++)
915 free(repo->send_branches[i]);
916 free(repo->send_branches);
917 repo->send_branches = NULL;
918 repo->nsend_branches = 0;
921 const struct got_error *
922 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
923 const char *input_path)
925 const struct got_error *err = NULL;
926 const char *repo_abspath = NULL;
928 char *canonpath, *path = NULL;
930 *in_repo_path = NULL;
932 canonpath = strdup(input_path);
933 if (canonpath == NULL) {
934 err = got_error_from_errno("strdup");
937 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
941 repo_abspath = got_repo_get_path(repo);
943 if (canonpath[0] == '\0') {
944 path = strdup(canonpath);
946 err = got_error_from_errno("strdup");
950 path = realpath(canonpath, NULL);
952 if (errno != ENOENT) {
953 err = got_error_from_errno2("realpath",
958 * Path is not on disk.
959 * Assume it is already relative to repository root.
961 path = strdup(canonpath);
963 err = got_error_from_errno("strdup");
968 repolen = strlen(repo_abspath);
972 if (strcmp(path, repo_abspath) == 0) {
976 err = got_error_from_errno("strdup");
979 } else if (len > repolen &&
980 got_path_is_child(path, repo_abspath, repolen)) {
981 /* Matched an on-disk path inside repository. */
982 if (got_repo_is_bare(repo)) {
984 * Matched an on-disk path inside repository
985 * database. Treat input as repository-relative.
992 /* Strip common prefix with repository path. */
993 err = got_path_skip_common_ancestor(&child,
1002 * Matched unrelated on-disk path.
1003 * Treat input as repository-relative.
1011 /* Make in-repository path absolute */
1012 if (path[0] != '/') {
1014 if (asprintf(&abspath, "/%s", path) == -1) {
1015 err = got_error_from_errno("asprintf");
1027 *in_repo_path = path;
1031 static const struct got_error *
1032 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1033 const char *path_packidx)
1035 const struct got_error *err = NULL;
1038 for (i = 0; i < repo->pack_cache_size; i++) {
1039 if (repo->packidx_cache[i] == NULL)
1041 if (strcmp(repo->packidx_cache[i]->path_packidx,
1042 path_packidx) == 0) {
1043 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1046 if (i == repo->pack_cache_size) {
1049 } while (i > 0 && repo->pinned_packidx >= 0 &&
1050 i == repo->pinned_packidx);
1051 err = got_packidx_close(repo->packidx_cache[i]);
1056 repo->packidx_cache[i] = packidx;
1062 got_repo_is_packidx_filename(const char *name, size_t len)
1064 if (len != GOT_PACKIDX_NAMELEN)
1067 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1070 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1071 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1077 static struct got_packidx_bloom_filter *
1078 get_packidx_bloom_filter(struct got_repository *repo,
1079 const char *path, size_t path_len)
1081 struct got_packidx_bloom_filter key;
1083 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1084 return NULL; /* XXX */
1085 key.path_len = path_len;
1087 return RB_FIND(got_packidx_bloom_filter_tree,
1088 &repo->packidx_bloom_filters, &key);
1092 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1093 const char *path_packidx, struct got_object_id *id)
1095 struct got_packidx_bloom_filter *bf;
1097 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1099 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1101 /* No bloom filter means this pack index must be searched. */
1105 static const struct got_error *
1106 add_packidx_bloom_filter(struct got_repository *repo,
1107 struct got_packidx *packidx, const char *path_packidx)
1109 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1110 struct got_packidx_bloom_filter *bf;
1114 * Don't use bloom filters for very large pack index files.
1115 * Large pack files will contain a relatively large fraction
1116 * of our objects so we will likely need to visit them anyway.
1117 * The more objects a pack file contains the higher the probability
1118 * of a false-positive match from the bloom filter. And reading
1119 * all object IDs from a large pack index file can be expensive.
1121 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1124 /* Do we already have a filter for this pack index? */
1125 if (get_packidx_bloom_filter(repo, path_packidx,
1126 strlen(path_packidx)) != NULL)
1129 bf = calloc(1, sizeof(*bf));
1131 return got_error_from_errno("calloc");
1132 bf->bloom = calloc(1, sizeof(*bf->bloom));
1133 if (bf->bloom == NULL) {
1135 return got_error_from_errno("calloc");
1138 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1139 if (len >= sizeof(bf->path)) {
1142 return got_error(GOT_ERR_NO_SPACE);
1146 /* Minimum size supported by our bloom filter is 1000 entries. */
1147 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1148 for (i = 0; i < nobjects; i++) {
1149 struct got_packidx_object_id *id;
1150 id = &packidx->hdr.sorted_ids[i];
1151 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1154 RB_INSERT(got_packidx_bloom_filter_tree,
1155 &repo->packidx_bloom_filters, bf);
1160 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1162 struct got_pathlist_entry *pe;
1164 while (!TAILQ_EMPTY(packidx_paths)) {
1165 pe = TAILQ_FIRST(packidx_paths);
1166 TAILQ_REMOVE(packidx_paths, pe, entry);
1167 free((char *)pe->path);
1172 static const struct got_error *
1173 refresh_packidx_paths(struct got_repository *repo)
1175 const struct got_error *err = NULL;
1176 char *objects_pack_dir = NULL;
1179 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1180 if (objects_pack_dir == NULL)
1181 return got_error_from_errno("got_repo_get_path_objects_pack");
1183 if (stat(objects_pack_dir, &sb) == -1) {
1184 if (errno != ENOENT) {
1185 err = got_error_from_errno2("stat", objects_pack_dir);
1188 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1189 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1190 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1191 purge_packidx_paths(&repo->packidx_paths);
1192 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1197 free(objects_pack_dir);
1201 const struct got_error *
1202 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1203 struct got_repository *repo, struct got_object_id *id)
1205 const struct got_error *err;
1206 struct got_pathlist_entry *pe;
1209 /* Search pack index cache. */
1210 for (i = 0; i < repo->pack_cache_size; i++) {
1211 if (repo->packidx_cache[i] == NULL)
1213 if (!got_repo_check_packidx_bloom_filter(repo,
1214 repo->packidx_cache[i]->path_packidx, id))
1215 continue; /* object will not be found in this index */
1216 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1218 *packidx = repo->packidx_cache[i];
1220 * Move this cache entry to the front. Repeatedly
1221 * searching a wrong pack index can be expensive.
1224 memmove(&repo->packidx_cache[1],
1225 &repo->packidx_cache[0],
1226 i * sizeof(repo->packidx_cache[0]));
1227 repo->packidx_cache[0] = *packidx;
1228 if (repo->pinned_packidx >= 0 &&
1229 repo->pinned_packidx < i)
1230 repo->pinned_packidx++;
1231 else if (repo->pinned_packidx == i)
1232 repo->pinned_packidx = 0;
1237 /* No luck. Search the filesystem. */
1239 err = refresh_packidx_paths(repo);
1243 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1244 const char *path_packidx = pe->path;
1247 if (!got_repo_check_packidx_bloom_filter(repo,
1249 continue; /* object will not be found in this index */
1251 for (i = 0; i < repo->pack_cache_size; i++) {
1252 if (repo->packidx_cache[i] == NULL)
1254 if (strcmp(repo->packidx_cache[i]->path_packidx,
1255 path_packidx) == 0) {
1261 continue; /* already searched */
1263 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1268 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1272 err = cache_packidx(repo, *packidx, path_packidx);
1276 *idx = got_packidx_get_object_idx(*packidx, id);
1278 err = NULL; /* found the object */
1283 err = got_error_no_obj(id);
1288 const struct got_error *
1289 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1290 struct got_repository *repo)
1292 const struct got_error *err = NULL;
1293 DIR *packdir = NULL;
1294 struct dirent *dent;
1295 char *path_packidx = NULL;
1299 packdir_fd = openat(got_repo_get_fd(repo),
1300 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1301 if (packdir_fd == -1) {
1302 return got_error_from_errno_fmt("openat: %s/%s",
1303 got_repo_get_path_git_dir(repo),
1304 GOT_OBJECTS_PACK_DIR);
1307 packdir = fdopendir(packdir_fd);
1308 if (packdir == NULL) {
1309 err = got_error_from_errno("fdopendir");
1313 if (fstat(packdir_fd, &sb) == -1) {
1314 err = got_error_from_errno("fstat");
1317 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1318 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1320 while ((dent = readdir(packdir)) != NULL) {
1321 if (!got_repo_is_packidx_filename(dent->d_name,
1322 strlen(dent->d_name)))
1325 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1326 dent->d_name) == -1) {
1327 err = got_error_from_errno("asprintf");
1328 path_packidx = NULL;
1332 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1339 if (packdir && closedir(packdir) != 0 && err == NULL)
1340 err = got_error_from_errno("closedir");
1344 const struct got_error *
1345 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1346 struct got_repository *repo)
1348 const struct got_error *err;
1353 /* Search pack index cache. */
1354 for (i = 0; i < repo->pack_cache_size; i++) {
1355 if (repo->packidx_cache[i] == NULL)
1357 if (strcmp(repo->packidx_cache[i]->path_packidx,
1358 path_packidx) == 0) {
1359 *packidx = repo->packidx_cache[i];
1363 /* No luck. Search the filesystem. */
1365 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1370 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1374 err = cache_packidx(repo, *packidx, path_packidx);
1377 got_packidx_close(*packidx);
1383 static const struct got_error *
1384 read_packfile_hdr(int fd, struct got_packidx *packidx)
1386 const struct got_error *err = NULL;
1387 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1388 struct got_packfile_hdr hdr;
1391 n = read(fd, &hdr, sizeof(hdr));
1393 return got_error_from_errno("read");
1394 if (n != sizeof(hdr))
1395 return got_error(GOT_ERR_BAD_PACKFILE);
1397 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1398 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1399 be32toh(hdr.nobjects) != totobj)
1400 err = got_error(GOT_ERR_BAD_PACKFILE);
1405 static const struct got_error *
1406 open_packfile(int *fd, struct got_repository *repo,
1407 const char *relpath, struct got_packidx *packidx)
1409 const struct got_error *err = NULL;
1411 *fd = openat(got_repo_get_fd(repo), relpath,
1412 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1414 return got_error_from_errno_fmt("openat: %s/%s",
1415 got_repo_get_path_git_dir(repo), relpath);
1418 err = read_packfile_hdr(*fd, packidx);
1428 const struct got_error *
1429 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1430 const char *path_packfile, struct got_packidx *packidx)
1432 const struct got_error *err = NULL;
1433 struct got_pack *pack = NULL;
1440 for (i = 0; i < repo->pack_cache_size; i++) {
1441 pack = &repo->packs[i];
1442 if (pack->path_packfile == NULL)
1444 if (strcmp(pack->path_packfile, path_packfile) == 0)
1445 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1448 if (i == repo->pack_cache_size) {
1449 struct got_pack tmp;
1452 } while (i > 0 && repo->pinned_pack >= 0 &&
1453 i == repo->pinned_pack);
1454 err = got_pack_close(&repo->packs[i]);
1457 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1458 return got_error_from_errno("ftruncate");
1459 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1460 return got_error_from_errno("ftruncate");
1461 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1462 memcpy(&repo->packs[i], &repo->packs[0],
1463 sizeof(repo->packs[i]));
1464 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1465 if (repo->pinned_pack == 0)
1466 repo->pinned_pack = i;
1467 else if (repo->pinned_pack == i)
1468 repo->pinned_pack = 0;
1472 pack = &repo->packs[i];
1474 pack->path_packfile = strdup(path_packfile);
1475 if (pack->path_packfile == NULL) {
1476 err = got_error_from_errno("strdup");
1480 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1484 if (fstat(pack->fd, &sb) != 0) {
1485 err = got_error_from_errno("fstat");
1488 pack->filesize = sb.st_size;
1490 pack->privsep_child = NULL;
1492 err = got_delta_cache_alloc(&pack->delta_cache);
1496 #ifndef GOT_PACK_NO_MMAP
1497 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1498 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1500 if (pack->map == MAP_FAILED) {
1501 if (errno != ENOMEM) {
1502 err = got_error_from_errno("mmap");
1505 pack->map = NULL; /* fall back to read(2) */
1512 got_pack_close(pack);
1519 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1521 struct got_pack *pack = NULL;
1524 for (i = 0; i < repo->pack_cache_size; i++) {
1525 pack = &repo->packs[i];
1526 if (pack->path_packfile == NULL)
1528 if (strcmp(pack->path_packfile, path_packfile) == 0)
1535 const struct got_error *
1536 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1537 struct got_pack *pack)
1540 int pinned_pack = -1, pinned_packidx = -1;
1542 for (i = 0; i < repo->pack_cache_size; i++) {
1543 if (repo->packidx_cache[i] &&
1544 strcmp(repo->packidx_cache[i]->path_packidx,
1545 packidx->path_packidx) == 0)
1547 if (repo->packs[i].path_packfile &&
1548 strcmp(repo->packs[i].path_packfile,
1549 pack->path_packfile) == 0)
1553 if (pinned_packidx == -1 || pinned_pack == -1)
1554 return got_error(GOT_ERR_PIN_PACK);
1556 repo->pinned_pack = pinned_pack;
1557 repo->pinned_packidx = pinned_packidx;
1558 if (repo->packs[pinned_pack].privsep_child)
1559 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1564 got_repo_get_pinned_pack(struct got_repository *repo)
1566 if (repo->pinned_pack >= 0 &&
1567 repo->pinned_pack < repo->pack_cache_size)
1568 return &repo->packs[repo->pinned_pack];
1574 got_repo_unpin_pack(struct got_repository *repo)
1576 repo->pinned_packidx = -1;
1577 repo->pinned_pack = -1;
1578 repo->pinned_pid = 0;
1581 const struct got_error *
1582 got_repo_init(const char *repo_path, const char *head_name)
1584 const struct got_error *err = NULL;
1585 const char *dirnames[] = {
1587 GOT_OBJECTS_PACK_DIR,
1590 const char *description_str = "Unnamed repository; "
1591 "edit this file 'description' to name the repository.";
1592 const char *headref = "ref: refs/heads/";
1593 const char *gitconfig_str = "[core]\n"
1594 "\trepositoryformatversion = 0\n"
1595 "\tfilemode = true\n"
1597 char *headref_str, *path;
1600 if (!got_path_dir_is_empty(repo_path))
1601 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1603 for (i = 0; i < nitems(dirnames); i++) {
1604 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1605 return got_error_from_errno("asprintf");
1607 err = got_path_mkdir(path);
1613 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1614 return got_error_from_errno("asprintf");
1615 err = got_path_create_file(path, description_str);
1620 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1621 return got_error_from_errno("asprintf");
1622 if (asprintf(&headref_str, "%s%s", headref,
1623 head_name ? head_name : "main") == -1) {
1625 return got_error_from_errno("asprintf");
1627 err = got_path_create_file(path, headref_str);
1633 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1634 return got_error_from_errno("asprintf");
1635 err = got_path_create_file(path, gitconfig_str);
1643 static const struct got_error *
1644 match_packed_object(struct got_object_id **unique_id,
1645 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1647 const struct got_error *err = NULL;
1648 struct got_object_id_queue matched_ids;
1649 struct got_pathlist_entry *pe;
1651 STAILQ_INIT(&matched_ids);
1653 err = refresh_packidx_paths(repo);
1657 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1658 const char *path_packidx = pe->path;
1659 struct got_packidx *packidx;
1660 struct got_object_qid *qid;
1662 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1667 err = got_packidx_match_id_str_prefix(&matched_ids,
1668 packidx, id_str_prefix);
1670 got_packidx_close(packidx);
1673 err = got_packidx_close(packidx);
1677 STAILQ_FOREACH(qid, &matched_ids, entry) {
1678 if (obj_type != GOT_OBJ_TYPE_ANY) {
1680 err = got_object_get_type(&matched_type, repo,
1684 if (matched_type != obj_type)
1687 if (*unique_id == NULL) {
1688 *unique_id = got_object_id_dup(&qid->id);
1689 if (*unique_id == NULL) {
1690 err = got_error_from_errno("malloc");
1694 if (got_object_id_cmp(*unique_id,
1696 continue; /* packed multiple times */
1697 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1703 got_object_id_queue_free(&matched_ids);
1711 static const struct got_error *
1712 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1713 const char *object_dir, const char *id_str_prefix, int obj_type,
1714 struct got_repository *repo)
1716 const struct got_error *err = NULL;
1717 char *path, *id_str = NULL;
1719 struct dirent *dent;
1720 struct got_object_id id;
1722 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1723 err = got_error_from_errno("asprintf");
1727 dir = opendir(path);
1729 if (errno == ENOENT) {
1733 err = got_error_from_errno2("opendir", path);
1736 while ((dent = readdir(dir)) != NULL) {
1742 if (strcmp(dent->d_name, ".") == 0 ||
1743 strcmp(dent->d_name, "..") == 0)
1746 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1747 err = got_error_from_errno("asprintf");
1751 if (!got_parse_sha1_digest(id.sha1, id_str))
1755 * Directory entries do not necessarily appear in
1756 * sorted order, so we must iterate over all of them.
1758 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1762 if (*unique_id == NULL) {
1763 if (obj_type != GOT_OBJ_TYPE_ANY) {
1765 err = got_object_get_type(&matched_type, repo,
1769 if (matched_type != obj_type)
1772 *unique_id = got_object_id_dup(&id);
1773 if (*unique_id == NULL) {
1774 err = got_error_from_errno("got_object_id_dup");
1778 if (got_object_id_cmp(*unique_id, &id) == 0)
1779 continue; /* both packed and loose */
1780 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1785 if (dir && closedir(dir) != 0 && err == NULL)
1786 err = got_error_from_errno("closedir");
1796 const struct got_error *
1797 got_repo_match_object_id_prefix(struct got_object_id **id,
1798 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1800 const struct got_error *err = NULL;
1801 char *path_objects = NULL, *object_dir = NULL;
1807 path_objects = got_repo_get_path_objects(repo);
1809 len = strlen(id_str_prefix);
1810 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1811 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1815 for (i = 0; i < len; i++) {
1816 if (isxdigit((unsigned char)id_str_prefix[i]))
1818 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1823 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1826 object_dir = strndup(id_str_prefix, 2);
1827 if (object_dir == NULL) {
1828 err = got_error_from_errno("strdup");
1831 err = match_loose_object(id, path_objects, object_dir,
1832 id_str_prefix, obj_type, repo);
1833 } else if (len == 1) {
1835 for (i = 0; i < 0xf; i++) {
1836 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1838 err = got_error_from_errno("asprintf");
1841 err = match_packed_object(id, repo, object_dir,
1845 err = match_loose_object(id, path_objects, object_dir,
1846 id_str_prefix, obj_type, repo);
1851 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1860 } else if (*id == NULL) {
1862 case GOT_OBJ_TYPE_BLOB:
1863 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1864 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1866 case GOT_OBJ_TYPE_TREE:
1867 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1868 GOT_OBJ_LABEL_TREE, id_str_prefix);
1870 case GOT_OBJ_TYPE_COMMIT:
1871 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1872 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1874 case GOT_OBJ_TYPE_TAG:
1875 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1876 GOT_OBJ_LABEL_TAG, id_str_prefix);
1879 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1887 const struct got_error *
1888 got_repo_match_object_id(struct got_object_id **id, char **label,
1889 const char *id_str, int obj_type, struct got_reflist_head *refs,
1890 struct got_repository *repo)
1892 const struct got_error *err;
1893 struct got_tag_object *tag;
1894 struct got_reference *ref = NULL;
1901 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1904 *id = got_object_id_dup(
1905 got_object_tag_get_object_id(tag));
1907 err = got_error_from_errno("got_object_id_dup");
1908 else if (label && asprintf(label, "refs/tags/%s",
1909 got_object_tag_get_name(tag)) == -1) {
1910 err = got_error_from_errno("asprintf");
1914 got_object_tag_close(tag);
1916 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1917 err->code != GOT_ERR_NO_OBJ)
1921 err = got_ref_open(&ref, repo, id_str, 0);
1923 err = got_ref_resolve(id, repo, ref);
1927 *label = strdup(got_ref_get_name(ref));
1928 if (*label == NULL) {
1929 err = got_error_from_errno("strdup");
1934 if (err->code != GOT_ERR_NOT_REF &&
1935 err->code != GOT_ERR_BAD_REF_NAME)
1937 err = got_repo_match_object_id_prefix(id, id_str,
1940 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1941 err = got_error_not_ref(id_str);
1945 err = got_object_id_str(label, *id);
1946 if (*label == NULL) {
1947 err = got_error_from_errno("strdup");
1958 const struct got_error *
1959 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1960 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1962 const struct got_error *err = NULL;
1963 struct got_reflist_entry *re;
1964 struct got_object_id *tag_id;
1965 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1969 TAILQ_FOREACH(re, refs, entry) {
1970 const char *refname;
1971 refname = got_ref_get_name(re->ref);
1972 if (got_ref_is_symbolic(re->ref))
1974 if (strncmp(refname, "refs/tags/", 10) != 0)
1976 if (!name_is_absolute)
1977 refname += strlen("refs/tags/");
1978 if (strcmp(refname, name) != 0)
1980 err = got_ref_resolve(&tag_id, repo, re->ref);
1983 err = got_object_open_as_tag(tag, repo, tag_id);
1987 if (obj_type == GOT_OBJ_TYPE_ANY ||
1988 got_object_tag_get_object_type(*tag) == obj_type)
1990 got_object_tag_close(*tag);
1994 if (err == NULL && *tag == NULL)
1995 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1996 GOT_OBJ_LABEL_TAG, name);
2000 static const struct got_error *
2001 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2002 const char *name, mode_t mode, struct got_object_id *blob_id)
2004 const struct got_error *err = NULL;
2008 *new_te = calloc(1, sizeof(**new_te));
2009 if (*new_te == NULL)
2010 return got_error_from_errno("calloc");
2012 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2013 sizeof((*new_te)->name)) {
2014 err = got_error(GOT_ERR_NO_SPACE);
2018 if (S_ISLNK(mode)) {
2019 (*new_te)->mode = S_IFLNK;
2021 (*new_te)->mode = S_IFREG;
2022 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2024 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2026 if (err && *new_te) {
2033 static const struct got_error *
2034 import_file(struct got_tree_entry **new_te, struct dirent *de,
2035 const char *path, struct got_repository *repo)
2037 const struct got_error *err;
2038 struct got_object_id *blob_id = NULL;
2042 if (asprintf(&filepath, "%s%s%s", path,
2043 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2044 return got_error_from_errno("asprintf");
2046 if (lstat(filepath, &sb) != 0) {
2047 err = got_error_from_errno2("lstat", path);
2051 err = got_object_blob_create(&blob_id, filepath, repo);
2055 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2064 static const struct got_error *
2065 insert_tree_entry(struct got_tree_entry *new_te,
2066 struct got_pathlist_head *paths)
2068 const struct got_error *err = NULL;
2069 struct got_pathlist_entry *new_pe;
2071 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2075 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2079 static const struct got_error *write_tree(struct got_object_id **,
2080 const char *, struct got_pathlist_head *, struct got_repository *,
2081 got_repo_import_cb progress_cb, void *progress_arg);
2083 static const struct got_error *
2084 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2085 const char *path, struct got_pathlist_head *ignores,
2086 struct got_repository *repo,
2087 got_repo_import_cb progress_cb, void *progress_arg)
2089 const struct got_error *err;
2090 struct got_object_id *id = NULL;
2093 if (asprintf(&subdirpath, "%s%s%s", path,
2094 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2095 return got_error_from_errno("asprintf");
2097 (*new_te) = calloc(1, sizeof(**new_te));
2098 if (*new_te == NULL)
2099 return got_error_from_errno("calloc");
2100 (*new_te)->mode = S_IFDIR;
2101 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2102 sizeof((*new_te)->name)) {
2103 err = got_error(GOT_ERR_NO_SPACE);
2106 err = write_tree(&id, subdirpath, ignores, repo,
2107 progress_cb, progress_arg);
2110 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2122 static const struct got_error *
2123 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2124 struct got_pathlist_head *ignores, struct got_repository *repo,
2125 got_repo_import_cb progress_cb, void *progress_arg)
2127 const struct got_error *err = NULL;
2131 struct got_tree_entry *new_te = NULL;
2132 struct got_pathlist_head paths;
2133 struct got_pathlist_entry *pe;
2135 *new_tree_id = NULL;
2139 dir = opendir(path_dir);
2141 err = got_error_from_errno2("opendir", path_dir);
2146 while ((de = readdir(dir)) != NULL) {
2150 if (strcmp(de->d_name, ".") == 0 ||
2151 strcmp(de->d_name, "..") == 0)
2154 err = got_path_dirent_type(&type, path_dir, de);
2158 TAILQ_FOREACH(pe, ignores, entry) {
2159 if (type == DT_DIR && pe->path_len > 0 &&
2160 pe->path[pe->path_len - 1] == '/') {
2161 char stripped[PATH_MAX];
2163 if (strlcpy(stripped, pe->path,
2164 sizeof(stripped)) >= sizeof(stripped)) {
2165 err = got_error(GOT_ERR_NO_SPACE);
2168 got_path_strip_trailing_slashes(stripped);
2169 if (fnmatch(stripped, de->d_name, 0) == 0) {
2173 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2181 if (type == DT_DIR) {
2182 err = import_subdir(&new_te, de, path_dir,
2183 ignores, repo, progress_cb, progress_arg);
2185 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2190 } else if (type == DT_REG || type == DT_LNK) {
2191 err = import_file(&new_te, de, path_dir, repo);
2197 err = insert_tree_entry(new_te, &paths);
2203 if (TAILQ_EMPTY(&paths)) {
2204 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2205 "cannot create tree without any entries");
2209 TAILQ_FOREACH(pe, &paths, entry) {
2210 struct got_tree_entry *te = pe->data;
2212 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2214 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2215 err = got_error_from_errno("asprintf");
2218 err = (*progress_cb)(progress_arg, path);
2224 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2228 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2232 const struct got_error *
2233 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2234 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2235 struct got_repository *repo, got_repo_import_cb progress_cb,
2238 const struct got_error *err;
2239 struct got_object_id *new_tree_id;
2241 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2242 progress_cb, progress_arg);
2246 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2247 author, time(NULL), author, time(NULL), logmsg, repo);
2252 const struct got_error *
2253 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2254 struct got_repository *repo)
2256 const struct got_error *err = NULL;
2257 char *path_objects = NULL, *path = NULL;
2259 struct got_object_id id;
2265 path_objects = got_repo_get_path_objects(repo);
2266 if (path_objects == NULL)
2267 return got_error_from_errno("got_repo_get_path_objects");
2269 for (i = 0; i <= 0xff; i++) {
2270 struct dirent *dent;
2272 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2273 err = got_error_from_errno("asprintf");
2277 dir = opendir(path);
2279 if (errno == ENOENT) {
2283 err = got_error_from_errno2("opendir", path);
2287 while ((dent = readdir(dir)) != NULL) {
2292 if (strcmp(dent->d_name, ".") == 0 ||
2293 strcmp(dent->d_name, "..") == 0)
2296 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2297 err = got_error_from_errno("asprintf");
2301 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2307 err = got_object_open_loose_fd(&fd, &id, repo);
2311 if (fstat(fd, &sb) == -1) {
2312 err = got_error_from_errno("fstat");
2317 (*ondisk_size) += sb.st_size;
2319 if (close(fd) == -1) {
2320 err = got_error_from_errno("close");
2325 if (closedir(dir) != 0) {
2326 err = got_error_from_errno("closedir");
2335 if (dir && closedir(dir) != 0 && err == NULL)
2336 err = got_error_from_errno("closedir");
2347 const struct got_error *
2348 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2349 off_t *total_packsize, struct got_repository *repo)
2351 const struct got_error *err = NULL;
2352 DIR *packdir = NULL;
2353 struct dirent *dent;
2354 struct got_packidx *packidx = NULL;
2356 char *path_packfile;
2362 *total_packsize = 0;
2364 packdir_fd = openat(got_repo_get_fd(repo),
2365 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2366 if (packdir_fd == -1) {
2367 return got_error_from_errno_fmt("openat: %s/%s",
2368 got_repo_get_path_git_dir(repo),
2369 GOT_OBJECTS_PACK_DIR);
2372 packdir = fdopendir(packdir_fd);
2373 if (packdir == NULL) {
2374 err = got_error_from_errno("fdopendir");
2378 while ((dent = readdir(packdir)) != NULL) {
2379 if (!got_repo_is_packidx_filename(dent->d_name,
2380 strlen(dent->d_name)))
2383 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2384 dent->d_name) == -1) {
2385 err = got_error_from_errno("asprintf");
2389 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2395 if (fstat(packidx->fd, &sb) == -1)
2397 *total_packsize += sb.st_size;
2399 err = got_packidx_get_packfile_path(&path_packfile,
2400 packidx->path_packidx);
2404 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2406 free(path_packfile);
2409 free(path_packfile);
2410 *total_packsize += sb.st_size;
2412 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2416 got_packidx_close(packidx);
2421 got_packidx_close(packidx);
2422 if (packdir && closedir(packdir) != 0 && err == NULL)
2423 err = got_error_from_errno("closedir");
2427 *total_packsize = 0;
2432 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2433 got_packidx_bloom_filter_cmp);