Skip to content

Instantly share code, notes, and snippets.

@andrewslotin
Created March 10, 2016 10:30
Show Gist options
  • Save andrewslotin/13dcdd40395c8ae3e244 to your computer and use it in GitHub Desktop.
Save andrewslotin/13dcdd40395c8ae3e244 to your computer and use it in GitHub Desktop.
Pre-commit git hook to avoid committing non-formatted Go code
#!/bin/sh
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.go$')
[ -z "$gofiles" ] && exit 0
unformatted=$(gofmt -l $gofiles)
if [ ! -z "$unformatted" ]; then
echo "Go files must be formatted with gofmt. Please run:" >&2
for fn in $unformatted; do
echo " gofmt -w $PWD/$fn" >&2
done
exit 1
fi
exit 0
@andrewslotin
Copy link
Author

Place this into .git/hooks/pre-commit and make it executable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment