Skip to content

Commit db1aadc

Browse files
lucasoshirogitster
authored andcommitted
repo: add --all to git-repo-info
Add a new flag `--all` to git-repo-info for requesting values for all the available keys. By using this flag, the user can retrieve all the values instead of searching what are the desired keys for what they wants. Helped-by: Karthik Nayak <karthik.188@gmail.com> Helped-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 6ee2b18 commit db1aadc

3 files changed

Lines changed: 59 additions & 4 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

1313
DESCRIPTION
1414
-----------
@@ -18,13 +18,14 @@ THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
1818

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

builtin/repo.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "shallow.h"
1010

1111
static const char *const repo_usage[] = {
12-
"git repo info [--format=(keyvalue|nul)] [-z] [<key>...]",
12+
"git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...]",
1313
NULL
1414
};
1515

@@ -124,6 +124,26 @@ static int print_fields(int argc, const char **argv,
124124
return ret;
125125
}
126126

127+
static void print_all_fields(struct repository *repo,
128+
enum output_format format)
129+
{
130+
struct strbuf valbuf = STRBUF_INIT;
131+
struct strbuf quotbuf = STRBUF_INIT;
132+
133+
for (unsigned long i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
134+
struct field field = repo_info_fields[i];
135+
get_value_fn *get_value = field.get_value;
136+
const char *key = field.key;
137+
138+
strbuf_reset(&valbuf);
139+
get_value(repo, &valbuf);
140+
print_field(format, key, &valbuf, &quotbuf);
141+
}
142+
143+
strbuf_release(&valbuf);
144+
strbuf_release(&quotbuf);
145+
}
146+
127147
static int parse_format_cb(const struct option *opt,
128148
const char *arg, int unset UNUSED)
129149
{
@@ -145,6 +165,7 @@ static int repo_info(int argc, const char **argv, const char *prefix,
145165
struct repository *repo)
146166
{
147167
enum output_format format = FORMAT_KEYVALUE;
168+
int all_keys = 0;
148169
struct option options[] = {
149170
OPT_CALLBACK_F(0, "format", &format, N_("format"),
150171
N_("output format"),
@@ -153,11 +174,15 @@ static int repo_info(int argc, const char **argv, const char *prefix,
153174
N_("synonym for --format=nul"),
154175
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
155176
parse_format_cb),
177+
OPT_BOOL(0, "all", &all_keys, N_("return all keys")),
156178
OPT_END()
157179
};
158180

159181
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
160182

183+
if (all_keys)
184+
print_all_fields(repo, format);
185+
161186
return print_fields(argc, argv, repo, format);
162187
}
163188

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)