Browse Source

Merge pull request #33 from fancycode/check_gofmt

Added pre-commit hook.
pull/35/head
Simon Eisenmann 11 years ago
parent
commit
ee7a2e5b5c
  1. 11
      Makefile
  2. 48
      src/hooks/pre-commit.hook

11
Makefile

@ -53,7 +53,14 @@ endif
# Tools # Tools
AUTOPREFIXER_BROWSER_SUPPORT := "> 1%, last 2 versions, Firefox ESR, Opera 12.1" AUTOPREFIXER_BROWSER_SUPPORT := "> 1%, last 2 versions, Firefox ESR, Opera 12.1"
build: get binary assets build: hook get binary assets
# the pre-commit hook checks for formatted Go files
hook:
if test ! \( -x .git/hooks/pre-commit -a -L .git/hooks/pre-commit \); then \
rm -f .git/hooks/pre-commit; \
ln -s ../../src/hooks/pre-commit.hook .git/hooks/pre-commit; \
fi
gopath: gopath:
@echo GOPATH=$(GOPATH) @echo GOPATH=$(GOPATH)
@ -171,4 +178,4 @@ tarball: distclean release install
echo -n $(VERSION) > $(TARPATH)/version.txt echo -n $(VERSION) > $(TARPATH)/version.txt
tar czf $(DIST)/$(PACKAGE_NAME).tar.gz -C $(DIST) $(PACKAGE_NAME) tar czf $(DIST)/$(PACKAGE_NAME).tar.gz -C $(DIST) $(PACKAGE_NAME)
.PHONY: clean distclean pristine get getupdate build styles javascript release releasetest dist_gopath install gopath binary binaryrace binaryall tarball assets .PHONY: hook clean distclean pristine get getupdate build styles javascript release releasetest dist_gopath install gopath binary binaryrace binaryall tarball assets

48
src/hooks/pre-commit.hook

@ -0,0 +1,48 @@
#!/bin/sh
#
# Check that Go files have been formatted
#
for file in `git diff-index --cached --name-only HEAD --diff-filter=ACMR| grep "\.go$"` ; do
# nf is the temporary checkout. This makes sure we check against the
# revision in the index (and not the checked out version).
nf=`git checkout-index --temp ${file} | cut -f 1`
newfile=`mktemp /tmp/${nf}.XXXXXX` || exit 1
gofmt ${nf} > "${newfile}" 2>> /dev/null
diff -u -p "${nf}" "${newfile}"
r=$?
rm "${newfile}"
rm "${nf}"
if [ $r != 0 ] ; then
echo "================================================================================================="
echo " Code format error in: $file "
echo " "
echo " Please fix before committing. Don't forget to run git add before trying to commit again. "
echo " If the whole file is to be committed, this should work (run from the top-level directory): "
echo " "
echo " go fmt $file; git add $file; git commit"
echo " "
echo "================================================================================================="
exit 1
fi
done
check_styles=0
for file in `git diff-index --cached --name-only HEAD --diff-filter=ACMR| grep "\.scss$"` ; do
check_styles=1
done
if [ "${check_styles}" = "1" ]; then
make styles
r=$?
if [ $r != 0 ] ; then
echo "================================================================================================="
echo " Error in styles "
echo " "
echo " Please fix before committing. Don't forget to run git add before trying to commit again. "
echo " "
echo "================================================================================================="
exit 1
fi
fi
Loading…
Cancel
Save