Skip to content

Instantly share code, notes, and snippets.

@tyrm
Created October 4, 2021 17:45
Show Gist options
  • Save tyrm/cc8af608a86cef24b56a8d2b2bab0211 to your computer and use it in GitHub Desktop.
Save tyrm/cc8af608a86cef24b56a8d2b2bab0211 to your computer and use it in GitHub Desktop.
#!/bin/sh
PATH=$PATH:~/go/bin
test_fmt() {
hash gofmt 2>&- || { echo >&2 "gofmt not in PATH."; exit 1; }
IFS='
'
for file in `git diff --cached --name-only --diff-filter=ACM | grep '\.go$'`
do
output=`git cat-file -p :$file | gofmt -l 2>&1`
if test $? -ne 0
then
output=`echo "$output" | sed "s,<standard input>,$file,"`
syntaxerrors="${list}${output}\n"
elif test -n "$output"
then
list="${list}${file}\n"
fi
done
exitcode=0
if test -n "$syntaxerrors"
then
echo >&2 "gofmt found syntax errors:"
printf "$syntaxerrors"
exitcode=1
fi
if test -n "$list"
then
echo >&2 "gofmt needs to format these files (run gofmt -w and git add):"
printf "$list"
exitcode=1
fi
if test $exitcode -ne 0
then
exit $exitcode
fi
echo running tests
go test ./...
exitcode=$?
exit $exitcode
}
case "$1" in
--about )
echo "Check Go code formatting"
;;
* )
test_fmt
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment