Skip to content

Commit c30eb85

Browse files
dotdashgitster
authored andcommitted
pre-commit.sample: Diff against the empty tree when HEAD is invalid
This was already the case for the old "diff --check" call, but the new one that checks whether there are any non-ascii file names was missing it, making that check fail for root commits. Signed-off-by: Björn Steinbrink <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 78d553b commit c30eb85

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

templates/hooks--pre-commit.sample

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
#
88
# To enable this hook, rename this file to "pre-commit".
99

10+
if git-rev-parse --verify HEAD >/dev/null 2>&1
11+
then
12+
against=HEAD
13+
else
14+
# Initial commit: diff against an empty tree object
15+
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
16+
fi
17+
1018
# If you want to allow non-ascii filenames set this variable to true.
1119
allownonascii=$(git config hooks.allownonascii)
1220

@@ -17,7 +25,7 @@ if [ "$allownonascii" != "true" ] &&
1725
# Note that the use of brackets around a tr range is ok here, (it's
1826
# even required, for portability to Solaris 10's /usr/bin/tr), since
1927
# the square bracket bytes happen to fall in the designated range.
20-
test "$(git diff --cached --name-only --diff-filter=A -z |
28+
test "$(git diff --cached --name-only --diff-filter=A -z $against |
2129
LC_ALL=C tr -d '[ -~]\0')"
2230
then
2331
echo "Error: Attempt to add a non-ascii file name."
@@ -35,12 +43,4 @@ then
3543
exit 1
3644
fi
3745

38-
if git-rev-parse --verify HEAD >/dev/null 2>&1
39-
then
40-
against=HEAD
41-
else
42-
# Initial commit: diff against an empty tree object
43-
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
44-
fi
45-
4646
exec git diff-index --check --cached $against --

0 commit comments

Comments
 (0)