Skip to content

Commit a92f5ca

Browse files
lucasoshirogitster
authored andcommitted
repo: add the flag -z as an alias for --format=nul
Other Git commands that have nul-terminated output (e.g. git-config, git-status, git-ls-files) have a flag `-z` for using the null character as the record separator. Add the `-z` flag to git-repo-info as an alias for `--format=nul`, making it consistent with the behavior of the other commands. 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 c8f660a commit a92f5ca

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

Documentation/git-repo.adoc

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

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

1919
COMMANDS
2020
--------
21-
`info [--format=(keyvalue|nul)] [<key>...]`::
21+
`info [--format=(keyvalue|nul)] [-z] [<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).
@@ -40,6 +40,8 @@ supported:
4040
between the key and the value and using a NUL character after each value.
4141
This format is better suited for being parsed by another applications than
4242
`keyvalue`. Unlike in the `keyvalue` format, the values are never quoted.
43+
+
44+
`-z` is an alias for `--format=nul`.
4345

4446
INFO KEYS
4547
---------

builtin/repo.c

Lines changed: 26 additions & 12 deletions
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)] [<key>...]",
12+
"git repo info [--format=(keyvalue|nul)] [-z] [<key>...]",
1313
NULL
1414
};
1515

@@ -112,26 +112,40 @@ static int print_fields(int argc, const char **argv,
112112
return ret;
113113
}
114114

115+
static int parse_format_cb(const struct option *opt,
116+
const char *arg, int unset UNUSED)
117+
{
118+
enum output_format *format = opt->value;
119+
120+
if (opt->short_name == 'z')
121+
*format = FORMAT_NUL_TERMINATED;
122+
else if (!strcmp(arg, "nul"))
123+
*format = FORMAT_NUL_TERMINATED;
124+
else if (!strcmp(arg, "keyvalue"))
125+
*format = FORMAT_KEYVALUE;
126+
else
127+
die(_("invalid format '%s'"), arg);
128+
129+
return 0;
130+
}
131+
115132
static int repo_info(int argc, const char **argv, const char *prefix,
116133
struct repository *repo)
117134
{
118-
const char *format_str = "keyvalue";
119-
enum output_format format;
135+
enum output_format format = FORMAT_KEYVALUE;
120136
struct option options[] = {
121-
OPT_STRING(0, "format", &format_str, N_("format"),
122-
N_("output format")),
137+
OPT_CALLBACK_F(0, "format", &format, N_("format"),
138+
N_("output format"),
139+
PARSE_OPT_NONEG, parse_format_cb),
140+
OPT_CALLBACK_F('z', NULL, &format, NULL,
141+
N_("synonym for --format=nul"),
142+
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
143+
parse_format_cb),
123144
OPT_END()
124145
};
125146

126147
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
127148

128-
if (!strcmp(format_str, "keyvalue"))
129-
format = FORMAT_KEYVALUE;
130-
else if (!strcmp(format_str, "nul"))
131-
format = FORMAT_NUL_TERMINATED;
132-
else
133-
die(_("invalid format '%s'"), format_str);
134-
135149
return print_fields(argc, argv, repo, format);
136150
}
137151

t/t1900-repo.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,16 @@ test_expect_success 'git-repo-info aborts when requesting an invalid format' '
9292
test_cmp expect actual
9393
'
9494

95+
test_expect_success '-z uses nul-terminated format' '
96+
printf "layout.bare\nfalse\0layout.shallow\nfalse\0" >expected &&
97+
git repo info -z layout.bare layout.shallow >actual &&
98+
test_cmp expected actual
99+
'
100+
101+
test_expect_success 'git repo info uses the last requested format' '
102+
echo "layout.bare=false" >expected &&
103+
git repo info --format=nul -z --format=keyvalue layout.bare >actual &&
104+
test_cmp expected actual
105+
'
106+
95107
test_done

0 commit comments

Comments
 (0)