Merge branch 'lo/repo-info-keys' into jch

"git repo info" learns "--keys" action to list known keys.

* lo/repo-info-keys:
  repo: add new flag --keys to git-repo-info
  repo: rename the output format "keyvalue" to "lines"
This commit is contained in:
Junio C Hamano
2026-02-23 14:25:42 -08:00
4 changed files with 97 additions and 34 deletions

View File

@@ -8,8 +8,9 @@ git-repo - Retrieve information about the repository
SYNOPSIS
--------
[synopsis]
git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]
git repo structure [--format=(table|keyvalue|nul) | -z]
git repo info [--format=(lines|nul) | -z] [--all | <key>...]
git repo info --keys [--format=(lines|nul) | -z]
git repo structure [--format=(table|lines|nul) | -z]
DESCRIPTION
-----------
@@ -19,7 +20,7 @@ THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
COMMANDS
--------
`info [--format=(keyvalue|nul) | -z] [--all | <key>...]`::
`info [--format=(lines|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).
@@ -30,21 +31,32 @@ requested. The `--all` flag requests the values for all the available keys.
The output format can be chosen through the flag `--format`. Two formats are
supported:
+
`keyvalue`:::
`lines`:::
output key-value pairs one per line using the `=` character as
the delimiter between the key and the value. Values containing "unusual"
characters are quoted as explained for the configuration variable
`core.quotePath` (see linkgit:git-config[1]). This is the default.
`nul`:::
similar to `keyvalue`, but using a newline character as the delimiter
similar to `lines`, but using a newline character as the delimiter
between the key and the value and using a NUL character after each value.
This format is better suited for being parsed by another applications than
`keyvalue`. Unlike in the `keyvalue` format, the values are never quoted.
`lines`. Unlike in the `lines` format, the values are never quoted.
+
`-z` is an alias for `--format=nul`.
`structure [--format=(table|keyvalue|nul) | -z]`::
`info --keys [--format=(lines|nul) | -z]`::
List all the available keys, one per line. The output format can be chosen
through the flag `--format`. The following formats are supported:
+
`lines`:::
Output the keys one per line. This is the default.
`nul`:::
Similar to `lines`, but using a _NUL_ character after each value.
`structure [--format=(table|lines|nul) | -z]`::
Retrieve statistics about the current repository structure. The
following kinds of information are reported:
+
@@ -61,17 +73,17 @@ supported:
change and is not intended for machine parsing. This is the default
format.
`keyvalue`:::
`lines`:::
Each line of output contains a key-value pair for a repository stat.
The '=' character is used to delimit between the key and the value.
Values containing "unusual" characters are quoted as explained for the
configuration variable `core.quotePath` (see linkgit:git-config[1]).
`nul`:::
Similar to `keyvalue`, but uses a NUL character to delimit between
Similar to `lines`, but uses a NUL character to delimit between
key-value pairs instead of a newline. Also uses a newline character as
the delimiter between the key and value instead of '='. Unlike the
`keyvalue` format, values containing "unusual" characters are never
`lines` format, values containing "unusual" characters are never
quoted.
+
`-z` is an alias for `--format=nul`.

View File

@@ -17,8 +17,9 @@
#include "utf8.h"
static const char *const repo_usage[] = {
"git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]",
"git repo structure [--format=(table|keyvalue|nul) | -z]",
"git repo info [--format=(lines|nul) | -z] [--all | <key>...]",
"git repo info --keys [--format=(lines|nul) | -z]",
"git repo structure [--format=(table|lines|nul) | -z]",
NULL
};
@@ -26,7 +27,7 @@ typedef int get_value_fn(struct repository *repo, struct strbuf *buf);
enum output_format {
FORMAT_TABLE,
FORMAT_KEYVALUE,
FORMAT_NEWLINE_TERMINATED,
FORMAT_NUL_TERMINATED,
};
@@ -91,7 +92,7 @@ static void print_field(enum output_format format, const char *key,
const char *value)
{
switch (format) {
case FORMAT_KEYVALUE:
case FORMAT_NEWLINE_TERMINATED:
printf("%s=", key);
quote_c_style(value, NULL, stdout, 0);
putchar('\n');
@@ -148,6 +149,29 @@ static int print_all_fields(struct repository *repo,
return 0;
}
static int print_keys(enum output_format format)
{
char sep;
switch (format) {
case FORMAT_NEWLINE_TERMINATED:
sep = '\n';
break;
case FORMAT_NUL_TERMINATED:
sep = '\0';
break;
default:
die(_("--keys can only be used with --format=lines or --format=nul"));
}
for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
const struct field *field = &repo_info_fields[i];
printf("%s%c", field->key, sep);
}
return 0;
}
static int parse_format_cb(const struct option *opt,
const char *arg, int unset UNUSED)
{
@@ -157,8 +181,8 @@ static int parse_format_cb(const struct option *opt,
*format = FORMAT_NUL_TERMINATED;
else if (!strcmp(arg, "nul"))
*format = FORMAT_NUL_TERMINATED;
else if (!strcmp(arg, "keyvalue"))
*format = FORMAT_KEYVALUE;
else if (!strcmp(arg, "lines"))
*format = FORMAT_NEWLINE_TERMINATED;
else if (!strcmp(arg, "table"))
*format = FORMAT_TABLE;
else
@@ -170,8 +194,9 @@ static int parse_format_cb(const struct option *opt,
static int cmd_repo_info(int argc, const char **argv, const char *prefix,
struct repository *repo)
{
enum output_format format = FORMAT_KEYVALUE;
enum output_format format = FORMAT_NEWLINE_TERMINATED;
int all_keys = 0;
int show_keys = 0;
struct option options[] = {
OPT_CALLBACK_F(0, "format", &format, N_("format"),
N_("output format"),
@@ -181,11 +206,19 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
parse_format_cb),
OPT_BOOL(0, "all", &all_keys, N_("print all keys/values")),
OPT_BOOL(0, "keys", &show_keys, N_("show keys")),
OPT_END()
};
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
if (format != FORMAT_KEYVALUE && format != FORMAT_NUL_TERMINATED)
if (show_keys && (all_keys || argc))
die(_("--keys cannot be used with a <key> or --all"));
if (show_keys)
return print_keys(format);
if (format != FORMAT_NEWLINE_TERMINATED && format != FORMAT_NUL_TERMINATED)
die(_("unsupported output format"));
if (all_keys && argc)
@@ -671,7 +704,7 @@ static int cmd_repo_structure(int argc, const char **argv, const char *prefix,
stats_table_setup_structure(&table, &stats);
stats_table_print_structure(&table);
break;
case FORMAT_KEYVALUE:
case FORMAT_NEWLINE_TERMINATED:
structure_keyvalue_print(&stats, '=', '\n');
break;
case FORMAT_NUL_TERMINATED:

View File

@@ -4,15 +4,6 @@ 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>
@@ -34,7 +25,7 @@ test_repo_info () {
eval "$init_command $repo_name"
'
test_expect_success "keyvalue: $label" '
test_expect_success "lines: $label" '
echo "$key=$expected_value" > expect &&
git -C "$repo_name" repo info "$key" >actual &&
test_cmp expect actual
@@ -115,12 +106,12 @@ test_expect_success '-z uses nul-terminated format' '
test_expect_success 'git repo info uses the last requested format' '
echo "layout.bare=false" >expected &&
git repo info --format=nul -z --format=keyvalue layout.bare >actual &&
git repo info --format=nul -z --format=lines layout.bare >actual &&
test_cmp expected actual
'
test_expect_success 'git repo info --all returns all key-value pairs' '
git repo info $REPO_INFO_KEYS >expect &&
test_expect_success 'git repo info --all and git repo info $(git repo info --keys) output the same data' '
git repo info $(git repo info --keys) >expect &&
git repo info --all >actual &&
test_cmp expect actual
'
@@ -131,4 +122,31 @@ test_expect_success 'git repo info --all <key> aborts' '
test_cmp expect actual
'
test_expect_success 'git repo info --keys --format=nul uses nul-terminated output' '
git repo info --keys --format=lines >lines &&
lf_to_nul <lines >expect &&
git repo info --keys --format=nul >actual &&
test_cmp expect actual
'
test_expect_success 'git repo info --keys aborts when using --format other than lines or nul' '
echo "fatal: --keys can only be used with --format=lines or --format=nul" >expect &&
test_must_fail git repo info --keys --format=table 2>actual &&
test_cmp expect actual
'
test_expect_success 'git repo info --keys aborts when requesting keys' '
echo "fatal: --keys cannot be used with a <key> or --all" >expect &&
test_must_fail git repo info --keys --all 2>actual_all &&
test_must_fail git repo info --keys some.key 2>actual_key &&
test_cmp expect actual_all &&
test_cmp expect actual_key
'
test_expect_success 'git repo info --keys uses lines as its default output format' '
git repo info --keys --format=lines >expect &&
git repo info --keys >actual &&
test_cmp expect actual
'
test_done

View File

@@ -113,7 +113,7 @@ test_expect_success SHA1 'repository with references and objects' '
)
'
test_expect_success SHA1 'keyvalue and nul format' '
test_expect_success SHA1 'lines and nul format' '
test_when_finished "rm -rf repo" &&
git init repo &&
(
@@ -140,7 +140,7 @@ test_expect_success SHA1 'keyvalue and nul format' '
objects.tags.disk_size=$(object_type_disk_usage tag)
EOF
git repo structure --format=keyvalue >out 2>err &&
git repo structure --format=lines >out 2>err &&
test_cmp expect out &&
test_line_count = 0 err &&