From b9dbf59a986cd219097c8cbccc75cff224f6a866 Mon Sep 17 00:00:00 2001 From: Martin Thomson Date: Tue, 9 Jan 2024 02:41:11 +1100 Subject: [PATCH] Run pre-commit using a python venv This ensures that you don't need to rely on the environment quite so much. Also: Add .gitignore for venv dir --- .gitignore | 3 +++ pre-commit | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..bb03f1e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.swp +*~ +/venv/ diff --git a/pre-commit b/pre-commit index 6f728692..f292f581 100755 --- a/pre-commit +++ b/pre-commit @@ -7,6 +7,7 @@ # $ ln -s ../../hooks/pre-commit .git/pre-commit root="$(git rev-parse --show-toplevel 2>/dev/null)" +venv="$root/venv" # Some sanity checking. hash python3 || exit 1 @@ -17,7 +18,7 @@ if [[ "$1" == "install" ]]; then hook="$root"/.git/hooks/pre-commit if [[ -e "$hook" ]]; then echo "Hook already installed:" - ls -l "$hook" + ls -l "$hook" else ln -s ../../pre-commit "$hook" echo "Installed git pre-commit hook at $hook" @@ -25,6 +26,19 @@ if [[ "$1" == "install" ]]; then exit fi +# venv setup +if [ ! -d "$venv" ]; then + echo "Setting up python venv" + python3 -m venv "$venv" + source "$venv/bin/activate" + pip3 install -r requirements.txt +elif [ requirements.txt -nt "$venv" ]; then + source "$venv/bin/activate" + pip3 install --upgrade --upgrade-stategy eager -r requirements.txt +else + source "$venv/bin/activate" +fi + exec 1>&2 # Perform validation.