Skip to content

Commit 204c3f4

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 <stolee@gmail.com>
1 parent 5289664 commit 204c3f4

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-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 1 count 2
54+
help 1 help 1
55+
help 1 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: 32 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

@@ -104,6 +105,9 @@ static size_t parse_token(char **data, size_t *data_len,
104105
return parse_whitespace_token(data, data_len, token, err);
105106
}
106107

108+
static int help_command_1(struct repository *repo,
109+
char *data, size_t data_len);
110+
107111
enum value_match_mode {
108112
MATCH_ALL,
109113
MATCH_EXACT,
@@ -302,6 +306,11 @@ struct command {
302306
};
303307

304308
static struct command commands[] = {
309+
{
310+
.name = HELP_COMMAND,
311+
.fn = help_command_1,
312+
.version = 1,
313+
},
305314
{
306315
.name = GET_COMMAND,
307316
.fn = get_command_1,
@@ -316,6 +325,29 @@ static struct command commands[] = {
316325

317326
#define COMMAND_COUNT ((size_t)(sizeof(commands) / sizeof(*commands)))
318327

328+
static int help_command_1(struct repository *repo UNUSED,
329+
char *data UNUSED, size_t data_len UNUSED)
330+
{
331+
struct strbuf fmt_str = STRBUF_INIT;
332+
333+
strbuf_addf(&fmt_str, "%"PRIuMAX, COMMAND_COUNT - 1);
334+
emit_response(HELP_COMMAND, "1", "count", fmt_str.buf, NULL);
335+
strbuf_reset(&fmt_str);
336+
337+
for (size_t i = 0; i < COMMAND_COUNT; i++) {
338+
/* Halt at unknown command. */
339+
if (!commands[i].name[0])
340+
break;
341+
342+
strbuf_addf(&fmt_str, "%d", commands[i].version);
343+
emit_response(HELP_COMMAND, "1", commands[i].name, fmt_str.buf, NULL);
344+
strbuf_reset(&fmt_str);
345+
}
346+
347+
strbuf_release(&fmt_str);
348+
return 0;
349+
}
350+
319351
/**
320352
* Process a single line from stdin and process the command.
321353
*

t/t1312-config-batch.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ test_expect_success 'completely broken input' '
2323
test_grep "an unrecoverable error occurred during command execution" err
2424
'
2525

26+
test_expect_success 'help command' '
27+
echo "help 1" >in &&
28+
29+
cat >expect <<-\EOF &&
30+
help 1 count 2
31+
help 1 help 1
32+
help 1 get 1
33+
EOF
34+
35+
git config-batch >out <in &&
36+
test_cmp expect out
37+
'
38+
2639
test_expect_success 'failed to parse version' '
2740
echo "bogus BAD_VERSION line of tokens" >in &&
2841
test_must_fail git config-batch 2>err <in &&

0 commit comments

Comments
 (0)