diff --git a/.clang-format b/.clang-format index e7f2f7fce4..c22f5aa42d 100644 --- a/.clang-format +++ b/.clang-format @@ -1,147 +1,12 @@ --- -Language: Cpp +Language: Cpp +BasedOnStyle: Google AccessModifierOffset: -3 -AlignAfterOpenBracket: Align -AlignConsecutiveMacros: true -AlignConsecutiveAssignments: false -AlignConsecutiveBitFields: false -AlignConsecutiveDeclarations: false -AlignEscapedNewlines: Right -AlignOperands: Align -AlignTrailingComments: true -AllowAllArgumentsOnNextLine: true -AllowAllConstructorInitializersOnNextLine: true -AllowAllParametersOfDeclarationOnNextLine: true -AllowShortEnumsOnASingleLine: true -AllowShortBlocksOnASingleLine: Never -AllowShortCaseLabelsOnASingleLine: false -AllowShortFunctionsOnASingleLine: All -AllowShortLambdasOnASingleLine: All -AllowShortIfStatementsOnASingleLine: Never -AllowShortLoopsOnASingleLine: false -AlwaysBreakAfterDefinitionReturnType: None -AlwaysBreakAfterReturnType: None -AlwaysBreakBeforeMultilineStrings: false -AlwaysBreakTemplateDeclarations: MultiLine -BinPackArguments: true -BinPackParameters: true -BraceWrapping: - AfterCaseLabel: false - AfterClass: false - AfterControlStatement: Never - AfterEnum: false - AfterFunction: false - AfterNamespace: false - AfterObjCDeclaration: false - AfterStruct: false - AfterUnion: false - AfterExternBlock: false - BeforeCatch: false - BeforeElse: false - BeforeLambdaBody: false - BeforeWhile: false - IndentBraces: false - SplitEmptyFunction: true - SplitEmptyRecord: true - SplitEmptyNamespace: true -BreakBeforeBinaryOperators: None -BreakBeforeBraces: Attach -BreakBeforeInheritanceComma: false -BreakInheritanceList: BeforeColon -BreakBeforeTernaryOperators: true -BreakConstructorInitializersBeforeComma: false -BreakConstructorInitializers: BeforeColon -BreakAfterJavaFieldAnnotations: false -BreakStringLiterals: true -ColumnLimit: 80 -CommentPragmas: '^ IWYU pragma:' -CompactNamespaces: false -ConstructorInitializerAllOnOneLineOrOnePerLine: false -ConstructorInitializerIndentWidth: 4 -ContinuationIndentWidth: 4 -Cpp11BracedListStyle: true -DeriveLineEnding: true DerivePointerAlignment: false -DisableFormat: false -ExperimentalAutoDetectBinPacking: false -FixNamespaceComments: true -ForEachMacros: - - foreach - - Q_FOREACH - - BOOST_FOREACH -IncludeBlocks: Preserve -IncludeCategories: - - Regex: '^"(llvm|llvm-c|clang|clang-c)/' - Priority: 2 - SortPriority: 0 - - Regex: '^(<|"(gtest|gmock|isl|json)/)' - Priority: 3 - SortPriority: 0 - - Regex: '.*' - Priority: 1 - SortPriority: 0 -IncludeIsMainRegex: '(Test)?$' -IncludeIsMainSourceRegex: '' -IndentCaseLabels: false -IndentCaseBlocks: false -IndentGotoLabels: true -IndentPPDirectives: None -IndentExternBlock: AfterExternBlock IndentWidth: 4 -IndentWrappedFunctionNames: false -InsertTrailingCommas: None -JavaScriptQuotes: Leave -JavaScriptWrapImports: true -KeepEmptyLinesAtTheStartOfBlocks: true -MacroBlockBegin: '' -MacroBlockEnd: '' -MaxEmptyLinesToKeep: 2 -NamespaceIndentation: None -ObjCBinPackProtocolList: Auto -ObjCBlockIndentWidth: 2 -ObjCBreakBeforeNestedBlockParam: true -ObjCSpaceAfterProperty: false -ObjCSpaceBeforeProtocolList: true -PenaltyBreakAssignment: 2 -PenaltyBreakBeforeFirstCallParameter: 19 -PenaltyBreakComment: 300 -PenaltyBreakFirstLessLess: 120 -PenaltyBreakString: 1000 -PenaltyBreakTemplateDeclaration: 10 -PenaltyExcessCharacter: 1000000 -PenaltyReturnTypeOnItsOwnLine: 60 -PointerAlignment: Right -ReflowComments: true -SortIncludes: false -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: 2 -SpacesInAngles: false -SpacesInConditionalStatement: false -SpacesInContainerLiterals: true -SpacesInCStyleCastParentheses: false -SpacesInParentheses: false -SpacesInSquareBrackets: false -SpaceBeforeSquareBrackets: false -Standard: Cpp11 -StatementMacros: - - Q_UNUSED - - QT_REQUIRE_VERSION -TabWidth: 4 -UseCRLF: false -UseTab: Never -WhitespaceSensitiveMacros: - - STRINGIZE - - PP_STRINGIZE - - BOOST_PP_STRINGIZE +--- +Language: Proto +BasedOnStyle: Google +IndentWidth: 4 +ColumnLimit: 120 ... diff --git a/.github/workflows/clang-format.yaml b/.github/workflows/clang-format.yaml new file mode 100644 index 0000000000..d4d373c519 --- /dev/null +++ b/.github/workflows/clang-format.yaml @@ -0,0 +1,30 @@ +name: Code Formatter + +# run on pull requests to develop +on: + pull_request + +jobs: + format: + name: Clang Formatter + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + # fetch everything to be able to compare with any ref + fetch-depth: 0 + + - name: Check + env: + LANG: "C.UTF-8" + LC_ALL: "C.UTF-8" + LANGUAGE: "C.UTF-8" + run: | + sudo apt-get install -y clang-format-14 python3 git + diff_output_file=$(mktemp) + git --no-pager diff -U0 --diff-filter=ACMRT ${{github.event.pull_request.base.sha}}...${{ github.event.pull_request.head.sha }} | clang-format-diff-14 -p1 2>&1 | tee $diff_output_file + if [ -s $diff_output_file ]; then + exit 1 + else + exit 0 + fi diff --git a/Makefile b/Makefile index 7016723580..f986bb4777 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ tag?= "curvebs:unknown" case?= "*" os?= "debian11" ci?=0 - +commit_id="HEAD^" define help_msg ## list Usage: @@ -88,3 +88,5 @@ test: docker: @bash util/docker.sh --os=$(os) --ci=$(ci) +format: + @bash util/format.sh $(commit_id) diff --git a/developers_guide.md b/developers_guide.md index a5ece6ba46..0ba0136263 100644 --- a/developers_guide.md +++ b/developers_guide.md @@ -115,6 +115,21 @@ Curve Ci use ```cpplint``` check what your changed. $ cpplint --filter=-build/c++11 --quiet --recursive your_path ``` +- use clang-format-diff to check submitted code + ``` + $ make format + # Or + $ make format commit_id=$(commit_id) # commit_id is a sha of a commit, default HEAD^ + ``` +> Clang-format understands also special comments that switch formatting in a delimited range. The code between a comment `// clang-format off` or `/* clang-format off */` up to a comment `// clang-format on` or `/* clang-format on */` will not be formatted. + > ```cpp + > int formatted_code; + > // clang-format off + > void unformatted_code ; + > // clang-format on + > void formatted_code_again; + > ``` + For PR we have the following requirements: - The Curve coding standard strictly follows the [Google C++ Open Source Project Coding Guide](https://google.github.io/styleguide/cppguide.html), but we use 4 spaces to indent, Clang-format will more helpful for you. Of course, CI will check what your changed. diff --git a/developers_guide_cn.md b/developers_guide_cn.md index 1c36a8c0e3..62e768ffce 100644 --- a/developers_guide_cn.md +++ b/developers_guide_cn.md @@ -70,6 +70,20 @@ Curve CI 使用```cpplint```检查更改的代码, ```bash $ cpplint --filter=-build/c++11 --quiet --recursive your_path ``` +- 本地执行clang-format-diff对提交部分代码进行检查: + ``` + $ make format + # or + $ make format commit_id=$(commit_id) # commit_id为某一个提交的sha,默认commit_id为HEAD^ + ``` + > 如果需要对部分代码禁用格式检查,可以在开始处添加 `// clang-format off` 或 `/* clang-format off */` 注释,结尾处添加 `// clang-format on` 或 `/* clang-format on */` 注释 + > ```cpp + > int formatted_code; + > // clang-format off + > void unformatted_code ; + > // clang-format on + > void formatted_code_again; + > ``` 对于 PR 我们有如下要求: diff --git a/util/format.sh b/util/format.sh new file mode 100644 index 0000000000..81432e6039 --- /dev/null +++ b/util/format.sh @@ -0,0 +1,2 @@ +git --no-pager diff -U0 --diff-filter=ACMRT $1 HEAD | clang-format-diff-14 -p1 -i -style file +