Skip to content

Commit

Permalink
Add clang-format github workflow (#235)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #235

There have been quite a few imported PRs which end up triggering the clang format linter on sandcastle. The actual error is only visible to FB employees, while the original PR author can only see that a FB-internal CI job failed, and that only happens after the PR has already been imported.

This causes a lot of churn since we have to go back and forth.

Add a github workflow which replicates the sandcastle CLANGFORMAT linter. `.clang-format` file was copied from fbsource, which is the one that we use since we didn't have any fboss specific file which overrides.

Reviewed By: alandau

Differential Revision: D62764920

fbshipit-source-id: cc9b3b9c41cd6744e1f49039b4a18979ab9fbdc8
  • Loading branch information
Scott8440 authored and facebook-github-bot committed Sep 16, 2024
1 parent c88dbc8 commit 7673a73
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: ClangFormat Lint

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
clang-format-check:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Install clang-format
run: sudo apt-get install -y clang-format

- name: Get changed files
id: changed-files
run: |
if [ ${{ github.event_name }} == 'pull_request' ]; then
# For pull requests, compare against the base branch
echo "FILES=$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep -E '\.(cpp|h)$' | xargs)" >> $GITHUB_OUTPUT
else
# For pushes, compare against the parent commit
echo "FILES=$(git diff --name-only --diff-filter=ACMRT ${{ github.event.before }} ${{ github.sha }} | grep -E '\.(cpp|h)$' | xargs)" >> $GITHUB_OUTPUT
fi
- name: Run clang-format
run: |
for file in ${{ steps.changed-files.outputs.FILES }}; do
clang-format -style=file -i "$file"
done
git diff --exit-code
- name: Suggest changes if formatting is incorrect
if: failure()
run: |
echo "::error::Formatting issues found. Please run clang-format on your changes and commit the results."
git diff
114 changes: 114 additions & 0 deletions fboss/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# The primary clang-format config file.
# TODO(afuller): Set these settings when they aren't broken:
# - AllowShortBlocksOnASingleLine: Empty
---
AccessModifierOffset: -1
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveBitFields: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: DontAlign
AlignTrailingComments: false
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: false
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
FixNamespaceComments: true
ForEachMacros:
- FOR_EACH
- FOR_EACH_R
- FOR_EACH_RANGE
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^<.*\.h(pp)?>'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IndentCaseLabels: true
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentWidth: 2
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: Latest
TabWidth: 8
UseCRLF: false
UseTab: Never
...

0 comments on commit 7673a73

Please sign in to comment.