Skip to content

Commit bf19490

Browse files
committed
Merge branch 'lo/repo-info-keys' into seen
"git repo info" learns "--keys" action to list known keys. Comments? * lo/repo-info-keys: repo: add new flag --keys to git-repo-info repo: add a default output format to enum output_format
2 parents f03a283 + ac3e74d commit bf19490

File tree

3 files changed

+73
-12
lines changed

3 files changed

+73
-12
lines changed

Documentation/git-repo.adoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SYNOPSIS
99
--------
1010
[synopsis]
1111
git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]
12+
git repo info --keys [--format=(default|nul) | -z]
1213
git repo structure [--format=(table|keyvalue|nul) | -z]
1314

1415
DESCRIPTION
@@ -44,6 +45,16 @@ supported:
4445
+
4546
`-z` is an alias for `--format=nul`.
4647

48+
`info --keys [--format=(default|nul) | -z]`::
49+
List all the available keys, one per line. The output format can be chosen
50+
through the flag `--format`. The following formats are supported:
51+
+
52+
`default`:::
53+
output the keys one per line.
54+
55+
`nul`:::
56+
similar to `default`, but using a NUL character after each value.
57+
4758
`structure [--format=(table|keyvalue|nul) | -z]`::
4859
Retrieve statistics about the current repository structure. The
4960
following kinds of information are reported:

builtin/repo.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

1919
static const char *const repo_usage[] = {
2020
"git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]",
21+
"git repo info --keys [--format=(default|nul) | -z]",
2122
"git repo structure [--format=(table|keyvalue|nul) | -z]",
2223
NULL
2324
};
2425

2526
typedef int get_value_fn(struct repository *repo, struct strbuf *buf);
2627

2728
enum output_format {
29+
FORMAT_DEFAULT,
2830
FORMAT_TABLE,
2931
FORMAT_KEYVALUE,
3032
FORMAT_NUL_TERMINATED,
@@ -148,6 +150,29 @@ static int print_all_fields(struct repository *repo,
148150
return 0;
149151
}
150152

153+
static int print_keys(enum output_format format)
154+
{
155+
char sep;
156+
157+
switch (format) {
158+
case FORMAT_DEFAULT:
159+
sep = '\n';
160+
break;
161+
case FORMAT_NUL_TERMINATED:
162+
sep = '\0';
163+
break;
164+
default:
165+
die(_("--keys can only be used with --format=default or --format=nul"));
166+
}
167+
168+
for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
169+
const struct field *field = &repo_info_fields[i];
170+
printf("%s%c", field->key, sep);
171+
}
172+
173+
return 0;
174+
}
175+
151176
static int parse_format_cb(const struct option *opt,
152177
const char *arg, int unset UNUSED)
153178
{
@@ -161,6 +186,8 @@ static int parse_format_cb(const struct option *opt,
161186
*format = FORMAT_KEYVALUE;
162187
else if (!strcmp(arg, "table"))
163188
*format = FORMAT_TABLE;
189+
else if (!strcmp(arg, "default"))
190+
*format = FORMAT_DEFAULT;
164191
else
165192
die(_("invalid format '%s'"), arg);
166193

@@ -170,8 +197,9 @@ static int parse_format_cb(const struct option *opt,
170197
static int cmd_repo_info(int argc, const char **argv, const char *prefix,
171198
struct repository *repo)
172199
{
173-
enum output_format format = FORMAT_KEYVALUE;
200+
enum output_format format = FORMAT_DEFAULT;
174201
int all_keys = 0;
202+
int show_keys = 0;
175203
struct option options[] = {
176204
OPT_CALLBACK_F(0, "format", &format, N_("format"),
177205
N_("output format"),
@@ -181,10 +209,21 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
181209
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
182210
parse_format_cb),
183211
OPT_BOOL(0, "all", &all_keys, N_("print all keys/values")),
212+
OPT_BOOL(0, "keys", &show_keys, N_("show keys")),
184213
OPT_END()
185214
};
186215

187216
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
217+
218+
if (show_keys && (all_keys || argc))
219+
die(_("--keys cannot be used with a <key> or --all"));
220+
221+
if (show_keys)
222+
return print_keys(format);
223+
224+
if (format == FORMAT_DEFAULT)
225+
format = FORMAT_KEYVALUE;
226+
188227
if (format != FORMAT_KEYVALUE && format != FORMAT_NUL_TERMINATED)
189228
die(_("unsupported output format"));
190229

t/t1900-repo.sh

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ test_description='test git repo-info'
44

55
. ./test-lib.sh
66

7-
# git-repo-info keys. It must contain the same keys listed in the const
8-
# repo_info_fields, in lexicographical order.
9-
REPO_INFO_KEYS='
10-
layout.bare
11-
layout.shallow
12-
object.format
13-
references.format
14-
'
15-
167
# Test whether a key-value pair is correctly returned
178
#
189
# Usage: test_repo_info <label> <init command> <repo_name> <key> <expected value>
@@ -119,8 +110,8 @@ test_expect_success 'git repo info uses the last requested format' '
119110
test_cmp expected actual
120111
'
121112

122-
test_expect_success 'git repo info --all returns all key-value pairs' '
123-
git repo info $REPO_INFO_KEYS >expect &&
113+
test_expect_success 'git repo info --all and git repo info $(git repo info --keys) output the same data' '
114+
git repo info $(git repo info --keys) >expect &&
124115
git repo info --all >actual &&
125116
test_cmp expect actual
126117
'
@@ -131,4 +122,24 @@ test_expect_success 'git repo info --all <key> aborts' '
131122
test_cmp expect actual
132123
'
133124

125+
test_expect_success 'git repo info --keys --format=nul uses nul-terminated output' '
126+
git repo info --keys --format=default >default &&
127+
lf_to_nul <default > expect &&
128+
git repo info --keys --format=nul >actual &&
129+
test_cmp expect actual
130+
'
131+
132+
test_expect_success 'git repo info --keys aborts when using --format other than default or nul' '
133+
echo "fatal: --keys can only be used with --format=default or --format=nul" >expect &&
134+
test_must_fail git repo info --keys --format=keyvalue 2>actual &&
135+
test_cmp expect actual
136+
'
137+
138+
test_expect_success 'git repo info --keys aborts when requesting keys' '
139+
echo "fatal: --keys cannot be used with a <key> or --all" >expect &&
140+
test_must_fail git repo info --keys --all 2>actual_all &&
141+
test_must_fail git repo info --keys some.key 2>actual_key &&
142+
test_cmp expect actual_all &&
143+
test_cmp expect actual_key
144+
'
134145
test_done

0 commit comments

Comments
 (0)