Skip to content

Instantly share code, notes, and snippets.

@nilsding
Created July 3, 2018 09:49
Show Gist options
  • Save nilsding/b6105a8949955ce81f82dabc37650640 to your computer and use it in GitHub Desktop.
Save nilsding/b6105a8949955ce81f82dabc37650640 to your computer and use it in GitHub Desktop.
shellcheck pre-commit hook
#!/usr/bin/env bash
# Print a message
#
# Params:
# $1: colour (red | green | yellow)
# $*: status to print
#
# Example usage:
# print_msg green "Successfully built $something"
#
# print_msg red "An error occurred:" $msg
print_msg() {
color="1"
case $1 in
red) color="31;1" ;;
green) color="32;1" ;;
yellow) color="33;1" ;;
esac
shift
# shellcheck disable=SC1117
printf " \033[${color}m*\033[0;1m %s\033[0m\n" "$*"
}
# Require an application to be in $PATH.
#
# Params:
# $1: app name
#
# Example usage:
# require_app ruby
require_app() {
if ! command -v "$1" > /dev/null; then
print_msg red "$1 not found, please install it"
return 1
fi
}
###############################################################################
require_app shellcheck || exit 0
changed_shell_files=$(
git diff --cached --name-only --diff-filter=ACM |
xargs grep -lE '^#!/.*(sh|bash|ksh)'
)
print_msg yellow "Running shellcheck..."
# shellcheck disable=SC2086
if shellcheck -a $changed_shell_files; then
print_msg green "shellcheck approved your shell scripts, nice"
else
print_msg red "shellcheck disapproves of your changes (return code: $?), "\
"fix the errors and come back later"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment