Skip to content

Commit 0fec747

Browse files
committed
Merge branch 'lo/repo-info-all'
"git repo info" learned "--all" option. * lo/repo-info-all: repo: add --all to git-repo-info repo: factor out field printing to dedicated function
2 parents b31ab93 + 155caac commit 0fec747

File tree

3 files changed

+69
-21
lines changed

3 files changed

+69
-21
lines changed

Documentation/git-repo.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-repo - Retrieve information about the repository
88
SYNOPSIS
99
--------
1010
[synopsis]
11-
git repo info [--format=(keyvalue|nul)] [-z] [<key>...]
11+
git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...]
1212
git repo structure [--format=(table|keyvalue|nul)]
1313

1414
DESCRIPTION
@@ -19,13 +19,13 @@ THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
1919

2020
COMMANDS
2121
--------
22-
`info [--format=(keyvalue|nul)] [-z] [<key>...]`::
22+
`info [--format=(keyvalue|nul)] [-z] [--all | <key>...]`::
2323
Retrieve metadata-related information about the current repository. Only
2424
the requested data will be returned based on their keys (see "INFO KEYS"
2525
section below).
2626
+
2727
The values are returned in the same order in which their respective keys were
28-
requested.
28+
requested. The `--all` flag requests the values for all the available keys.
2929
+
3030
The output format can be chosen through the flag `--format`. Two formats are
3131
supported:

builtin/repo.c

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "utf8.h"
1616

1717
static const char *const repo_usage[] = {
18-
"git repo info [--format=(keyvalue|nul)] [-z] [<key>...]",
18+
"git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...]",
1919
"git repo structure [--format=(table|keyvalue|nul)]",
2020
NULL
2121
};
@@ -85,13 +85,29 @@ static get_value_fn *get_value_fn_for_key(const char *key)
8585
return found ? found->get_value : NULL;
8686
}
8787

88+
static void print_field(enum output_format format, const char *key,
89+
const char *value)
90+
{
91+
switch (format) {
92+
case FORMAT_KEYVALUE:
93+
printf("%s=", key);
94+
quote_c_style(value, NULL, stdout, 0);
95+
putchar('\n');
96+
break;
97+
case FORMAT_NUL_TERMINATED:
98+
printf("%s\n%s%c", key, value, '\0');
99+
break;
100+
default:
101+
BUG("not a valid output format: %d", format);
102+
}
103+
}
104+
88105
static int print_fields(int argc, const char **argv,
89106
struct repository *repo,
90107
enum output_format format)
91108
{
92109
int ret = 0;
93110
struct strbuf valbuf = STRBUF_INIT;
94-
struct strbuf quotbuf = STRBUF_INIT;
95111

96112
for (int i = 0; i < argc; i++) {
97113
get_value_fn *get_value;
@@ -105,28 +121,31 @@ static int print_fields(int argc, const char **argv,
105121
}
106122

107123
strbuf_reset(&valbuf);
108-
strbuf_reset(&quotbuf);
109-
110124
get_value(repo, &valbuf);
111-
112-
switch (format) {
113-
case FORMAT_KEYVALUE:
114-
quote_c_style(valbuf.buf, &quotbuf, NULL, 0);
115-
printf("%s=%s\n", key, quotbuf.buf);
116-
break;
117-
case FORMAT_NUL_TERMINATED:
118-
printf("%s\n%s%c", key, valbuf.buf, '\0');
119-
break;
120-
default:
121-
BUG("not a valid output format: %d", format);
122-
}
125+
print_field(format, key, valbuf.buf);
123126
}
124127

125128
strbuf_release(&valbuf);
126-
strbuf_release(&quotbuf);
127129
return ret;
128130
}
129131

132+
static int print_all_fields(struct repository *repo,
133+
enum output_format format)
134+
{
135+
struct strbuf valbuf = STRBUF_INIT;
136+
137+
for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
138+
const struct field *field = &repo_info_fields[i];
139+
140+
strbuf_reset(&valbuf);
141+
field->get_value(repo, &valbuf);
142+
print_field(format, field->key, valbuf.buf);
143+
}
144+
145+
strbuf_release(&valbuf);
146+
return 0;
147+
}
148+
130149
static int parse_format_cb(const struct option *opt,
131150
const char *arg, int unset UNUSED)
132151
{
@@ -150,6 +169,7 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
150169
struct repository *repo)
151170
{
152171
enum output_format format = FORMAT_KEYVALUE;
172+
int all_keys = 0;
153173
struct option options[] = {
154174
OPT_CALLBACK_F(0, "format", &format, N_("format"),
155175
N_("output format"),
@@ -158,14 +178,21 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
158178
N_("synonym for --format=nul"),
159179
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
160180
parse_format_cb),
181+
OPT_BOOL(0, "all", &all_keys, N_("print all keys/values")),
161182
OPT_END()
162183
};
163184

164185
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
165186
if (format != FORMAT_KEYVALUE && format != FORMAT_NUL_TERMINATED)
166187
die(_("unsupported output format"));
167188

168-
return print_fields(argc, argv, repo, format);
189+
if (all_keys && argc)
190+
die(_("--all and <key> cannot be used together"));
191+
192+
if (all_keys)
193+
return print_all_fields(repo, format);
194+
else
195+
return print_fields(argc, argv, repo, format);
169196
}
170197

171198
struct ref_stats {

t/t1900-repo.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ 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+
716
# Test whether a key-value pair is correctly returned
817
#
918
# Usage: test_repo_info <label> <init command> <repo_name> <key> <expected value>
@@ -110,4 +119,16 @@ test_expect_success 'git repo info uses the last requested format' '
110119
test_cmp expected actual
111120
'
112121

122+
test_expect_success 'git repo info --all returns all key-value pairs' '
123+
git repo info $REPO_INFO_KEYS >expect &&
124+
git repo info --all >actual &&
125+
test_cmp expect actual
126+
'
127+
128+
test_expect_success 'git repo info --all <key> aborts' '
129+
echo "fatal: --all and <key> cannot be used together" >expect &&
130+
test_must_fail git repo info --all object.format 2>actual &&
131+
test_cmp expect actual
132+
'
133+
113134
test_done

0 commit comments

Comments
 (0)