Commit a12229db authored by Karol Latecki's avatar Karol Latecki Committed by Jim Harris
Browse files

scripts/check_format: add bash styling check

Add Bash styling checks using shellcheck:
https://github.com/koalaman/shellcheck

Shellcheck can be used in check_format just like astyle
and pep8 to improve our bash scripts quality by checking
for syntax errors.
It could also allow us to enforce (at least to some
extent) consistent coding style - using $() instead of
backticks for command calls, string quoting, etc.

Shellcheck maintains it's own wiki with error descriptions:
https://github.com/koalaman/shellcheck/wiki/
To check for specific error go to, for example:
https://github.com/koalaman/shellcheck/wiki/SC2006


Each wiki entry includes an example of problematic code, an
example of suggested correct code and a rationale.

Currently all of the found problems are excluded in check_format.sh
and will be fixed incrementally.

Change-Id: Ib1d6f628e101e0e2b2d56956b679942630a73f95
Signed-off-by: default avatarKarol Latecki <karol.latecki@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/463172


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: default avatarPaul Luse <paul.e.luse@intel.com>
Reviewed-by: default avatarSeth Howell <seth.howell@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
parent a805c7de
Loading
Loading
Loading
Loading
+48 −0
Original line number Diff line number Diff line
@@ -228,6 +228,54 @@ else
	echo "You do not have pycodestyle or pep8 installed so your Python style is not being checked!"
fi

if hash shellcheck 2>/dev/null; then
	echo -n "Checking Bash style..."

	shellcheck_v=$(shellcheck --version | grep -P "version: [0-9\.]+" | cut -d " " -f2)

	# Exclude list currently holds all of reported errors. Errors will be fixed and list
	# reduced over time. If you find any new error which is not on the list and you think
	# that it should be excluded - please create a GitHub issue.
	# For more information about the topic and a list of human-friendly error descripions
	# go to: https://trello.com/c/29Z90j1W
	# Error descriptions can also be found at: https://github.com/koalaman/shellcheck/wiki
	SHCK_EXCLUDE="SC1001,SC1003,SC1004,SC1009,SC1010,SC1019,SC1020,SC1072,SC1073,\
SC1083,SC1087,SC1090,SC1091,SC1113,SC2001,SC2002,SC2003,SC2004,SC2005,SC2006,\
SC2007,SC2009,SC2010,SC2012,SC2013,SC2015,SC2016,SC2018,SC2019,SC2022,SC2026,\
SC2027,SC2030,SC2031,SC2034,SC2035,SC2039,SC2043,SC2044,SC2045,SC2046,SC2048,\
SC2059,SC2064,SC2068,SC2070,SC2074,SC2086,SC2088,SC2089,SC2090,SC2091,SC2094,\
SC2097,SC2098,SC2103,SC2115,SC2116,SC2119,SC2120,SC2121,SC2124,SC2126,SC2128,\
SC2129,SC2140,SC2142,SC2143,SC2145,SC2146,SC2148,SC2152,SC2153,SC2154,SC2155,\
SC2162,SC2164,SC2165,SC2166,SC2167,SC2174,SC2178,SC2181,SC2191,SC2192,SC2195,\
SC2199,SC2206,SC2207,SC2209,SC2214,SC2219,SC2220,SC2223,SC2230,SC2231,SC2233,SC2235,SC2236"

	SHCK_FORMAT="diff"
	SHCK_APPLY=true
	if [ "$shellcheck_v" \< "0.7.0" ]; then
		SHCK_FORMAT="tty"
		SHCK_APPLY=false
	fi
	SHCH_ARGS=" -e $SHCK_EXCLUDE -f $SHCK_FORMAT"

	error=0
	git ls-files '*.sh' | xargs -P$(nproc) -n1 shellcheck $SHCH_ARGS > shellcheck.log || error=1
	if [ $error -ne 0 ]; then
		echo " Bash formatting errors detected!"
		cat shellcheck.log
		if $SHCK_APPLY; then
			git apply shellcheck.log
			echo "Bash errors were automatically corrected."
			echo "Please remember to add the changes to your commit."
		fi
		rc=1
	else
		echo " OK"
	fi
	rm -f shellcheck.log
else
	echo "You do not have shellcheck installed so your Bash style is not being checked!"
fi

# Check if any of the public interfaces were modified by this patch.
# Warn the user to consider updating the changelog any changes
# are detected.