Skip to content

Commit ac3e74d

Browse files
lucasoshirogitster
authored andcommitted
repo: add new flag --keys to git-repo-info
If the user wants to find what are the available keys, they need to either check the documentation or to ask for all the key-value pairs by using --all. Add a new flag --keys for listing only the available keys without listing the values. Signed-off-by: Lucas Seiki Oshiro <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2402082 commit ac3e74d

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
static const char *const repo_usage[] = {
1818
"git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]",
19+
"git repo info --keys [--format=(default|nul) | -z]",
1920
"git repo structure [--format=(table|keyvalue|nul) | -z]",
2021
NULL
2122
};
@@ -147,6 +148,29 @@ static int print_all_fields(struct repository *repo,
147148
return 0;
148149
}
149150

151+
static int print_keys(enum output_format format)
152+
{
153+
char sep;
154+
155+
switch (format) {
156+
case FORMAT_DEFAULT:
157+
sep = '\n';
158+
break;
159+
case FORMAT_NUL_TERMINATED:
160+
sep = '\0';
161+
break;
162+
default:
163+
die(_("--keys can only be used with --format=default or --format=nul"));
164+
}
165+
166+
for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
167+
const struct field *field = &repo_info_fields[i];
168+
printf("%s%c", field->key, sep);
169+
}
170+
171+
return 0;
172+
}
173+
150174
static int parse_format_cb(const struct option *opt,
151175
const char *arg, int unset UNUSED)
152176
{
@@ -173,6 +197,7 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
173197
{
174198
enum output_format format = FORMAT_DEFAULT;
175199
int all_keys = 0;
200+
int show_keys = 0;
176201
struct option options[] = {
177202
OPT_CALLBACK_F(0, "format", &format, N_("format"),
178203
N_("output format"),
@@ -182,11 +207,18 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
182207
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
183208
parse_format_cb),
184209
OPT_BOOL(0, "all", &all_keys, N_("print all keys/values")),
210+
OPT_BOOL(0, "keys", &show_keys, N_("show keys")),
185211
OPT_END()
186212
};
187213

188214
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
189215

216+
if (show_keys && (all_keys || argc))
217+
die(_("--keys cannot be used with a <key> or --all"));
218+
219+
if (show_keys)
220+
return print_keys(format);
221+
190222
if (format == FORMAT_DEFAULT)
191223
format = FORMAT_KEYVALUE;
192224

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)