Skip to content

Commit bce7e52

Browse files
KarthikNayakgitster
authored andcommitted
ci: run style check on GitHub and GitLab
We don't run style checks on our CI, even though we have a '.clang-format' setup in the repository. Let's add one, the job will validate only against the new commits added and will only run on merge requests. Since we're introducing it for the first time, let's allow this job to fail, so we can validate if this is useful and eventually enforce it. For GitHub, we allow the job to pass by adding 'continue-on-error: true' to the workflow. This means the job would show as passed, even if the style check failed. To know the status of the job, users have to manually check the logs. For GitLab, we allow the job to pass by adding 'allow_failure: true', to the job. Unlike GitHub, here the job will show as failed with a yellow warning symbol, but the pipeline would still show as passed. Also for GitLab, we use the 'CI_MERGE_REQUEST_TARGET_BRANCH_SHA' variable by default to obtain the base SHA of the merged pipeline (which is only available for merged pipelines [1]). Otherwise we use the 'CI_MERGE_REQUEST_DIFF_BASE_SHA' variable. [1]: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html#predefined-variables-for-merge-request-pipelines Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1993918 commit bce7e52

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

.github/workflows/check-style.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: check-style
2+
3+
# Get the repository with all commits to ensure that we can analyze
4+
# all of the commits contributed via the Pull Request.
5+
6+
on:
7+
pull_request:
8+
types: [opened, synchronize]
9+
10+
# Avoid unnecessary builds. Unlike the main CI jobs, these are not
11+
# ci-configurable (but could be).
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.ref }}
14+
cancel-in-progress: true
15+
16+
jobs:
17+
check-style:
18+
env:
19+
CC: clang
20+
jobname: ClangFormat
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v4
24+
with:
25+
fetch-depth: 0
26+
27+
- run: ci/install-dependencies.sh
28+
29+
- name: git clang-format
30+
continue-on-error: true
31+
id: check_out
32+
run: |
33+
./ci/run-style-check.sh \
34+
"${{github.event.pull_request.base.sha}}"

.gitlab-ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ check-whitespace:
123123
rules:
124124
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
125125

126+
check-style:
127+
image: ubuntu:latest
128+
allow_failure: true
129+
variables:
130+
CC: clang
131+
jobname: ClangFormat
132+
before_script:
133+
- ./ci/install-dependencies.sh
134+
# Since $CI_MERGE_REQUEST_TARGET_BRANCH_SHA is only defined for merged
135+
# pipelines, we fallback to $CI_MERGE_REQUEST_DIFF_BASE_SHA, which should
136+
# be defined in all pipelines.
137+
script:
138+
- |
139+
R=${CI_MERGE_REQUEST_TARGET_BRANCH_SHA-${CI_MERGE_REQUEST_DIFF_BASE_SHA:?}} || exit
140+
./ci/run-style-check.sh "$R"
141+
rules:
142+
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
143+
126144
documentation:
127145
image: ubuntu:latest
128146
variables:

ci/install-dependencies.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ macos-*)
8787
esac
8888

8989
case "$jobname" in
90+
ClangFormat)
91+
sudo apt-get -q update
92+
sudo apt-get -q -y install clang-format
93+
;;
9094
StaticAnalysis)
9195
sudo apt-get -q update
9296
sudo apt-get -q -y install coccinelle libcurl4-openssl-dev libssl-dev \

ci/run-style-check.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# Perform style check
4+
#
5+
6+
baseCommit=$1
7+
8+
git clang-format --style file --diff --extensions c,h "$baseCommit"

0 commit comments

Comments
 (0)