Skip to content

Commit ad1058b

Browse files
committed
config-batch: create 'help' command
Tools that use the 'git config-batch' tool will want to know which commands are available in the current Git version. Having a 'help' command assists greatly to give a clear set of available commands and their versions. Signed-off-by: Derrick Stolee <[email protected]>
1 parent bb7584c commit ad1058b

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

Documentation/git-config-batch.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ unknown_command LF
3838

3939
These are the commands that are currently understood:
4040

41+
`help` version 1::
42+
The `help` command lists the currently-available commands in
43+
this version of Git. The output is multi-line, but the first
44+
line provides the count of possible commands via `help count <N>`.
45+
The next `<N>` lines are of the form `help <command> <version>`
46+
to state that this Git version supports that `<command>` at
47+
version `<version>`. Note that the same command may have multiple
48+
available versions.
49+
+
50+
Here is the currentl output of the help text at the latest version:
51+
+
52+
------------
53+
help count 2
54+
help help 1
55+
help get 1
56+
------------
57+
4158
`get` version 1::
4259
The `get` command searches the config key-value pairs within a
4360
given `<scope>` for values that match the fixed `<key>` and

builtin/config-batch.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static const char *const builtin_config_batch_usage[] = {
1212
};
1313

1414
#define UNKNOWN_COMMAND "unknown_command"
15+
#define HELP_COMMAND "help"
1516
#define GET_COMMAND "get"
1617
#define COMMAND_PARSE_ERROR "command_parse_error"
1718

@@ -55,6 +56,9 @@ static int unknown_command(struct repository *repo UNUSED,
5556
return emit_response(UNKNOWN_COMMAND, NULL);
5657
}
5758

59+
static int help_command_1(struct repository *repo,
60+
int argc, const char **argv);
61+
5862
enum value_match_mode {
5963
MATCH_ALL,
6064
MATCH_EXACT,
@@ -240,6 +244,11 @@ struct command {
240244
};
241245

242246
static struct command commands[] = {
247+
{
248+
.name = HELP_COMMAND,
249+
.fn = help_command_1,
250+
.version = 1,
251+
},
243252
{
244253
.name = GET_COMMAND,
245254
.fn = get_command_1,
@@ -254,6 +263,30 @@ static struct command commands[] = {
254263

255264
#define COMMAND_COUNT ((size_t)(sizeof(commands) / sizeof(*commands)))
256265

266+
267+
static int help_command_1(struct repository *repo UNUSED,
268+
int argc UNUSED, const char **argv UNUSED)
269+
{
270+
struct strbuf fmt_str = STRBUF_INIT;
271+
272+
strbuf_addf(&fmt_str, "%"PRIuMAX, COMMAND_COUNT - 1);
273+
emit_response(HELP_COMMAND, "count", fmt_str.buf, NULL);
274+
strbuf_reset(&fmt_str);
275+
276+
for (size_t i = 0; i < COMMAND_COUNT; i++) {
277+
/* Halt at unknown command. */
278+
if (!commands[i].name[0])
279+
break;
280+
281+
strbuf_addf(&fmt_str, "%d", commands[i].version);
282+
emit_response(HELP_COMMAND, commands[i].name, fmt_str.buf, NULL);
283+
strbuf_reset(&fmt_str);
284+
}
285+
286+
strbuf_release(&fmt_str);
287+
return 0;
288+
}
289+
257290
/**
258291
* Process a single line from stdin and process the command.
259292
*

t/t1312-config-batch.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ test_expect_success 'unknown_command' '
1616
test_cmp expect out
1717
'
1818

19+
test_expect_success 'help command' '
20+
echo "help 1" >in &&
21+
22+
cat >expect <<-\EOF &&
23+
help count 2
24+
help help 1
25+
help get 1
26+
EOF
27+
28+
git config-batch >out <in &&
29+
test_cmp expect out
30+
'
31+
1932
test_expect_success 'failed to parse version' '
2033
echo "bogus BAD_VERSION line of tokens" >in &&
2134
test_must_fail git config-batch 2>err <in &&

0 commit comments

Comments
 (0)