aboutsummaryrefslogtreecommitdiffstats
path: root/.githooks/pre-commit.shellcheck
diff options
context:
space:
mode:
Diffstat (limited to '.githooks/pre-commit.shellcheck')
-rwxr-xr-x.githooks/pre-commit.shellcheck41
1 files changed, 41 insertions, 0 deletions
diff --git a/.githooks/pre-commit.shellcheck b/.githooks/pre-commit.shellcheck
new file mode 100755
index 0000000..f218922
--- /dev/null
+++ b/.githooks/pre-commit.shellcheck
@@ -0,0 +1,41 @@
+#!/bin/sh
+#
+# Git hook to run ShellCheck.
+#
+# ShellCheck <https://www.shellcheck.net/>
+
+# Treat unset variables as an error when performing parameter expansion.
+set -u
+
+TRUE=0
+FALSE=1
+
+die() {
+ echo "$@" >&2
+ exit 1
+}
+
+if ! command -v shellcheck >/dev/null; then
+ echo 'unable to locate shellcheck' >&2
+ return 0
+fi
+
+success=${TRUE}
+for f in $(git diff --cached --name-only); do
+ # Check for file deletion.
+ if [ ! -r "${f}" ]; then
+ continue
+ fi
+
+ cmd=':'
+ case "${f}" in
+ shflags|shflags_test_helpers) cmd="shellcheck -s sh ${f}" ;;
+ *.sh) cmd="shellcheck ${f}" ;;
+ esac
+ if ! ${cmd}; then
+ success=${FALSE}
+ echo "shellcheck error for '${f}'" >&2
+ fi
+done
+
+exit ${success}