make the sample pre-commit hook script reject names with newlines, too

The sample pre-commit hook script would fail to reject a file name like "a\nb" because of the way newlines are handled in "$(...)". Adjust the test to count filtered bytes and require there be 0. Also print all diagnostics to standard error, not stdout, so they will actually be seen. Signed-off-by: Jim Meyering <meyering@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jim Meyering committed Oct 22, 2011 at 19:44 UTC c14daa4845ff2ec0d27807fa50f9fac1ca4d6462
1 file changed +6 -2
templates/hooks--pre-commit.sample
+6 -2
@@ -18,6 +18,9 @@ fi
18 # If you want to allow non-ascii filenames set this variable to true.
19 allownonascii=$(git config hooks.allownonascii)
20
21 +# Redirect output to stderr.
22 +exec 1>&2
23 +
24 # Cross platform projects tend to avoid non-ascii filenames; prevent
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.
@@ -25,8 +28,8 @@ if [ "$allownonascii" != "true" ] &&
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.
28 - test "$(git diff --cached --name-only --diff-filter=A -z $against |
29 - LC_ALL=C tr -d '[ -~]\0')"
31 + test $(git diff --cached --name-only --diff-filter=A -z $against |
32 + LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
33 then
34 echo "Error: Attempt to add a non-ascii file name."
35 echo
@@ -43,4 +46,5 @@ then
46 exit 1
47 fi
48
49 +# If there are whitespace errors, print the offending file names and fail.
50 exec git diff-index --check --cached $against --