Subject: [PATCH v2] Extend sample pre-commit hook to check for non ascii filenames

From: Heiko Voigt <[email-masked]>
Date: Thu, 14 May 2009 19:59:01 +0200

At the moment non-ascii encodings of filenames are not portably converted
between different filesystems by git. This will most likely change in the
future but to allow repositories to be portable among different file/operating
systems this check is enabled by default.

Signed-off-by: Heiko Voigt <[email-masked]>
---
On Tue, May 12, 2009 at 11:55:59PM +0200, Jakub Narebski wrote:
> On Tue, 12 May 2009, Heiko Voigt wrote:
> 
> > At the moment non-ascii encodings of file/usernames are not very well
> > supported by git. This will most likely change in the future but to
> > allow repositories to be portable among different file/operating systems
> > this check is enabled by default.
> 
> > +	# non-ascii username issue a warning in git gui so tell the
> > +	# user to change it
> > +	if ! git config user.name | is_ascii; then
> > +		echo "Please only use ascii characters in your username!"
> > +		exit 1
> > +	fi
> > +
> > +	if ! git config user.email | is_ascii; then
> > +		echo "Please only use ascii characters in your email!"
> > +		exit 1
> > +	fi
> 
> Actually 1.) there is no easy way to avoid non-ASCII names at least
> in user.name (I think they are not allowed in email), but 2.) there
> is no trouble with non-ASCII encoding of commits, as they have 
> 'encoding' header if it is not uft-8 (see *encoding* config variables).

I tried it and indeed it seems to work now. This hook originated from a
windows installation were having non-ascii characters resulted in a
strange warning from git gui each time you commit. So here is the
corrected patch.

 templates/hooks--pre-commit.sample |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample
index 0e49279..3083735 100755
--- a/templates/hooks--pre-commit.sample
+++ b/templates/hooks--pre-commit.sample
@@ -7,6 +7,26 @@
 #
 # To enable this hook, rename this file to "pre-commit".
 
+# If you want to allow non-ascii filenames set this variable to true.
+allownonascii=$(git config hooks.allownonascii)
+
+function is_ascii () {
+    test -z "$(cat | sed -e "s/[\ -~]*//g")"
+    return $?
+}
+
+if [ "$allownonascii" != "true" ]
+then
+	# until git can handle non-ascii filenames gracefully
+	# prevent them to be added into the repository
+	if ! git diff --cached --name-only --diff-filter=A -z \
+			| tr "\0" "\n" | is_ascii; then
+		echo "Non-ascii filenames are not allowed !"
+		echo "Please rename the file ..."
+		exit 1
+	fi
+fi
+
 if git-rev-parse --verify HEAD 2>/dev/null
 then
 	against=HEAD
-- 
1.6.3