Last active
October 14, 2020 10:48
-
-
Save chmouel/c7ba8f045f103d6623c9e89b56fe55a9 to your computer and use it in GitHub Desktop.
yamllint yaml files on GIT pre-commit hook script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# To be copied in .git/hooks/pre-commit (0755 mode) | |
if git rev-parse --verify HEAD >/dev/null 2>&1 | |
then | |
against=HEAD | |
else | |
# Initial commit: diff against an empty tree object | |
against=$(git hash-object -t tree /dev/null) | |
fi | |
# Redirect output to stderr. | |
exec 1>&2 | |
ret=0 | |
set -x | |
CHANGED_FILES=($(git diff --cached --name-only --diff-filter=ACM "${against}")) | |
for yaml in ${CHANGED_FILES[*]};do | |
[[ ${yaml} =~ .y(a)?ml$ ]] || continue | |
yamllint --no-warnings -s -f colored "${yaml}" | |
[[ ${ret} == 0 && $? != 0 ]] && ret=$? | |
done | |
for shscript in ${CHANGED_FILES[*]};do | |
if file "$shscript"|grep -qw "shell script";then | |
shellcheck -S error "$shscript" | |
[[ ${ret} == 0 && $? != 0 ]] && ret=$? | |
fi | |
done | |
exit ${ret} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment