Skip to content

Instantly share code, notes, and snippets.

@paulsmith
Created September 4, 2013 20:26
Show Gist options
  • Save paulsmith/6442399 to your computer and use it in GitHub Desktop.
Save paulsmith/6442399 to your computer and use it in GitHub Desktop.
gofmt git pre-commit hook
#!/bin/bash
if git rev-parse HEAD >/dev/null 2>&1; then
FILES=$(git diff --staged --name-only | grep -e '\.go$')
else
FILES=$(git ls-files -c | grep -e '\.go$')
fi
for file in $FILES; do
if test -n "$(gofmt -l $file)"; then
echo "Error: git pre-commit failed, file needs gofmt: $file"
exit 1
fi
done
@paulsmith
Copy link
Author

Save as .git/hooks/pre-commit and chmod +x

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