Skip to content

Commit 03b4043

Browse files
10ne1gitster
authored andcommitted
hook: include hooks from the config
Teach the hook.[hc] library to parse configs to populate the list of hooks to run for a given event. Multiple commands can be specified for a given hook by providing "hook.<friendly-name>.command = <path-to-hook>" and "hook.<friendly-name>.event = <hook-event>" lines. Hooks will be started in config order of the "hook.<name>.event" lines and will be run sequentially (.jobs == 1) like before. Running the hooks in parallel will be enabled in a future patch. The "traditional" hook from the hookdir is run last, if present. A strmap cache is added to struct repository to avoid re-reading the configs on each rook run. This is useful for hooks like the ref-transaction which gets executed multiple times per process. Examples: $ git config --get-regexp "^hook\." hook.bar.command=~/bar.sh hook.bar.event=pre-commit # Will run ~/bar.sh, then .git/hooks/pre-commit $ git hook run pre-commit Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 9fdaa67 commit 03b4043

File tree

8 files changed

+513
-5
lines changed

8 files changed

+513
-5
lines changed

Documentation/config/hook.adoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
hook.<name>.command::
2+
The command to execute for `hook.<name>`. `<name>` is a unique
3+
"friendly" name that identifies this hook. (The hook events that
4+
trigger the command are configured with `hook.<name>.event`.) The
5+
value can be an executable path or a shell oneliner. If more than
6+
one value is specified for the same `<name>`, only the last value
7+
parsed is used. See linkgit:git-hook[1].
8+
9+
hook.<name>.event::
10+
The hook events that trigger `hook.<name>`. The value is the name
11+
of a hook event, like "pre-commit" or "update". (See
12+
linkgit:githooks[5] for a complete list of hook events.) On the
13+
specified event, the associated `hook.<name>.command` is executed.
14+
This is a multi-valued key. To run `hook.<name>` on multiple
15+
events, specify the key more than once. See linkgit:git-hook[1].

Documentation/git-hook.adoc

Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,96 @@ DESCRIPTION
1717
A command interface for running git hooks (see linkgit:githooks[5]),
1818
for use by other scripted git commands.
1919

20+
This command parses the default configuration files for sets of configs like
21+
so:
22+
23+
[hook "linter"]
24+
event = pre-commit
25+
command = ~/bin/linter --cpp20
26+
27+
In this example, `[hook "linter"]` represents one script - `~/bin/linter
28+
--cpp20` - which can be shared by many repos, and even by many hook events, if
29+
appropriate.
30+
31+
To add an unrelated hook which runs on a different event, for example a
32+
spell-checker for your commit messages, you would write a configuration like so:
33+
34+
[hook "linter"]
35+
event = pre-commit
36+
command = ~/bin/linter --cpp20
37+
[hook "spellcheck"]
38+
event = commit-msg
39+
command = ~/bin/spellchecker
40+
41+
With this config, when you run 'git commit', first `~/bin/linter --cpp20` will
42+
have a chance to check your files to be committed (during the `pre-commit` hook
43+
event`), and then `~/bin/spellchecker` will have a chance to check your commit
44+
message (during the `commit-msg` hook event).
45+
46+
Commands are run in the order Git encounters their associated
47+
`hook.<name>.event` configs during the configuration parse (see
48+
linkgit:git-config[1]). Although multiple `hook.linter.event` configs can be
49+
added, only one `hook.linter.command` event is valid - Git uses "last-one-wins"
50+
to determine which command to run.
51+
52+
So if you wanted your linter to run when you commit as well as when you push,
53+
you would configure it like so:
54+
55+
[hook "linter"]
56+
event = pre-commit
57+
event = pre-push
58+
command = ~/bin/linter --cpp20
59+
60+
With this config, `~/bin/linter --cpp20` would be run by Git before a commit is
61+
generated (during `pre-commit`) as well as before a push is performed (during
62+
`pre-push`).
63+
64+
And if you wanted to run your linter as well as a secret-leak detector during
65+
only the "pre-commit" hook event, you would configure it instead like so:
66+
67+
[hook "linter"]
68+
event = pre-commit
69+
command = ~/bin/linter --cpp20
70+
[hook "no-leaks"]
71+
event = pre-commit
72+
command = ~/bin/leak-detector
73+
74+
With this config, before a commit is generated (during `pre-commit`), Git would
75+
first start `~/bin/linter --cpp20` and second start `~/bin/leak-detector`. It
76+
would evaluate the output of each when deciding whether to proceed with the
77+
commit.
78+
79+
For a full list of hook events which you can set your `hook.<name>.event` to,
80+
and how hooks are invoked during those events, see linkgit:githooks[5].
81+
82+
Git will ignore any `hook.<name>.event` that specifies an event it doesn't
83+
recognize. This is intended so that tools which wrap Git can use the hook
84+
infrastructure to run their own hooks; see "WRAPPERS" for more guidance.
85+
86+
In general, when instructions suggest adding a script to
87+
`.git/hooks/<hook-event>`, you can specify it in the config instead by running:
88+
89+
----
90+
git config set hook.<some-name>.command <path-to-script>
91+
git config set --append hook.<some-name>.event <hook-event>
92+
----
93+
94+
This way you can share the script between multiple repos. That is, `cp
95+
~/my-script.sh ~/project/.git/hooks/pre-commit` would become:
96+
97+
----
98+
git config set hook.my-script.command ~/my-script.sh
99+
git config set --append hook.my-script.event pre-commit
100+
----
101+
20102
SUBCOMMANDS
21103
-----------
22104
23105
run::
24-
Run the `<hook-name>` hook. See linkgit:githooks[5] for
25-
supported hook names.
106+
Runs hooks configured for `<hook-name>`, in the order they are
107+
discovered during the config parse. The default `<hook-name>` from
108+
the hookdir is run last. See linkgit:githooks[5] for supported
109+
hook names.
26110
+
27111
28112
Any positional arguments to the hook should be passed after a
@@ -46,6 +130,46 @@ OPTIONS
46130
tools that want to do a blind one-shot run of a hook that may
47131
or may not be present.
48132

133+
WRAPPERS
134+
--------
135+
136+
`git hook run` has been designed to make it easy for tools which wrap Git to
137+
configure and execute hooks using the Git hook infrastructure. It is possible to
138+
provide arguments and stdin via the command line, as well as specifying parallel
139+
or series execution if the user has provided multiple hooks.
140+
141+
Assuming your wrapper wants to support a hook named "mywrapper-start-tests", you
142+
can have your users specify their hooks like so:
143+
144+
[hook "setup-test-dashboard"]
145+
event = mywrapper-start-tests
146+
command = ~/mywrapper/setup-dashboard.py --tap
147+
148+
Then, in your 'mywrapper' tool, you can invoke any users' configured hooks by
149+
running:
150+
151+
----
152+
git hook run mywrapper-start-tests \
153+
# providing something to stdin
154+
--stdin some-tempfile-123 \
155+
# execute hooks in serial
156+
# plus some arguments of your own...
157+
-- \
158+
--testname bar \
159+
baz
160+
----
161+
162+
Take care to name your wrapper's hook events in a way which is unlikely to
163+
overlap with Git's native hooks (see linkgit:githooks[5]) - a hook event named
164+
`mywrappertool-validate-commit` is much less likely to be added to native Git
165+
than a hook event named `validate-commit`. If Git begins to use a hook event
166+
named the same thing as your wrapper hook, it may invoke your users' hooks in
167+
unintended and unsupported ways.
168+
169+
CONFIGURATION
170+
-------------
171+
include::config/hook.adoc[]
172+
49173
SEE ALSO
50174
--------
51175
linkgit:githooks[5]

builtin/hook.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ static int list(int argc, const char **argv, const char *prefix,
6868
case HOOK_TRADITIONAL:
6969
printf("%s\n", _("hook from hookdir"));
7070
break;
71+
case HOOK_CONFIGURED:
72+
printf("%s\n", h->u.configured.friendly_name);
73+
break;
7174
default:
7275
BUG("unknown hook kind");
7376
}

0 commit comments

Comments
 (0)