Skip to content

Commit 9cdc09d

Browse files
committed
ci: add Python format check workflow using pre-commit
1 parent 851f84a commit 9cdc09d

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Python Format Check
2+
3+
on:
4+
push:
5+
pull_request:
6+
types: [opened, synchronize, reopened]
7+
8+
jobs:
9+
pre-commit:
10+
name: Ruff format changed files
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
18+
- name: Set up Python
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: "3.13"
22+
23+
- name: Install pre-commit
24+
run: python -m pip install pre-commit==4.6.1
25+
26+
- name: Check changed files
27+
env:
28+
BASE_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }}
29+
HEAD_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
30+
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
31+
run: |
32+
if ! git cat-file -e "${BASE_SHA}^{commit}" 2>/dev/null; then
33+
if git show-ref --verify --quiet "refs/remotes/origin/${DEFAULT_BRANCH}"; then
34+
BASE_SHA=$(git merge-base "$HEAD_SHA" "origin/${DEFAULT_BRANCH}")
35+
elif git cat-file -e "${HEAD_SHA}^" 2>/dev/null; then
36+
BASE_SHA=$(git rev-parse "${HEAD_SHA}^")
37+
else
38+
pre-commit run --all-files
39+
git diff --cached --exit-code
40+
exit 0
41+
fi
42+
fi
43+
44+
pre-commit run --from-ref "$BASE_SHA" --to-ref "$HEAD_SHA"
45+
46+
# The local hook formats and stages files. A clean index proves that
47+
# every changed Python file was already formatted before CI ran.
48+
git diff --cached --exit-code

0 commit comments

Comments
 (0)