Skip to content

Commit 4b12cd3

Browse files
10ne1gitster
authored andcommitted
hook: add -z option to "git hook list"
Add a NUL-terminate mode to git hook list, just in case hooks are configured with weird characters like newlines in their names. Suggested-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b51e238 commit 4b12cd3

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

Documentation/git-hook.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SYNOPSIS
99
--------
1010
[verse]
1111
'git hook' run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]
12-
'git hook' list <hook-name>
12+
'git hook' list [-z] <hook-name>
1313

1414
DESCRIPTION
1515
-----------
@@ -113,9 +113,10 @@ Any positional arguments to the hook should be passed after a
113113
mandatory `--` (or `--end-of-options`, see linkgit:gitcli[7]). See
114114
linkgit:githooks[5] for arguments hooks might expect (if any).
115115
116-
list::
116+
list [-z]::
117117
Print a list of hooks which will be run on `<hook-name>` event. If no
118118
hooks are configured for that event, print a warning and return 1.
119+
Use `-z` to terminate output lines with NUL instead of newlines.
119120
120121
OPTIONS
121122
-------
@@ -130,6 +131,9 @@ OPTIONS
130131
tools that want to do a blind one-shot run of a hook that may
131132
or may not be present.
132133

134+
-z::
135+
Terminate "list" output lines with NUL instead of newlines.
136+
133137
WRAPPERS
134138
--------
135139

builtin/hook.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define BUILTIN_HOOK_RUN_USAGE \
1212
N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
1313
#define BUILTIN_HOOK_LIST_USAGE \
14-
N_("git hook list <hook-name>")
14+
N_("git hook list [-z] <hook-name>")
1515

1616
static const char * const builtin_hook_usage[] = {
1717
BUILTIN_HOOK_RUN_USAGE,
@@ -34,9 +34,12 @@ static int list(int argc, const char **argv, const char *prefix,
3434
struct string_list *head;
3535
struct string_list_item *item;
3636
const char *hookname = NULL;
37+
int line_terminator = '\n';
3738
int ret = 0;
3839

3940
struct option list_options[] = {
41+
OPT_SET_INT('z', NULL, &line_terminator,
42+
N_("use NUL as line terminator"), '\0'),
4043
OPT_END(),
4144
};
4245

@@ -66,10 +69,10 @@ static int list(int argc, const char **argv, const char *prefix,
6669

6770
switch (h->kind) {
6871
case HOOK_TRADITIONAL:
69-
printf("%s\n", _("hook from hookdir"));
72+
printf("%s%c", _("hook from hookdir"), line_terminator);
7073
break;
7174
case HOOK_CONFIGURED:
72-
printf("%s\n", h->u.configured.friendly_name);
75+
printf("%s%c", h->u.configured.friendly_name, line_terminator);
7376
break;
7477
default:
7578
BUG("unknown hook kind");

t/t1800-hook.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ test_expect_success 'git hook list: configured hook' '
6161
test_cmp expect actual
6262
'
6363

64+
test_expect_success 'git hook list: -z shows NUL-terminated output' '
65+
test_hook test-hook <<-EOF &&
66+
echo Test hook
67+
EOF
68+
test_config hook.myhook.command "echo Hello" &&
69+
test_config hook.myhook.event test-hook --add &&
70+
71+
printf "myhookQhook from hookdirQ" >expect &&
72+
git hook list -z test-hook >actual.raw &&
73+
nul_to_q <actual.raw >actual &&
74+
test_cmp expect actual
75+
'
76+
6477
test_expect_success 'git hook run: nonexistent hook' '
6578
cat >stderr.expect <<-\EOF &&
6679
error: cannot find a hook named test-hook

0 commit comments

Comments
 (0)