Fourth batch
[git/raj.git] / Documentation / githooks.txt
blob31b601e4bce9c9fbb7f96f2f5308d0166f92f999
1 githooks(5)
2 ===========
4 NAME
5 ----
6 githooks - Hooks used by Git
8 SYNOPSIS
9 --------
10 $GIT_DIR/hooks/* (or \`git config core.hooksPath`/*)
13 DESCRIPTION
14 -----------
16 Hooks are programs you can place in a hooks directory to trigger
17 actions at certain points in git's execution. Hooks that don't have
18 the executable bit set are ignored.
20 By default the hooks directory is `$GIT_DIR/hooks`, but that can be
21 changed via the `core.hooksPath` configuration variable (see
22 linkgit:git-config[1]).
24 Before Git invokes a hook, it changes its working directory to either
25 $GIT_DIR in a bare repository or the root of the working tree in a non-bare
26 repository. An exception are hooks triggered during a push ('pre-receive',
27 'update', 'post-receive', 'post-update', 'push-to-checkout') which are always
28 executed in $GIT_DIR.
30 Hooks can get their arguments via the environment, command-line
31 arguments, and stdin. See the documentation for each hook below for
32 details.
34 `git init` may copy hooks to the new repository, depending on its
35 configuration. See the "TEMPLATE DIRECTORY" section in
36 linkgit:git-init[1] for details. When the rest of this document refers
37 to "default hooks" it's talking about the default template shipped
38 with Git.
40 The currently supported hooks are described below.
42 HOOKS
43 -----
45 applypatch-msg
46 ~~~~~~~~~~~~~~
48 This hook is invoked by linkgit:git-am[1].  It takes a single
49 parameter, the name of the file that holds the proposed commit
50 log message.  Exiting with a non-zero status causes `git am` to abort
51 before applying the patch.
53 The hook is allowed to edit the message file in place, and can
54 be used to normalize the message into some project standard
55 format. It can also be used to refuse the commit after inspecting
56 the message file.
58 The default 'applypatch-msg' hook, when enabled, runs the
59 'commit-msg' hook, if the latter is enabled.
61 pre-applypatch
62 ~~~~~~~~~~~~~~
64 This hook is invoked by linkgit:git-am[1].  It takes no parameter, and is
65 invoked after the patch is applied, but before a commit is made.
67 If it exits with non-zero status, then the working tree will not be
68 committed after applying the patch.
70 It can be used to inspect the current working tree and refuse to
71 make a commit if it does not pass certain test.
73 The default 'pre-applypatch' hook, when enabled, runs the
74 'pre-commit' hook, if the latter is enabled.
76 post-applypatch
77 ~~~~~~~~~~~~~~~
79 This hook is invoked by linkgit:git-am[1].  It takes no parameter,
80 and is invoked after the patch is applied and a commit is made.
82 This hook is meant primarily for notification, and cannot affect
83 the outcome of `git am`.
85 pre-commit
86 ~~~~~~~~~~
88 This hook is invoked by linkgit:git-commit[1], and can be bypassed
89 with the `--no-verify` option.  It takes no parameters, and is
90 invoked before obtaining the proposed commit log message and
91 making a commit.  Exiting with a non-zero status from this script
92 causes the `git commit` command to abort before creating a commit.
94 The default 'pre-commit' hook, when enabled, catches introduction
95 of lines with trailing whitespaces and aborts the commit when
96 such a line is found.
98 All the `git commit` hooks are invoked with the environment
99 variable `GIT_EDITOR=:` if the command will not bring up an editor
100 to modify the commit message.
102 The default 'pre-commit' hook, when enabled--and with the
103 `hooks.allownonascii` config option unset or set to false--prevents
104 the use of non-ASCII filenames.
106 pre-merge-commit
107 ~~~~~~~~~~~~~~~~
109 This hook is invoked by linkgit:git-merge[1], and can be bypassed
110 with the `--no-verify` option.  It takes no parameters, and is
111 invoked after the merge has been carried out successfully and before
112 obtaining the proposed commit log message to
113 make a commit.  Exiting with a non-zero status from this script
114 causes the `git merge` command to abort before creating a commit.
116 The default 'pre-merge-commit' hook, when enabled, runs the
117 'pre-commit' hook, if the latter is enabled.
119 This hook is invoked with the environment variable
120 `GIT_EDITOR=:` if the command will not bring up an editor
121 to modify the commit message.
123 If the merge cannot be carried out automatically, the conflicts
124 need to be resolved and the result committed separately (see
125 linkgit:git-merge[1]). At that point, this hook will not be executed,
126 but the 'pre-commit' hook will, if it is enabled.
128 prepare-commit-msg
129 ~~~~~~~~~~~~~~~~~~
131 This hook is invoked by linkgit:git-commit[1] right after preparing the
132 default log message, and before the editor is started.
134 It takes one to three parameters.  The first is the name of the file
135 that contains the commit log message.  The second is the source of the commit
136 message, and can be: `message` (if a `-m` or `-F` option was
137 given); `template` (if a `-t` option was given or the
138 configuration option `commit.template` is set); `merge` (if the
139 commit is a merge or a `.git/MERGE_MSG` file exists); `squash`
140 (if a `.git/SQUASH_MSG` file exists); or `commit`, followed by
141 a commit SHA-1 (if a `-c`, `-C` or `--amend` option was given).
143 If the exit status is non-zero, `git commit` will abort.
145 The purpose of the hook is to edit the message file in place, and
146 it is not suppressed by the `--no-verify` option.  A non-zero exit
147 means a failure of the hook and aborts the commit.  It should not
148 be used as replacement for pre-commit hook.
150 The sample `prepare-commit-msg` hook that comes with Git removes the
151 help message found in the commented portion of the commit template.
153 commit-msg
154 ~~~~~~~~~~
156 This hook is invoked by linkgit:git-commit[1] and linkgit:git-merge[1], and can be
157 bypassed with the `--no-verify` option.  It takes a single parameter,
158 the name of the file that holds the proposed commit log message.
159 Exiting with a non-zero status causes the command to abort.
161 The hook is allowed to edit the message file in place, and can be used
162 to normalize the message into some project standard format. It
163 can also be used to refuse the commit after inspecting the message
164 file.
166 The default 'commit-msg' hook, when enabled, detects duplicate
167 "Signed-off-by" lines, and aborts the commit if one is found.
169 post-commit
170 ~~~~~~~~~~~
172 This hook is invoked by linkgit:git-commit[1]. It takes no parameters, and is
173 invoked after a commit is made.
175 This hook is meant primarily for notification, and cannot affect
176 the outcome of `git commit`.
178 pre-rebase
179 ~~~~~~~~~~
181 This hook is called by linkgit:git-rebase[1] and can be used to prevent a
182 branch from getting rebased.  The hook may be called with one or
183 two parameters.  The first parameter is the upstream from which
184 the series was forked.  The second parameter is the branch being
185 rebased, and is not set when rebasing the current branch.
187 post-checkout
188 ~~~~~~~~~~~~~
190 This hook is invoked when a linkgit:git-checkout[1] or
191 linkgit:git-switch[1] is run after having updated the
192 worktree.  The hook is given three parameters: the ref of the previous HEAD,
193 the ref of the new HEAD (which may or may not have changed), and a flag
194 indicating whether the checkout was a branch checkout (changing branches,
195 flag=1) or a file checkout (retrieving a file from the index, flag=0).
196 This hook cannot affect the outcome of `git switch` or `git checkout`.
198 It is also run after linkgit:git-clone[1], unless the `--no-checkout` (`-n`) option is
199 used. The first parameter given to the hook is the null-ref, the second the
200 ref of the new HEAD and the flag is always 1. Likewise for `git worktree add`
201 unless `--no-checkout` is used.
203 This hook can be used to perform repository validity checks, auto-display
204 differences from the previous HEAD if different, or set working dir metadata
205 properties.
207 post-merge
208 ~~~~~~~~~~
210 This hook is invoked by linkgit:git-merge[1], which happens when a `git pull`
211 is done on a local repository.  The hook takes a single parameter, a status
212 flag specifying whether or not the merge being done was a squash merge.
213 This hook cannot affect the outcome of `git merge` and is not executed,
214 if the merge failed due to conflicts.
216 This hook can be used in conjunction with a corresponding pre-commit hook to
217 save and restore any form of metadata associated with the working tree
218 (e.g.: permissions/ownership, ACLS, etc).  See contrib/hooks/setgitperms.perl
219 for an example of how to do this.
221 pre-push
222 ~~~~~~~~
224 This hook is called by linkgit:git-push[1] and can be used to prevent
225 a push from taking place.  The hook is called with two parameters
226 which provide the name and location of the destination remote, if a
227 named remote is not being used both values will be the same.
229 Information about what is to be pushed is provided on the hook's standard
230 input with lines of the form:
232   <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF
234 For instance, if the command +git push origin master:foreign+ were run the
235 hook would receive a line like the following:
237   refs/heads/master 67890 refs/heads/foreign 12345
239 although the full, 40-character SHA-1s would be supplied.  If the foreign ref
240 does not yet exist the `<remote SHA-1>` will be 40 `0`.  If a ref is to be
241 deleted, the `<local ref>` will be supplied as `(delete)` and the `<local
242 SHA-1>` will be 40 `0`.  If the local commit was specified by something other
243 than a name which could be expanded (such as `HEAD~`, or a SHA-1) it will be
244 supplied as it was originally given.
246 If this hook exits with a non-zero status, `git push` will abort without
247 pushing anything.  Information about why the push is rejected may be sent
248 to the user by writing to standard error.
250 [[pre-receive]]
251 pre-receive
252 ~~~~~~~~~~~
254 This hook is invoked by linkgit:git-receive-pack[1] when it reacts to
255 `git push` and updates reference(s) in its repository.
256 Just before starting to update refs on the remote repository, the
257 pre-receive hook is invoked.  Its exit status determines the success
258 or failure of the update.
260 This hook executes once for the receive operation. It takes no
261 arguments, but for each ref to be updated it receives on standard
262 input a line of the format:
264   <old-value> SP <new-value> SP <ref-name> LF
266 where `<old-value>` is the old object name stored in the ref,
267 `<new-value>` is the new object name to be stored in the ref and
268 `<ref-name>` is the full name of the ref.
269 When creating a new ref, `<old-value>` is 40 `0`.
271 If the hook exits with non-zero status, none of the refs will be
272 updated. If the hook exits with zero, updating of individual refs can
273 still be prevented by the <<update,'update'>> hook.
275 Both standard output and standard error output are forwarded to
276 `git send-pack` on the other end, so you can simply `echo` messages
277 for the user.
279 The number of push options given on the command line of
280 `git push --push-option=...` can be read from the environment
281 variable `GIT_PUSH_OPTION_COUNT`, and the options themselves are
282 found in `GIT_PUSH_OPTION_0`, `GIT_PUSH_OPTION_1`,...
283 If it is negotiated to not use the push options phase, the
284 environment variables will not be set. If the client selects
285 to use push options, but doesn't transmit any, the count variable
286 will be set to zero, `GIT_PUSH_OPTION_COUNT=0`.
288 See the section on "Quarantine Environment" in
289 linkgit:git-receive-pack[1] for some caveats.
291 [[update]]
292 update
293 ~~~~~~
295 This hook is invoked by linkgit:git-receive-pack[1] when it reacts to
296 `git push` and updates reference(s) in its repository.
297 Just before updating the ref on the remote repository, the update hook
298 is invoked.  Its exit status determines the success or failure of
299 the ref update.
301 The hook executes once for each ref to be updated, and takes
302 three parameters:
304  - the name of the ref being updated,
305  - the old object name stored in the ref,
306  - and the new object name to be stored in the ref.
308 A zero exit from the update hook allows the ref to be updated.
309 Exiting with a non-zero status prevents `git receive-pack`
310 from updating that ref.
312 This hook can be used to prevent 'forced' update on certain refs by
313 making sure that the object name is a commit object that is a
314 descendant of the commit object named by the old object name.
315 That is, to enforce a "fast-forward only" policy.
317 It could also be used to log the old..new status.  However, it
318 does not know the entire set of branches, so it would end up
319 firing one e-mail per ref when used naively, though.  The
320 <<post-receive,'post-receive'>> hook is more suited to that.
322 In an environment that restricts the users' access only to git
323 commands over the wire, this hook can be used to implement access
324 control without relying on filesystem ownership and group
325 membership. See linkgit:git-shell[1] for how you might use the login
326 shell to restrict the user's access to only git commands.
328 Both standard output and standard error output are forwarded to
329 `git send-pack` on the other end, so you can simply `echo` messages
330 for the user.
332 The default 'update' hook, when enabled--and with
333 `hooks.allowunannotated` config option unset or set to false--prevents
334 unannotated tags to be pushed.
336 [[post-receive]]
337 post-receive
338 ~~~~~~~~~~~~
340 This hook is invoked by linkgit:git-receive-pack[1] when it reacts to
341 `git push` and updates reference(s) in its repository.
342 It executes on the remote repository once after all the refs have
343 been updated.
345 This hook executes once for the receive operation.  It takes no
346 arguments, but gets the same information as the
347 <<pre-receive,'pre-receive'>>
348 hook does on its standard input.
350 This hook does not affect the outcome of `git receive-pack`, as it
351 is called after the real work is done.
353 This supersedes the <<post-update,'post-update'>> hook in that it gets
354 both old and new values of all the refs in addition to their
355 names.
357 Both standard output and standard error output are forwarded to
358 `git send-pack` on the other end, so you can simply `echo` messages
359 for the user.
361 The default 'post-receive' hook is empty, but there is
362 a sample script `post-receive-email` provided in the `contrib/hooks`
363 directory in Git distribution, which implements sending commit
364 emails.
366 The number of push options given on the command line of
367 `git push --push-option=...` can be read from the environment
368 variable `GIT_PUSH_OPTION_COUNT`, and the options themselves are
369 found in `GIT_PUSH_OPTION_0`, `GIT_PUSH_OPTION_1`,...
370 If it is negotiated to not use the push options phase, the
371 environment variables will not be set. If the client selects
372 to use push options, but doesn't transmit any, the count variable
373 will be set to zero, `GIT_PUSH_OPTION_COUNT=0`.
375 [[post-update]]
376 post-update
377 ~~~~~~~~~~~
379 This hook is invoked by linkgit:git-receive-pack[1] when it reacts to
380 `git push` and updates reference(s) in its repository.
381 It executes on the remote repository once after all the refs have
382 been updated.
384 It takes a variable number of parameters, each of which is the
385 name of ref that was actually updated.
387 This hook is meant primarily for notification, and cannot affect
388 the outcome of `git receive-pack`.
390 The 'post-update' hook can tell what are the heads that were pushed,
391 but it does not know what their original and updated values are,
392 so it is a poor place to do log old..new. The
393 <<post-receive,'post-receive'>> hook does get both original and
394 updated values of the refs. You might consider it instead if you need
395 them.
397 When enabled, the default 'post-update' hook runs
398 `git update-server-info` to keep the information used by dumb
399 transports (e.g., HTTP) up to date.  If you are publishing
400 a Git repository that is accessible via HTTP, you should
401 probably enable this hook.
403 Both standard output and standard error output are forwarded to
404 `git send-pack` on the other end, so you can simply `echo` messages
405 for the user.
407 reference-transaction
408 ~~~~~~~~~~~~~~~~~~~~~
410 This hook is invoked by any Git command that performs reference
411 updates. It executes whenever a reference transaction is prepared,
412 committed or aborted and may thus get called multiple times.
414 The hook takes exactly one argument, which is the current state the
415 given reference transaction is in:
417     - "prepared": All reference updates have been queued to the
418       transaction and references were locked on disk.
420     - "committed": The reference transaction was committed and all
421       references now have their respective new value.
423     - "aborted": The reference transaction was aborted, no changes
424       were performed and the locks have been released.
426 For each reference update that was added to the transaction, the hook
427 receives on standard input a line of the format:
429   <old-value> SP <new-value> SP <ref-name> LF
431 The exit status of the hook is ignored for any state except for the
432 "prepared" state. In the "prepared" state, a non-zero exit status will
433 cause the transaction to be aborted. The hook will not be called with
434 "aborted" state in that case.
436 push-to-checkout
437 ~~~~~~~~~~~~~~~~
439 This hook is invoked by linkgit:git-receive-pack[1] when it reacts to
440 `git push` and updates reference(s) in its repository, and when
441 the push tries to update the branch that is currently checked out
442 and the `receive.denyCurrentBranch` configuration variable is set to
443 `updateInstead`.  Such a push by default is refused if the working
444 tree and the index of the remote repository has any difference from
445 the currently checked out commit; when both the working tree and the
446 index match the current commit, they are updated to match the newly
447 pushed tip of the branch.  This hook is to be used to override the
448 default behaviour.
450 The hook receives the commit with which the tip of the current
451 branch is going to be updated.  It can exit with a non-zero status
452 to refuse the push (when it does so, it must not modify the index or
453 the working tree).  Or it can make any necessary changes to the
454 working tree and to the index to bring them to the desired state
455 when the tip of the current branch is updated to the new commit, and
456 exit with a zero status.
458 For example, the hook can simply run `git read-tree -u -m HEAD "$1"`
459 in order to emulate `git fetch` that is run in the reverse direction
460 with `git push`, as the two-tree form of `git read-tree -u -m` is
461 essentially the same as `git switch` or `git checkout`
462 that switches branches while
463 keeping the local changes in the working tree that do not interfere
464 with the difference between the branches.
467 pre-auto-gc
468 ~~~~~~~~~~~
470 This hook is invoked by `git gc --auto` (see linkgit:git-gc[1]). It
471 takes no parameter, and exiting with non-zero status from this script
472 causes the `git gc --auto` to abort.
474 post-rewrite
475 ~~~~~~~~~~~~
477 This hook is invoked by commands that rewrite commits
478 (linkgit:git-commit[1] when called with `--amend` and
479 linkgit:git-rebase[1]; however, full-history (re)writing tools like
480 linkgit:git-fast-import[1] or
481 https://github.com/newren/git-filter-repo[git-filter-repo] typically
482 do not call it!).  Its first argument denotes the command it was
483 invoked by: currently one of `amend` or `rebase`.  Further
484 command-dependent arguments may be passed in the future.
486 The hook receives a list of the rewritten commits on stdin, in the
487 format
489   <old-sha1> SP <new-sha1> [ SP <extra-info> ] LF
491 The 'extra-info' is again command-dependent.  If it is empty, the
492 preceding SP is also omitted.  Currently, no commands pass any
493 'extra-info'.
495 The hook always runs after the automatic note copying (see
496 "notes.rewrite.<command>" in linkgit:git-config[1]) has happened, and
497 thus has access to these notes.
499 The following command-specific comments apply:
501 rebase::
502         For the 'squash' and 'fixup' operation, all commits that were
503         squashed are listed as being rewritten to the squashed commit.
504         This means that there will be several lines sharing the same
505         'new-sha1'.
507 The commits are guaranteed to be listed in the order that they were
508 processed by rebase.
510 sendemail-validate
511 ~~~~~~~~~~~~~~~~~~
513 This hook is invoked by linkgit:git-send-email[1].  It takes a single parameter,
514 the name of the file that holds the e-mail to be sent.  Exiting with a
515 non-zero status causes `git send-email` to abort before sending any
516 e-mails.
518 fsmonitor-watchman
519 ~~~~~~~~~~~~~~~~~~
521 This hook is invoked when the configuration option `core.fsmonitor` is
522 set to `.git/hooks/fsmonitor-watchman` or `.git/hooks/fsmonitor-watchmanv2`
523 depending on the version of the hook to use.
525 Version 1 takes two arguments, a version (1) and the time in elapsed
526 nanoseconds since midnight, January 1, 1970.
528 Version 2 takes two arguments, a version (2) and a token that is used
529 for identifying changes since the token. For watchman this would be
530 a clock id. This version must output to stdout the new token followed
531 by a NUL before the list of files.
533 The hook should output to stdout the list of all files in the working
534 directory that may have changed since the requested time.  The logic
535 should be inclusive so that it does not miss any potential changes.
536 The paths should be relative to the root of the working directory
537 and be separated by a single NUL.
539 It is OK to include files which have not actually changed.  All changes
540 including newly-created and deleted files should be included. When
541 files are renamed, both the old and the new name should be included.
543 Git will limit what files it checks for changes as well as which
544 directories are checked for untracked files based on the path names
545 given.
547 An optimized way to tell git "all files have changed" is to return
548 the filename `/`.
550 The exit status determines whether git will use the data from the
551 hook to limit its search.  On error, it will fall back to verifying
552 all files and folders.
554 p4-changelist
555 ~~~~~~~~~~~~~
557 This hook is invoked by `git-p4 submit`.
559 The `p4-changelist` hook is executed after the changelist
560 message has been edited by the user. It can be bypassed with the
561 `--no-verify` option. It takes a single parameter, the name
562 of the file that holds the proposed changelist text. Exiting
563 with a non-zero status causes the command to abort.
565 The hook is allowed to edit the changelist file and can be used
566 to normalize the text into some project standard format. It can
567 also be used to refuse the Submit after inspect the message file.
569 Run `git-p4 submit --help` for details.
571 p4-prepare-changelist
572 ~~~~~~~~~~~~~~~~~~~~~
574 This hook is invoked by `git-p4 submit`.
576 The `p4-prepare-changelist` hook is executed right after preparing
577 the default changelist message and before the editor is started.
578 It takes one parameter, the name of the file that contains the
579 changelist text. Exiting with a non-zero status from the script
580 will abort the process.
582 The purpose of the hook is to edit the message file in place,
583 and it is not supressed by the `--no-verify` option. This hook
584 is called even if `--prepare-p4-only` is set.
586 Run `git-p4 submit --help` for details.
588 p4-post-changelist
589 ~~~~~~~~~~~~~~~~~~
591 This hook is invoked by `git-p4 submit`.
593 The `p4-post-changelist` hook is invoked after the submit has
594 successfully occured in P4. It takes no parameters and is meant
595 primarily for notification and cannot affect the outcome of the
596 git p4 submit action.
598 Run `git-p4 submit --help` for details.
600 p4-pre-submit
601 ~~~~~~~~~~~~~
603 This hook is invoked by `git-p4 submit`. It takes no parameters and nothing
604 from standard input. Exiting with non-zero status from this script prevent
605 `git-p4 submit` from launching. It can be bypassed with the `--no-verify`
606 command line option. Run `git-p4 submit --help` for details.
610 post-index-change
611 ~~~~~~~~~~~~~~~~~
613 This hook is invoked when the index is written in read-cache.c
614 do_write_locked_index.
616 The first parameter passed to the hook is the indicator for the
617 working directory being updated.  "1" meaning working directory
618 was updated or "0" when the working directory was not updated.
620 The second parameter passed to the hook is the indicator for whether
621 or not the index was updated and the skip-worktree bit could have
622 changed.  "1" meaning skip-worktree bits could have been updated
623 and "0" meaning they were not.
625 Only one parameter should be set to "1" when the hook runs.  The hook
626 running passing "1", "1" should not be possible.
630 Part of the linkgit:git[1] suite