forked from tudat-team/tudat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add github action to format code on push and the clang format file.
- Loading branch information
Showing
2 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |