* [PATCH] make the sample pre-commit hook script reject names with newlines, too
@ 2011-10-22 17:19 Jim Meyering
2011-10-22 17:35 ` Andreas Schwab
0 siblings, 1 reply; 3+ messages in thread
From: Jim Meyering @ 2011-10-22 17:19 UTC (permalink / raw)
To: git list
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>
---
templates/hooks--pre-commit.sample | 6 +++++-
1 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample
index b187c4b..1addec5 100755
--- a/templates/hooks--pre-commit.sample
+++ b/templates/hooks--pre-commit.sample
@@ -18,6 +18,9 @@ fi
# If you want to allow non-ascii filenames set this variable to true.
allownonascii=$(git config hooks.allownonascii)
+# Redirect output to stderr.
+exec 1>&2
+
# Cross platform projects tend to avoid non-ascii filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
@@ -26,7 +29,7 @@ if [ "$allownonascii" != "true" ] &&
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
test "$(git diff --cached --name-only --diff-filter=A -z $against |
- LC_ALL=C tr -d '[ -~]\0')"
+ LC_ALL=C tr -d '[ -~]\0' | wc -c)" != 0
then
echo "Error: Attempt to add a non-ascii file name."
echo
@@ -43,4 +46,5 @@ then
exit 1
fi
+# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --
--
1.7.7.419.g87009
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] make the sample pre-commit hook script reject names with newlines, too
2011-10-22 17:19 [PATCH] make the sample pre-commit hook script reject names with newlines, too Jim Meyering
@ 2011-10-22 17:35 ` Andreas Schwab
2011-10-22 17:44 ` Jim Meyering
0 siblings, 1 reply; 3+ messages in thread
From: Andreas Schwab @ 2011-10-22 17:35 UTC (permalink / raw)
To: Jim Meyering; +Cc: git list
Jim Meyering <jim@meyering.net> writes:
> @@ -26,7 +29,7 @@ if [ "$allownonascii" != "true" ] &&
> # even required, for portability to Solaris 10's /usr/bin/tr), since
> # the square bracket bytes happen to fall in the designated range.
> test "$(git diff --cached --name-only --diff-filter=A -z $against |
> - LC_ALL=C tr -d '[ -~]\0')"
> + LC_ALL=C tr -d '[ -~]\0' | wc -c)" != 0
This will fail if the output of wc contains leading spaces.
Andreas.
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] make the sample pre-commit hook script reject names with newlines, too
2011-10-22 17:35 ` Andreas Schwab
@ 2011-10-22 17:44 ` Jim Meyering
0 siblings, 0 replies; 3+ messages in thread
From: Jim Meyering @ 2011-10-22 17:44 UTC (permalink / raw)
To: Andreas Schwab; +Cc: git list
Andreas Schwab wrote:
> Jim Meyering <jim@meyering.net> writes:
>
>> @@ -26,7 +29,7 @@ if [ "$allownonascii" != "true" ] &&
>> # even required, for portability to Solaris 10's /usr/bin/tr), since
>> # the square bracket bytes happen to fall in the designated range.
>> test "$(git diff --cached --name-only --diff-filter=A -z $against |
>> - LC_ALL=C tr -d '[ -~]\0')"
>> + LC_ALL=C tr -d '[ -~]\0' | wc -c)" != 0
>
> This will fail if the output of wc contains leading spaces.
Good point. Thanks. That's a portability bug.
GNU wc outputs no leading spaces, but others certainly do.
Removing the double quotes fixes that:
-- >8 --
Subject: [PATCH] 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>
---
templates/hooks--pre-commit.sample | 8 ++++++--
1 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample
index b187c4b..18c4829 100755
--- a/templates/hooks--pre-commit.sample
+++ b/templates/hooks--pre-commit.sample
@@ -18,6 +18,9 @@ fi
# If you want to allow non-ascii filenames set this variable to true.
allownonascii=$(git config hooks.allownonascii)
+# Redirect output to stderr.
+exec 1>&2
+
# Cross platform projects tend to avoid non-ascii filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
@@ -25,8 +28,8 @@ if [ "$allownonascii" != "true" ] &&
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
- test "$(git diff --cached --name-only --diff-filter=A -z $against |
- LC_ALL=C tr -d '[ -~]\0')"
+ test $(git diff --cached --name-only --diff-filter=A -z $against |
+ LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
echo "Error: Attempt to add a non-ascii file name."
echo
@@ -43,4 +46,5 @@ then
exit 1
fi
+# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --
--
1.7.7.419.g87009
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2011-10-22 17:44 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-22 17:19 [PATCH] make the sample pre-commit hook script reject names with newlines, too Jim Meyering
2011-10-22 17:35 ` Andreas Schwab
2011-10-22 17:44 ` Jim Meyering
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).