This commit is part of the series that introduce the new command 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 information, fitting in the purpose of git-repo-info. Then, add a new field `references.format` to the repo-info command containing that information. Helped-by: Phillip Wood <phillip.wood@xxxxxxxxxxxxx> Helped-by: Junio C Hamano <gitster@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-info.adoc | 4 ++ builtin/repo-info.c | 105 ++++++++++++++++++++++++++++++- t/meson.build | 1 + t/t1900-repo-info.sh | 64 +++++++++++++++++++ 4 files changed, 171 insertions(+), 3 deletions(-) create mode 100755 t/t1900-repo-info.sh diff --git a/Documentation/git-repo-info.adoc b/Documentation/git-repo-info.adoc index bf1d391482..dd221b236e 100644 --- a/Documentation/git-repo-info.adoc +++ b/Documentation/git-repo-info.adoc @@ -66,6 +66,10 @@ CATEGORIES AND FIELDS The set of data that `git repo-info` can return is divided into categories. Each category is composed by one or more fields. +`references`:: +Reference-related data: +* `format`: the reference storage format, either `files` or `reftable`. + SEE ALSO -------- linkgit:git-rev-parse[1] diff --git a/builtin/repo-info.c b/builtin/repo-info.c index cb4785169f..98a0d83d51 100644 --- a/builtin/repo-info.c +++ b/builtin/repo-info.c @@ -1,20 +1,39 @@ #include "builtin.h" #include "json-writer.h" #include "parse-options.h" +#include "refs.h" enum output_format { FORMAT_JSON, FORMAT_NULL_TERMINATED, }; +enum repo_info_category { + CATEGORY_REFERENCES = 1 << 0, +}; + +enum repo_info_references_field { + FIELD_REFERENCES_FORMAT = 1 << 0, +}; + +struct repo_info_field { + enum repo_info_category category; + union { + enum repo_info_references_field references; + } u; +}; + struct repo_info { struct repository *repo; enum output_format format; + size_t fields_nr; + struct repo_info_field *fields; }; static void repo_info_init(struct repo_info *repo_info, struct repository *repo, - const char *format) + const char *format, + int argc, const char **argv) { repo_info->repo = repo; @@ -24,15 +43,93 @@ static void repo_info_init(struct repo_info *repo_info, repo_info->format = FORMAT_NULL_TERMINATED; else die("invalid format %s", format); + + repo_info->fields_nr = argc; + ALLOC_ARRAY(repo_info->fields, argc); + + for (int i = 0; i < argc; i++) { + const char *arg = argv[i]; + struct repo_info_field *field = repo_info->fields + i; + if (!strcmp(arg, "references.format")) { + field->category = CATEGORY_REFERENCES; + field->u.references = FIELD_REFERENCES_FORMAT; + } else { + die("invalid field '%s'", arg); + } + } +} + +static void repo_info_release(struct repo_info *repo_info) +{ + free(repo_info->fields); } -static void repo_info_print_json(struct repo_info *repo_info UNUSED) +static void append_null_terminated_field(struct strbuf *buf, + struct repo_info *repo_info, + struct repo_info_field *field) +{ + struct repository *repo = repo_info->repo; + + switch (field->category) { + case CATEGORY_REFERENCES: + strbuf_addstr(buf, "references."); + switch (field->u.references) { + case FIELD_REFERENCES_FORMAT: + strbuf_addstr(buf, "format\n"); + strbuf_addstr(buf, ref_storage_format_to_name( + repo->ref_storage_format)); + break; + } + break; + } + + strbuf_addch(buf, '\0'); +} + +static void repo_info_print_null_terminated(struct repo_info *repo_info) +{ + struct strbuf buf; + + strbuf_init(&buf, 256); + + for (size_t i = 0; i < repo_info->fields_nr; i++) { + struct repo_info_field *field = &repo_info->fields[i]; + append_null_terminated_field(&buf, repo_info, field); + } + + fwrite(buf.buf, 1, buf.len, stdout); + strbuf_release(&buf); +} + +static void repo_info_print_json(struct repo_info *repo_info) { struct json_writer jw; + unsigned int categories = 0; + unsigned int references_fields = 0; + struct repository *repo = repo_info->repo; + + for (size_t i = 0; i < repo_info->fields_nr; i++) { + struct repo_info_field *field = repo_info->fields + i; + categories |= field->category; + switch (field->category) { + case CATEGORY_REFERENCES: + references_fields |= field->u.references; + break; + } + } jw_init(&jw); jw_object_begin(&jw, 1); + if (categories & CATEGORY_REFERENCES) { + jw_object_inline_begin_object(&jw, "references"); + if (references_fields & FIELD_REFERENCES_FORMAT) { + const char *format_name = ref_storage_format_to_name( + repo->ref_storage_format); + jw_object_string(&jw, "format", format_name); + } + jw_end(&jw); + } jw_end(&jw); puts(jw.json.buf); @@ -46,6 +143,7 @@ static void repo_info_print(struct repo_info *repo_info) repo_info_print_json(repo_info); break; case FORMAT_NULL_TERMINATED: + repo_info_print_null_terminated(repo_info); break; default: BUG("%d: not a valid repo-info format", repo_info->format); @@ -71,8 +169,9 @@ int cmd_repo_info(int argc, argc = parse_options(argc, argv, prefix, options, repo_info_usage, 0); - repo_info_init(&repo_info, repo, format); + repo_info_init(&repo_info, repo, format, argc, argv); repo_info_print(&repo_info); + repo_info_release(&repo_info); return 0; } diff --git a/t/meson.build b/t/meson.build index 6d7fe6b117..e2c9393189 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-info.sh', 't2000-conflict-when-checking-files-out.sh', 't2002-checkout-cache-u.sh', 't2003-checkout-cache-mkdir.sh', diff --git a/t/t1900-repo-info.sh b/t/t1900-repo-info.sh new file mode 100755 index 0000000000..2af9d1d9c3 --- /dev/null +++ b/t/t1900-repo-info.sh @@ -0,0 +1,64 @@ +#!/bin/sh + +test_description='test git repo-info' + +. ./test-lib.sh + +parse_json () { + tr '\n' ' ' | "$PERL_PATH" "$TEST_DIRECTORY/t0019/parse_json.perl" +} + +test_lazy_prereq PERLJSON ' + perl -MJSON -e "exit 0" +' + +# Test if a field is correctly returned in both null-terminated and json formats. +# +# 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 PERLJSON "json: $label" ' + test_when_finished "rm -rf repo" && + eval "$init_command" && + echo "$expected_value" >expect && + git -C repo repo-info "$key" >output && + parse_json <output >parsed && + grep -F "row[0].$key" parsed | cut -d " " -f 2 >value && + sed -n -e "/row[0].$key/{ + s/^[^ ]* // + s/^1\$/true/ + s/^0\$/false/ + p; + }" parsed >actual && + sed "s/^0$/false/" <value| sed "s/^1$/true/" >actual && + test_cmp expect actual + ' + + test_expect_success "null-terminated: $label" ' + test_when_finished "rm -rf repo" && + eval "$init_command" && + echo "$expected_value" | lf_to_nul >expect && + git -C repo repo-info --format=null-terminated "$key" >output && + tail -n 1 output >actual && + test_cmp expect 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_done -- 2.39.5 (Apple Git-154)