42 lines
883 B
Bash
Executable File
42 lines
883 B
Bash
Executable File
#!/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
|
|
}
|
|
|
|
main
|