aboutsummaryrefslogtreecommitdiffstats
path: root/.githooks/pre-commit.shellcheck
blob: f2189221a42b2854952a48976afeb93a8f824d0b (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
37
38
39
40
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}