Skip to content

Commit 3fc99d0

Browse files
committed
Merge branch 'jt/port-ci-whitespace-check-to-gitlab'
The "whitespace check" task that was enabled for GitHub Actions CI has been ported to GitLab CI. * jt/port-ci-whitespace-check-to-gitlab: gitlab-ci: add whitespace error check ci: make the whitespace report optional ci: separate whitespace check script github-ci: fix link to whitespace error ci: pre-collapse GitLab CI sections
2 parents 60521f6 + 8f19e82 commit 3fc99d0

File tree

4 files changed

+109
-64
lines changed

4 files changed

+109
-64
lines changed

.github/workflows/check-whitespace.yml

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -26,66 +26,7 @@ jobs:
2626
- name: git log --check
2727
id: check_out
2828
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-
"---")
39-
if test -z "${commit}"
40-
then
41-
goodparent=${sha}
42-
fi
43-
commit="${sha}"
44-
commitText="${sha} ${etc}"
45-
commitTextmd="[${sha}](https://github.com/${{ github.repository }}/commit/${sha}) ${etc}"
46-
;;
47-
"")
48-
;;
49-
*)
50-
if test -n "${commit}"
51-
then
52-
problems+=("1) --- ${commitTextmd}")
53-
echo ""
54-
echo "--- ${commitText}"
55-
commit=
56-
fi
57-
case "${dash}" in
58-
*:[1-9]*:) # contains file and line number information
59-
dashend=${dash#*:}
60-
problems+=("[${dash}](https://github.com/${{ github.repository }}/blob/${{github.event.pull_request.head.ref}}/${dash%%:*}#L${dashend%:}) ${sha} ${etc}")
61-
;;
62-
*)
63-
problems+=("\`${dash} ${sha} ${etc}\`")
64-
;;
65-
esac
66-
echo "${dash} ${sha} ${etc}"
67-
;;
68-
esac
69-
done <<< $(git log --check --pretty=format:"---% h% s" ${baseSha}..)
70-
71-
if test ${#problems[*]} -gt 0
72-
then
73-
if test -z "${commit}"
74-
then
75-
goodparent=${baseSha: 0:7}
76-
fi
77-
echo "🛑 Please review the Summary output for further information."
78-
echo "### :x: A whitespace issue was found in one or more of the commits." >$GITHUB_STEP_SUMMARY
79-
echo "" >>$GITHUB_STEP_SUMMARY
80-
echo "Run these commands to correct the problem:" >>$GITHUB_STEP_SUMMARY
81-
echo "1. \`git rebase --whitespace=fix ${goodparent}\`" >>$GITHUB_STEP_SUMMARY
82-
echo "1. \`git push --force\`" >>$GITHUB_STEP_SUMMARY
83-
echo " " >>$GITHUB_STEP_SUMMARY
84-
echo "Errors:" >>$GITHUB_STEP_SUMMARY
85-
for i in "${problems[@]}"
86-
do
87-
echo "${i}" >>$GITHUB_STEP_SUMMARY
88-
done
89-
90-
exit 2
91-
fi
29+
./ci/check-whitespace.sh \
30+
"${{github.event.pull_request.base.sha}}" \
31+
"$GITHUB_STEP_SUMMARY" \
32+
"https://github.com/${{github.repository}}"

.gitlab-ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,12 @@ static-analysis:
113113
script:
114114
- ./ci/run-static-analysis.sh
115115
- ./ci/check-directional-formatting.bash
116+
117+
check-whitespace:
118+
image: ubuntu:latest
119+
before_script:
120+
- ./ci/install-dependencies.sh
121+
script:
122+
- ./ci/check-whitespace.sh "$CI_MERGE_REQUEST_TARGET_BRANCH_SHA"
123+
rules:
124+
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'

ci/check-whitespace.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Check that commits after a specified point do not contain new or modified
4+
# lines with whitespace errors. An optional formatted summary can be generated
5+
# by providing an output file path and url as additional arguments.
6+
#
7+
8+
baseCommit=$1
9+
outputFile=$2
10+
url=$3
11+
12+
if test "$#" -ne 1 && test "$#" -ne 3
13+
then
14+
echo "USAGE: $0 <BASE_COMMIT> [<OUTPUT_FILE> <URL>]"
15+
exit 1
16+
fi
17+
18+
problems=()
19+
commit=
20+
commitText=
21+
commitTextmd=
22+
goodParent=
23+
24+
while read dash sha etc
25+
do
26+
case "${dash}" in
27+
"---") # Line contains commit information.
28+
if test -z "${goodParent}"
29+
then
30+
# Assume the commit has no whitespace errors until detected otherwise.
31+
goodParent=${sha}
32+
fi
33+
34+
commit="${sha}"
35+
commitText="${sha} ${etc}"
36+
commitTextmd="[${sha}](${url}/commit/${sha}) ${etc}"
37+
;;
38+
"")
39+
;;
40+
*) # Line contains whitespace error information for current commit.
41+
if test -n "${goodParent}"
42+
then
43+
problems+=("1) --- ${commitTextmd}")
44+
echo ""
45+
echo "--- ${commitText}"
46+
goodParent=
47+
fi
48+
49+
case "${dash}" in
50+
*:[1-9]*:) # contains file and line number information
51+
dashend=${dash#*:}
52+
problems+=("[${dash}](${url}/blob/${commit}/${dash%%:*}#L${dashend%:}) ${sha} ${etc}")
53+
;;
54+
*)
55+
problems+=("\`${dash} ${sha} ${etc}\`")
56+
;;
57+
esac
58+
echo "${dash} ${sha} ${etc}"
59+
;;
60+
esac
61+
done <<< "$(git log --check --pretty=format:"---% h% s" "${baseCommit}"..)"
62+
63+
if test ${#problems[*]} -gt 0
64+
then
65+
if test -z "${goodParent}"
66+
then
67+
goodParent=${baseCommit: 0:7}
68+
fi
69+
70+
echo "A whitespace issue was found in onen of more of the commits."
71+
echo "Run the following command to resolve whitespace issues:"
72+
echo "git rebase --whitespace=fix ${goodParent}"
73+
74+
# If target output file is provided, write formatted output.
75+
if test -n "$outputFile"
76+
then
77+
echo "🛑 Please review the Summary output for further information."
78+
(
79+
echo "### :x: A whitespace issue was found in one or more of the commits."
80+
echo ""
81+
echo "Run these commands to correct the problem:"
82+
echo "1. \`git rebase --whitespace=fix ${goodParent}\`"
83+
echo "1. \`git push --force\`"
84+
echo ""
85+
echo "Errors:"
86+
87+
for i in "${problems[@]}"
88+
do
89+
echo "${i}"
90+
done
91+
) >"$outputFile"
92+
fi
93+
94+
exit 2
95+
fi

ci/lib.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ elif test true = "$GITLAB_CI"
1818
then
1919
begin_group () {
2020
need_to_end_group=t
21-
printf "\e[0Ksection_start:$(date +%s):$(echo "$1" | tr ' ' _)\r\e[0K$1\n"
21+
printf "\e[0Ksection_start:$(date +%s):$(echo "$1" | tr ' ' _)[collapsed=true]\r\e[0K$1\n"
2222
trap "end_group '$1'" EXIT
2323
set -x
2424
}

0 commit comments

Comments
 (0)