Skip to content

Commit b1e1328

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 6e61fe4 commit b1e1328

File tree

3 files changed

+83
-11
lines changed

3 files changed

+83
-11
lines changed

Documentation/git-repo.adoc

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

1415
DESCRIPTION
@@ -47,6 +48,19 @@ supported:
4748
+
4849
`-z` is an alias for `--format=nul`.
4950

51+
`info --keys [--format=(default|lines|nul) | -z]`::
52+
List all the available keys, one per line. The output format can be chosen
53+
through the flag `--format`. The following formats are supported:
54+
+
55+
`default`:::
56+
synonym for `lines`.
57+
58+
`lines`:::
59+
output the keys one per line.
60+
61+
`nul`:::
62+
similar to `default`, but using a _NUL_ character after each value.
63+
5064
`structure [--format=(default|table|keyvalue|nul) | -z]`::
5165
Retrieve statistics about the current repository structure. The
5266
following kinds of information are reported:

builtin/repo.c

Lines changed: 38 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=(default|keyvalue|nul) | -z] [--all | <key>...]",
19+
"git repo info --keys [--format=(default|lines|nul) | -z]",
1920
"git repo structure [--format=(default|table|keyvalue|nul) | -z]",
2021
NULL
2122
};
@@ -27,6 +28,7 @@ enum output_format {
2728
FORMAT_TABLE,
2829
FORMAT_KEYVALUE,
2930
FORMAT_NUL_TERMINATED,
31+
FORMAT_LINES
3032
};
3133

3234
struct field {
@@ -147,6 +149,32 @@ static int print_all_fields(struct repository *repo,
147149
return 0;
148150
}
149151

152+
static int print_keys(enum output_format format)
153+
{
154+
char sep;
155+
156+
if (format == FORMAT_DEFAULT)
157+
format = FORMAT_LINES;
158+
159+
switch (format) {
160+
case FORMAT_LINES:
161+
sep = '\n';
162+
break;
163+
case FORMAT_NUL_TERMINATED:
164+
sep = '\0';
165+
break;
166+
default:
167+
die(_("--keys can only be used with --format=default or --format=nul"));
168+
}
169+
170+
for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
171+
const struct field *field = &repo_info_fields[i];
172+
printf("%s%c", field->key, sep);
173+
}
174+
175+
return 0;
176+
}
177+
150178
static int parse_format_cb(const struct option *opt,
151179
const char *arg, int unset UNUSED)
152180
{
@@ -160,6 +188,8 @@ static int parse_format_cb(const struct option *opt,
160188
*format = FORMAT_KEYVALUE;
161189
else if (!strcmp(arg, "table"))
162190
*format = FORMAT_TABLE;
191+
else if (!strcmp(arg, "lines"))
192+
*format = FORMAT_LINES;
163193
else if (!strcmp(arg, "default"))
164194
*format = FORMAT_DEFAULT;
165195
else
@@ -173,6 +203,7 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
173203
{
174204
enum output_format format = FORMAT_DEFAULT;
175205
int all_keys = 0;
206+
int show_keys = 0;
176207
struct option options[] = {
177208
OPT_CALLBACK_F(0, "format", &format, N_("format"),
178209
N_("output format"),
@@ -182,11 +213,18 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
182213
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
183214
parse_format_cb),
184215
OPT_BOOL(0, "all", &all_keys, N_("print all keys/values")),
216+
OPT_BOOL(0, "keys", &show_keys, N_("show keys")),
185217
OPT_END()
186218
};
187219

188220
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
189221

222+
if (show_keys && (all_keys || argc))
223+
die(_("--keys cannot be used with a <key> or --all"));
224+
225+
if (show_keys)
226+
return print_keys(format);
227+
190228
if (format == FORMAT_DEFAULT)
191229
format = FORMAT_KEYVALUE;
192230

t/t1900-repo.sh

Lines changed: 31 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
'
@@ -143,4 +134,33 @@ test_expect_success '--format=default resets the format' '
143134
test_cmp expect actual
144135
'
145136

137+
test_expect_success 'git repo info --keys --format=nul uses nul-terminated output' '
138+
git repo info --keys --format=default >default &&
139+
lf_to_nul <default >expect &&
140+
git repo info --keys --format=nul >actual &&
141+
test_cmp expect actual
142+
'
143+
144+
test_expect_success 'git repo info --keys aborts when using --format other than default or nul' '
145+
echo "fatal: --keys can only be used with --format=default or --format=nul" >expect &&
146+
test_must_fail git repo info --keys --format=keyvalue 2>actual &&
147+
test_cmp expect actual
148+
'
149+
150+
test_expect_success 'git repo info --keys aborts when requesting keys' '
151+
echo "fatal: --keys cannot be used with a <key> or --all" >expect &&
152+
test_must_fail git repo info --keys --all 2>actual_all &&
153+
test_must_fail git repo info --keys some.key 2>actual_key &&
154+
test_cmp expect actual_all &&
155+
test_cmp expect actual_key
156+
'
157+
158+
test_expect_success 'git repo info --keys uses lines as its default output format' '
159+
git repo info --keys --format=lines >expect &&
160+
git repo info --keys --format=default >actual_explicit &&
161+
git repo info --keys >actual_implicit &&
162+
test_cmp expect actual_explicit &&
163+
test_cmp expect actual_implicit
164+
'
165+
146166
test_done

0 commit comments

Comments
 (0)