We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 74957da + 7348ffe commit 49fd726Copy full SHA for 49fd726
pre-commit.sh
@@ -0,0 +1,29 @@
1
+#!/bin/bash
2
+
3
+# This script runs gofmt on all staged files to ensure proper formatting.
4
+# Any detected errors will be printed to stdout and the commit aborted.
5
6
+# NOTE: This script probably won't work for partial changes added with
7
+# git add -p or git commit -p.
8
9
+command -v gofmt >/dev/null 2>&1 || { echo >&2 "gofmt not on PATH."; exit 1; }
10
11
+git stash -q --keep-index >/dev/null
12
13
+IFS='\n'
14
+exitcode=0
15
+for file in $(git diff --staged --name-only --diff-filter=ACMR | grep '\.go$')
16
+do
17
+ output=`gofmt -w=true "$file"`
18
+ if [ -n $output ]
19
+ then
20
+ # any output is a syntax error
21
+ echo >&2 "$output"
22
+ exitcode=1
23
+ fi
24
+ git add "$file"
25
+done
26
27
+git stash pop -q >/dev/null 2>&1
28
29
+exit $exitcode
0 commit comments