From 87851b79cbf080d6cd8d170438b1caa972441181 Mon Sep 17 00:00:00 2001 From: Hannah Li Date: Tue, 2 Jun 2026 09:47:02 +0800 Subject: [PATCH] Add pre-commit hooks and run them from format.sh Introduce a pre-commit configuration (whitespace, end-of-file, YAML/JSON validation, JSON pretty-format, and ruff lint/format) and a CI workflow that runs the hooks on the files changed by each pull request. format.sh now installs and runs the pre-commit hooks before the existing SPDX header and ruff steps, so 'bash format.sh' applies the full set of checks locally in one command. Skill content under skills/ is excluded from the hooks: each skill ships a signed skill.oms.sig, so reformatting those files would invalidate the signature. --- .github/workflows/pre-commit.yml | 41 +++++++++++++++++++++++++++++ .pre-commit-config.yaml | 45 ++++++++++++++++++++++++++++++++ format.sh | 25 +++++++++++++++--- 3 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/pre-commit.yml create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 00000000..0c7bbb0c --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,41 @@ +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# +# SPDX-License-Identifier: MIT + +name: pre-commit + +on: + pull_request: + push: + branches: [main] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: actions/setup-python@v5 + with: + python-version: '3.11' + - name: Install pre-commit + run: pip install pre-commit + - name: Run pre-commit on changed files + run: | + set -e + if [ "${{ github.event_name }}" = "pull_request" ]; then + base_sha="${{ github.event.pull_request.base.sha }}" + else + base_sha="${{ github.event.before }}" + fi + echo "Base SHA: ${base_sha}" + echo "Head SHA: ${{ github.sha }}" + changed=$(git diff --name-only "${base_sha}" "${{ github.sha }}" || true) + if [ -n "${changed}" ]; then + echo "Checking changed files:" + echo "${changed}" + pre-commit run --files ${changed} --show-diff-on-failure + else + echo "No changed files to check." + fi diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..2e16c30a --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,45 @@ +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# +# SPDX-License-Identifier: MIT + +# Pre-commit hooks for TileGym. +# +# Run locally with `bash format.sh` (which installs and runs these hooks), or +# directly with `pre-commit run --all-files`. CI runs these hooks on the files +# changed by each pull request (see .github/workflows/pre-commit.yml). +# +# Skill content under `skills/` is intentionally excluded: those files are +# signed artifacts (each skill ships a `skill.oms.sig`), so reformatting them +# would invalidate the signature. Skills are validated and signed by the +# NVSkills pipeline, not hand-formatted. + +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.3.0 + hooks: + - id: trailing-whitespace + exclude: '^skills/|\.patch$' + - id: end-of-file-fixer + exclude: '^skills/|\.patch$' + - id: check-yaml + exclude: '^skills/' + - id: check-added-large-files + exclude: '^skills/|\.patch$' + - id: check-json + exclude: '^skills/' + - id: pretty-format-json + args: ['--autofix', '--indent=2'] + exclude: '^skills/' + +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.14.9 + hooks: + - id: ruff-check + name: ruff-lint + args: [--fix] + types_or: [python] + exclude: '^skills/|\.patch$' + - id: ruff-format + name: ruff-format + types_or: [python] + exclude: '^skills/|\.patch$' diff --git a/format.sh b/format.sh index ad27823e..b7dc7a7a 100755 --- a/format.sh +++ b/format.sh @@ -3,13 +3,32 @@ # # SPDX-License-Identifier: MIT -# Quick formatting script for TileGym development -# Formats code and sorts imports using ruff +# Quick formatting script for TileGym development. +# Runs the pre-commit hooks, adds SPDX headers, and formats/sorts with ruff. set -e RUFF_VERSION="0.14.9" +echo "🔍 Checking pre-commit installation..." +if ! command -v pre-commit &> /dev/null; then + echo "📦 Installing pre-commit..." + pip install pre-commit +fi + +echo "" +echo "Installing pre-commit hooks..." +pre-commit install-hooks + +echo "" +echo "Running all pre-commit hooks..." +# Run pre-commit on all files; continue even if some hooks fail +# (several hooks are auto-fixers and will modify files). +set +e +pre-commit run --all-files +set -e + +echo "" echo "🔍 Checking ruff installation..." if ! python3 -m ruff --version 2>/dev/null | grep -q "$RUFF_VERSION"; then echo "📦 Installing ruff $RUFF_VERSION..." @@ -29,4 +48,4 @@ echo "✨ Formatting code..." python3 -m ruff format . echo "" -echo "✅ Done! SPDX headers added, code is formatted, and imports are sorted." +echo "✅ Done! Pre-commit hooks ran, SPDX headers added, code formatted, imports sorted."