Skip to content

Commit 173c43b

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 <lucasseikioshiro@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent ebb667a commit 173c43b

3 files changed

Lines changed: 72 additions & 11 deletions

File tree

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=(lines|nul) | -z] [--all | <key>...]
12+
git repo info --keys [--format=(lines|nul) | -z]
1213
git repo structure [--format=(table|lines|nul) | -z]
1314

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

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

builtin/repo.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
static const char *const repo_usage[] = {
2020
"git repo info [--format=(lines|nul) | -z] [--all | <key>...]",
21+
"git repo info --keys [--format=(lines|nul) | -z]",
2122
"git repo structure [--format=(table|lines|nul) | -z]",
2223
NULL
2324
};
@@ -148,6 +149,29 @@ static int print_all_fields(struct repository *repo,
148149
return 0;
149150
}
150151

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

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

215+
if (show_keys && (all_keys || argc))
216+
die(_("--keys cannot be used with a <key> or --all"));
217+
218+
if (show_keys)
219+
return print_keys(format);
220+
189221
if (format != FORMAT_NEWLINE_TERMINATED && format != FORMAT_NUL_TERMINATED)
190222
die(_("unsupported output format"));
191223

t/t1900-repo.sh

Lines changed: 29 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,31 @@ 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=lines >lines &&
127+
lf_to_nul <lines >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 lines or nul' '
133+
echo "fatal: --keys can only be used with --format=lines or --format=nul" >expect &&
134+
test_must_fail git repo info --keys --format=table 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+
'
145+
146+
test_expect_success 'git repo info --keys uses lines as its default output format' '
147+
git repo info --keys --format=lines >expect &&
148+
git repo info --keys >actual &&
149+
test_cmp expect actual
150+
'
151+
134152
test_done

0 commit comments

Comments
 (0)