Skip to content

Instantly share code, notes, and snippets.

@michaelbukachi
Last active June 8, 2023 15:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelbukachi/7bb61ae1f3ff76df6d9a13692ea7bf0d to your computer and use it in GitHub Desktop.
Save michaelbukachi/7bb61ae1f3ff76df6d9a13692ea7bf0d to your computer and use it in GitHub Desktop.
Pycharm precommits fix

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