Skip to content

Commit

Permalink
Add github action to format code on push and the clang format file.
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriof7 committed Feb 4, 2025
1 parent 7e82b62 commit 7b630f5
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Language: Cpp
BasedOnStyle: Google
IndentWidth: 4
TabWidth: 4
UseTab: Never
AllowShortFunctionsOnASingleLine: Empty

SortIncludes: Never

NamespaceIndentation: None

BraceWrapping:
AfterNamespace: true
AfterFunction: true
AfterControlStatement: true
AfterClass: true
SplitEmptyFunction: false
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: false

AlignAfterOpenBracket: Align
AlignOperands: false
AccessModifierOffset: -4

BreakBeforeBraces: Custom

BinPackParameters: false
BinPackArguments: false
Cpp11BracedListStyle: false

PointerAlignment: Left

SpacesInContainerLiterals: true
SpaceBeforeParens: Never
SpaceBeforeRangeBasedForLoopColon: false

AllowAllParametersOfDeclarationOnNextLine: false

CommentPragmas: '^!|^@'
SpaceAfterTemplateKeyword: false

ContinuationIndentWidth: 8
ConstructorInitializerIndentWidth: 4
BreakConstructorInitializers: AfterColon
ConstructorInitializerAllOnOneLineOrOnePerLine: false

SpaceBeforeCtorInitializerColon: false
SpacesInAngles: true
SpacesInParentheses: true
SpacesInSquareBrackets: true
SpaceInEmptyParentheses: true
SpaceInEmptyBlock: true

ColumnLimit: 140
90 changes: 90 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Clang-Format Check and Fix

on:
push:
branches:
- develop
- feature/github_action

jobs:
format-check:
if: ${{ !contains(github.event.head_commit.message, 'Apply clang-format') }}
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
token: ${{ secrets.GH_PAT }}
fetch-depth: 1
persist-credentials: true

- name: Install Clang-Format
run: |
sudo apt-get update
sudo apt-get install -y clang-format
clang-format --version
- name: Ensure the previous commit is available
run: |
# Try to fetch the commit corresponding to github.event.before
git fetch origin ${{ github.event.before }} --depth=1
- name: Get list of changed files
id: changed-files
run: |
CHANGED_FILES=$(git diff --name-only "${{ github.event.before }}" "${{ github.sha }}" | grep -E "\.(cpp|h|c)$" || true)
echo "Changed files:"
echo "$CHANGED_FILES"
if [ -z "$CHANGED_FILES" ]; then
echo "No changed files detected."
echo "files=" >> "$GITHUB_OUTPUT"
else
# Use heredoc syntax to correctly handle multi-line output
echo "files<<EOF" >> "$GITHUB_OUTPUT"
echo "$CHANGED_FILES" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
fi
- name: Run Clang-Format on Changed Files
run: |
if [ -z "${{ steps.changed-files.outputs.files }}" ]; then
echo "No changed C/C++ files detected. Exiting."
else
echo "Running clang-format on the following files:"
echo "${{ steps.changed-files.outputs.files }}" | tr ' ' '\n'
# Using newline as the delimiter for xargs
echo "${{ steps.changed-files.outputs.files }}" | tr ' ' '\n' | xargs -r clang-format -style=file -i
fi
- name: Check for Formatting Differences
id: check_diff
run: |
# Instead of exiting with non-zero, check if there is any diff and store that as an output.
if git diff --quiet; then
echo "No formatting differences."
echo "changed=false" >> "$GITHUB_OUTPUT"
else
echo "Formatting differences detected."
echo "changed=true" >> "$GITHUB_OUTPUT"
fi
- name: Auto-commit Formatting Changes
if: steps.check_diff.outputs.changed == 'true'
run: |
git config --local user.name "github-actions[bot]"
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git add .
# Commit only if there is something to commit.
git commit -m "Apply clang-format" || exit 0
branch=${GITHUB_REF#refs/heads/}
git push origin HEAD:"$branch"
- name: Report Success or Failure
run: |
if [ "${{ steps.check_diff.outputs.changed }}" == "true" ]; then
echo "Formatting issues were detected and fixed."
else
echo "Code is properly formatted."
fi

0 comments on commit 7b630f5

Please sign in to comment.