Skip to content

Commit 616b7b2

Browse files
committed
[AOMP] Add --check-only option to aomp-shellcheck
This patch adds a --check-only option to aomp-shellcheck to avoid automatically altering files, e.g. when called via an automation system.
1 parent 553f6de commit 616b7b2

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

bin/aomp-shellcheck

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,20 @@ _fixByHandOpts=(--shell=bash --include="$(join ',' "${_fixByHand[@]}")")
3939

4040
_patchable=(SC2164 # Use cd ... || exit
4141
SC2006) # Use $(...) instead of legacy backticks
42-
_patchableOpts=(--shell=bash -f diff --include="$(join ',' "${_patchable[@]}")")
42+
_patchableOpts=(--shell=bash --include="$(join ',' "${_patchable[@]}")")
4343

4444
_alwaysExclude=(SC2016 # Use double quote instead of single
4545
SC2002 # Useless cat
4646
SC2004) # '$' on arithmetic vars
4747
_alwaysExcludeOpts=(--shell=bash --exclude="$(join ',' "${_alwaysExclude[@]}")")
4848

49+
_check_only=false
50+
51+
if [ $1 == "--check-only" ]; then
52+
_check_only=true
53+
shift
54+
fi
55+
4956
declare -a _shellfiles=( "$@" )
5057
_missing=0
5158
for _shellfile in "${_shellfiles[@]}"; do
@@ -66,12 +73,6 @@ if [ "$_shellcheck_bin" == "" ] ; then
6673
exit 1
6774
fi
6875

69-
_patchfile=/tmp/patchfile$$
70-
71-
if ! touch "$_patchfile"; then
72-
echo "ERROR: Could not create or update $_patchfile"
73-
exit 1
74-
fi
7576
echo
7677
echo "---- STEP 1 ---- Check for fixByHand fails -----"
7778
echo "$_shellcheck_bin -x ${_fixByHandOpts[*]} ${_shellfiles[*]}"
@@ -83,25 +84,54 @@ if [[ $_rc != 0 ]]; then
8384
echo "ERROR: shellcheck found errors that must be fixed by hand. rc=$_rc"
8485
exit 1
8586
fi
86-
echo
87-
echo "---- STEP 2 ---- Check and repair patchable fails -----"
88-
echo "$_shellcheck_bin -x ${_patchableOpts[*]} ${_shellfiles[*]} \>$_patchfile"
89-
$_shellcheck_bin -x "${_patchableOpts[@]}" "${_shellfiles[@]}" >$_patchfile
9087

91-
if ! patch -p1 --dry-run <"$_patchfile"; then
92-
echo "ERROR: Could not dryrun patch in $_patchfile to file ${_shellfiles[*]}"
93-
exit 1
88+
if $_check_only; then
89+
echo
90+
echo "---- STEP 2 ---- Check patchable fails -----"
91+
echo "$_shellcheck_bin -x ${_patchableOpts[@]} ${_shellfiles[@]}"
92+
$_shellcheck_bin -x "${_patchableOpts[@]}" "${_shellfiles[@]}"
93+
94+
echo
95+
echo "---- STEP 3 ---- (Skipping for --check-only)"
96+
else
97+
echo
98+
echo "---- STEP 2 ---- Check and repair patchable fails -----"
99+
100+
_patchfile=/tmp/patchfile$$
101+
102+
if ! touch "$_patchfile"; then
103+
echo "ERROR: Could not create or update $_patchfile"
104+
exit 1
105+
fi
106+
107+
echo "$_shellcheck_bin -x ${_patchableOpts[*]} -f diff ${_shellfiles[*]} \>$_patchfile"
108+
$_shellcheck_bin -x "${_patchableOpts[@]}" -f diff "${_shellfiles[@]}" >$_patchfile
109+
110+
if ! patch -p1 --dry-run <"$_patchfile"; then
111+
echo "ERROR: Could not dryrun patch in $_patchfile to file ${_shellfiles[*]}"
112+
exit 1
113+
fi
114+
echo
115+
echo "---- STEP 3 ---- Applying patch $_patchfile to ${_shellfiles[*]}"
116+
patch -p1 <"$_patchfile"
117+
echo "rm $_patchfile"
118+
rm "$_patchfile" # patch should work because of dryrun test above
94119
fi
95-
echo
96-
echo "---- STEP 3 ---- Applying patch $_patchfile to ${_shellfiles[*]}"
97-
patch -p1 <"$_patchfile"
98-
echo "rm $_patchfile"
99-
rm "$_patchfile" # patch should work because of dryrun test above
120+
100121
echo
101122
echo "---- STEP 4 ---- Test for codes not handled in $0"
102-
echo "$_shellcheck_bin -x ${_alwaysExcludeOpts[*]} ${_shellfiles[*]}"
103-
$_shellcheck_bin -x "${_alwaysExcludeOpts[@]}" "${_shellfiles[@]}"
104-
_rc=$?
123+
if $_check_only; then
124+
# A non-check-only run would avoid triggering the patchable checks again
125+
# in this invocation. Mask them out for a check-only run.
126+
_excludeOpts=(--shell=bash --exclude="$(join ',' "${_alwaysExclude[@]}" "${_patchable[@]}")")
127+
echo "$_shellcheck_bin -x ${_excludeOpts[*]} ${_shellfiles[*]}"
128+
$_shellcheck_bin -x "${_excludeOpts[@]}" "${_shellfiles[@]}"
129+
_rc=$?
130+
else
131+
echo "$_shellcheck_bin -x ${_alwaysExcludeOpts[*]} ${_shellfiles[*]}"
132+
$_shellcheck_bin -x "${_alwaysExcludeOpts[@]}" "${_shellfiles[@]}"
133+
_rc=$?
134+
fi
105135
if [ $_rc != 0 ] ; then
106136
echo
107137
echo "ERROR: shellcheck found codes not yet handled, must fix $0"

0 commit comments

Comments
 (0)