Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add pre-commit/push hooks install scripts #164

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
mhordynski marked this conversation as resolved.
Show resolved Hide resolved

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
akonarski-ds marked this conversation as resolved.
Show resolved Hide resolved
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()
Loading