1
1
## Git Hooks ##
2
2
3
- [ Git Hooks] ( http://www.kernel.org/pub/software/scm/git/docs/githooks.html )
3
+ Hooks are little scripts you can place in $GIT_DIR/hooks directory to trigger
4
+ action at certain points. When git-init is run, a handful example hooks are
5
+ copied in the hooks directory of the new repository, but by default they are
6
+ all disabled. To enable a hook, rename it by removing its .sample suffix.
4
7
5
- ### Server Side Hooks ###
6
8
7
- #### Post Receive # ###
9
+ ### applypatch-msg ###
8
10
11
+ GIT_DIR/hooks/applypatch-msg
12
+
13
+ This hook is invoked by git-am script. It takes a single parameter, the name
14
+ of the file that holds the proposed commit log message. Exiting with non-zero
15
+ status causes git-am to abort before applying the patch.
9
16
10
- GIT_DIR/hooks/post-receive
17
+ The hook is allowed to edit the message file in place, and can be used to
18
+ normalize the message into some project standard format (if the project has one).
19
+ It can also be used to refuse the commit after inspecting the message file.
20
+ The default applypatch-msg hook, when enabled, runs the commit-msg hook, if the
21
+ latter is enabled.
11
22
12
- If you wrote it in Ruby, you might get the args this way:
13
23
14
- ruby
15
- rev_old, rev_new, ref = STDIN.read.split(" ")
24
+ ### pre-applypatch ###
16
25
17
- Or in a bash script, something like this would work:
18
-
19
- #!/bin/sh
20
- # <oldrev > <newrev > <refname >
21
- # update a blame tree
22
- while read oldrev newrev ref
23
- do
24
- echo "STARTING [ $oldrev $newrev $ref] "
25
- for path in ` git diff-tree -r $oldrev..$newrev | awk '{print $6}' `
26
- do
27
- echo "git update-ref refs/blametree/$ref/$path $newrev"
28
- ` git update-ref refs/blametree/$ref/$path $newrev `
29
- done
30
- done
31
-
26
+ GIT_DIR/hooks/pre-applypatch
27
+
28
+ This hook is invoked by git-am. It takes no parameter, and is invoked after the
29
+ patch is applied, but before a commit is made.
30
+ If it exits with non-zero status, then the working tree will not be committed
31
+ after applying the patch.
32
+
33
+ It can be used to inspect the current working tree and refuse to make a commit
34
+ if it does not pass certain test.
35
+ The default pre-applypatch hook, when enabled, runs the pre-commit hook, if the
36
+ latter is enabled.
37
+
38
+
39
+ ### post-applypatch ###
32
40
33
- ### Client Side Hooks ###
41
+ GIT_DIR/hooks/post-applypatch
42
+
43
+ This hook is invoked by 'git-am'. It takes no parameter,
44
+ and is invoked after the patch is applied and a commit is made.
34
45
46
+ This hook is meant primarily for notification, and cannot affect
47
+ the outcome of 'git-am'.
35
48
36
- #### Pre Commit ####
37
49
38
- Running your tests automatically before you commit
50
+ ### pre-commit ###
51
+
52
+ GIT_DIR/hooks/pre-commit
39
53
40
- GIT_DIR/hooks/pre-commit
54
+ This hook is invoked by 'git-commit', and can be bypassed
55
+ with ` \--no-verify ` option. It takes no parameter, and is
56
+ invoked before obtaining the proposed commit log message and
57
+ making a commit. Exiting with non-zero status from this script
58
+ causes the 'git-commit' to abort.
59
+
60
+ The default 'pre-commit' hook, when enabled, catches introduction
61
+ of lines with trailing whitespaces and aborts the commit when
62
+ such a line is found.
63
+
64
+ All the 'git-commit' hooks are invoked with the environment
65
+ variable ` GIT_EDITOR=: ` if the command will not bring up an editor
66
+ to modify the commit message.
41
67
42
68
Here is an example of a Ruby script that runs RSpec tests before allowing a commit.
43
69
@@ -61,4 +87,272 @@ Here is an example of a Ruby script that runs RSpec tests before allowing a comm
61
87
exit 1
62
88
end
63
89
90
+
91
+ ### prepare-commit-msg ###
92
+
93
+ GIT_DIR/hooks/prepare-commit-msg
94
+
95
+ This hook is invoked by 'git-commit' right after preparing the
96
+ default log message, and before the editor is started.
97
+
98
+ It takes one to three parameters. The first is the name of the file
99
+ that the commit log message. The second is the source of the commit
100
+ message, and can be: ` message ` (if a ` -m ` or ` -F ` option was
101
+ given); ` template ` (if a ` -t ` option was given or the
102
+ configuration option ` commit.template ` is set); ` merge ` (if the
103
+ commit is a merge or a ` .git/MERGE_MSG ` file exists); ` squash `
104
+ (if a ` .git/SQUASH_MSG ` file exists); or ` commit ` , followed by
105
+ a commit SHA1 (if a ` -c ` , ` -C ` or ` \--amend ` option was given).
106
+
107
+ If the exit status is non-zero, 'git-commit' will abort.
108
+
109
+ The purpose of the hook is to edit the message file in place, and
110
+ it is not suppressed by the ` \--no-verify ` option. A non-zero exit
111
+ means a failure of the hook and aborts the commit. It should not
112
+ be used as replacement for pre-commit hook.
113
+
114
+ The sample ` prepare-commit-msg ` hook that comes with git comments
115
+ out the ` Conflicts: ` part of a merge's commit message.
116
+
117
+
118
+ ### commit-msg ###
119
+
120
+ GIT_DIR/hooks/commit-msg
121
+
122
+ This hook is invoked by 'git-commit', and can be bypassed
123
+ with ` \--no-verify ` option. It takes a single parameter, the
124
+ name of the file that holds the proposed commit log message.
125
+ Exiting with non-zero status causes the 'git-commit' to
126
+ abort.
127
+
128
+ The hook is allowed to edit the message file in place, and can
129
+ be used to normalize the message into some project standard
130
+ format (if the project has one). It can also be used to refuse
131
+ the commit after inspecting the message file.
132
+
133
+ The default 'commit-msg' hook, when enabled, detects duplicate
134
+ "Signed-off-by" lines, and aborts the commit if one is found.
135
+
136
+
137
+ ### post-commit ###
138
+
139
+ GIT_DIR/hooks/post-commit
140
+
141
+ This hook is invoked by 'git-commit'. It takes no
142
+ parameter, and is invoked after a commit is made.
143
+
144
+ This hook is meant primarily for notification, and cannot affect
145
+ the outcome of 'git-commit'.
146
+
147
+
148
+ ### pre-rebase ###
149
+
150
+ GIT_DIR/hooks/pre-rebase
151
+
152
+ This hook is called by 'git-rebase' and can be used to prevent a branch
153
+ from getting rebased.
154
+
155
+
156
+ ### post-checkout ###
157
+
158
+ GIT_DIR/hooks/post-checkout
159
+
160
+ This hook is invoked when a 'git-checkout' is run after having updated the
161
+ worktree. The hook is given three parameters: the ref of the previous HEAD,
162
+ the ref of the new HEAD (which may or may not have changed), and a flag
163
+ indicating whether the checkout was a branch checkout (changing branches,
164
+ flag=1) or a file checkout (retrieving a file from the index, flag=0).
165
+ This hook cannot affect the outcome of 'git-checkout'.
166
+
167
+ This hook can be used to perform repository validity checks, auto-display
168
+ differences from the previous HEAD if different, or set working dir metadata
169
+ properties.
170
+
171
+
172
+ ### post-merge ###
173
+
174
+ GIT_DIR/hooks/post-merge
175
+
176
+ This hook is invoked by 'git-merge', which happens when a 'git-pull'
177
+ is done on a local repository. The hook takes a single parameter, a status
178
+ flag specifying whether or not the merge being done was a squash merge.
179
+ This hook cannot affect the outcome of 'git-merge' and is not executed,
180
+ if the merge failed due to conflicts.
181
+
182
+ This hook can be used in conjunction with a corresponding pre-commit hook to
183
+ save and restore any form of metadata associated with the working tree
184
+ (eg: permissions/ownership, ACLS, etc). See contrib/hooks/setgitperms.perl
185
+ for an example of how to do this.
186
+
187
+
188
+ ### pre-receive ###
189
+
190
+ GIT_DIR/hooks/pre-receive
191
+
192
+ This hook is invoked by 'git-receive-pack' on the remote repository,
193
+ which happens when a 'git-push' is done on a local repository.
194
+ Just before starting to update refs on the remote repository, the
195
+ pre-receive hook is invoked. Its exit status determines the success
196
+ or failure of the update.
197
+
198
+ This hook executes once for the receive operation. It takes no
199
+ arguments, but for each ref to be updated it receives on standard
200
+ input a line of the format:
201
+
202
+ <old-value > SP <new-value > SP <ref-name > LF
203
+
204
+ where ` <old-value> ` is the old object name stored in the ref,
205
+ ` <new-value> ` is the new object name to be stored in the ref and
206
+ ` <ref-name> ` is the full name of the ref.
207
+ When creating a new ref, ` <old-value> ` is 40 ` 0 ` .
208
+
209
+ If the hook exits with non-zero status, none of the refs will be
210
+ updated. If the hook exits with zero, updating of individual refs can
211
+ still be prevented by the <<update,'update'>> hook.
212
+
213
+ Both standard output and standard error output are forwarded to
214
+ 'git-send-pack' on the other end, so you can simply ` echo ` messages
215
+ for the user.
216
+
217
+ If you wrote it in Ruby, you might get the args this way:
218
+
219
+ ruby
220
+ rev_old, rev_new, ref = STDIN.read.split(" ")
221
+
222
+ Or in a bash script, something like this would work:
223
+
224
+ #!/bin/sh
225
+ # <oldrev > <newrev > <refname >
226
+ # update a blame tree
227
+ while read oldrev newrev ref
228
+ do
229
+ echo "STARTING [ $oldrev $newrev $ref] "
230
+ for path in ` git diff-tree -r $oldrev..$newrev | awk '{print $6}' `
231
+ do
232
+ echo "git update-ref refs/blametree/$ref/$path $newrev"
233
+ ` git update-ref refs/blametree/$ref/$path $newrev `
234
+ done
235
+ done
236
+
237
+
238
+ ### update ###
239
+
240
+ GIT_DIR/hooks/update
241
+
242
+ This hook is invoked by 'git-receive-pack' on the remote repository,
243
+ which happens when a 'git-push' is done on a local repository.
244
+ Just before updating the ref on the remote repository, the update hook
245
+ is invoked. Its exit status determines the success or failure of
246
+ the ref update.
247
+
248
+ The hook executes once for each ref to be updated, and takes
249
+ three parameters:
250
+
251
+ - the name of the ref being updated,
252
+ - the old object name stored in the ref,
253
+ - and the new objectname to be stored in the ref.
254
+
255
+ A zero exit from the update hook allows the ref to be updated.
256
+ Exiting with a non-zero status prevents 'git-receive-pack'
257
+ from updating that ref.
258
+
259
+ This hook can be used to prevent 'forced' update on certain refs by
260
+ making sure that the object name is a commit object that is a
261
+ descendant of the commit object named by the old object name.
262
+ That is, to enforce a "fast forward only" policy.
263
+
264
+ It could also be used to log the old..new status. However, it
265
+ does not know the entire set of branches, so it would end up
266
+ firing one e-mail per ref when used naively, though. The
267
+ <<post-receive,'post-receive'>> hook is more suited to that.
268
+
269
+ Another use suggested on the mailing list is to use this hook to
270
+ implement access control which is finer grained than the one
271
+ based on filesystem group.
272
+
273
+ Both standard output and standard error output are forwarded to
274
+ 'git-send-pack' on the other end, so you can simply ` echo ` messages
275
+ for the user.
276
+
277
+ The default 'update' hook, when enabled--and with
278
+ ` hooks.allowunannotated ` config option turned on--prevents
279
+ unannotated tags to be pushed.
280
+
281
+
282
+ ### post-receive ###
283
+
284
+ GIT_DIR/hooks/post-receive
285
+
286
+ This hook is invoked by 'git-receive-pack' on the remote repository,
287
+ which happens when a 'git-push' is done on a local repository.
288
+ It executes on the remote repository once after all the refs have
289
+ been updated.
290
+
291
+ This hook executes once for the receive operation. It takes no
292
+ arguments, but gets the same information as the
293
+ <<pre-receive,'pre-receive'>>
294
+ hook does on its standard input.
295
+
296
+ This hook does not affect the outcome of 'git-receive-pack', as it
297
+ is called after the real work is done.
298
+
299
+ This supersedes the <<post-update,'post-update'>> hook in that it gets
300
+ both old and new values of all the refs in addition to their
301
+ names.
302
+
303
+ Both standard output and standard error output are forwarded to
304
+ 'git-send-pack' on the other end, so you can simply ` echo ` messages
305
+ for the user.
306
+
307
+ The default 'post-receive' hook is empty, but there is
308
+ a sample script ` post-receive-email ` provided in the ` contrib/hooks `
309
+ directory in git distribution, which implements sending commit
310
+ emails.
311
+
312
+
313
+ ### post-update ###
314
+
315
+ GIT_DIR/hooks/post-update
316
+
317
+ This hook is invoked by 'git-receive-pack' on the remote repository,
318
+ which happens when a 'git-push' is done on a local repository.
319
+ It executes on the remote repository once after all the refs have
320
+ been updated.
321
+
322
+ It takes a variable number of parameters, each of which is the
323
+ name of ref that was actually updated.
324
+
325
+ This hook is meant primarily for notification, and cannot affect
326
+ the outcome of 'git-receive-pack'.
327
+
328
+ The 'post-update' hook can tell what are the heads that were pushed,
329
+ but it does not know what their original and updated values are,
330
+ so it is a poor place to do log old..new. The
331
+ <<post-receive,'post-receive'>> hook does get both original and
332
+ updated values of the refs. You might consider it instead if you need
333
+ them.
334
+
335
+ When enabled, the default 'post-update' hook runs
336
+ 'git-update-server-info' to keep the information used by dumb
337
+ transports (e.g., HTTP) up-to-date. If you are publishing
338
+ a git repository that is accessible via HTTP, you should
339
+ probably enable this hook.
340
+
341
+ Both standard output and standard error output are forwarded to
342
+ 'git-send-pack' on the other end, so you can simply ` echo ` messages
343
+ for the user.
344
+
345
+
346
+ ### pre-auto-gc ###
347
+
348
+ GIT_DIR/hooks/pre-auto-gc
349
+
350
+ This hook is invoked by 'git-gc --auto'. It takes no parameter, and
351
+ exiting with non-zero status from this script causes the 'git-gc --auto'
352
+ to abort.
353
+
354
+
355
+ ### References ###
356
+
357
+ [ Git Hooks] ( http://www.kernel.org/pub/software/scm/git/docs/githooks.html )
64
358
* http://probablycorey.wordpress.com/2008/03/07/git-hooks-make-me-giddy/
0 commit comments