[PATCH v2 2/2] repo: add --all to git-repo-info
- From
Lucas Seiki Oshiro <lucasseikioshiro@gmail.com>
- Date
- Oct 20, 2025, 16:19 UTC
- Message-ID
- <20251020181943.6314-3-lucasseikioshiro@gmail.com>
- In-Reply-To
- <20251020181943.6314-1-lucasseikioshiro@gmail.com>
Add a new flag `--all` to git-repo-info for requesting values for all the available keys. By using this flag, the user can retrieve all the values instead of searching what are the desired keys for what they wants.
Helped-by: Karthik Nayak <karthik.188@gmail.com> Helped-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> --- Documentation/git-repo.adoc | 7 ++++--- builtin/repo.c | 27 ++++++++++++++++++++++++++- t/t1900-repo.sh | 29 +++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-)
diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc index 209afd1b61..1a9d0c50a9 100644 --- a/Documentation/git-repo.adoc +++ b/Documentation/git-repo.adoc @@ -8,7 +8,7 @@ git-repo - Retrieve information about the repository SYNOPSIS -------- [synopsis] -git repo info [--format=(keyvalue|nul)] [-z] [<key>...] +git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...] DESCRIPTION ----------- @@ -18,13 +18,14 @@ THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE. COMMANDS -------- -`info [--format=(keyvalue|nul)] [-z] [<key>...]`:: +`info [--format=(keyvalue|nul)] [-z] [--all | <key>...]`:: Retrieve metadata-related information about the current repository. Only the requested data will be returned based on their keys (see "INFO KEYS" section below). + The values are returned in the same order in which their respective keys were -requested. +requested. The `--all` flag requests the values for all the available keys. +Keys requested after `--all` will be duplicated. + The output format can be chosen through the flag `--format`. Two formats are supported: diff --git a/builtin/repo.c b/builtin/repo.c index 3b071e9a50..1c54db5810 100644 --- a/builtin/repo.c +++ b/builtin/repo.c @@ -9,7 +9,7 @@ #include "shallow.h" static const char *const repo_usage[] = { - "git repo info [--format=(keyvalue|nul)] [-z] [<key>...]", + "git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...]", NULL }; @@ -124,6 +124,26 @@ static int print_fields(int argc, const char **argv, return ret; } +static void print_all_fields(struct repository *repo, + enum output_format format) +{ + struct strbuf valbuf = STRBUF_INIT; + struct strbuf quotbuf = STRBUF_INIT; + + for (unsigned long i = 0; i < ARRAY_SIZE(repo_info_fields); i++) { + struct field field = repo_info_fields[i]; + get_value_fn *get_value = field.get_value; + const char *key = field.key; + + strbuf_reset(&valbuf); + get_value(repo, &valbuf); + print_field(format, key, &valbuf, "buf); + } + + strbuf_release(&valbuf); + strbuf_release("buf); +} + static int parse_format_cb(const struct option *opt, const char *arg, int unset UNUSED) { @@ -145,6 +165,7 @@ static int repo_info(int argc, const char **argv, const char *prefix, struct repository *repo) { enum output_format format = FORMAT_KEYVALUE; + int all_keys = 0; struct option options[] = { OPT_CALLBACK_F(0, "format", &format, N_("format"), N_("output format"), @@ -153,11 +174,15 @@ static int repo_info(int argc, const char **argv, const char *prefix, N_("synonym for --format=nul"), PARSE_OPT_NONEG | PARSE_OPT_NOARG, parse_format_cb), + OPT_BOOL(0, "all", &all_keys, N_("return all keys")), OPT_END() }; argc = parse_options(argc, argv, prefix, options, repo_usage, 0); + if (all_keys) + print_all_fields(repo, format); + return print_fields(argc, argv, repo, format); } diff --git a/t/t1900-repo.sh b/t/t1900-repo.sh index 2beba67889..28635d0f92 100755 --- a/t/t1900-repo.sh +++ b/t/t1900-repo.sh @@ -4,6 +4,15 @@ test_description='test git repo-info' . ./test-lib.sh +# git-repo-info keys. It must contain the same keys listed in the const +# repo_info_fields, in lexicographical order. +REPO_INFO_KEYS=' + layout.bare + layout.shallow + object.format + references.format +' + # Test whether a key-value pair is correctly returned # # Usage: test_repo_info <label> <init command> <repo_name> <key> <expected value> @@ -110,4 +119,24 @@ test_expect_success 'git repo info uses the last requested format' ' test_cmp expected actual ' +test_expect_success 'git repo info --all returns all key-value pairs' ' + git repo info $REPO_INFO_KEYS >expect && + git repo info --all >actual && + test_cmp expect actual +' + +test_expect_success 'git repo info --all <key> duplicates <key>' ' + git repo info $REPO_INFO_KEYS object.format >expect && + git repo info --all object.format >actual && + test_cmp expect actual +' + +test_expect_success 'git repo info --all <invalid key> warns about invalid key' ' + git repo info $REPO_INFO_KEYS >expect && + echo "error: key ${SQ}no.key${SQ} not found" >expect_err && + test_must_fail git repo info --all no.key >actual 2>actual_err && + test_cmp expect actual && + test_cmp expect_err actual_err +' + test_done
-- 2.50.1 (Apple Git-155)