2022-12-24 12:04:59 -08:00
|
|
|
#!/bin/sh
|
|
|
|
# shellcheck disable=SC1090,SC1091,SC2016
|
|
|
|
|
|
|
|
# @file .config/husky/pre-commit
|
|
|
|
# @brief A git hook script for the `pre-commit` hook
|
|
|
|
|
2022-12-24 18:35:29 -08:00
|
|
|
# @description Register appropriate logging utility
|
|
|
|
if [ -f "$(dirname "$0")/../.config/log" ]; then
|
|
|
|
alias logger="$(dirname "$0")/../.config/log"
|
|
|
|
chmod +x "$(dirname "$0")/../.config/log"
|
|
|
|
elif command -v logg > /dev/null; then
|
|
|
|
alias logger='logg'
|
|
|
|
fi
|
|
|
|
|
2022-12-24 12:04:59 -08:00
|
|
|
if [ -f "$(dirname "$0")/_/husky.sh" ]; then
|
|
|
|
. "$(dirname "$0")/_/husky.sh"
|
|
|
|
|
2022-12-24 18:35:29 -08:00
|
|
|
# Attempt to register Task from common places if it is not in PATH
|
2022-12-24 12:04:59 -08:00
|
|
|
if ! type task > /dev/null; then
|
|
|
|
PATH="$PATH:$HOME/.local/go/bin:$HOME/.local/bin:$HOME/bin:$HOME/go/bin:$HOME/.asdf/shims"
|
|
|
|
if ! type task > /dev/null; then
|
|
|
|
for DOTFILE in .profile .bashrc .bash_profile .zshrc; do
|
|
|
|
. "$HOME/$DOTFILE"
|
|
|
|
if type task > /dev/null; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-12-24 18:35:29 -08:00
|
|
|
# Show warning if Task is still not registered/installed, else proceed with hook
|
2022-12-24 12:04:59 -08:00
|
|
|
if ! type task > /dev/null; then
|
2022-12-24 18:35:29 -08:00
|
|
|
.config/log warn '`task` does not appear to be installed or is not registered in the `PATH` variable - please manually include it'
|
|
|
|
logger info 'Get `task` here -> https://taskfile.dev'
|
2022-12-24 12:04:59 -08:00
|
|
|
else
|
2022-12-24 18:35:29 -08:00
|
|
|
logger info "Performing various pre-commit tasks on staged files (like autofixing, detecting private keys, etc.)"
|
2022-12-24 12:04:59 -08:00
|
|
|
STAGED_FILES=$(git diff --cached --name-only)
|
|
|
|
for FILE in "$STAGED_FILES"; do
|
|
|
|
if [ -f "$1" ]; then
|
|
|
|
task git:hook:pre-commit -- "$FILE" &
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
wait
|
|
|
|
|
2022-12-24 18:35:29 -08:00
|
|
|
logger info 'Linting and fixing the staged files with `lint-staged`'
|
2022-12-24 12:04:59 -08:00
|
|
|
task lint:lint-staged
|
|
|
|
|
2022-12-24 18:35:29 -08:00
|
|
|
logger info 'Reporting possible spelling errors in the staged files with `cspell`'
|
2022-12-24 12:04:59 -08:00
|
|
|
task lint:spelling
|
|
|
|
|
2022-12-24 18:35:29 -08:00
|
|
|
logger success 'Pre-commit validation complete!'
|
2022-12-24 12:04:59 -08:00
|
|
|
fi
|
|
|
|
else
|
2022-12-24 18:35:29 -08:00
|
|
|
logger warn 'Husky pre-commit hooks are currently not properly setup.'
|
2022-12-24 12:04:59 -08:00
|
|
|
fi
|