17

I have created a little pre-commit hook in python. This hook works like a charm under Linux, but in Windows it keeps telling me:

error: cannot spawn .git/hooks/pre-commit: No such file or directory

I know there have been similar questions here about the same issue and the conclusion seams to be the shebang. My script has this on the very first line:

#!F:\PortableApps\PortablePython3.2\App\python.exe

It's also interesting to note that executing the script simply by writing .git/hooks/pre-commit works wonderful, but as soon as I try to commit, git spits out the above message.

Another interesting thing is, when I convert the encoding from ANSI to UTF-8 (using Notepad++), I get the following error when trying to execute the script:

.git/hooks/pre-commit: Cannot execute binary file

I'm using the following tools:

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • possible duplicate of [How can I get my git (msysgit on windows) post-commit script to invoke my python script as python rather than bash?](http://stackoverflow.com/questions/1547005/how-can-i-get-my-git-msysgit-on-windows-post-commit-script-to-invoke-my-python) – Amber Feb 16 '12 at 09:02
  • @Amber using a bash-script as a wrapper to call `python.exe pre-commit.py` works. But is there a Python-only version? – Lukas Knuth Feb 16 '12 at 09:15

1 Answers1

19

I used the proxy-approach to make the python script work under windows (with msysgit). The complete script (with description on how I did it) might be found here: https://gist.github.com/1839424

Here is the important part about making it work under Windows


If you're working with Windows (and "msysgit"), it's a little more complicated. Since "msysgit" seems to have a problem handling the SHEBANG, you'll have to use a little trick to make the script executable (further information on this problem can be found here).

In order to make the script work, you'll want to remove the SHEBANG from the Python script ("pre-commit.py") and use a wrapper bash-script to call the interpreter. This script should look something like this:

#!/bin/sh
python .git/hooks/pre-commit.py

Store this script as a file called "pre-commit" (no file-ending). This assumes that you have Python in your PATH. If you don't, you can also specify the full path to your interpreter-executable.

This script will be called by "git commit" and call the python-script to check for the huge files. The path after the SHEBANG should not be changed, as "msysgit" will remap it automatically. You must specify a path relative to the repo-root for the Python script to be executed (because thats from where the script is called).

Afterwards you'll want to copy both the wrapper-file ("pre-commit") and the Python-script ("pre-commit.py") to your repos ".git/hooks"-directory, personalize the Python-script ("max_file_size" and "git_binary_path") and mark the "pre-commit"-file executable.

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • 4
    If you want to propagate arguments (e.g. for the post-checkout hook), append a `$*`: `python .git/hooks/pre-commit.py $*` – Onur Oct 18 '12 at 12:07
  • The Python script itself does not have to be inside the .git directory. It can be elsewhere in your repository if you prefer, so that it is committed as part of the repository. Then, it's also fairly straightforward to also automate installation of the git hooks into a new clone of the repo by programatically creating the shell scripts described here in the .git/hooks folder. – bsa Oct 15 '13 at 09:35
  • 1
    what if team memebers have heterogenous OSes. For example we have an Android repository, and developers work on it both on OS X, and Windows. how can we ensure that our hook works across different Operating Systems? – Saeed Neamati Jun 27 '16 at 05:54
  • You can use @Luks' solution on Mac OS, GNU/Linux and Windows. – Alireza Bashiri Jun 27 '16 at 06:39