repo: add new flag --keys to git-repo-info

If the user wants to find what are the available keys, they need to either check the documentation or to ask for all the key-value pairs by using --all. Add a new flag --keys for listing only the available keys without listing the values. Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lucas Seiki Oshiro committed Feb 13, 2026 at 21:35 UTC 173c43be54f0039a9f6e13afaad3c953bb6b06dc
3 files changed +72 -11
Documentation/git-repo.adoc
+11
@@ -9,6 +9,7 @@ SYNOPSIS
9 --------
10 [synopsis]
11 git repo info [--format=(lines|nul) | -z] [--all | <key>...]
12 +git repo info --keys [--format=(lines|nul) | -z]
13 git repo structure [--format=(table|lines|nul) | -z]
14
15 DESCRIPTION
@@ -45,6 +46,16 @@ supported:
46 +
47 `-z` is an alias for `--format=nul`.
48
49 +`info --keys [--format=(lines|nul) | -z]`::
50 + List all the available keys, one per line. The output format can be chosen
51 + through the flag `--format`. The following formats are supported:
52 ++
53 +`lines`:::
54 + Output the keys one per line. This is the default.
55 +
56 +`nul`:::
57 + Similar to `lines`, but using a _NUL_ character after each value.
58 +
59 `structure [--format=(table|lines|nul) | -z]`::
60 Retrieve statistics about the current repository structure. The
61 following kinds of information are reported:
builtin/repo.c
+32
@@ -18,6 +18,7 @@
18
19 static const char *const repo_usage[] = {
20 "git repo info [--format=(lines|nul) | -z] [--all | <key>...]",
21 + "git repo info --keys [--format=(lines|nul) | -z]",
22 "git repo structure [--format=(table|lines|nul) | -z]",
23 NULL
24 };
@@ -148,6 +149,29 @@ static int print_all_fields(struct repository *repo,
149 return 0;
150 }
151
152 +static int print_keys(enum output_format format)
153 +{
154 + char sep;
155 +
156 + switch (format) {
157 + case FORMAT_NEWLINE_TERMINATED:
158 + sep = '\n';
159 + break;
160 + case FORMAT_NUL_TERMINATED:
161 + sep = '\0';
162 + break;
163 + default:
164 + die(_("--keys can only be used with --format=lines or --format=nul"));
165 + }
166 +
167 + for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
168 + const struct field *field = &repo_info_fields[i];
169 + printf("%s%c", field->key, sep);
170 + }
171 +
172 + return 0;
173 +}
174 +
175 static int parse_format_cb(const struct option *opt,
176 const char *arg, int unset UNUSED)
177 {
@@ -172,6 +196,7 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
196 {
197 enum output_format format = FORMAT_NEWLINE_TERMINATED;
198 int all_keys = 0;
199 + int show_keys = 0;
200 struct option options[] = {
201 OPT_CALLBACK_F(0, "format", &format, N_("format"),
202 N_("output format"),
@@ -181,11 +206,18 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
206 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
207 parse_format_cb),
208 OPT_BOOL(0, "all", &all_keys, N_("print all keys/values")),
209 + OPT_BOOL(0, "keys", &show_keys, N_("show keys")),
210 OPT_END()
211 };
212
213 argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
214
215 + if (show_keys && (all_keys || argc))
216 + die(_("--keys cannot be used with a <key> or --all"));
217 +
218 + if (show_keys)
219 + return print_keys(format);
220 +
221 if (format != FORMAT_NEWLINE_TERMINATED && format != FORMAT_NUL_TERMINATED)
222 die(_("unsupported output format"));
223
t/t1900-repo.sh
+29 -11
@@ -4,15 +4,6 @@ test_description='test git repo-info'
4
5 . ./test-lib.sh
6
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 -
7 # Test whether a key-value pair is correctly returned
8 #
9 # Usage: test_repo_info <label> <init command> <repo_name> <key> <expected value>
@@ -119,8 +110,8 @@ test_expect_success 'git repo info uses the last requested format' '
110 test_cmp expected actual
111 '
112
122 -test_expect_success 'git repo info --all returns all key-value pairs' '
123 - git repo info $REPO_INFO_KEYS >expect &&
113 +test_expect_success 'git repo info --all and git repo info $(git repo info --keys) output the same data' '
114 + git repo info $(git repo info --keys) >expect &&
115 git repo info --all >actual &&
116 test_cmp expect actual
117 '
@@ -131,4 +122,31 @@ test_expect_success 'git repo info --all <key> aborts' '
122 test_cmp expect actual
123 '
124
125 +test_expect_success 'git repo info --keys --format=nul uses nul-terminated output' '
126 + git repo info --keys --format=lines >lines &&
127 + lf_to_nul <lines >expect &&
128 + git repo info --keys --format=nul >actual &&
129 + test_cmp expect actual
130 +'
131 +
132 +test_expect_success 'git repo info --keys aborts when using --format other than lines or nul' '
133 + echo "fatal: --keys can only be used with --format=lines or --format=nul" >expect &&
134 + test_must_fail git repo info --keys --format=table 2>actual &&
135 + test_cmp expect actual
136 +'
137 +
138 +test_expect_success 'git repo info --keys aborts when requesting keys' '
139 + echo "fatal: --keys cannot be used with a <key> or --all" >expect &&
140 + test_must_fail git repo info --keys --all 2>actual_all &&
141 + test_must_fail git repo info --keys some.key 2>actual_key &&
142 + test_cmp expect actual_all &&
143 + test_cmp expect actual_key
144 +'
145 +
146 +test_expect_success 'git repo info --keys uses lines as its default output format' '
147 + git repo info --keys --format=lines >expect &&
148 + git repo info --keys >actual &&
149 + test_cmp expect actual
150 +'
151 +
152 test_done