diff --git a/.github/workflows/clang-format-check.yml b/.github/workflows/clang-format-check.yml new file mode 100644 index 000000000..e3c1b369b --- /dev/null +++ b/.github/workflows/clang-format-check.yml @@ -0,0 +1,36 @@ +name: Clang Format Check + +on: + push: + branches: [ 'main', 'release-*' ] + pull_request: + branches: [ 'main', 'release-*' ] + +jobs: + clang-format: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Install Clang-Format + run: sudo apt-get install clang-format + + - name: Check Clang Format + run: | + # Check for modified C/C++ files + files=$(git diff --name-only origin/main...HEAD | grep -E '\.(c|cpp|h)$') + if [ -z "$files" ]; then + echo "No C/C++ files modified." + exit 0 + fi + + # Run clang-format and check for changes + clang-format -i $files + git diff --exit-code $files + #env: + # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + diff --git a/hooks/pre-commit b/hooks/pre-commit new file mode 100755 index 000000000..a32b8cd5e --- /dev/null +++ b/hooks/pre-commit @@ -0,0 +1,15 @@ +#!/bin/bash + +# Format all staged C/C++ files +files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(c|cpp|h)$') + +if [ -n "$files" ]; then + echo "Running clang-format on:" + echo "$files" + + # Run clang-format on the staged files + for file in $files; do + clang-format -i "$file" + git add "$file" # Re-add formatted files to staging + done +fi