This commit is part of the series that introduces the new subcommand git-repo-info. The flag `--show-ref-format` from git-rev-parse is used for retrieving the reference format (i.e. `files` or `reftable`). This way, it is used for querying repository metadata, fitting in the purpose of git-repo-info. Add a new field `references.format` to the repo-info subcommand containing that information. Helped-by: Phillip Wood <phillip.wood@xxxxxxxxxxxxx> Helped-by: Junio C Hamano <gitster@xxxxxxxxx> Helped-by: Justin Tobler <jltobler@xxxxxxxxx> Mentored-by: Karthik Nayak <karthik.188@xxxxxxxxx> Mentored-by: Patrick Steinhardt <ps@xxxxxx> Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@xxxxxxxxx> --- Documentation/git-repo.adoc | 4 ++ builtin/repo.c | 82 ++++++++++++++++++++++++++++++++++++- t/meson.build | 1 + t/t1900-repo.sh | 57 ++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 2 deletions(-) create mode 100755 t/t1900-repo.sh diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc index aca76b131b..ac2578299f 100644 --- a/Documentation/git-repo.adoc +++ b/Documentation/git-repo.adoc @@ -29,6 +29,10 @@ INFO KEYS The set of data that `git repo` can return is grouped into the following categories: +`references`:: +Reference-related data: +* `format`: the reference storage format + SEE ALSO -------- linkgit:git-rev-parse[1] diff --git a/builtin/repo.c b/builtin/repo.c index d4f01e35e2..02d5821c77 100644 --- a/builtin/repo.c +++ b/builtin/repo.c @@ -1,12 +1,90 @@ #include "builtin.h" #include "parse-options.h" +#include "refs.h" +#include "strbuf.h" -static int repo_info(int argc UNUSED, const char **argv UNUSED, - const char *prefix UNUSED, struct repository *repo UNUSED) +typedef int get_value_fn(struct repository *repo, struct strbuf *buf); + +struct field { + const char *key; + get_value_fn *get_value; +}; + +static int get_references_format(struct repository *repo, struct strbuf *buf) +{ + strbuf_addstr(buf, + ref_storage_format_to_name(repo->ref_storage_format)); + return 0; +} + +/* repo_info_fields keys should be in lexicographical order */ +static const struct field repo_info_fields[] = { + { "references.format", get_references_format }, +}; + +static int repo_info_fields_cmp(const void *va, const void *vb) +{ + const struct field *a = va; + const struct field *b = vb; + + return strcmp(a->key, b->key); +} + +static get_value_fn *get_value_fn_for_key(const char *key) +{ + const struct field search_key = { key, NULL }; + const struct field *found = bsearch(&search_key, repo_info_fields, + ARRAY_SIZE(repo_info_fields), + sizeof(*found), + repo_info_fields_cmp); + return found ? found->get_value : NULL; +} + +static int qsort_strcmp(const void *va, const void *vb) { + const char *a = *(const char **)va; + const char *b = *(const char **)vb; + + return strcmp(a, b); +} + +static int print_fields(int argc, const char **argv, struct repository *repo) +{ + const char *last = ""; + + QSORT(argv, argc, qsort_strcmp); + + for (int i = 0; i < argc; i++) { + get_value_fn *get_value; + const char *key = argv[i]; + struct strbuf value; + + if (!strcmp(key, last)) + continue; + + strbuf_init(&value, 64); + get_value = get_value_fn_for_key(key); + + if (!get_value) { + strbuf_release(&value); + return error(_("key '%s' not found"), key); + } + + get_value(repo, &value); + printf("%s=%s\n", key, value.buf); + last = key; + strbuf_release(&value); + } + return 0; } +static int repo_info(int argc, const char **argv, const char *prefix UNUSED, + struct repository *repo) +{ + return print_fields(argc - 1, argv + 1, repo); +} + int cmd_repo(int argc, const char **argv, const char *prefix, struct repository *repo) { diff --git a/t/meson.build b/t/meson.build index 660d780dcc..5de9c3c7e9 100644 --- a/t/meson.build +++ b/t/meson.build @@ -245,6 +245,7 @@ integration_tests = [ 't1700-split-index.sh', 't1701-racy-split-index.sh', 't1800-hook.sh', + 't1900-repo.sh', 't2000-conflict-when-checking-files-out.sh', 't2002-checkout-cache-u.sh', 't2003-checkout-cache-mkdir.sh', diff --git a/t/t1900-repo.sh b/t/t1900-repo.sh new file mode 100755 index 0000000000..cc54b0644d --- /dev/null +++ b/t/t1900-repo.sh @@ -0,0 +1,57 @@ +#!/bin/sh + +test_description='test git repo-info' + +. ./test-lib.sh + +# Test if a field is correctly returned in the null-terminated format +# +# Usage: test_repo_info <label> <init command> <key> <expected value> +# +# Arguments: +# label: the label of the test +# init command: a command that creates a repository called 'repo', configured +# accordingly to what is being tested +# key: the key of the field that is being tested +# expected value: the value that the field should contain +test_repo_info () { + label=$1 + init_command=$2 + key=$3 + expected_value=$4 + + test_expect_success "$label" ' + test_when_finished "rm -rf repo" && + eval "$init_command" && + echo "$expected_value" >expected && + git -C repo repo info "$key" >output && + cut -d "=" -f 2 <output >actual && + test_cmp expected actual + ' +} + +test_repo_info 'ref format files is retrieved correctly' ' + git init --ref-format=files repo' 'references.format' 'files' + +test_repo_info 'ref format reftable is retrieved correctly' ' + git init --ref-format=reftable repo' 'references.format' 'reftable' + +test_expect_success 'git-repo-info aborts if an invalid key is requested' ' + test_when_finished "rm -rf expected err" && + echo "error: key '\'foo\'' not found" >expected && + test_must_fail git repo info foo 2>err && + test_cmp expected err +' + +test_expect_success "only one value is returned if the same key is requested twice" ' + test_when_finished "rm -f expected_key expected_value actual_key actual_value output" && + echo "references.format" >expected_key && + git rev-parse --show-ref-format >expected_value && + git repo info references.format references.format >output && + cut -d "=" -f 1 <output >actual_key && + cut -d "=" -f 2 <output >actual_value && + test_cmp expected_key actual_key && + test_cmp expected_value actual_value +' + +test_done -- 2.39.5 (Apple Git-154)