Am Dienstag, 20. Juni 2006 19:27 schrieb emerson cargnin:
> I found two threads about using pre-commit hooks to format code, its
> pro and cons. Can someone point me somewhere with a scrip to just
> validate and to not accept a code not formatted?
Here is my current pre-commit hook. It
1) checks for a non-empty log message
2) disallows commits to the /tags directory
3) runs checkstyle on each java file, and aborts commit if an error is
found.
#!/bin/sh
# Path configuration
REPOS="$1"
TXN="$2"
SVNLOOK=/path/to/subversion/bin/svnlook
JAVA=/path/to/java/bin/java
CHECKSTYLE=/path/to/checkstyle/checkstyle-all-4.1.jar
TMPDIR=/tmp/$TXN
REPORT=/tmp/$TXN/report
CHECKSTYLE_CONFIG=/path/to/checkstyle-config/checkstyle-config.xml
# Make sure there is a log message
if ! $SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" > /dev/null ; then
echo "Bitte eine Log-Nachricht eingeben" > /dev/stderr
exit 1
fi
# Do not allow commits to the /tags directory
if $SVNLOOK changed -t "$TXN" "$REPOS" | awk '{print $2}' |\
grep -q "^tags" ; then
/bin/echo "Cannot commit to tags" 1>&2
exit 1
fi
# Run checkstyle on java files
CHANGED=`$SVNLOOK changed -t "$TXN" "$REPOS" | grep -v "^D" |\
awk '{print $2}'`
mkdir -p $TMPDIR
X=0
for LINE in $CHANGED ; do
FILE=`echo $LINE | egrep \\.java$`
if [ -n "$FILE" ] ; then
$SVNLOOK cat $REPOS --transaction $TXN $FILE > $TMPDIR/tmpfile.java
$JAVA -jar $CHECKSTYLE -c $CHECKSTYLE_CONFIG $TMPDIR/tmpfile.java \
> $TMPDIR/tmpfile.checkstyle
X=$(($X + $?))
cat $TMPDIR/tmpfile.checkstyle |\
grep -v "^Starting audit...$\|^Audit done.$" |\
sed -e "s@$TMPDIR/tmpfile.java@$FILE@" \
>> $REPORT
fi
done
if [ $X -ne 0 ] ; then
cat $REPORT > /dev/stderr
rm -Rf $TMPDIR
exit 1
fi
rm -Rf $TMPDIR
exit 0
--
Dipl. Inf. Boris Stumm, Technische Universität Kaiserslautern
http://wwwhis.informatik.uni-kl.de/aghis/staff/Stumm/
Und für alle, die mir Emails schreiben wollen:
http://learn.to/quote
- application/pgp-signature attachment: stored
Received on Wed Jun 21 13:16:14 2006