githooks/pre-commit.shellcheck

42 lines
882 B
Plaintext
Raw Normal View History

2023-12-07 15:17:02 +00:00
#!/bin/bash
set -e
function check_dependencies_installed() {
for dependency in xargs shellcheck; do
if ! command -v "$dependency" &>/dev/null; then
printf '\e[1;31m[error]\e[0m \e[1mdependency\[0m is not installed.\n'
exit 1
fi
done
}
function get_staged_files {
if ! git rev-parse --verify HEAD &> /dev/null; then
printf '\e[1;31m[error]\e[0m \e[1mHEAD\e[0m does not point to a commit.\n'
exit 1
fi
git diff-index --cached --name-only HEAD
}
function get_shell_files {
get_staged_files | xargs 'file' | grep 'shell script' | cut -d ':' -f 1
get_staged_files | grep --regexp=".*\.sh$" --regexp="\.bash.*$" --regexp="\.zsh.*$"
}
function main {
check_dependencies_installed
declare -a files=()
while read -r file; do
files[${files[@]}+1]="$file"
done < <(get_shell_files)
if [[ "${#files[@]}" -ne 0 ]]; then
shellcheck "${files[@]}"
fi
2023-12-07 15:17:02 +00:00
}
main