1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-10-28 12:59:41 +01:00
git/ci/check-whitespace.sh
Karthik Nayak 30c4f7e350 check-whitespace: detect if no base_commit is provided
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>
2024-07-23 09:56:50 -07:00

101 lines
2.3 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# Check that commits after a specified point do not contain new or modified
# lines with whitespace errors. An optional formatted summary can be generated
# by providing an output file path and url as additional arguments.
#
baseCommit=$1
outputFile=$2
url=$3
if test "$#" -ne 1 && test "$#" -ne 3 || test -z "$1"
then
echo "USAGE: $0 <BASE_COMMIT> [<OUTPUT_FILE> <URL>]"
exit 1
fi
problems=()
commit=
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
"---") # Line contains commit information.
if test -z "${goodParent}"
then
# Assume the commit has no whitespace errors until detected otherwise.
goodParent=${sha}
fi
commit="${sha}"
commitText="${sha} ${etc}"
commitTextmd="[${sha}](${url}/commit/${sha}) ${etc}"
;;
"")
;;
*) # Line contains whitespace error information for current commit.
if test -n "${goodParent}"
then
problems+=("1) --- ${commitTextmd}")
echo ""
echo "--- ${commitText}"
goodParent=
fi
case "${dash}" in
*:[1-9]*:) # contains file and line number information
dashend=${dash#*:}
problems+=("[${dash}](${url}/blob/${commit}/${dash%%:*}#L${dashend%:}) ${sha} ${etc}")
;;
*)
problems+=("\`${dash} ${sha} ${etc}\`")
;;
esac
echo "${dash} ${sha} ${etc}"
;;
esac
done <<< "$(git log --check --pretty=format:"---% h% s" "${baseCommit}"..)"
if test ${#problems[*]} -gt 0
then
if test -z "${goodParent}"
then
goodParent=${baseCommit: 0:7}
fi
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}"
# If target output file is provided, write formatted output.
if test -n "$outputFile"
then
echo "🛑 Please review the Summary output for further information."
(
echo "### :x: A whitespace issue was found in one or more of the commits."
echo ""
echo "Run these commands to correct the problem:"
echo "1. \`git rebase --whitespace=fix ${goodParent}\`"
echo "1. \`git push --force\`"
echo ""
echo "Errors:"
for i in "${problems[@]}"
do
echo "${i}"
done
) >"$outputFile"
fi
exit 2
fi