Created
April 9, 2010 12:00
-
-
Save sebnow/361089 to your computer and use it in GitHub Desktop.
Git pre-commit hook which checks if gofmt fails on any *.go files
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 | |
root=$(git rev-parse --show-cdup) | |
[ "x$root" = "x" ] && root=. | |
IFS=$'\n' | |
status=0 | |
files='' | |
# Get a list of staged refs | |
for object in $(git ls-files -s --full-name $root | sed -e 's/^.* \([a-zA-Z0-9][a-zA-Z0-9]*\)[\t ]*[0-9][0-9]*.\(.*\)$/\1 \2/'); do | |
ref=$(echo $object | cut -d' ' -f1) | |
file=$(echo $object | cut -d' ' -f2) | |
# Only check *.go files | |
if [ ! "$(echo $file | sed -e 's/^.*\.\([^.]*\)$/\1/')" = "go" ]; then | |
continue | |
fi | |
formatted="$(git cat-file -p $ref | gofmt -l $GOFMTFLAGS 2> /dev/null)" | |
# If parsing fails or formatting is incorrect, fail the commit | |
if (($?)) || [ "x$formatted" != "x" ]; then | |
files="$files\t$file\n" | |
status=1 | |
fi | |
done | |
echo "gofmt failed on the following files:" | |
echo -n -e "$files" | |
exit $status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment