Skip to content

Run clang-format in GitHub Actions #30

Run clang-format in GitHub Actions

Run clang-format in GitHub Actions #30

Workflow file for this run

name: Clang Format Check
on:
pull_request:
paths:
- 'Sources/**'
- 'Tests/**'
- 'Samples/Common/Sources/CrashTriggers/**'
- '.github/workflows/clang-format.yml'
- '.clang-format-ignore'
jobs:
formatting-check:
name: Formatting Check
runs-on: ubuntu-latest
env:
DIRS: 'Sources Tests Samples/Common/Sources/CrashTriggers'
steps:
- uses: actions/checkout@v4
- name: Install clang-format-18
run: |
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 18
sudo apt-get install -y clang-format-18
- name: Check formatting
id: check_format
run: |
find $DIRS -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.m' -o -name '*.mm' | \
xargs -r clang-format-18 -style=file -n -Werror 2> clang_format_errors.log || true
- name: Apply formatting changes
if: failure()
run: |
find $DIRS -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.m' -o -name '*.mm' | \
xargs -r clang-format-18 -style=file -i
- name: Show formatting diff
if: failure()
run: |
git diff > clang_format_diff.patch
cat clang_format_diff.patch
- name: Annotate formatting issues
if: failure()
run: |
echo "::group::Formatting issues"
while IFS= read -r line
do
if [[ $line =~ ^diff\ --git\ a/(.*)\ b/.* ]]; then
file=${BASH_REMATCH[1]}
elif [[ $line =~ ^@@\ -([0-9]+),[0-9]+\ \+([0-9]+),[0-9]+\ @@ ]]; then
line=${BASH_REMATCH[2]}
elif [[ $line =~ ^\+\+\+\ b/.* ]]; then
continue
elif [[ $line =~ ^\-\-\-\ a/.* ]]; then
continue
elif [[ $line =~ ^\+(.*) ]]; then
echo "::error file=$file,line=$line::Added line: ${BASH_REMATCH[1]}"
elif [[ $line =~ ^\-(.*) ]]; then
echo "::warning file=$file,line=$line::Removed line: ${BASH_REMATCH[1]}"
fi
done < clang_format_diff.patch
echo "::endgroup::"
- name: Suggest formatting fixes
if: failure()
run: |
echo "##[error]Formatting issues found. Please run clang-format-18 on your code."