Skip to content

Commit eceb0a6

Browse files
committed
Merge branch 'lo/repo-info-all' into seen
* lo/repo-info-all: repo: add --all to git-repo-info repo: factor out field printing to dedicated function
2 parents 053832b + db1aadc commit eceb0a6

3 files changed

Lines changed: 78 additions & 18 deletions

File tree

Documentation/git-repo.adoc

Lines changed: 4 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,14 @@ 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.
29+
Keys requested after `--all` will be duplicated.
2930
+
3031
The output format can be chosen through the flag `--format`. Two formats are
3132
supported:

builtin/repo.c

Lines changed: 45 additions & 15 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,6 +85,24 @@ 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+
struct strbuf *valbuf, struct strbuf *quotbuf)
90+
{
91+
strbuf_reset(quotbuf);
92+
93+
switch (format) {
94+
case FORMAT_KEYVALUE:
95+
quote_c_style(valbuf->buf, quotbuf, NULL, 0);
96+
printf("%s=%s\n", key, quotbuf->buf);
97+
break;
98+
case FORMAT_NUL_TERMINATED:
99+
printf("%s\n%s%c", key, valbuf->buf, '\0');
100+
break;
101+
default:
102+
BUG("not a valid output format: %d", format);
103+
}
104+
}
105+
88106
static int print_fields(int argc, const char **argv,
89107
struct repository *repo,
90108
enum output_format format)
@@ -105,28 +123,35 @@ static int print_fields(int argc, const char **argv,
105123
}
106124

107125
strbuf_reset(&valbuf);
108-
strbuf_reset(&quotbuf);
109-
110126
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-
}
127+
print_field(format, key, &valbuf, &quotbuf);
123128
}
124129

125130
strbuf_release(&valbuf);
126131
strbuf_release(&quotbuf);
127132
return ret;
128133
}
129134

135+
static void print_all_fields(struct repository *repo,
136+
enum output_format format)
137+
{
138+
struct strbuf valbuf = STRBUF_INIT;
139+
struct strbuf quotbuf = STRBUF_INIT;
140+
141+
for (unsigned long i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
142+
struct field field = repo_info_fields[i];
143+
get_value_fn *get_value = field.get_value;
144+
const char *key = field.key;
145+
146+
strbuf_reset(&valbuf);
147+
get_value(repo, &valbuf);
148+
print_field(format, key, &valbuf, &quotbuf);
149+
}
150+
151+
strbuf_release(&valbuf);
152+
strbuf_release(&quotbuf);
153+
}
154+
130155
static int parse_format_cb(const struct option *opt,
131156
const char *arg, int unset UNUSED)
132157
{
@@ -150,6 +175,7 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
150175
struct repository *repo)
151176
{
152177
enum output_format format = FORMAT_KEYVALUE;
178+
int all_keys = 0;
153179
struct option options[] = {
154180
OPT_CALLBACK_F(0, "format", &format, N_("format"),
155181
N_("output format"),
@@ -158,13 +184,17 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
158184
N_("synonym for --format=nul"),
159185
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
160186
parse_format_cb),
187+
OPT_BOOL(0, "all", &all_keys, N_("return all keys")),
161188
OPT_END()
162189
};
163190

164191
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
165192
if (format != FORMAT_KEYVALUE && format != FORMAT_NUL_TERMINATED)
166193
die(_("unsupported output format"));
167194

195+
if (all_keys)
196+
print_all_fields(repo, format);
197+
168198
return print_fields(argc, argv, repo, format);
169199
}
170200

t/t1900-repo.sh

Lines changed: 29 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,24 @@ 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> duplicates <key>' '
129+
git repo info $REPO_INFO_KEYS object.format >expect &&
130+
git repo info --all object.format >actual &&
131+
test_cmp expect actual
132+
'
133+
134+
test_expect_success 'git repo info --all <invalid key> warns about invalid key' '
135+
git repo info $REPO_INFO_KEYS >expect &&
136+
echo "error: key ${SQ}no.key${SQ} not found" >expect_err &&
137+
test_must_fail git repo info --all no.key >actual 2>actual_err &&
138+
test_cmp expect actual &&
139+
test_cmp expect_err actual_err
140+
'
141+
113142
test_done

0 commit comments

Comments
 (0)