| 1 |
#!/usr/bin/env bash |
| 2 |
# |
| 3 |
# An example hook script to verify what is about to be committed. |
| 4 |
# Called by "git commit" with no arguments. The hook should |
| 5 |
# exit with non-zero status after issuing an appropriate message if |
| 6 |
# it wants to stop the commit. |
| 7 |
# |
| 8 |
# To enable this hook, rename this file to "pre-commit". |
| 9 |
|
| 10 |
# The git hooks repo |
| 11 |
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)" |
| 12 |
REPO_ROOT=$(cd "${DIR}" && git rev-parse --show-toplevel) |
| 13 |
# The current repo root, in case the repo is different from the hooks (this allows using a single pre-commit across multiple repos) |
| 14 |
CURRENT_REPO=$(git rev-parse --show-toplevel) |
| 15 |
|
| 16 |
# Redirect output to stderr. |
| 17 |
exec 1>&2 |
| 18 |
|
| 19 |
# check to ensure all tools exist |
| 20 |
tools=('pre-commit' 'checkov' 'shfmt') |
| 21 |
for tool in "${tools[@]}"; do |
| 22 |
if ! command -v "${tool}" > /dev/null 2>&1; then |
| 23 |
cat << EOF |
| 24 |
Error: ${tool} not found |
| 25 |
Please install via brew or package manager |
| 26 |
'brew install ${tool}' |
| 27 |
or |
| 28 |
install required tools |
| 29 |
${tools[*]} |
| 30 |
EOF |
| 31 |
exit 2 |
| 32 |
fi |
| 33 |
done |
| 34 |
|
| 35 |
# run pre-commit checks |
| 36 |
pre-commit hook-impl --config="${REPO_ROOT}"/.pre-commit-config.yaml --hook-type=pre-commit --hook-dir "${CURRENT_REPO}" -- "$@" |