Skip to content

Commit

Permalink
chore: add pre-commit/push hooks install script (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhordynski authored Oct 31, 2024
1 parent b245164 commit 5db589f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 11 deletions.
16 changes: 5 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
76 changes: 76 additions & 0 deletions scripts/install_git_hooks.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 5db589f

Please sign in to comment.