Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git cannot execute python-script as hook

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:

  • PortablePython 3.2.1.1
  • msysgit 1.7.6 (Portable)
like image 956
Lukas Knuth Avatar asked Feb 16 '12 08:02

Lukas Knuth


People also ask

Can you make a Git hook with a python script?

Thankfully it's practical and simple to make the git hook gracefully hand off the grunt work to a python script (or any other language for that matter). Today we're going to delve into the client side git hooks.

What is a hook in Git?

Git-hooks are scripts that run automatically every time a particular event occurs in a Git repository. A “pre-commit hook” runs before a commit takes place. These Python hooks are considered a method of static analysis. Static code analysis is a method of debugging done by examining code without executing it.

How do I run a Git hook pre-commit?

For example, the Git hook pre-commit that ships with an initialization of a Git repository can be run without any modification. Navigate to the Git hooks directory with the command cd .git\hooks This directory holds all the Git hook scripts. Create a file named pre-commit. Note:

Where are Python hooks stored in Git?

Since Python our Python hooks are stored as YAML in the source code, they are distributed automatically when we upload this file to Git. This makes setting up a project’s pre-commit hooks on a new machine is as easy as cloning the repo and running the above line.


1 Answers

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.

like image 124
Lukas Knuth Avatar answered Oct 24 '22 09:10

Lukas Knuth