Skip to content

Instantly share code, notes, and snippets.

@ssimono
Created July 7, 2016 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssimono/5f1e7d21d31559fb8d16184e0e2c97fd to your computer and use it in GitHub Desktop.
Save ssimono/5f1e7d21d31559fb8d16184e0e2c97fd to your computer and use it in GitHub Desktop.
Pre commit hook for checking go source conventions usage
#!/bin/bash
# This pre hook runs all go checking tools on every relevant go file.
# Returns status 1 if checks fail, preventing commit to be done
function fmtdiff {
gofmt -d $@ > /tmp/gofmtdiff
if [ `wc -l < /tmp/gofmtdiff` != 0 ]; then
cat /tmp/gofmtdiff
echo "Run gofmt to apply those changes"
echo ""
return 1
fi
}
function gocheck {
go vet $@ && golint $@ && fmtdiff $@
}
filter=''
filter=$filter'/^[AM]/!d;' # Take only changed and added files
filter=$filter's/^.{3}//;' # Strip off git status meta-info characters
filter=$filter'/^vendor\//d;' # Discard files in vendor directory
filter=$filter'/\.go/!d;' # Only take go files
files=$(git status --porcelain | sed -nE '{'$filter';p;}')
# Run all checks for relevant files
status=0
for file_name in $files; do
gocheck $file_name || status=1
done
exit $status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment