blob: e144712c85c055bcf3248ab342592b440a477062 [file] [log] [blame]
Junio C Hamano89e2c5f2005-08-18 17:20:08 -07001#!/bin/sh
2#
3# An example hook script to verify what is about to be committed.
Ben Walton100e7622010-03-20 10:48:09 -04004# Called by "git commit" with no arguments. The hook should
Junio C Hamano89e2c5f2005-08-18 17:20:08 -07005# exit with non-zero status after issuing an appropriate message if
6# it wants to stop the commit.
7#
Junio C Hamanof98f8cb2008-06-24 18:45:21 -07008# To enable this hook, rename this file to "pre-commit".
Junio C Hamano89e2c5f2005-08-18 17:20:08 -07009
Ben Walton100e7622010-03-20 10:48:09 -040010if git rev-parse --verify HEAD >/dev/null 2>&1
Björn Steinbrinkc30eb852009-11-05 11:57:57 +010011then
12 against=HEAD
13else
14 # Initial commit: diff against an empty tree object
brian m. carlson03a7f382018-05-02 00:26:08 +000015 against=$(git hash-object -t tree /dev/null)
Björn Steinbrinkc30eb852009-11-05 11:57:57 +010016fi
17
Richard Hartmann7b3742f2013-07-14 18:21:16 +020018# If you want to allow non-ASCII filenames set this variable to true.
Lucius Hu81e3db42020-01-19 22:53:32 +000019allownonascii=$(git config --type=bool hooks.allownonascii)
Heiko Voigtd00e3642009-05-19 22:01:54 +020020
Jim Meyeringc14daa42011-10-22 19:44:40 +020021# Redirect output to stderr.
22exec 1>&2
23
Richard Hartmann7b3742f2013-07-14 18:21:16 +020024# Cross platform projects tend to avoid non-ASCII filenames; prevent
Heiko Voigtd00e3642009-05-19 22:01:54 +020025# 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.
27if [ "$allownonascii" != "true" ] &&
Jim Meyeringf1e31562009-09-21 13:00:34 +020028 # 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 Meyeringc14daa42011-10-22 19:44:40 +020031 test $(git diff --cached --name-only --diff-filter=A -z $against |
32 LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
Heiko Voigtd00e3642009-05-19 22:01:54 +020033then
Richard Hartmann27b6e172013-07-14 18:21:14 +020034 cat <<\EOF
Richard Hartmann7b3742f2013-07-14 18:21:16 +020035Error: Attempt to add a non-ASCII file name.
Richard Hartmann27b6e172013-07-14 18:21:14 +020036
Richard Hartmannb1d5a572013-07-14 18:21:15 +020037This can cause problems if you want to work with people on other platforms.
Richard Hartmann27b6e172013-07-14 18:21:14 +020038
39To be portable it is advisable to rename the file.
40
Richard Hartmannb1d5a572013-07-14 18:21:15 +020041If you know what you are doing you can disable this check using:
Richard Hartmann27b6e172013-07-14 18:21:14 +020042
43 git config hooks.allownonascii true
44EOF
Heiko Voigtd00e3642009-05-19 22:01:54 +020045 exit 1
46fi
47
Jim Meyeringc14daa42011-10-22 19:44:40 +020048# If there are whitespace errors, print the offending file names and fail.
Junio C Hamano03e2b632008-06-26 16:08:05 -070049exec git diff-index --check --cached $against --