-
-
Save KuiKui/5d7912eb92bb658ed7f6d9bbfbb27b85 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 | |
#-------------------------------------------------------------------------------------------------- | |
# WARNING : due to a 'git stash save -u' command, check your .gitignore file before using this hook | |
# -> http://blog.icefusion.co.uk/git-stash-can-delete-ignored-files-git-stash-u/ | |
#-------------------------------------------------------------------------------------------------- | |
binary="coke" | |
file=".coke" | |
files="" | |
stash=0 | |
uid=$(date +"%Y%m%d%H%M%S") | |
#----------------- | |
# Output functions | |
#----------------- | |
function success | |
{ | |
if [ -n "$TERM" ]; then | |
echo "[$(tput bold)$(tput setaf 2)SUCCESS$(tput sgr0)] $(tput setaf 6)$1$(tput sgr0)" | |
else | |
echo "[SUCCESS] $1" | |
fi | |
exit 0 | |
} | |
function fail | |
{ | |
if [ -n "$TERM" ]; then | |
echo "[$(tput bold)$(tput setaf 1)FAIL$(tput sgr0)] $(tput setaf 6)$1$(tput sgr0)" | |
else | |
echo "[FAIL] $1" | |
fi | |
exit 1 | |
} | |
function warning | |
{ | |
if [ -n "$TERM" ]; then | |
echo "[$(tput bold)$(tput setaf 3)WARNING$(tput sgr0)] $(tput setaf 6)$1$(tput sgr0)" | |
else | |
echo "[WARNING] $1" | |
fi | |
} | |
#-------------------------- | |
# Stash / unstash functions | |
#-------------------------- | |
function stash | |
{ | |
if git stash save -u -k -q $uid | |
then | |
git stash list | grep "$uid" > /dev/null | |
if [ $? -eq 0 ] | |
then | |
stash=1 | |
fi | |
else | |
fail "Unable to stash changes" | |
fi | |
} | |
function unstash | |
{ | |
if [ "$stash" -eq 1 ] | |
then | |
git stash pop -q | |
if [ $? -gt 0 ] | |
then | |
warning "Unable to revert stash command" | |
fi | |
fi | |
} | |
#------------- | |
# Check binary | |
#------------- | |
if [ ! -s $binary ] | |
then | |
binary="bin/$binary" | |
if [ ! -s $binary ] | |
then | |
binary="vendor/$binary" | |
if [ ! -s $binary ] | |
then | |
warning "Can't find coke binary : \"coke\", \"bin/coke\", \"vendor/bin/coke\", aborting check" | |
exit 1 | |
fi | |
fi | |
fi | |
#------------------------- | |
# Check configuration file | |
#------------------------- | |
if [ ! -s $file ] | |
then | |
warning "Can't find \".coke\" configuration file, aborting check" | |
exit 1 | |
fi | |
#---------------------------- | |
# Stash to clean working tree | |
#---------------------------- | |
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 | |
stash | |
fi | |
#-------------------------------------------------------------------- | |
# Pick files to verify (merge between git information and Coke lists) | |
#-------------------------------------------------------------------- | |
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 | |
#---------------------- | |
# No files to be verify | |
#---------------------- | |
if [ ! -n "$files" ] | |
then | |
warning "No files has been verified by Coke" | |
unstash | |
exit 0 | |
fi | |
#--------------- | |
# Coke execution | |
#--------------- | |
$binary $files | |
CS_RESULT=$? | |
#------------------------------------------ | |
# Unstash to retrieve original working tree | |
#------------------------------------------ | |
unstash | |
#---------------- | |
# Display results | |
#---------------- | |
if [ $CS_RESULT -eq 1 ] | |
then | |
fail "You must fix coding standards before commit" | |
fi | |
success "Coding standards OK" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment