From 5db589f41a687a3718bfaab215ef5fec7549079c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Hordy=C5=84ski?= <26008518+mhordynski@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:55:56 +0100 Subject: [PATCH] chore: add pre-commit/push hooks install script (#164) --- CONTRIBUTING.md | 16 +++----- scripts/install_git_hooks.py | 76 ++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 scripts/install_git_hooks.py diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b650bf51..f7fd4b2d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,18 +31,12 @@ $ uv run mypy . ``` -## Install pre-commit +## Install pre-commit or pre-push hooks -We also run some checks through a pre-commit hook. To set it up, follow these steps: +We also recommend to run checkers on pre-commit/push hook. To set it up, follow these steps: +```bash +$ uv run scripts/install_git_hooks.py ``` -pre-commit install -``` - -All updated files will be reformatted and linted before the commit. - -To reformat and lint all files in the project, use: - -`pre-commit run --all-files` -The used linters are configured in `.pre-commit-config.yaml`. You can use `pre-commit autoupdate` to bump tools to the latest versions. +Then decide whether you want to run the checks before each commit or before each push. diff --git a/scripts/install_git_hooks.py b/scripts/install_git_hooks.py new file mode 100644 index 00000000..d1580486 --- /dev/null +++ b/scripts/install_git_hooks.py @@ -0,0 +1,76 @@ +# /// script +# requires-python = ">=3.10" +# dependencies = [ +# "inquirer", +# "rich", +# ] +# /// +# To run this script and install git hooks, run the following command: +# +# uv run scripts/install_git_hooks.py +# +from pathlib import Path + +from inquirer.shortcuts import list_input +from rich import print as pprint + +HOOK_BODY = """ +#!/usr/bin/env bash + +echo "๐Ÿงน Running formatting...\n" +uv run ruff format --check + +if [ $? -ne 0 ] +then + echo "โš  Formatting failed. Running autofix & aborting..." + uv run ruff format + exit 1 +fi + +echo "โœ… Formatting passed!" +echo "\n๐Ÿ“œ Running linting...\n" + +uv run ruff check + +if [ $? -ne 0 ] +then + echo "โš  Linting failed. Aborting..." + exit 1 +fi + +echo "โœ… Linting passed!" +echo "\n๐Ÿ”Ž Running type checking...\n" + +uv run mypy . + +if [ $? -ne 0 ] +then + echo "โš  Type checking failed. Aborting..." + exit 1 +fi + +echo "โœ… Type checking passed!" +""" + + +def main() -> None: + """ + Install pre-commit or pre-push git hooks. + """ + hooks_dir = Path(__file__).parent.parent / ".git" / "hooks" + hooks_dir.mkdir(exist_ok=True) + + hook_type = list_input("Select a hook to install", choices=["pre-commit", "pre-push"]) + + (hooks_dir / "pre-commit").unlink(missing_ok=True) + (hooks_dir / "pre-push").unlink(missing_ok=True) + + pre_commit_hook = hooks_dir / hook_type + pre_commit_hook.write_text(HOOK_BODY) + pre_commit_hook.chmod(0o755) + + pprint(f"[cyan]Git hook for [b]{hook_type}[/b] installed!") + + +if __name__ == "__main__": + main()