Skip to content

Commit 09b4d75

Browse files
nasamuffingitster
authored andcommitted
hook: add --porcelain to list command
Teach 'git hook list --porcelain <hookname>', which prints simply the commands to be run in the order suggested by the config. This option is intended for use by user scripts, wrappers, or out-of-process Git commands which still want to execute hooks. For example, the following snippet might be added to git-send-email.perl to introduce a `pre-send-email` hook: sub pre_send_email { open(my $fh, 'git hook list --porcelain pre-send-email |'); chomp(my @hooks = <$fh>); close($fh); foreach $hook (@hooks) { system $hook } Signed-off-by: Emily Shaffer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 933f1b2 commit 09b4d75

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

Documentation/git-hook.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-hook - Manage configured hooks
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git hook' list <hook-name>
11+
'git hook' list [--porcelain] <hook-name>
1212

1313
DESCRIPTION
1414
-----------
@@ -43,11 +43,20 @@ Local config
4343
COMMANDS
4444
--------
4545

46-
list <hook-name>::
46+
list [--porcelain] <hook-name>::
4747

4848
List the hooks which have been configured for <hook-name>. Hooks appear
4949
in the order they should be run, and note the config scope where the relevant
5050
`hook.<hook-name>.command` was specified, not the `hookcmd` (if applicable).
51+
+
52+
If `--porcelain` is specified, instead print the commands alone, separated by
53+
newlines, for easy parsing by a script.
54+
55+
OPTIONS
56+
-------
57+
--porcelain::
58+
With `list`, print the commands in the order they should be run,
59+
separated by newlines, for easy parsing by a script.
5160

5261
GIT
5362
---

builtin/hook.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ static int list(int argc, const char **argv, const char *prefix)
1616
struct list_head *head, *pos;
1717
struct hook *item;
1818
struct strbuf hookname = STRBUF_INIT;
19+
int porcelain = 0;
1920

2021
struct option list_options[] = {
22+
OPT_BOOL(0, "porcelain", &porcelain,
23+
"format for execution by a script"),
2124
OPT_END(),
2225
};
2326

@@ -29,6 +32,8 @@ static int list(int argc, const char **argv, const char *prefix)
2932
builtin_hook_usage, list_options);
3033
}
3134

35+
36+
3237
strbuf_addstr(&hookname, argv[0]);
3338

3439
head = hook_list(&hookname);
@@ -41,10 +46,14 @@ static int list(int argc, const char **argv, const char *prefix)
4146

4247
list_for_each(pos, head) {
4348
item = list_entry(pos, struct hook, list);
44-
if (item)
45-
printf("%s:\t%s\n",
46-
config_scope_name(item->origin),
47-
item->command.buf);
49+
if (item) {
50+
if (porcelain)
51+
printf("%s\n", item->command.buf);
52+
else
53+
printf("%s:\t%s\n",
54+
config_scope_name(item->origin),
55+
item->command.buf);
56+
}
4857
}
4958

5059
clear_hook_list();

t/t1360-config-based-hooks.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,15 @@ test_expect_success 'git hook list reorders on duplicate commands' '
5555
test_cmp expected actual
5656
'
5757

58+
test_expect_success 'git hook list --porcelain prints just the command' '
59+
cat >expected <<-\EOF &&
60+
/path/ghi
61+
/path/abc
62+
/path/def
63+
EOF
64+
65+
git hook list --porcelain pre-commit >actual &&
66+
test_cmp expected actual
67+
'
68+
5869
test_done

0 commit comments

Comments
 (0)