Skip to content

Commit 49fd726

Browse files
committed
Merge pull request #6 from Expez/master
Add pre-commit hook running gofmt
2 parents 74957da + 7348ffe commit 49fd726

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pre-commit.sh

+29
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)