Skip to content

Commit e6fa05d

Browse files
committed
Merge branch 'lo/repo-info-all' into seen
"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 3c952b9 + 5ab19b3 commit e6fa05d

File tree

3 files changed

+72
-18
lines changed

3 files changed

+72
-18
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: 48 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,33 @@ 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+
144+
strbuf_reset(&valbuf);
145+
field.get_value(repo, &valbuf);
146+
print_field(format, field.key, &valbuf, &quotbuf);
147+
}
148+
149+
strbuf_release(&valbuf);
150+
strbuf_release(&quotbuf);
151+
}
152+
130153
static int parse_format_cb(const struct option *opt,
131154
const char *arg, int unset UNUSED)
132155
{
@@ -150,6 +173,7 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
150173
struct repository *repo)
151174
{
152175
enum output_format format = FORMAT_KEYVALUE;
176+
int all_keys = 0;
153177
struct option options[] = {
154178
OPT_CALLBACK_F(0, "format", &format, N_("format"),
155179
N_("output format"),
@@ -158,13 +182,22 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
158182
N_("synonym for --format=nul"),
159183
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
160184
parse_format_cb),
185+
OPT_BOOL(0, "all", &all_keys, N_("return all keys")),
161186
OPT_END()
162187
};
163188

164189
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
165190
if (format != FORMAT_KEYVALUE && format != FORMAT_NUL_TERMINATED)
166191
die(_("unsupported output format"));
167192

193+
if (all_keys) {
194+
if (argc)
195+
die(_("--all and <key> cannot be used together"));
196+
197+
print_all_fields(repo, format);
198+
return 0;
199+
}
200+
168201
return print_fields(argc, argv, repo, format);
169202
}
170203

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)