Skip to content

Commit a44008e

Browse files
committed
Add pre-commit hook for formatting, type checking, and testing
- Create pre-commit hook script to run formatter, mypy, and pytest - Add setup script to install Git hooks - Update DEVELOPMENT.md with documentation for Git hooks - Ensure formatting changes are automatically added to commits
1 parent b3115d2 commit a44008e

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

docs/DEVELOPMENT.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,34 @@ The OAuth callback mechanism is implemented using two main classes:
300300
1. Create a new branch for your feature/fix
301301
2. Make your changes, following the style guidelines (see also:
302302
[LINTING](docs/LINTING.md))
303-
3. Run formatting checks (`pdm format`) and tests (`pdm run pytest`)
303+
3. Run formatting checks (`pdm run format`) and tests (`pdm run pytest`)
304304
4. Submit a pull request with a clear description of changes
305305

306+
### Git Hooks
307+
308+
The project includes pre-commit hooks to ensure code quality. To set up the
309+
hooks:
310+
311+
```bash
312+
# From the repository root
313+
./lint/setup-hooks.sh
314+
```
315+
316+
This will set up a pre-commit hook that automatically runs:
317+
318+
1. `pdm run format` - Runs black, isort, and adds file headers
319+
2. `pdm run mypy` - Runs type checking
320+
3. `pdm run pytest` - Runs all tests
321+
322+
If any of these checks fail, the commit will be blocked. Any formatting changes
323+
made by the hook will be automatically added to the commit.
324+
325+
To bypass the hooks for a specific commit (not recommended for normal usage):
326+
327+
```bash
328+
git commit --no-verify -m "Your commit message"
329+
```
330+
306331
## Release Process
307332

308333
This section will be documented as we near our first release.

lint/pre-commit.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
3+
echo "Running pre-commit hooks..."
4+
5+
# Save current staged changes temporarily
6+
echo "Stashing unstaged changes..."
7+
git stash -q --keep-index
8+
9+
# Get the directory of the repository
10+
REPO_DIR=$(git rev-parse --show-toplevel)
11+
cd "$REPO_DIR" || exit 1
12+
13+
# Execute linting and tests
14+
echo "Running formatter..."
15+
pdm run format
16+
17+
FORMATTING=$?
18+
19+
echo "Running mypy..."
20+
pdm run mypy
21+
22+
MYPY=$?
23+
24+
echo "Running pytest..."
25+
pdm run pytest
26+
27+
PYTEST=$?
28+
29+
# Restore unstaged changes
30+
echo "Restoring unstaged changes..."
31+
git stash pop -q
32+
33+
# If any command failed, abort the commit
34+
if [ $FORMATTING -ne 0 ] || [ $MYPY -ne 0 ] || [ $PYTEST -ne 0 ]; then
35+
echo "Pre-commit hooks failed!"
36+
if [ $FORMATTING -ne 0 ]; then
37+
echo "Formatting failed!"
38+
fi
39+
if [ $MYPY -ne 0 ]; then
40+
echo "Type checking failed!"
41+
fi
42+
if [ $PYTEST -ne 0 ]; then
43+
echo "Tests failed!"
44+
fi
45+
exit 1
46+
fi
47+
48+
# If any files were changed by formatting, add them to the commit
49+
if git diff --name-only | grep -q '.'; then
50+
echo "Formatting changed files. Adding them to the commit..."
51+
git add -u
52+
fi
53+
54+
echo "Pre-commit hooks passed!"
55+
exit 0

lint/setup-hooks.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
# Script to set up Git hooks
4+
5+
REPO_ROOT=$(git rev-parse --show-toplevel)
6+
HOOK_DIR="$REPO_ROOT/.git/hooks"
7+
SCRIPT_DIR="$REPO_ROOT/lint"
8+
9+
echo "Setting up Git hooks..."
10+
11+
# Create hooks directory if it doesn't exist
12+
mkdir -p "$HOOK_DIR"
13+
14+
# Create symlink for pre-commit hook
15+
ln -sf ../../lint/pre-commit.sh "$HOOK_DIR/pre-commit"
16+
chmod +x "$SCRIPT_DIR/pre-commit.sh"
17+
18+
echo "Git hooks setup complete!"
19+
echo "Pre-commit hook will run formatter, type checker, and tests before each commit."

0 commit comments

Comments
 (0)