name: format

on:
  workflow_call:

jobs:
  check-format:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - uses: ./.github/actions/setup-pixi
        with:
          environments: format

      - name: Validate update-llvm-version.py can still patch package.cmake
        run: |
          python3 scripts/update-llvm-version.py --check \
            --manifest-dest config/llvm-manifest.json \
            --package-cmake cmake/package.cmake

      - name: Run formatter
        run: pixi run format
        continue-on-error: true

      - name: Auto correct
        uses: huacnlee/autocorrect-action@v2
        with:
          args: --lint ./docs
        continue-on-error: true

      - name: Check diff
        run: |
          if ! git diff --quiet; then
            echo "::error::Formatting changes detected. Please run 'pixi run format' and commit the result."
            git --no-pager diff --stat
            git --no-pager diff
            exit 1
          fi
