File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments