3

I am developing on a Linux machine. It happened once that I added a file 'sin(x):2.tex' or something similar. This caused some trouble for a friend who uses a Windows system and wanted to clone my repository.

I know there are much more invalid file names on Windows than on Linux. Is there a possibility to make git warn me if I try to add such an invalid file name to my repository?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • 1
    Have you tried a [`pre-commit` hook](http://www.git-scm.com/book/en/v2/Customizing-Git-Git-Hooks)? – Elliott Frisch Nov 28 '14 at 06:41
  • @ElliottFrisch: No, I didn't know that this was possible. I have to take a look at that. Thanks! (I see pre-commit and post-commit at a first glance, but no pre-add ... but I have to read that more carefully to see if / how it works) – Martin Thoma Nov 28 '14 at 06:43

1 Answers1

2

You can see a good example of pre-commit hook in this gist, in reference to this answer about valid Windows names

# A hook script to check that the to-be-commited files are valid
# filenames on a windows platform.
# Sources:
# - https://stackoverflow.com/a/62888
# - http://msdn.microsoft.com/en-us/library/aa365247.aspx
#
# To enable this hook, rename this file to "pre-commit", move it to ".git/hook" and make it executable

It uses git diff:

git diff --cached --name-only --diff-filter=A -z $against

Note that a pre-commit hook is a client-side hook, meaning it has to be deployed in all repos for all users.
And it can be bypassed (with git commit --no-verify).

Another approach is to set a pre-receive hook (server-side hook) which will block any push including invalid filenames.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250