Skip to content

Commit 490cd28

Browse files
authored
Merge pull request #53 from DeepLabCut/cy/lint-config
Add standardized linting workflow and config
2 parents cc5c3a2 + 2cccafc commit 490cd28

File tree

2 files changed

+87
-16
lines changed

2 files changed

+87
-16
lines changed

.github/workflows/format.yml

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,61 @@
1-
name: pre-commit-format
1+
name: pre-commit (PR only on changed files)
22

33
on:
4-
push:
5-
branches: [main]
64
pull_request:
7-
branches: [main]
5+
types: [opened, synchronize, reopened]
86

97
jobs:
10-
pre_commit_checks:
8+
detect_changes:
119
runs-on: ubuntu-latest
10+
outputs:
11+
changed: ${{ steps.changed_files.outputs.changed }}
12+
1213
steps:
13-
- uses: actions/checkout@v3
14+
- name: Checkout full history
15+
uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
19+
- name: Detect changed files
20+
id: changed_files
21+
run: |
22+
git fetch origin ${{ github.base_ref }}
23+
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD)
24+
25+
{
26+
echo "changed<<EOF"
27+
echo "$CHANGED_FILES"
28+
echo "EOF"
29+
} >> "$GITHUB_OUTPUT"
30+
31+
- name: Show changed files
32+
run: |
33+
echo "Changed files:"
34+
echo "${{ steps.changed_files.outputs.changed }}"
35+
36+
precommit:
37+
needs: detect_changes
38+
runs-on: ubuntu-latest
39+
if: ${{ needs.detect_changes.outputs.changed != '' }}
40+
41+
steps:
42+
- name: Checkout PR branch
43+
uses: actions/checkout@v4
1444
with:
1545
fetch-depth: 0
1646
ref: ${{ github.head_ref }}
1747

18-
- uses: actions/setup-python@v4
48+
- name: Set up Python
49+
uses: actions/setup-python@v5
1950
with:
20-
python-version: '3.10'
51+
python-version: "3.12"
52+
53+
- name: Install pre-commit
54+
run: pip install pre-commit
2155

22-
- run: pip install pre-commit
23-
- run: pre-commit run --all-files
56+
- name: Run pre-commit (CI check-only stage) on changed files
57+
env:
58+
CHANGED_FILES: ${{ needs.detect_changes.outputs.changed }}
59+
run: |
60+
mapfile -t files <<< "$CHANGED_FILES"
61+
pre-commit run --hook-stage manual --files "${files[@]}" --show-diff-on-failure

.pre-commit-config.yaml

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,61 @@
1+
default_stages: [pre-commit]
2+
13
repos:
24
- repo: https://github.com/pre-commit/pre-commit-hooks
35
rev: v6.0.0
46
hooks:
7+
# These are safe to run in both local & CI (they don't require "fix vs check" split)
58
- id: check-added-large-files
9+
stages: [pre-commit, manual]
610
- id: check-yaml
11+
stages: [pre-commit, manual]
712
- id: check-toml
13+
stages: [pre-commit, manual]
14+
- id: check-merge-conflict
15+
stages: [pre-commit, manual]
16+
17+
# These modify files. Run locally only (pre-commit stage).
818
- id: end-of-file-fixer
9-
- id: name-tests-test
10-
args: [--pytest-test-first]
19+
stages: [pre-commit]
1120
- id: trailing-whitespace
12-
- id: check-merge-conflict
21+
stages: [pre-commit]
22+
1323
- repo: https://github.com/tox-dev/pyproject-fmt
1424
rev: v2.15.2
1525
hooks:
1626
- id: pyproject-fmt
27+
stages: [pre-commit] # modifies -> local only
28+
1729
- repo: https://github.com/abravalheri/validate-pyproject
1830
rev: v0.25
1931
hooks:
2032
- id: validate-pyproject
33+
stages: [pre-commit, manual]
34+
2135
- repo: https://github.com/astral-sh/ruff-pre-commit
2236
rev: v0.15.0
2337
hooks:
24-
# Run the formatter.
38+
# --------------------------
39+
# LOCAL AUTOFIX (developers)
40+
# --------------------------
41+
- id: ruff-check
42+
name: ruff-check (fix)
43+
args: [--fix, --unsafe-fixes]
44+
stages: [pre-commit]
45+
2546
- id: ruff-format
26-
# Run the linter.
47+
name: ruff-format (write)
48+
stages: [pre-commit]
49+
50+
# --------------------------
51+
# CI CHECK-ONLY (no writes)
52+
# --------------------------
2753
- id: ruff-check
28-
args: [--fix,--unsafe-fixes]
54+
name: ruff-check (ci)
55+
args: [--output-format=github]
56+
stages: [manual]
57+
58+
- id: ruff-format
59+
name: ruff-format (ci)
60+
args: [--check, --diff]
61+
stages: [manual]

0 commit comments

Comments
 (0)