]> git.ipfire.org Git - thirdparty/git.git/commitdiff
check-whitespace: detect if no base_commit is provided
authorKarthik Nayak <karthik.188@gmail.com>
Tue, 23 Jul 2024 08:21:10 +0000 (10:21 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 23 Jul 2024 16:56:50 +0000 (09:56 -0700)
The 'check-whitespace' CI script exits gracefully if no base commit is
provided or if an invalid revision is provided. This is not good because
if a particular CI provides an incorrect base_commit, it would fail
successfully.

This is exactly the case with the GitLab CI. The CI is using the
"$CI_MERGE_REQUEST_TARGET_BRANCH_SHA" variable to get the base commit
SHA, but variable is only defined for _merged_ pipelines. So it is empty
for regular pipelines [1]. This should've failed the check-whitespace
job.

Let's fallback to 'CI_MERGE_REQUEST_DIFF_BASE_SHA' if
"CI_MERGE_REQUEST_TARGET_BRANCH_SHA" isn't available in GitLab CI,
similar to the previous commit. Let's also add a check for incorrect
base_commit in the 'check-whitespace.sh' script. While here, fix a small
typo too.

[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>
.gitlab-ci.yml
ci/check-whitespace.sh

index 2886f6e182eeb04a43b6ed1ce3f990d7979f3ea2..2589098eff72b36add85e210a0376c0a84834a44 100644 (file)
@@ -118,8 +118,13 @@ 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'
 
index db399097a52db65c3a8d00830e578049de96801d..c40804394cb079a1b011c11ed13f990b7a0c6316 100755 (executable)
@@ -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}"