| 0 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,46 @@ |
| 0 |
+#!/bin/sh |
|
| 1 |
+ |
|
| 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 |
+ |
|
| 6 |
+test_fmt() {
|
|
| 7 |
+ hash gofmt 2>&- || { echo >&2 "gofmt not in PATH."; exit 1; }
|
|
| 8 |
+ IFS=' |
|
| 9 |
+' |
|
| 10 |
+ for file in `git diff --cached --name-only --diff-filter=ACM | grep '\.go$'` |
|
| 11 |
+ do |
|
| 12 |
+ output=`git cat-file -p :$file | gofmt -l 2>&1` |
|
| 13 |
+ if test $? -ne 0 |
|
| 14 |
+ then |
|
| 15 |
+ output=`echo "$output" | sed "s,<standard input>,$file,"` |
|
| 16 |
+ syntaxerrors="${list}${output}\n"
|
|
| 17 |
+ elif test -n "$output" |
|
| 18 |
+ then |
|
| 19 |
+ list="${list}${file}\n"
|
|
| 20 |
+ fi |
|
| 21 |
+ done |
|
| 22 |
+ exitcode=0 |
|
| 23 |
+ if test -n "$syntaxerrors" |
|
| 24 |
+ then |
|
| 25 |
+ echo >&2 "gofmt found syntax errors:" |
|
| 26 |
+ printf "$syntaxerrors" |
|
| 27 |
+ exitcode=1 |
|
| 28 |
+ fi |
|
| 29 |
+ if test -n "$list" |
|
| 30 |
+ then |
|
| 31 |
+ echo >&2 "gofmt needs to format these files (run gofmt -w and git add):" |
|
| 32 |
+ printf "$list" |
|
| 33 |
+ exitcode=1 |
|
| 34 |
+ fi |
|
| 35 |
+ exit $exitcode |
|
| 36 |
+} |
|
| 37 |
+ |
|
| 38 |
+case "$1" in |
|
| 39 |
+ --about ) |
|
| 40 |
+ echo "Check Go code formatting" |
|
| 41 |
+ ;; |
|
| 42 |
+ * ) |
|
| 43 |
+ test_fmt |
|
| 44 |
+ ;; |
|
| 45 |
+esac |