Last active
July 20, 2016 13:39
-
-
Save JJK801/5867810 to your computer and use it in GitHub Desktop.
Git Pre-Commit hook for coding standards validation
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 | |
binary="coke" | |
file=".coke" | |
files="" | |
stash=0 | |
function success | |
{ | |
echo "[$(tput bold)$(tput setaf 2)SUCCESS$(tput sgr0)] $(tput setaf 6)$1$(tput sgr0)" | |
exit 0 | |
} | |
function fail | |
{ | |
echo "[$(tput bold)$(tput setaf 1)FAIL$(tput sgr0)] $(tput setaf 6)$1$(tput sgr0)" | |
exit 1 | |
} | |
function warning | |
{ | |
echo "[$(tput bold)$(tput setaf 3)WARNING$(tput sgr0)] $(tput setaf 6)$1$(tput sgr0)" | |
} | |
function info | |
{ | |
echo "[$(tput bold)$(tput setaf 4)INFO$(tput sgr0)] $(tput setaf 6)$1$(tput sgr0)" | |
} | |
function unstash | |
{ | |
if [ "$stash" -eq 1 ] | |
then | |
if git stash pop -q | |
then | |
warning "Unable to revert stash command" | |
fi | |
fi | |
} | |
# Préparation de l'execution | |
STATUS_OUTPUT=$(git status --porcelain) | |
if echo "$STATUS_OUTPUT"|grep '^[MARCDU][MARCDU]' > /dev/null | |
then | |
fail "Some files appears both in your staging area and your unadded files." | |
elif echo "$STATUS_OUTPUT"|grep '^ [MARCD]' > /dev/null | |
then | |
if git stash save -u -k -q | |
then | |
stash=1 | |
else | |
fail "Unable to stash changes" | |
fi | |
fi | |
if [ ! -e $binary ] | |
then | |
binary="bin/$binary" | |
if [ ! -e $binary ] | |
then | |
binary="vendor/$binary" | |
if [ ! -e $binary ] | |
then | |
warning "Can't find coke binary : \"coke\", \"bin/coke\", \"vendor/bin/coke\", aborting check" | |
unstash | |
exit 0 | |
fi | |
fi | |
fi | |
if [ ! -e $file ] | |
then | |
warning "Can't find \".coke\" configuration file, aborting check" | |
unstash | |
exit 0 | |
fi | |
cokePaths=$( grep "^[^\#][^\=]*$" $file ) | |
for file in $(git status --porcelain | grep '^[MARC]' | colrm 1 3 | cut -f2 -d">") | |
do | |
allowed=0 | |
for ligne in $cokePaths | |
do | |
if [ "${ligne:0:1}" = '!' ] && [[ "$file" == *"${ligne:1}"* ]] | |
then | |
continue 2 | |
elif [[ "$file" == "$ligne"* ]] | |
then | |
allowed=1 | |
fi | |
done | |
if [ "$allowed" -eq 1 ] | |
then | |
files="$files $file" | |
fi | |
done | |
# Execution de la commande | |
if [ -n "$files" ] | |
then | |
$binary $files | |
CS_RESULT=$? | |
else | |
CS_RESULT=0 | |
fi | |
unstash | |
if [ $CS_RESULT -eq 1 ] | |
then | |
fail "You must fix coding standards before commit" | |
fi | |
success "Coding standards" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment