1
0
mirror of https://github.com/git/git.git synced 2024-09-08 03:50:44 +02:00

Merge branch 'kn/ci-clang-format' into next

A CI job that use clang-format to check coding style issues in new
code has been added.

* kn/ci-clang-format:
  ci/style-check: add `RemoveBracesLLVM` in CI job
  check-whitespace: detect if no base_commit is provided
  ci: run style check on GitHub and GitLab
  clang-format: formalize some of the spacing rules
  clang-format: avoid spacing around bitfield colon
  clang-format: indent preprocessor directives after hash
This commit is contained in:
Junio C Hamano 2024-07-23 11:05:31 -07:00
commit 3aca45d08a
6 changed files with 120 additions and 3 deletions

View File

@ -72,6 +72,10 @@ AlwaysBreakAfterReturnType: None
BinPackArguments: true
BinPackParameters: true
# Add no space around the bit field
# unsigned bf:2;
BitFieldColonSpacing: None
# Attach braces to surrounding context except break before braces on function
# definitions.
# void foo()
@ -96,6 +100,12 @@ BreakStringLiterals: false
# Switch statement body is always indented one level more than case labels.
IndentCaseLabels: false
# Indents directives before the hash.
# #if FOO
# # include <foo>
# #endif
IndentPPDirectives: AfterHash
# Don't indent a function definition or declaration if it is wrapped after the
# type
IndentWrappedFunctionNames: false
@ -108,11 +118,18 @@ PointerAlignment: Right
# x = (int32)y; not x = (int32) y;
SpaceAfterCStyleCast: false
# No space is inserted after the logical not operator
SpaceAfterLogicalNot: false
# Insert spaces before and after assignment operators
# int a = 5; not int a=5;
# a += 42; a+=42;
SpaceBeforeAssignmentOperators: true
# Spaces will be removed before case colon.
# case 1: break; not case 1 : break;
SpaceBeforeCaseColon: false
# Put a space before opening parentheses only after control statement keywords.
# void f() {
# if (true) {
@ -124,6 +141,14 @@ SpaceBeforeParens: ControlStatements
# Don't insert spaces inside empty '()'
SpaceInEmptyParentheses: false
# No space before first '[' in arrays
# int a[5][5]; not int a [5][5];
SpaceBeforeSquareBrackets: false
# No space will be inserted into {}
# while (true) {} not while (true) { }
SpaceInEmptyBlock: false
# The number of spaces before trailing line comments (// - comments).
# This does not affect trailing block comments (/* - comments).
SpacesBeforeTrailingComments: 1

34
.github/workflows/check-style.yml vendored Normal file
View File

@ -0,0 +1,34 @@
name: check-style
# Get the repository with all commits to ensure that we can analyze
# all of the commits contributed via the Pull Request.
on:
pull_request:
types: [opened, synchronize]
# Avoid unnecessary builds. Unlike the main CI jobs, these are not
# ci-configurable (but could be).
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
check-style:
env:
CC: clang
jobname: ClangFormat
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- run: ci/install-dependencies.sh
- name: git clang-format
continue-on-error: true
id: check_out
run: |
./ci/run-style-check.sh \
"${{github.event.pull_request.base.sha}}"

View File

@ -118,8 +118,31 @@ check-whitespace:
image: ubuntu:latest
before_script:
- ./ci/install-dependencies.sh
# Since $CI_MERGE_REQUEST_TARGET_BRANCH_SHA is only defined for merged
# pipelines, we fallback to $CI_MERGE_REQUEST_DIFF_BASE_SHA, which should
# be defined in all pipelines.
script:
- ./ci/check-whitespace.sh "$CI_MERGE_REQUEST_TARGET_BRANCH_SHA"
- |
R=${CI_MERGE_REQUEST_TARGET_BRANCH_SHA-${CI_MERGE_REQUEST_DIFF_BASE_SHA:?}} || exit
./ci/check-whitespace.sh "$R"
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
check-style:
image: ubuntu:latest
allow_failure: true
variables:
CC: clang
jobname: ClangFormat
before_script:
- ./ci/install-dependencies.sh
# Since $CI_MERGE_REQUEST_TARGET_BRANCH_SHA is only defined for merged
# pipelines, we fallback to $CI_MERGE_REQUEST_DIFF_BASE_SHA, which should
# be defined in all pipelines.
script:
- |
R=${CI_MERGE_REQUEST_TARGET_BRANCH_SHA-${CI_MERGE_REQUEST_DIFF_BASE_SHA:?}} || exit
./ci/run-style-check.sh "$R"
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'

View File

@ -9,7 +9,7 @@ baseCommit=$1
outputFile=$2
url=$3
if test "$#" -ne 1 && test "$#" -ne 3
if test "$#" -ne 1 && test "$#" -ne 3 || test -z "$1"
then
echo "USAGE: $0 <BASE_COMMIT> [<OUTPUT_FILE> <URL>]"
exit 1
@ -21,6 +21,12 @@ commitText=
commitTextmd=
goodParent=
if ! git rev-parse --quiet --verify "${baseCommit}"
then
echo "Invalid <BASE_COMMIT> '${baseCommit}'"
exit 1
fi
while read dash sha etc
do
case "${dash}" in
@ -67,7 +73,7 @@ then
goodParent=${baseCommit: 0:7}
fi
echo "A whitespace issue was found in onen of more of the commits."
echo "A whitespace issue was found in one or more of the commits."
echo "Run the following command to resolve whitespace issues:"
echo "git rebase --whitespace=fix ${goodParent}"

View File

@ -87,6 +87,10 @@ macos-*)
esac
case "$jobname" in
ClangFormat)
sudo apt-get -q update
sudo apt-get -q -y install clang-format
;;
StaticAnalysis)
sudo apt-get -q update
sudo apt-get -q -y install coccinelle libcurl4-openssl-dev libssl-dev \

25
ci/run-style-check.sh Executable file
View File

@ -0,0 +1,25 @@
#!/bin/sh
#
# Perform style check
#
baseCommit=$1
# Remove optional braces of control statements (if, else, for, and while)
# according to the LLVM coding style. This avoids braces on simple
# single-statement bodies of statements but keeps braces if one side of
# if/else if/.../else cascade has multi-statement body.
#
# As this rule comes with a warning [1], we want to experiment with it
# before adding it in-tree. since the CI job for the style check is allowed
# to fail, appending the rule here allows us to validate its efficacy.
# While also ensuring that end-users are not affected directly.
#
# [1]: https://clang.llvm.org/docs/ClangFormatStyleOptions.html#removebracesllvm
{
cat .clang-format
echo "RemoveBracesLLVM: true"
} >/tmp/clang-format-rules
git clang-format --style=file:/tmp/clang-format-rules \
--diff --extensions c,h "$baseCommit"