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 <gitster@pobox.com> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Jul 23, 2024 at 10:21 UTC bce7e52d4e70a8d8d824596654bac85545bf9d07
4 files changed +64
.github/workflows/check-style.yml new
+34
@@ -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
+18
@@ -123,6 +123,24 @@ check-whitespace:
123 rules:
124 - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
125
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 +
144 documentation:
145 image: ubuntu:latest
146 variables:
ci/install-dependencies.sh
+4
@@ -87,6 +87,10 @@ macos-*)
87 esac
88
89 case "$jobname" in
90 +ClangFormat)
91 + sudo apt-get -q update
92 + sudo apt-get -q -y install clang-format
93 + ;;
94 StaticAnalysis)
95 sudo apt-get -q update
96 sudo apt-get -q -y install coccinelle libcurl4-openssl-dev libssl-dev \
ci/run-style-check.sh new
+8
@@ -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"