@@ -17,12 +17,96 @@ DESCRIPTION
1717A command interface for running git hooks (see linkgit:githooks[5]),
1818for 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+
20102SUBCOMMANDS
21103-----------
22104
23105run::
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
28112Any 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+
49173SEE ALSO
50174--------
51175linkgit:githooks[5]
0 commit comments