Add coding style check with clang-format to CI

This commit is contained in:
Martin Jäger
2023-01-13 19:32:18 +01:00
parent 5ea0d2657e
commit d88420feac
2 changed files with 47 additions and 8 deletions
+7 -8
View File
@@ -5,7 +5,7 @@ on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
container: zephyrprojectrtos/ci:v0.21.0
container: zephyrprojectrtos/ci:latest
env:
CMAKE_PREFIX_PATH: /opt/toolchains
steps:
@@ -13,22 +13,21 @@ jobs:
uses: actions/checkout@v2
with:
path: charge-controller-firmware
- name: Set up Python
uses: actions/setup-python@v1
fetch-depth: 0
- name: Initialize
working-directory: charge-controller-firmware
run: |
pip3 install -U west
west init -l .
west update --narrow
pip3 install -r ../zephyr/scripts/requirements-base.txt
- name: Trailing whitespace check
- name: Coding style check
working-directory: charge-controller-firmware
run: |
git diff --check `git rev-list --max-parents=0 HEAD`..HEAD
apt-get update
apt-get install -y clang-format-12 colordiff
git status
bash scripts/check-style.sh
- name: Build firmware
working-directory: charge-controller-firmware
+40
View File
@@ -0,0 +1,40 @@
#!/bin/bash
#
# Copyright (c) The Libre Solar Project Contributors
# SPDX-License-Identifier: Apache-2.0
DEFAULT_BRANCH="origin/main"
if [ -x "$(command -v clang-format-diff)" ]; then
CLANG_FORMAT_DIFF="clang-format-diff"
elif [ -x "$(command -v clang-format-diff-12)" ]; then
CLANG_FORMAT_DIFF="clang-format-diff-12"
elif [ -x "$(command -v /usr/share/clang/clang-format-diff.py)" ]; then
CLANG_FORMAT_DIFF="/usr/share/clang/clang-format-diff.py -v"
fi
echo "Style check for diff between branch $DEFAULT_BRANCH"
STYLE_ERROR=0
echo "Checking trailing whitespaces with git diff --check"
git diff --check --color=always $DEFAULT_BRANCH
if [[ $? -ne 0 ]]; then
STYLE_ERROR=1
else
echo "No trailing whitespaces found."
fi
echo "Checking coding style with clang-format"
# clang-format-diff returns 0 even for style differences, so we have to check the length of the
# response
CLANG_FORMAT_DIFF=`git diff $DEFAULT_BRANCH | $CLANG_FORMAT_DIFF -p1 | colordiff`
if [[ "$(echo -n $CLANG_FORMAT_DIFF | wc -c)" -ne 0 ]]; then
echo "${CLANG_FORMAT_DIFF}"
STYLE_ERROR=1
else
echo "Coding style valid."
fi
exit $STYLE_ERROR