Skip to content
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
96 changes: 96 additions & 0 deletions .github/workflows/check-precommit-applied.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: pre-commit

on:
pull_request:
push:
branches:
- main
- develop

jobs:
pre-commit:
runs-on: ubuntu-latest

steps:
# Checkout full repo history to ensure diff calculation works
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-python@v4
with:
python-version: '3.12'

# Cache pre-commit environments
- name: Cache pre-commit hooks
uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: pre-commit-${{ runner.os }}-${{ hashFiles('.pre-commit-config.yaml') }}
restore-keys: |
pre-commit-${{ runner.os }}-

- name: Install pre-commit
run: pip install pre-commit

- name: Run pre-commit on diff
id: run_precommit
env:
EVENT_NAME: ${{ github.event_name }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.sha }}
BEFORE_SHA: ${{ github.event.before }}
run: |
set -e

echo "🔍 Determining diff range..."

if [ "$EVENT_NAME" = "pull_request" ]; then
FROM_REF="$BASE_SHA"
TO_REF="$HEAD_SHA"
echo "PR event: checking diff from $FROM_REF to $TO_REF"
elif [ -n "$BEFORE_SHA" ] && [ "$BEFORE_SHA" != "0000000000000000000000000000000000000000" ]; then
FROM_REF="$BEFORE_SHA"
TO_REF="$HEAD_SHA"
echo "Push event: checking diff from $FROM_REF to $TO_REF"
else
# Edge case: unknown base; check all files
echo "⚠️ Unable to determine diff automatically — running on all files."
set +e
pre-commit run --all-files
EXIT_CODE=$?
set -e
echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT
exit 0
fi

# Run pre-commit on diff
set +e
pre-commit run --from-ref "$FROM_REF" --to-ref "$TO_REF"
EXIT_CODE=$?
set -e

echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT

- name: Check for uncommitted changes from pre-commit
run: |
if ! git diff --quiet; then
echo "❌ pre-commit hooks modified files."
echo " Please run the following locally:"
echo ""
echo " pre-commit install"
echo " pre-commit run --all-files"
echo ""
echo " Then commit and push the changes."
echo ""
git diff --color
exit 1
fi

echo "✔️ No changes detected. pre-commit checks passed."

- name: Fail if hooks returned errors
if: steps.run_precommit.outputs.exit_code != '0'
run: |
echo "❌ pre-commit hooks reported issues."
exit 1
15 changes: 11 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.12.10
hooks:
# Run the linter.
# Run the linter (fix issues automatically).
- id: ruff-check
args: [ --fix ]
args: [--fix]
pass_filenames: true
stages: [commit]

# Run the formatter.
- id: ruff-format

pass_filenames: true
stages: [commit]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-yaml
- id: check-case-conflict
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/kynan/nbstripout
rev: 0.8.1
hooks:
- id: nbstripout
pass_filenames: true
stages: [commit]
15 changes: 13 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,24 @@ pip install pytest pytest-cov black flake8

### Submitting a Pull Request

1. **Push your branch** to your fork:
1. **Ensure pre-commit checks pass** (Important!)
```bash
# Run pre-commit on all files
pre-commit run --all-files

# Or run on specific files
pre-commit run --files path/to/your/files
```

The pre-commit checks will automatically run in CI, but running them locally first saves time.

2. **Push your branch** to your fork:
```bash
git push origin feature/your-feature-name
```

2. **Open a Pull Request** on GitHub:
- **Target branch**:
- **Target branch**:
- Use `develop` for new features and non-critical bug fixes
- Use `main` only for hotfixes or critical patches to the current stable release
- Most contributions should target the `develop` branch
Expand Down
1 change: 1 addition & 0 deletions workflow/envs/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ dependencies:
- mkdocs-awesome-nav
- mike>=1.1.0
- python-markdown-math
- pre-commit>=4.0.0
Loading