Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pycharm precommits fix

From https://youtrack.jetbrains.com/issue/PY-12988#focus=Comments-27-4122075.0-0

We are using https://github.com/pre-commit/pre-commit-hooks for hooks and this is a really annoying issue This is how I solved it for now (linux only but can be ported to windows) The only requirement is to be in your root folder with the virtualenv activated when you install the hooks this is my shell script in /usr/bin/install_pre_commit_hooks.sh:

#!/bin/bash

pre-commit install
mv .git/hooks/pre-commit .git/hooks/pre-commit-python

cat <<EOF > .git/hooks/pre-commit
#!/bin/bash
source ${VIRTUAL_ENV}/bin/activate
DIR="\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
python \${DIR}/pre-commit-python
EOF

chmod +x .git/hooks/pre-commit

This will:

  • call pre-commit install
  • move the installed script to .git/hooks/pre-commit-python
  • replace the pre-commit by a bash script:
  • activate the virtualenv (static full path found at install time)
  • dynamically find the directory of the bash script is running from
  • call pre-commit-python with the virtual env activated

If you add more hooks using .pre-commit-config.yaml or you change the path of your virtualenv just call install_pre_commit_hooks.sh again 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment