Skip to content

Commit 02a0d29

Browse files
committed
Merge branch 'lo/repo-info-keys' into lo/repo-leftover-bits
* lo/repo-info-keys: repo: add new flag --keys to git-repo-info repo: rename the output format "keyvalue" to "lines"
2 parents 73fd778 + 173c43b commit 02a0d29

4 files changed

Lines changed: 97 additions & 34 deletions

File tree

Documentation/git-repo.adoc

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ git-repo - Retrieve information about the repository
88
SYNOPSIS
99
--------
1010
[synopsis]
11-
git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]
12-
git repo structure [--format=(table|keyvalue|nul) | -z]
11+
git repo info [--format=(lines|nul) | -z] [--all | <key>...]
12+
git repo info --keys [--format=(lines|nul) | -z]
13+
git repo structure [--format=(table|lines|nul) | -z]
1314

1415
DESCRIPTION
1516
-----------
@@ -19,7 +20,7 @@ THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
1920

2021
COMMANDS
2122
--------
22-
`info [--format=(keyvalue|nul) | -z] [--all | <key>...]`::
23+
`info [--format=(lines|nul) | -z] [--all | <key>...]`::
2324
Retrieve metadata-related information about the current repository. Only
2425
the requested data will be returned based on their keys (see "INFO KEYS"
2526
section below).
@@ -30,21 +31,32 @@ requested. The `--all` flag requests the values for all the available keys.
3031
The output format can be chosen through the flag `--format`. Two formats are
3132
supported:
3233
+
33-
`keyvalue`:::
34+
35+
`lines`:::
3436
output key-value pairs one per line using the `=` character as
3537
the delimiter between the key and the value. Values containing "unusual"
3638
characters are quoted as explained for the configuration variable
3739
`core.quotePath` (see linkgit:git-config[1]). This is the default.
3840

3941
`nul`:::
40-
similar to `keyvalue`, but using a newline character as the delimiter
42+
similar to `lines`, but using a newline character as the delimiter
4143
between the key and the value and using a NUL character after each value.
4244
This format is better suited for being parsed by another applications than
43-
`keyvalue`. Unlike in the `keyvalue` format, the values are never quoted.
45+
`lines`. Unlike in the `lines` format, the values are never quoted.
4446
+
4547
`-z` is an alias for `--format=nul`.
4648

47-
`structure [--format=(table|keyvalue|nul) | -z]`::
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+
59+
`structure [--format=(table|lines|nul) | -z]`::
4860
Retrieve statistics about the current repository structure. The
4961
following kinds of information are reported:
5062
+
@@ -61,17 +73,17 @@ supported:
6173
change and is not intended for machine parsing. This is the default
6274
format.
6375

64-
`keyvalue`:::
76+
`lines`:::
6577
Each line of output contains a key-value pair for a repository stat.
6678
The '=' character is used to delimit between the key and the value.
6779
Values containing "unusual" characters are quoted as explained for the
6880
configuration variable `core.quotePath` (see linkgit:git-config[1]).
6981

7082
`nul`:::
71-
Similar to `keyvalue`, but uses a NUL character to delimit between
83+
Similar to `lines`, but uses a NUL character to delimit between
7284
key-value pairs instead of a newline. Also uses a newline character as
7385
the delimiter between the key and value instead of '='. Unlike the
74-
`keyvalue` format, values containing "unusual" characters are never
86+
`lines` format, values containing "unusual" characters are never
7587
quoted.
7688
+
7789
`-z` is an alias for `--format=nul`.

builtin/repo.c

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
#include "utf8.h"
1818

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

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

2728
enum output_format {
2829
FORMAT_TABLE,
29-
FORMAT_KEYVALUE,
30+
FORMAT_NEWLINE_TERMINATED,
3031
FORMAT_NUL_TERMINATED,
3132
};
3233

@@ -91,7 +92,7 @@ static void print_field(enum output_format format, const char *key,
9192
const char *value)
9293
{
9394
switch (format) {
94-
case FORMAT_KEYVALUE:
95+
case FORMAT_NEWLINE_TERMINATED:
9596
printf("%s=", key);
9697
quote_c_style(value, NULL, stdout, 0);
9798
putchar('\n');
@@ -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
{
@@ -157,8 +181,8 @@ static int parse_format_cb(const struct option *opt,
157181
*format = FORMAT_NUL_TERMINATED;
158182
else if (!strcmp(arg, "nul"))
159183
*format = FORMAT_NUL_TERMINATED;
160-
else if (!strcmp(arg, "keyvalue"))
161-
*format = FORMAT_KEYVALUE;
184+
else if (!strcmp(arg, "lines"))
185+
*format = FORMAT_NEWLINE_TERMINATED;
162186
else if (!strcmp(arg, "table"))
163187
*format = FORMAT_TABLE;
164188
else
@@ -170,8 +194,9 @@ static int parse_format_cb(const struct option *opt,
170194
static int cmd_repo_info(int argc, const char **argv, const char *prefix,
171195
struct repository *repo)
172196
{
173-
enum output_format format = FORMAT_KEYVALUE;
197+
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,19 @@ 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);
188-
if (format != FORMAT_KEYVALUE && format != FORMAT_NUL_TERMINATED)
214+
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+
221+
if (format != FORMAT_NEWLINE_TERMINATED && format != FORMAT_NUL_TERMINATED)
189222
die(_("unsupported output format"));
190223

191224
if (all_keys && argc)
@@ -671,7 +704,7 @@ static int cmd_repo_structure(int argc, const char **argv, const char *prefix,
671704
stats_table_setup_structure(&table, &stats);
672705
stats_table_print_structure(&table);
673706
break;
674-
case FORMAT_KEYVALUE:
707+
case FORMAT_NEWLINE_TERMINATED:
675708
structure_keyvalue_print(&stats, '=', '\n');
676709
break;
677710
case FORMAT_NUL_TERMINATED:

t/t1900-repo.sh

Lines changed: 31 additions & 13 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>
@@ -34,7 +25,7 @@ test_repo_info () {
3425
eval "$init_command $repo_name"
3526
'
3627

37-
test_expect_success "keyvalue: $label" '
28+
test_expect_success "lines: $label" '
3829
echo "$key=$expected_value" > expect &&
3930
git -C "$repo_name" repo info "$key" >actual &&
4031
test_cmp expect actual
@@ -115,12 +106,12 @@ test_expect_success '-z uses nul-terminated format' '
115106

116107
test_expect_success 'git repo info uses the last requested format' '
117108
echo "layout.bare=false" >expected &&
118-
git repo info --format=nul -z --format=keyvalue layout.bare >actual &&
109+
git repo info --format=nul -z --format=lines layout.bare >actual &&
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

t/t1901-repo-structure.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ test_expect_success SHA1 'repository with references and objects' '
113113
)
114114
'
115115

116-
test_expect_success SHA1 'keyvalue and nul format' '
116+
test_expect_success SHA1 'lines and nul format' '
117117
test_when_finished "rm -rf repo" &&
118118
git init repo &&
119119
(
@@ -140,7 +140,7 @@ test_expect_success SHA1 'keyvalue and nul format' '
140140
objects.tags.disk_size=$(object_type_disk_usage tag)
141141
EOF
142142
143-
git repo structure --format=keyvalue >out 2>err &&
143+
git repo structure --format=lines >out 2>err &&
144144
145145
test_cmp expect out &&
146146
test_line_count = 0 err &&

0 commit comments

Comments
 (0)