Skip to content

Commit de2924a

Browse files
committed
Update to fix lint issues and add githooks to avoid ci failures
1 parent b650886 commit de2924a

File tree

6 files changed

+255
-3
lines changed

6 files changed

+255
-3
lines changed

.githooks/README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Git Hooks
2+
3+
This directory contains git hooks that maintain code quality for this project.
4+
5+
## Installation
6+
7+
To install the hooks, run from the repository root:
8+
9+
```bash
10+
# On Unix/Mac/Linux:
11+
bash .githooks/install-hooks.sh
12+
13+
# On Windows (Git Bash or WSL):
14+
bash .githooks/install-hooks.sh
15+
16+
# Or manually copy the hooks:
17+
cp .githooks/pre-commit .git/hooks/pre-commit
18+
cp .githooks/pre-push .git/hooks/pre-push
19+
chmod +x .git/hooks/pre-commit .git/hooks/pre-push
20+
```
21+
22+
## Hooks Overview
23+
24+
### pre-commit
25+
26+
Runs automatically before each commit. Checks:
27+
- **black**: Code formatting
28+
- **isort**: Import sorting
29+
- **mypy**: Type checking
30+
31+
The hook will:
32+
1. Format your code automatically
33+
2. Re-stage the formatted files
34+
3. Fail the commit if type checking fails
35+
36+
### pre-push
37+
38+
Runs automatically before each push. Checks:
39+
- **pytest**: Full test suite (72 tests)
40+
41+
The hook will prevent pushing if any tests fail.
42+
43+
## Bypassing Hooks
44+
45+
If you need to bypass the hooks (not recommended):
46+
47+
```bash
48+
# Skip pre-commit hook
49+
git commit --no-verify
50+
51+
# Skip pre-push hook
52+
git push --no-verify
53+
```
54+
55+
## Manual Testing
56+
57+
You can manually run the checks:
58+
59+
```bash
60+
# Run formatting
61+
python -m black *.py tests/*.py
62+
python -m isort *.py tests/*.py
63+
64+
# Run type checking
65+
python -m mypy *.py tests/*.py --ignore-missing-imports
66+
67+
# Run tests
68+
python -m pytest tests/ -v
69+
```
70+
71+
## Why Git Hooks?
72+
73+
Git hooks help maintain code quality by:
74+
- Catching formatting issues before they reach CI
75+
- Preventing type errors from being committed
76+
- Ensuring all tests pass before pushing
77+
- Maintaining consistent code style across contributors
78+
- Saving time by catching issues locally
79+
80+
## Troubleshooting
81+
82+
**Hook not running:**
83+
- Make sure you ran the install script
84+
- Check that hooks are executable: `ls -la .git/hooks/pre-commit`
85+
- Verify you're in the git repository
86+
87+
**Hook failing:**
88+
- Read the error message carefully
89+
- Run the failing command manually to debug
90+
- Make sure you have the required tools installed (`black`, `isort`, `mypy`, `pytest`)
91+
92+
**Need to update hooks:**
93+
- Edit the files in `.githooks/` directory
94+
- Run the install script again to copy updated hooks

.githooks/install-hooks.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
# Install git hooks for code quality checks
3+
# Run this script from the repository root after cloning
4+
5+
set -e
6+
7+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8+
REPO_ROOT="$(git rev-parse --show-toplevel)"
9+
HOOKS_DIR="$REPO_ROOT/.git/hooks"
10+
11+
echo "Installing git hooks..."
12+
echo ""
13+
14+
# Check if we're in a git repository
15+
if [ ! -d "$REPO_ROOT/.git" ]; then
16+
echo "Error: Not in a git repository"
17+
exit 1
18+
fi
19+
20+
# Copy hooks
21+
echo "Copying pre-commit hook..."
22+
cp "$SCRIPT_DIR/pre-commit" "$HOOKS_DIR/pre-commit"
23+
chmod +x "$HOOKS_DIR/pre-commit"
24+
25+
echo "Copying pre-push hook..."
26+
cp "$SCRIPT_DIR/pre-push" "$HOOKS_DIR/pre-push"
27+
chmod +x "$HOOKS_DIR/pre-push"
28+
29+
echo ""
30+
echo "✅ Git hooks installed successfully!"
31+
echo ""
32+
echo "Installed hooks:"
33+
echo " - pre-commit: Runs black, isort, and mypy on staged files"
34+
echo " - pre-push: Runs full test suite before pushing"
35+
echo ""
36+
echo "To bypass hooks (not recommended):"
37+
echo " git commit --no-verify"
38+
echo " git push --no-verify"

.githooks/pre-commit

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
# Git pre-commit hook to run code quality checks
3+
# This runs black, isort, and mypy on all Python files
4+
5+
set -e
6+
7+
# Change to repo root directory
8+
cd "$(git rev-parse --show-toplevel)"
9+
10+
echo "Running pre-commit checks..."
11+
12+
# Get list of staged Python files
13+
PYTHON_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.py$' || true)
14+
15+
if [ -z "$PYTHON_FILES" ]; then
16+
echo "No Python files to check"
17+
exit 0
18+
fi
19+
20+
echo "Checking Python files:"
21+
echo "$PYTHON_FILES" | sed 's/^/ - /'
22+
echo ""
23+
24+
# Run black
25+
echo "Running black..."
26+
python -m black $PYTHON_FILES
27+
if [ $? -ne 0 ]; then
28+
echo "❌ Black formatting failed"
29+
exit 1
30+
fi
31+
32+
# Run isort
33+
echo "Running isort..."
34+
python -m isort $PYTHON_FILES
35+
if [ $? -ne 0 ]; then
36+
echo "❌ isort failed"
37+
exit 1
38+
fi
39+
40+
# Run mypy
41+
echo "Running mypy..."
42+
python -m mypy $PYTHON_FILES --ignore-missing-imports
43+
if [ $? -ne 0 ]; then
44+
echo "❌ mypy type checking failed"
45+
exit 1
46+
fi
47+
48+
# Re-add files that were formatted
49+
echo "Re-staging formatted files..."
50+
git add $PYTHON_FILES
51+
52+
echo "✅ All pre-commit checks passed"
53+
exit 0

.githooks/pre-push

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
# Git pre-push hook to run full test suite
3+
# This ensures all tests pass before pushing
4+
5+
set -e
6+
7+
# Change to repo root directory
8+
cd "$(git rev-parse --show-toplevel)"
9+
10+
echo "Running pre-push checks..."
11+
echo ""
12+
13+
# Run the full test suite
14+
echo "Running test suite..."
15+
python -m pytest tests/ -v --tb=short
16+
17+
if [ $? -ne 0 ]; then
18+
echo ""
19+
echo "❌ Tests failed! Push aborted."
20+
echo "Fix the failing tests before pushing."
21+
exit 1
22+
fi
23+
24+
echo ""
25+
echo "✅ All tests passed! Proceeding with push..."
26+
exit 0

CONTRIBUTING.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ We welcome several types of contributions:
8181
pip install -e .
8282
```
8383

84+
### Git Hooks
85+
86+
The project includes git hooks to maintain code quality automatically. **You must install them after cloning:**
87+
88+
```bash
89+
# Run from the repository root
90+
bash .githooks/install-hooks.sh
91+
```
92+
93+
This installs:
94+
- **pre-commit hook**: Runs black, isort, and mypy on all staged Python files
95+
- **pre-push hook**: Runs the full test suite before pushing
96+
97+
The hooks will:
98+
- Automatically format your code before commits
99+
- Prevent commits with type errors
100+
- Prevent pushes if tests fail
101+
102+
If you need to bypass the hooks temporarily (not recommended):
103+
```bash
104+
git commit --no-verify # Skip pre-commit hook
105+
git push --no-verify # Skip pre-push hook
106+
```
107+
108+
See `.githooks/README.md` for more details.
109+
84110
### Testing Your Setup
85111

86112
1. Run the basic functionality test:
@@ -201,10 +227,11 @@ Update documentation for new output formats
201227
### Pull Request Process
202228

203229
1. **Before submitting:**
204-
- Run tests and ensure they pass
230+
- Run tests and ensure they pass (`python -m pytest tests/`)
205231
- Update documentation if needed
206232
- Add tests for new features
207-
- Check code formatting
233+
- Format code with black and isort (automatically handled by git hooks)
234+
- Verify type checking passes (`python -m mypy *.py tests/*.py --ignore-missing-imports`)
208235

209236
2. **Pull request description:**
210237
- Clearly describe what changes were made

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,24 @@ The core library uses only Python standard library, but some examples require ad
7272
### Basic Installation
7373

7474
```bash
75-
# Simply copy the files to your working directory
75+
# Clone the repository
76+
git clone https://github.com/yourusername/ndf-labchart.git
77+
cd ndf-labchart
78+
7679
# Requires Python 3.6+
7780
```
7881

82+
### For Contributors
83+
84+
If you plan to contribute, install the git hooks to maintain code quality:
85+
86+
```bash
87+
# Install git hooks (runs code formatting and tests automatically)
88+
bash .githooks/install-hooks.sh
89+
```
90+
91+
This sets up pre-commit and pre-push hooks that automatically check code formatting and run tests.
92+
7993
### For Examples and Advanced Features
8094

8195
```bash

0 commit comments

Comments
 (0)