threads / patch / 42614

patchhooks--pre-commit.sample: check for chars, that are not allowed for a windows file name

Subject: [PATCH] hooks--pre-commit.sample: check for chars, that are not allowed for a windows file name

## tl;dr

4 messages between Jun 16, 2016 and Jun 17, 2016. Diffs are folded; open one to read it.

replies: 3people: 3as markdown or json

dexteritas· Jun 15, 2016, 08:02 UTC · lore
After the ASCII-check, test the windows compatibility of file names.
Can be disabled by:
git config hooks.allownonwindowschars true
---
 templates/hooks--pre-commit.sample | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
Show changes to templates/hooks--pre-commit.sample +22 −1
diff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample
index 68d62d5..120daf1 100755
--- a/templates/hooks--pre-commit.sample
+++ b/templates/hooks--pre-commit.sample
@@ -17,6 +17,7 @@ fi
 
 # If you want to allow non-ASCII filenames set this variable to true.
 allownonascii=$(git config --bool hooks.allownonascii)
+allownonwindowschars=$(git config --bool hooks.allownonwindowschars)
 
 # Redirect output to stderr.
 exec 1>&2
@@ -43,6 +44,27 @@ If you know what you are doing you can disable this check using:
   git config hooks.allownonascii true
 EOF
 	exit 1
+elif [ "$allownonwindowschars" != "true" ] &&
+	# If you work with linux and windows, there is a problem, if you use
+	# chars like \ / : * ? " < > |
+	# Check if there are used only windows compatible chars
+	test $(git diff --cached --name-only --diff-filter=A -z $against |
+	  LC_ALL=C tr -d '[0-9A-Za-z\[\]\{\}_ -)+-.]\0' | wc -c) != 0
+then
+	cat <<\EOF
+Error: Attempt to add a chars that are not allowed for a windows file name.
+
+This can cause problems if you want to work with people on other platforms.
+
+To be portable it is advisable to rename the file.
+
+Check your filenames for: \ / : * ? " < > |
+
+If you know what you are doing you can disable this check using:
+
+  git config hooks.allownonwindowschars true
+EOF
+	exit 2
 fi
 
 # If there are whitespace errors, print the offending file names and fail.

--
https://github.com/git/git/pull/252
Thomas Braun· Jun 15, 2016, 11:15 UTC · re: dexteritas · lore

Re: [PATCH] hooks--pre-commit.sample: check for chars, that are not allowed for a windows file name

Am 15.06.2016 um 10:02 schrieb dexteritas:
Show 47 quoted lines
> After the ASCII-check, test the windows compatibility of file names.
> Can be disabled by:
> git config hooks.allownonwindowschars true
> ---
>  templates/hooks--pre-commit.sample | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
> 
> diff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample
> index 68d62d5..120daf1 100755
> --- a/templates/hooks--pre-commit.sample
> +++ b/templates/hooks--pre-commit.sample
> @@ -17,6 +17,7 @@ fi
>  
>  # If you want to allow non-ASCII filenames set this variable to true.
>  allownonascii=$(git config --bool hooks.allownonascii)
> +allownonwindowschars=$(git config --bool hooks.allownonwindowschars)
>  
>  # Redirect output to stderr.
>  exec 1>&2
> @@ -43,6 +44,27 @@ If you know what you are doing you can disable this check using:
>    git config hooks.allownonascii true
>  EOF
>  	exit 1
> +elif [ "$allownonwindowschars" != "true" ] &&
> +	# If you work with linux and windows, there is a problem, if you use
> +	# chars like \ / : * ? " < > |
> +	# Check if there are used only windows compatible chars
> +	test $(git diff --cached --name-only --diff-filter=A -z $against |
> +	  LC_ALL=C tr -d '[0-9A-Za-z\[\]\{\}_ -)+-.]\0' | wc -c) != 0
> +then
> +	cat <<\EOF
> +Error: Attempt to add a chars that are not allowed for a windows file name.
> +
> +This can cause problems if you want to work with people on other platforms.
> +
> +To be portable it is advisable to rename the file.
> +
> +Check your filenames for: \ / : * ? " < > |
> +
> +If you know what you are doing you can disable this check using:
> +
> +  git config hooks.allownonwindowschars true
> +EOF
> +	exit 2
>  fi
>  
>  # If there are whitespace errors, print the offending file names and fail.

There are some cases of illegal file names missing. E.g. reserved names, trailing period and space. My trial with a precommit hook for avoiding illegal filenames on windows can be found at [1]. Feel free to loot my version for a reroll.

[1]: https://github.com/t-b/git-pre-commit-hook-windows-filenames/blob/master/pre-commit

dex teritas· Jun 17, 2016, 22:24 UTC · re: Thomas Braun · lore

Re: [PATCH] hooks--pre-commit.sample: check for chars, that are not allowed for a windows file name

Am 15.06.2016 um 13:15 schrieb Thomas Braun:
Show 56 quoted lines
> Am 15.06.2016 um 10:02 schrieb dexteritas:
>> After the ASCII-check, test the windows compatibility of file names.
>> Can be disabled by:
>> git config hooks.allownonwindowschars true
>> ---
>>  templates/hooks--pre-commit.sample | 22 ++++++++++++++++++++++
>>  1 file changed, 22 insertions(+)
>>
>> diff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample
>> index 68d62d5..120daf1 100755
>> --- a/templates/hooks--pre-commit.sample
>> +++ b/templates/hooks--pre-commit.sample
>> @@ -17,6 +17,7 @@ fi
>>  
>>  # If you want to allow non-ASCII filenames set this variable to true.
>>  allownonascii=$(git config --bool hooks.allownonascii)
>> +allownonwindowschars=$(git config --bool hooks.allownonwindowschars)
>>  
>>  # Redirect output to stderr.
>>  exec 1>&2
>> @@ -43,6 +44,27 @@ If you know what you are doing you can disable this check using:
>>    git config hooks.allownonascii true
>>  EOF
>>  	exit 1
>> +elif [ "$allownonwindowschars" != "true" ] &&
>> +	# If you work with linux and windows, there is a problem, if you use
>> +	# chars like \ / : * ? " < > |
>> +	# Check if there are used only windows compatible chars
>> +	test $(git diff --cached --name-only --diff-filter=A -z $against |
>> +	  LC_ALL=C tr -d '[0-9A-Za-z\[\]\{\}_ -)+-.]\0' | wc -c) != 0
>> +then
>> +	cat <<\EOF
>> +Error: Attempt to add a chars that are not allowed for a windows file name.
>> +
>> +This can cause problems if you want to work with people on other platforms.
>> +
>> +To be portable it is advisable to rename the file.
>> +
>> +Check your filenames for: \ / : * ? " < > |
>> +
>> +If you know what you are doing you can disable this check using:
>> +
>> +  git config hooks.allownonwindowschars true
>> +EOF
>> +	exit 2
>>  fi
>>  
>>  # If there are whitespace errors, print the offending file names and fail.
> There are some cases of illegal file names missing. E.g. reserved names,
> trailing period and space. My trial with a precommit hook for avoiding
> illegal filenames on windows can be found at [1]. Feel free to loot my
> version for a reroll.
>
> [1]:
> https://github.com/t-b/git-pre-commit-hook-windows-filenames/blob/master/pre-commit
>
You're right. Thanks for the example.
Junio C Hamano· Jun 15, 2016, 17:54 UTC · re: dexteritas · lore

Re: [PATCH] hooks--pre-commit.sample: check for chars, that are not allowed for a windows file name

dexteritas <dexteritas1@gmx.de> writes:
> After the ASCII-check, test the windows compatibility of file names.
> Can be disabled by:
> git config hooks.allownonwindowschars true
> ---
Missing sign off.
Show 20 quoted lines
>  templates/hooks--pre-commit.sample | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
>
> diff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample
> index 68d62d5..120daf1 100755
> --- a/templates/hooks--pre-commit.sample
> +++ b/templates/hooks--pre-commit.sample
> @@ -17,6 +17,7 @@ fi
>  
>  # If you want to allow non-ASCII filenames set this variable to true.
>  allownonascii=$(git config --bool hooks.allownonascii)
> +allownonwindowschars=$(git config --bool hooks.allownonwindowschars)
>  
>  # Redirect output to stderr.
>  exec 1>&2
> @@ -43,6 +44,27 @@ If you know what you are doing you can disable this check using:
>    git config hooks.allownonascii true
>  EOF
>  	exit 1
> +elif [ "$allownonwindowschars" != "true" ] &&

This line is doubly irritating because (1) the double negation is somewhat hard to grok, and (2) [] is not part of our CodingStyle.

Because you inherited this from the existing "allow-non-ascii" one, however, I do not want to see you change this line, if you need to reroll.

> +	# If you work with linux and windows, there is a problem, if you use
> +	# chars like \ / : * ? " < > |
There is no reason to single out Linux, is there?

This new check is only about Windows and because people on other platforms, not limited to Linux, may not be aware of some characters that are not usable on Windows, you are trying to help them, no?

> +	# Check if there are used only windows compatible chars
> +	test $(git diff --cached --name-only --diff-filter=A -z $against |
> +	  LC_ALL=C tr -d '[0-9A-Za-z\[\]\{\}_ -)+-.]\0' | wc -c) != 0

Because this is in "elif", we know we are allowing non-ascii characters when we come here. So you need to do a quite similar check from scratch, which is sensible. I do not offhand know if this covers all the characters that Windows users cannot use, though.

Show 15 quoted lines
> +then
> +	cat <<\EOF
> +Error: Attempt to add a chars that are not allowed for a windows file name.
> +
> +This can cause problems if you want to work with people on other platforms.
> +
> +To be portable it is advisable to rename the file.
> +
> +Check your filenames for: \ / : * ? " < > |
> +
> +If you know what you are doing you can disable this check using:
> +
> +  git config hooks.allownonwindowschars true
> +EOF
> +	exit 2
Why 2?
>  fi
>  
>  # If there are whitespace errors, print the offending file names and fail.
When the user says
	[hooks]
            allownonascii = false
	    allownonwindowschars = false
what happens?

The user's intention clearly is that the project wants to be usable on Windows and also wants to exclude characters from codepages that are not ASCII. I however suspect that the hook with your patch will allow people to create a "path>like?this.txt" happily.

← back to recent threads