git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4] Extend sample pre-commit hook to check for non ascii filenames

From
Heiko Voigt <hvoigt@hvoigt.net>
Date
May 19, 2009, 20:01 UTC
Message-ID
<20090519200153.GA1926@heiko-voigts-macbook.local>
In-Reply-To
<4A113B09.6040301@viscovery.net>

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 <hvoigt@hvoigt.net>
---
Thanks for all comments. I now hopefully have a satisfying patch.
On Mon, May 18, 2009 at 12:40:09PM +0200, Johannes Sixt wrote:
Show 12 quoted lines
> Heiko Voigt schrieb:
> > +	if ! git diff --cached --name-only --diff-filter=A -z \
> > +	   | tr "\0" "\n" | is_ascii; then
> 
> Will this not fail to add more than one file with allowed names? The \n is
> not removed in is_ascii(), and so the resulting string will not be empty.
> 
> BTW, not all tr work well with NULs. See the commit message of e85fe4d8,
> for example. Otherwise, I would have suggested to convert the NUL to some
> allowed ASCII character, e.g. 'A'. BTW, you should really use '\0' and
> '\n' (single-quotes) to guarantee that the shell does not ignore the
> backslash.

I removed all \0 characters and hopefully use the correct platform independent syntax as described in the commits you send.

On Mon, May 18, 2009 at 02:04:08PM +0200, Johannes Sixt wrote:
Show 16 quoted lines
> Heiko Voigt schrieb:
> > Are there any problems with '\0' and tr other than swallowing of it.
> 
> I can't tell. But the commits ae90e16..aab0abf are interesting to study in
> w.r.t. portability.
> 
> > In
> > case not I would just change
> > 
> > 	tr "\0" "\n"
> > to
> >   	tr -d '\0'
> 
> In which case I'd suggest that you call tr only once, in isascii():
> 
>      tr -d '[ -~]\0'

After reading a little about the portability things. This seems to be the right way and is now included.

On Mon, May 18, 2009 at 07:42:31AM -0700, Junio C Hamano wrote:
Show 11 quoted lines
> Heiko Voigt <hvoigt@hvoigt.net> writes:
> 
> > +if [ "$allownonascii" != "true" ]
> > +then
> > +	# until git can handle non-ascii filenames gracefully
> > +	# prevent them to be added into the repository
> 
> I think you can inline your is_ascii shell function in the pipeline below.
> You made it a separate function and I agree that it has a very good
> documentation value, but the mention of "non-ascii filenames" in this
> comment here is enough clue to let anybody know what is going on.

I agree. I thought it would probably be useful in other places but we just need it once so its inlined now.

Show 5 quoted lines
> 
> 	Side note: I am not sure "Until ... can ... gracefully" is a good
> 	description of the general problem.  It probably is more neutral
> 	to say "Cross platform projects tend to avoid non-ascii filenames;
>         prevent them from being added to the repository."
Changed that.
Show 12 quoted lines
> 
> > +	if ! git diff --cached --name-only --diff-filter=A -z \
> > +	   | tr "\0" "\n" | is_ascii; then
> 
> A standard trick while writing a long pipeline in shell is to change line
> after a pipe, like:
> 
> 	cmd1 |
>         cmd2 |
>         cmd3
> 
> which allows you to lose the BS-before-LF sequence.
Wasn't aware of that. Changed it accordingly.
On Mon, May 18, 2009 at 09:35:19PM +0100, Julian Phillips wrote:
Show 9 quoted lines
> On Mon, 18 May 2009, Heiko Voigt wrote:
>> +		echo "Error: Preventing to add a non-ascii filename."
>
> This would read better as:
>
> +		echo "Error: Attempt to add a non-ascii filename."
>
> (after all the prevention itself is a result of the error, not the cause  
> of it)
That really sounds better. Thanks.
 templates/hooks--pre-commit.sample |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
diff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample
index 0e49279..ad892a2 100755
--- a/templates/hooks--pre-commit.sample
+++ b/templates/hooks--pre-commit.sample
@@ -7,6 +7,31 @@
 #
 # 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)
+
+# 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.
+if [ "$allownonascii" != "true" ] &&
+	test "$(git diff --cached --name-only --diff-filter=A -z |
+	  LC_ALL=C tr -d '[ -~]\0')"
+then
+	echo "Error: Attempt to add a non-ascii filename."
+	echo
+	echo "This can cause problems if you want to work together"
+	echo "with people on other platforms than you."
+	echo
+	echo "To be portable it is adviseable to rename the file ..."
+	echo
+	echo "If you know what you are doing you can disable this"
+	echo "check using:"
+	echo
+	echo "  git config hooks.allownonascii true"
+	echo
+	exit 1
+fi
+
 if git-rev-parse --verify HEAD 2>/dev/null
 then
 	against=HEAD
-- 
1.6.3
Previous: Johannes SixtNext: Junio C Hamano
Message 42 of 59 in “Re: Cross-Platform Version Control”
  1. Esko LuontolaMay 12, 2009
  2. Shawn O. PearceMay 12, 2009
  3. Johannes SchindelinMay 12, 2009
  4. Esko LuontolaMay 12, 2009
  5. Johannes SchindelinMay 12, 2009
  6. Esko LuontolaMay 12, 2009
  7. Johannes SchindelinMay 13, 2009
  8. Esko LuontolaMay 13, 2009
  9. Alex RiesenMay 13, 2009
  10. Johannes SchindelinMay 13, 2009
  11. Jeff KingMay 12, 2009
  12. Johannes SchindelinMay 12, 2009
  13. Linus TorvaldsMay 13, 2009
  14. Linus TorvaldsMay 13, 2009
  15. Andreas EricssonMay 13, 2009
  16. Linus TorvaldsMay 13, 2009
  17. Martin LanghoffMay 13, 2009
  18. Linus TorvaldsMay 13, 2009
  19. Theodore TsoMay 13, 2009
  20. Linus TorvaldsMay 13, 2009
  21. Daniel BarkalowMay 13, 2009
  22. Linus TorvaldsMay 13, 2009
  23. Matthias AndreeMay 13, 2009
  24. Linus TorvaldsMay 13, 2009
  25. Jay SoffianMay 13, 2009
  26. Matthias AndreeMay 13, 2009
  27. Dmitry PotapovMay 12, 2009
  28. Martin LanghoffMay 12, 2009
  29. Jakub NarebskiMay 12, 2009
  30. Extend sample pre-commit hook to check for non ascii file/usernamesHeiko Voigt, May 12, 2009
  31. Jakub NarebskiMay 12, 2009
  32. Extend sample pre-commit hook to check for non ascii filenamesHeiko Voigt, May 14, 2009
  33. Martin LanghoffMay 15, 2009
  34. Heiko VoigtMay 18, 2009
  35. Jakub NarebskiMay 18, 2009
  36. check for filenames that only differ in case to sample pre-commit hookHeiko Voigt, Jun 20, 2009
  37. Jakub NarebskiMay 15, 2009
  38. Extend sample pre-commit hook to check for non ascii filenamesHeiko Voigt, May 18, 2009
  39. Johannes SixtMay 18, 2009
  40. Heiko VoigtMay 18, 2009
  41. Johannes SixtMay 18, 2009
  42. Extend sample pre-commit hook to check for non ascii filenamesHeiko Voigt, May 19, 2009
  43. Junio C HamanoMay 18, 2009
  44. Julian PhillipsMay 18, 2009
  45. Junio C HamanoMay 15, 2009
  46. Peter KreftingMay 14, 2009
  47. Esko LuontolaMay 14, 2009
  48. Andreas EricssonMay 14, 2009
  49. Johannes SchindelinMay 14, 2009
  50. Dmitry PotapovMay 15, 2009
  51. Cross-Platform Version ControlJohn Tapsell, May 13, 2009
  52. Jay SoffianMay 13, 2009
  53. Alex RiesenMay 13, 2009
  54. Jay SoffianMay 13, 2009
  55. John TapsellMay 13, 2009
  56. Nicolas PitreMay 13, 2009
  57. Johannes SchindelinMay 13, 2009
  58. Andreas EricssonMay 13, 2009
  59. Miles BaderMay 14, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.