From ca4eb0f97cbf6fab4a35f153dfa066930ae24e26 Mon Sep 17 00:00:00 2001 From: Mateusz Mazur Date: Sun, 10 Nov 2024 13:42:06 +0100 Subject: [PATCH] Clang format in the CI Signed-off-by: Mateusz Mazur --- .clang-format | 4 ++-- .github/workflows/clang-format.yml | 23 +++++++++++++++++++++++ reformat-all.sh | 15 +++++++++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/clang-format.yml diff --git a/.clang-format b/.clang-format index 7295fd4..2552fc3 100644 --- a/.clang-format +++ b/.clang-format @@ -5,7 +5,7 @@ AccessModifierOffset: -2 AlignAfterOpenBracket: Align AlignArrayOfStructures: None AlignConsecutiveAssignments: - Enabled: true + Enabled: true AcrossEmptyLines: false AcrossComments: false AlignCompound: false @@ -26,7 +26,7 @@ AlignConsecutiveDeclarations: AlignFunctionPointers: false PadOperators: false AlignConsecutiveMacros: - Enabled: false + Enabled: false AcrossEmptyLines: false AcrossComments: false AlignCompound: false diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 0000000..fe9b92e --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -0,0 +1,23 @@ +name: Clang formatting checker + +on: + push: + branches: ["master"] + pull_request: + branches: ["master"] + +jobs: + build: + runs-on: ubuntu-latest + container: + image: silkeh/clang # https://hub.docker.com/r/silkeh/clang + env: + DRY: "TRUE" + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 1 + + - name: Checking formatting + run: DRY=TRUE bash reformat-all.sh diff --git a/reformat-all.sh b/reformat-all.sh index bb42286..b0fe28e 100644 --- a/reformat-all.sh +++ b/reformat-all.sh @@ -1,3 +1,14 @@ -#!/bin/sh +#!/usr/bin/env bash -find . -name '*.cpp' -o -name '*.hpp' | xargs clang-format -i -style=file +ALL_FORMATABLE_FILES=$(find src include tests example -name '*.cpp' -o -name '*.hpp') +CLANG_FORMAT_ARGS="-style=file" + +echo "*** Modbus Clang Format Wrapper **" +clang-format --version + +if [[ "$DRY" = "TRUE" ]]; then + diff_count=`clang-format --dry-run $CLANG_FORMAT_ARGS $ALL_FORMATABLE_FILES 2>&1 | wc -l` + exit $(($diff_count > 0)) +else + clang-format -i --verbose $CLANG_FORMAT_ARGS $ALL_FORMATABLE_FILES +fi