# Copyright (c) 2010, Evan Shaw ################################################################################### # NOTE: This file needs to be copied to .git/hooks/pre-commit to enable the hook. ################################################################################### # Taken from https://github.com/edsrzf/gofmt-git-hook/blob/master/fmt-check # Runs gofmt on the changes before committing. # Aborts the commit with a warning if gofmt needs to be run first. ################################################################################### #!/bin/sh 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,,$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 exit $exitcode } case "$1" in --about ) echo "Check Go code formatting" ;; * ) test_fmt ;; esac