perkeep/misc/pre-commit.githook

59 lines
1.0 KiB
Bash
Executable File

#!/usr/bin/env sh
exitstatus=0
#third_party imports.
cd third_party
rewrite=`./rewrite-imports.sh -l`
cd ..
if [ -n "$rewrite" ]
then
exitstatus=1
for r in $rewrite
do
echo $r
done
echo "The above need their imports to be fixed. Use rewrite-imports.sh -w"
fi
#gofmt and trailing space errors
committed=`git diff-index --cached --name-only HEAD`
needFmt=""
for c in $committed
do
if [ ! -e "$c" ]
then
continue
fi
gofile=`echo $c | grep -E '.*\.go$'`
javafile=`echo $c | grep -E '.*\.java$'`
if [ -n "$gofile" ]
then
fmtdiff=`git show ":$c" | gofmt -d 2>&1`
if [ -n "$fmtdiff" ]
then
needFmt="$needFmt $gofile"
exitstatus=1
fi
fi
if [ -n "$gofile" -o -n "$javafile" ]
then
trailspace=`git diff-index --cached --check HEAD $c | grep 'trailing whitespace'`
if [ -n "$trailspace" ]
then
echo $trailspace
exitstatus=1
fi
fi
done
if [ "$exitstatus" -ne 0 ]
then
if [ -n "$needFmt" ]
then
echo "gofmt -w $needFmt"
fi
echo "You can override this check with 'git commit --no-verify'"
fi
exit $exitstatus