Skip to content

Commit 9741b7c

Browse files
lucasoshirogitster
authored andcommitted
repo: add the --format flag
Add the --format flag to git-repo-info. By using this flag, the users can choose the format for obtaining the data they requested. Given that this command can be used for generating input for other applications and for being read by end users, it requires at least two formats: one for being read by humans and other for being read by machines. Some other Git commands also have two output formats, notably git-config which was the inspiration for the two formats that were chosen here: - keyvalue, where the retrieved data is printed one per line, using = for delimiting the key and the value. This is the default format, targeted for end users. - nul, where the retrieved data is separated by null characters, using the newline character for delimiting the key and the value. This format is targeted for being read by machines. Helped-by: Phillip Wood <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Justin Tobler <[email protected]> Helped-by: Eric Sunshine <[email protected]> Mentored-by: Karthik Nayak <[email protected]> Mentored-by: Patrick Steinhardt <[email protected]> Signed-off-by: Lucas Seiki Oshiro <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 16a64b5 commit 9741b7c

File tree

3 files changed

+97
-20
lines changed

3 files changed

+97
-20
lines changed

Documentation/git-repo.adoc

Lines changed: 30 additions & 6 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 [<key>...]
11+
git repo info [--format=(keyvalue|nul)] [<key>...]
1212

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

1919
COMMANDS
2020
--------
21-
`info [<key>...]`::
21+
`info [--format=(keyvalue|nul)] [<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 returned data is lexicographically sorted by the keys.
2727
+
28-
The output format consists of key-value pairs one per line using the `=`
29-
character as the delimiter between the key and the value. Values containing
30-
"unusual" characters are quoted as explained for the configuration variable
28+
The output format can be chosen through the flag `--format`. Two formats are
29+
supported:
30+
+
31+
* `keyvalue`: output key-value pairs one per line using the `=` character as
32+
the delimiter between the key and the value. Values containing "unusual"
33+
characters are quoted as explained for the configuration variable
3134
`core.quotePath` (see linkgit:git-config[1]). This is the default.
3235

36+
* `nul`: similar to `keyvalue`, but using a newline character as the delimiter
37+
between the key and the value and using a null character after each value.
38+
This format is better suited for being parsed by another applications than
39+
`keyvalue`. Unlike in the `keyvalue` format, the values are never quoted.
40+
3341
INFO KEYS
3442
---------
35-
3643
In order to obtain a set of values from `git repo info`, you should provide
3744
the keys that identify them. Here's a list of the available keys and the
3845
values that they return:
@@ -48,6 +55,23 @@ The reference storage format. The valid values are:
4855
+
4956
include::ref-storage-format.adoc[]
5057

58+
EXAMPLES
59+
--------
60+
61+
* Retrieves the reference format of the current repository:
62+
+
63+
------------
64+
git repo info references.format
65+
------------
66+
+
67+
68+
* Retrieves whether the current repository is bare and whether it is shallow
69+
using the `nul` format:
70+
+
71+
------------
72+
git repo info --format=nul layout.bare layout.shallow
73+
------------
74+
5175
SEE ALSO
5276
--------
5377
linkgit:git-rev-parse[1]

builtin/repo.c

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99
#include "shallow.h"
1010

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

1616
typedef int get_value_fn(struct repository *repo, struct strbuf *buf);
1717

18+
enum output_format {
19+
FORMAT_KEYVALUE,
20+
FORMAT_NUL_TERMINATED,
21+
};
22+
1823
struct field {
1924
const char *key;
2025
get_value_fn *get_value;
@@ -73,7 +78,9 @@ static int qsort_strcmp(const void *va, const void *vb)
7378
return strcmp(a, b);
7479
}
7580

76-
static int print_fields(int argc, const char **argv, struct repository *repo)
81+
static int print_fields(int argc, const char **argv,
82+
struct repository *repo,
83+
enum output_format format)
7784
{
7885
int ret = 0;
7986
const char *last = "";
@@ -101,19 +108,46 @@ static int print_fields(int argc, const char **argv, struct repository *repo)
101108
}
102109

103110
get_value(repo, &valbuf);
104-
quote_c_style(valbuf.buf, &quotbuf, NULL, 0);
105-
printf("%s=%s\n", key, quotbuf.buf);
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("%d: not a valid output format", format);
122+
}
106123
}
107124

108125
strbuf_release(&valbuf);
109126
strbuf_release(&quotbuf);
110127
return ret;
111128
}
112129

113-
static int repo_info(int argc, const char **argv, const char *prefix UNUSED,
130+
static int repo_info(int argc, const char **argv, const char *prefix,
114131
struct repository *repo)
115132
{
116-
return print_fields(argc - 1, argv + 1, repo);
133+
const char *format_str = "keyvalue";
134+
enum output_format format;
135+
struct option options[] = {
136+
OPT_STRING(0, "format", &format_str, N_("format"),
137+
N_("output format")),
138+
OPT_END()
139+
};
140+
141+
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
142+
143+
if (!strcmp(format_str, "keyvalue"))
144+
format = FORMAT_KEYVALUE;
145+
else if (!strcmp(format_str, "nul"))
146+
format = FORMAT_NUL_TERMINATED;
147+
else
148+
die(_("invalid format '%s'"), format_str);
149+
150+
return print_fields(argc, argv, repo, format);
117151
}
118152

119153
int cmd_repo(int argc, const char **argv, const char *prefix,

t/t1900-repo.sh

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ test_repo_info () {
2121
key=$4
2222
expected_value=$5
2323

24-
test_expect_success "$label" '
25-
eval "$init_command $repo_name" &&
26-
echo "$key=$expected_value" >expected &&
27-
git -C $repo_name repo info "$key" >actual &&
24+
repo_name_keyvalue="$repo_name"-keyvalue
25+
repo_name_nul="$repo_name"-nul
26+
27+
test_expect_success "keyvalue: $label" '
28+
eval "$init_command $repo_name_keyvalue" &&
29+
echo "$key=$expected_value" > expected &&
30+
git -C "$repo_name_keyvalue" repo info "$key" >actual &&
2831
test_cmp expected actual
2932
'
33+
34+
test_expect_success "nul: $label" '
35+
eval "$init_command $repo_name_nul" &&
36+
printf "%s\n%s\0" "$key" "$expected_value" >expected &&
37+
git -C "$repo_name_nul" repo info --format=nul "$key" >actual &&
38+
test_cmp_bin expected actual
39+
'
3040
}
3141

3242
test_repo_info 'ref format files is retrieved correctly' '
@@ -44,12 +54,15 @@ test_repo_info 'bare repository = true is retrieved correctly' \
4454
test_repo_info 'shallow repository = false is retrieved correctly' \
4555
'git init' 'nonshallow' 'layout.shallow' 'false'
4656

47-
test_repo_info 'shallow repository = true is retrieved correctly' \
48-
'git init remote &&
57+
test_expect_success 'setup remote' '
58+
git init remote &&
4959
echo x >remote/x &&
5060
git -C remote add x &&
51-
git -C remote commit -m x &&
52-
git clone --depth 1 "file://$PWD/remote"' 'shallow' 'layout.shallow' 'true'
61+
git -C remote commit -m x
62+
'
63+
64+
test_repo_info 'shallow repository = true is retrieved correctly' \
65+
'git clone --depth 1 "file://$PWD/remote"' 'shallow' 'layout.shallow' 'true'
5366

5467
test_expect_success 'git-repo-info fails if an invalid key is requested' '
5568
echo "error: key '\'foo\'' not found" >expected_err &&
@@ -80,4 +93,10 @@ test_expect_success 'output is returned correctly when two keys are requested' '
8093
test_cmp expected actual
8194
'
8295

96+
test_expect_success 'git-repo-info aborts when requesting an invalid format' '
97+
echo "fatal: invalid format '\'foo\''" >expected &&
98+
test_must_fail git repo info --format=foo 2>err &&
99+
test_cmp expected err
100+
'
101+
83102
test_done

0 commit comments

Comments
 (0)