ci: separate whitespace check script

The `check-whitespace` CI job is only available as a GitHub action. To help enable this job with other CI providers, first separate the logic performing the whitespace check into its own script. In subsequent commits, this script is further generalized allowing its reuse. Helped-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Justin Tobler <jltobler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Justin Tobler committed May 3, 2024 at 12:21 UTC 66820fb7bfc47e0356dd8c27af3abd2a27daafcd
2 files changed +78 -64
.github/workflows/check-whitespace.yml
+4 -64
@@ -26,67 +26,7 @@ jobs:
26 - name: git log --check
27 id: check_out
28 run: |
29 - baseSha=${{github.event.pull_request.base.sha}}
30 - problems=()
31 - commit=
32 - commitText=
33 - commitTextmd=
34 - goodParent=
35 - while read dash sha etc
36 - do
37 - case "${dash}" in
38 - "---") # Line contains commit information.
39 - if test -z "${goodParent}"
40 - then
41 - # Assume the commit has no whitespace errors until detected otherwise.
42 - goodParent=${sha}
43 - fi
44 - commit="${sha}"
45 - commitText="${sha} ${etc}"
46 - commitTextmd="[${sha}](https://github.com/${{ github.repository }}/commit/${sha}) ${etc}"
47 - ;;
48 - "")
49 - ;;
50 - *) # Line contains whitespace error information for current commit.
51 - if test -n "${goodParent}"
52 - then
53 - problems+=("1) --- ${commitTextmd}")
54 - echo ""
55 - echo "--- ${commitText}"
56 - goodParent=
57 - fi
58 - case "${dash}" in
59 - *:[1-9]*:) # contains file and line number information
60 - dashend=${dash#*:}
61 - problems+=("[${dash}](https://github.com/${{ github.repository }}/blob/${commit}/${dash%%:*}#L${dashend%:}) ${sha} ${etc}")
62 - ;;
63 - *)
64 - problems+=("\`${dash} ${sha} ${etc}\`")
65 - ;;
66 - esac
67 - echo "${dash} ${sha} ${etc}"
68 - ;;
69 - esac
70 - done <<< $(git log --check --pretty=format:"---% h% s" ${baseSha}..)
71 -
72 - if test ${#problems[*]} -gt 0
73 - then
74 - if test -z "${goodParent}"
75 - then
76 - goodParent=${baseSha: 0:7}
77 - fi
78 - echo "🛑 Please review the Summary output for further information."
79 - echo "### :x: A whitespace issue was found in one or more of the commits." >$GITHUB_STEP_SUMMARY
80 - echo "" >>$GITHUB_STEP_SUMMARY
81 - echo "Run these commands to correct the problem:" >>$GITHUB_STEP_SUMMARY
82 - echo "1. \`git rebase --whitespace=fix ${goodParent}\`" >>$GITHUB_STEP_SUMMARY
83 - echo "1. \`git push --force\`" >>$GITHUB_STEP_SUMMARY
84 - echo " " >>$GITHUB_STEP_SUMMARY
85 - echo "Errors:" >>$GITHUB_STEP_SUMMARY
86 - for i in "${problems[@]}"
87 - do
88 - echo "${i}" >>$GITHUB_STEP_SUMMARY
89 - done
90 -
91 - exit 2
92 - fi
29 + ./ci/check-whitespace.sh \
30 + "${{github.event.pull_request.base.sha}}" \
31 + "$GITHUB_STEP_SUMMARY" \
32 + "https://github.com/${{github.repository}}"
ci/check-whitespace.sh new
+74
@@ -0,0 +1,74 @@
1 +#!/usr/bin/env bash
2 +
3 +baseCommit=$1
4 +outputFile=$2
5 +url=$3
6 +
7 +problems=()
8 +commit=
9 +commitText=
10 +commitTextmd=
11 +goodParent=
12 +
13 +while read dash sha etc
14 +do
15 + case "${dash}" in
16 + "---") # Line contains commit information.
17 + if test -z "${goodParent}"
18 + then
19 + # Assume the commit has no whitespace errors until detected otherwise.
20 + goodParent=${sha}
21 + fi
22 +
23 + commit="${sha}"
24 + commitText="${sha} ${etc}"
25 + commitTextmd="[${sha}](${url}/commit/${sha}) ${etc}"
26 + ;;
27 + "")
28 + ;;
29 + *) # Line contains whitespace error information for current commit.
30 + if test -n "${goodParent}"
31 + then
32 + problems+=("1) --- ${commitTextmd}")
33 + echo ""
34 + echo "--- ${commitText}"
35 + goodParent=
36 + fi
37 +
38 + case "${dash}" in
39 + *:[1-9]*:) # contains file and line number information
40 + dashend=${dash#*:}
41 + problems+=("[${dash}](${url}/blob/${commit}/${dash%%:*}#L${dashend%:}) ${sha} ${etc}")
42 + ;;
43 + *)
44 + problems+=("\`${dash} ${sha} ${etc}\`")
45 + ;;
46 + esac
47 + echo "${dash} ${sha} ${etc}"
48 + ;;
49 + esac
50 +done <<< "$(git log --check --pretty=format:"---% h% s" "${baseCommit}"..)"
51 +
52 +if test ${#problems[*]} -gt 0
53 +then
54 + if test -z "${goodParent}"
55 + then
56 + goodParent=${baseCommit: 0:7}
57 + fi
58 +
59 + echo "🛑 Please review the Summary output for further information."
60 + echo "### :x: A whitespace issue was found in one or more of the commits." >"$outputFile"
61 + echo "" >>"$outputFile"
62 + echo "Run these commands to correct the problem:" >>"$outputFile"
63 + echo "1. \`git rebase --whitespace=fix ${goodParent}\`" >>"$outputFile"
64 + echo "1. \`git push --force\`" >>"$outputFile"
65 + echo " " >>"$outputFile"
66 + echo "Errors:" >>"$outputFile"
67 +
68 + for i in "${problems[@]}"
69 + do
70 + echo "${i}" >>"$outputFile"
71 + done
72 +
73 + exit 2
74 +fi