aboutsummaryrefslogtreecommitdiff
path: root/.githooks/pre-commit.shellcheck
blob: 7615d4463c67b76050574a22970f7e332cd893d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/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
  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}