Junio C Hamano | 89e2c5f | 2005-08-18 17:20:08 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # An example hook script to verify what is about to be committed. |
Ben Walton | 100e762 | 2010-03-20 10:48:09 -0400 | [diff] [blame] | 4 | # Called by "git commit" with no arguments. The hook should |
Junio C Hamano | 89e2c5f | 2005-08-18 17:20:08 -0700 | [diff] [blame] | 5 | # exit with non-zero status after issuing an appropriate message if |
| 6 | # it wants to stop the commit. |
| 7 | # |
Junio C Hamano | f98f8cb | 2008-06-24 18:45:21 -0700 | [diff] [blame] | 8 | # To enable this hook, rename this file to "pre-commit". |
Junio C Hamano | 89e2c5f | 2005-08-18 17:20:08 -0700 | [diff] [blame] | 9 | |
Ben Walton | 100e762 | 2010-03-20 10:48:09 -0400 | [diff] [blame] | 10 | if git rev-parse --verify HEAD >/dev/null 2>&1 |
Björn Steinbrink | c30eb85 | 2009-11-05 11:57:57 +0100 | [diff] [blame] | 11 | then |
| 12 | against=HEAD |
| 13 | else |
| 14 | # Initial commit: diff against an empty tree object |
brian m. carlson | 03a7f38 | 2018-05-02 00:26:08 +0000 | [diff] [blame] | 15 | against=$(git hash-object -t tree /dev/null) |
Björn Steinbrink | c30eb85 | 2009-11-05 11:57:57 +0100 | [diff] [blame] | 16 | fi |
| 17 | |
Richard Hartmann | 7b3742f | 2013-07-14 18:21:16 +0200 | [diff] [blame] | 18 | # If you want to allow non-ASCII filenames set this variable to true. |
Lucius Hu | 81e3db4 | 2020-01-19 22:53:32 +0000 | [diff] [blame] | 19 | allownonascii=$(git config --type=bool hooks.allownonascii) |
Heiko Voigt | d00e364 | 2009-05-19 22:01:54 +0200 | [diff] [blame] | 20 | |
Jim Meyering | c14daa4 | 2011-10-22 19:44:40 +0200 | [diff] [blame] | 21 | # Redirect output to stderr. |
| 22 | exec 1>&2 |
| 23 | |
Richard Hartmann | 7b3742f | 2013-07-14 18:21:16 +0200 | [diff] [blame] | 24 | # Cross platform projects tend to avoid non-ASCII filenames; prevent |
Heiko Voigt | d00e364 | 2009-05-19 22:01:54 +0200 | [diff] [blame] | 25 | # them from being added to the repository. We exploit the fact that the |
| 26 | # printable range starts at the space character and ends with tilde. |
| 27 | if [ "$allownonascii" != "true" ] && |
Jim Meyering | f1e3156 | 2009-09-21 13:00:34 +0200 | [diff] [blame] | 28 | # Note that the use of brackets around a tr range is ok here, (it's |
| 29 | # even required, for portability to Solaris 10's /usr/bin/tr), since |
| 30 | # the square bracket bytes happen to fall in the designated range. |
Jim Meyering | c14daa4 | 2011-10-22 19:44:40 +0200 | [diff] [blame] | 31 | test $(git diff --cached --name-only --diff-filter=A -z $against | |
| 32 | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 |
Heiko Voigt | d00e364 | 2009-05-19 22:01:54 +0200 | [diff] [blame] | 33 | then |
Richard Hartmann | 27b6e17 | 2013-07-14 18:21:14 +0200 | [diff] [blame] | 34 | cat <<\EOF |
Richard Hartmann | 7b3742f | 2013-07-14 18:21:16 +0200 | [diff] [blame] | 35 | Error: Attempt to add a non-ASCII file name. |
Richard Hartmann | 27b6e17 | 2013-07-14 18:21:14 +0200 | [diff] [blame] | 36 | |
Richard Hartmann | b1d5a57 | 2013-07-14 18:21:15 +0200 | [diff] [blame] | 37 | This can cause problems if you want to work with people on other platforms. |
Richard Hartmann | 27b6e17 | 2013-07-14 18:21:14 +0200 | [diff] [blame] | 38 | |
| 39 | To be portable it is advisable to rename the file. |
| 40 | |
Richard Hartmann | b1d5a57 | 2013-07-14 18:21:15 +0200 | [diff] [blame] | 41 | If you know what you are doing you can disable this check using: |
Richard Hartmann | 27b6e17 | 2013-07-14 18:21:14 +0200 | [diff] [blame] | 42 | |
| 43 | git config hooks.allownonascii true |
| 44 | EOF |
Heiko Voigt | d00e364 | 2009-05-19 22:01:54 +0200 | [diff] [blame] | 45 | exit 1 |
| 46 | fi |
| 47 | |
Jim Meyering | c14daa4 | 2011-10-22 19:44:40 +0200 | [diff] [blame] | 48 | # If there are whitespace errors, print the offending file names and fail. |
Junio C Hamano | 03e2b63 | 2008-06-26 16:08:05 -0700 | [diff] [blame] | 49 | exec git diff-index --check --cached $against -- |