How can I run a virtualenv python script as a git pre-commit hook
This is my pre-commit script:
#!/bin/bash for f in .git/hooks/pre-commit.d/*; do if [ -x "$f" ]; then if ! "$f"; then echo "DID NOT COMMIT YOUR CHANGES!"; exit 1 fi fi done
One of the executables in pre-commit.d is a python script (pre-commit-pylint.py) that starts with:
- ERROR virtualenvwrapper in GitBash
- Django - syncdb causes error: “DatabaseError: no such table: django_site”
- virtualenv doesn't work on git bash MINGW32
- Corrupt pack file on git aws.push
- problems with pip install
- Using git post-receive hook to deploy python application in virtualenv
#!/usr/bin/env python import pylint
pylint is installed on my virtualenv. My problem is that git runs pre-commit prepending
$PATH, so even if my virtualenv is activated the
pre-commit.d/pre-commit-pylint.py script runs with the system
/usr/bin/python (instead of running with the virtualenv python).
I want to have hooks that are compatible for developers that are not using virtualenv. Is there any way to run my python script with virtualenv transparently (ie, staying compatible with developers that are using their system python)?
2 Solutions collect form web for “How can I run a virtualenv python script as a git pre-commit hook”
You can check in your pre-commit script for the $VIRTUAL_ENV variable and prepend it to $PATH accordingly:
#!/bin/bash if [ -n $VIRTUAL_ENV ]; then PATH=$VIRTUAL_ENV/bin:$PATH fi for f in .git/hooks/pre-commit.d/*; do if [ -x "$f" ]; then if ! "$f"; then echo "DID NOT COMMIT YOUR CHANGES!"; exit 1 fi fi done
What I ended up doing is: the
.git file structure:
#!/usr/bin/env bash export PATH="$THE_GOOD_PATH" python "$GIT_DIR/hooks/pre-commit-main.py"
#!/usr/bin/env python import sys print sys.version_info
Then, when you call
git commit, make sure that
THE_GOOD_PATH, is defined:
export THE_GOOD_PATH="$PATH" git commit
You could also export
THE_GOOD_PATH="$PATH" from your
.profile or the toplevel of your application and symlink all hooks to a single file.
This method has the advantage of being virtualenv agnostic: it also works with Ruby RVM rbenv.
I wrote to the Git developers at: http://permalink.gmane.org/gmane.comp.version-control.git/258454 asking them to leave our
PATH alone, but the initial reply was WONTFIX.