fmt-check.hook 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/sh
  2. # This pre-commit hook will abort if a committed file doesn't pass gofmt.
  3. # By Even Shaw <edsrzf@gmail.com>
  4. # http://github.com/edsrzf/gofmt-git-hook
  5. test_fmt() {
  6. hash gofmt 2>&- || { echo >&2 "gofmt not in PATH."; exit 1; }
  7. IFS='
  8. '
  9. for file in `git diff --cached --name-only --diff-filter=ACM | grep '\.go$'`
  10. do
  11. output=`git cat-file -p :$file | gofmt -l 2>&1`
  12. if test $? -ne 0
  13. then
  14. output=`echo "$output" | sed "s,<standard input>,$file,"`
  15. syntaxerrors="${list}${output}\n"
  16. elif test -n "$output"
  17. then
  18. list="${list}${file}\n"
  19. fi
  20. done
  21. exitcode=0
  22. if test -n "$syntaxerrors"
  23. then
  24. echo >&2 "gofmt found syntax errors:"
  25. printf "$syntaxerrors"
  26. exitcode=1
  27. fi
  28. if test -n "$list"
  29. then
  30. echo >&2 "gofmt needs to format these files (run gofmt -w and git add):"
  31. printf "$list"
  32. exitcode=1
  33. fi
  34. exit $exitcode
  35. }
  36. case "$1" in
  37. --about )
  38. echo "Check Go code formatting"
  39. ;;
  40. * )
  41. test_fmt
  42. ;;
  43. esac