Skip to content

Commit

Permalink
Add pre-commit hook and workflow check
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelroquetto committed Sep 18, 2024
1 parent 593ce93 commit d1204aa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/clang-format-check.yml
Original file line number Diff line number Diff line change
@@ -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 }}

15 changes: 15 additions & 0 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d1204aa

Please sign in to comment.