bash/pre-commit: git hook to lint files before committing

This commit is contained in:
Nick Chambers 2021-07-25 20:39:04 -05:00
parent 01a20181b4
commit cd2aaf04f9
1 changed files with 31 additions and 0 deletions

31
bash/pre-commit Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env bash
has() {
if (( $# )); then
hash "$1" 2>/dev/null
fi
}
is_a() {
if (( $# > 1 )); then
head -1 "$1" | grep -qE '^#!.*'"$2"'$'
fi
}
find . -name .DS_Store -exec rm -f -- {} +
git status -zu | while read -rd '' xy filename; do
if [[ $xy = "??" || $xy = D ]]; then
continue
fi
if [[ $filename = *.sh ]] || is_a "$filename" bash; then
if has shellcheck; then
shellcheck -s bash "$filename"
fi
elif [[ $filename = *.rb ]] || is_a "$filename" ruby; then
if has rubocop; then
rubocop -c "$(git rev-parse --show-toplevel)" "$filename"
fi
fi
done