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 <karthik.188@gmail.com> Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lucas Seiki Oshiro committed Sep 4, 2025 at 10:40 UTC a92f5ca0d5c1b27f70a519efba967d613fd48a7a
3 files changed +42 -14
Documentation/git-repo.adoc
+4 -2
@@ -8,7 +8,7 @@ git-repo - Retrieve information about the repository
8 SYNOPSIS
9 --------
10 [synopsis]
11 -git repo info [--format=(keyvalue|nul)] [<key>...]
11 +git repo info [--format=(keyvalue|nul)] [-z] [<key>...]
12
13 DESCRIPTION
14 -----------
@@ -18,7 +18,7 @@ THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
18
19 COMMANDS
20 --------
21 -`info [--format=(keyvalue|nul)] [<key>...]`::
21 +`info [--format=(keyvalue|nul)] [-z] [<key>...]`::
22 Retrieve metadata-related information about the current repository. Only
23 the requested data will be returned based on their keys (see "INFO KEYS"
24 section below).
@@ -40,6 +40,8 @@ supported:
40 between the key and the value and using a NUL character after each value.
41 This format is better suited for being parsed by another applications than
42 `keyvalue`. Unlike in the `keyvalue` format, the values are never quoted.
43 ++
44 +`-z` is an alias for `--format=nul`.
45
46 INFO KEYS
47 ---------
builtin/repo.c
+26 -12
@@ -9,7 +9,7 @@
9 #include "shallow.h"
10
11 static const char *const repo_usage[] = {
12 - "git repo info [--format=(keyvalue|nul)] [<key>...]",
12 + "git repo info [--format=(keyvalue|nul)] [-z] [<key>...]",
13 NULL
14 };
15
@@ -112,26 +112,40 @@ static int print_fields(int argc, const char **argv,
112 return ret;
113 }
114
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 +
132 static int repo_info(int argc, const char **argv, const char *prefix,
133 struct repository *repo)
134 {
118 - const char *format_str = "keyvalue";
119 - enum output_format format;
135 + enum output_format format = FORMAT_KEYVALUE;
136 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),
144 OPT_END()
145 };
146
147 argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
148
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 -
149 return print_fields(argc, argv, repo, format);
150 }
151
t/t1900-repo.sh
+12
@@ -92,4 +92,16 @@ test_expect_success 'git-repo-info aborts when requesting an invalid format' '
92 test_cmp expect actual
93 '
94
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 +
107 test_done