Created
October 11, 2016 01:33
-
-
Save paulgeringer/f193d02b00bfad06cd96a8c60382275a to your computer and use it in GitHub Desktop.
shellcheck precommit for git
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
#!/bin/bash | |
TEMP_FILE=/tmp/shellcheck.out | |
function setup() { | |
rm -f $TEMP_FILE | |
touch $TEMP_FILE | |
} | |
function loop() { | |
for file in $(git diff --diff-filter=ACMRTUXB --name-only --cached --relative | grep '\.sh$'); | |
do | |
shellcheck --format=gcc "$file" >> $TEMP_FILE | |
done | |
} | |
function notices() { | |
cat < $TEMP_FILE | grep_color 'note' '1;32' | |
} | |
function warnings() { | |
cat < $TEMP_FILE | grep_color 'warning' '1;33' | |
} | |
function errors() { | |
cat < $TEMP_FILE | grep_color 'error' '1;31' | |
} | |
function awk_strip() { | |
awk '{print $2}' | tr -d ':' | tr -d "^\d:" | |
} | |
function grep_color() { | |
local type=$1 | |
local color=$2 | |
GREP_COLOR="$color" grep -in "$type" | |
} | |
function main() { | |
loop | |
if [ 0 -lt "$(notices | awk_strip | wc -l)" ]; | |
then | |
notices | |
STATUS=0 | |
fi | |
if [ 0 -lt "$(warnings | awk_strip | wc -l)" ]; | |
then | |
warnings | |
STATUS=0 | |
fi | |
if [ 0 -lt "$(errors | awk_strip | wc -l)" ]; | |
then | |
errors | |
STATUS=1 | |
fi | |
exit $STATUS | |
} | |
setup | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment