Subversion Pre-Commit Script – Finally

Subversion Pre-Commit Script – Finally

I have been the administrator for our Subversion server at work for a couple years and it has been really nice to learn all about it.  I have put together some pre/post commit scripts for checking messages, sending emails and also updating lines of code produced into a mysql database.

It always drives me nuts when I see someone commit an bin or obj folder or the suo / user file into subversion.  I finally wrote some scripts to prevent this going forward.  If it is attempted, then an nice big error message is returned, and commit is prevented.

Enjoy:

#!/bin/bash
WORDMIN=3
ZERO=0
REPOS=”$1″
TXN=”$2″

# Make Sure Bin Folder Is Not Being Added
BINF=`/usr/bin/svnlook changed -t “$TXN” “$REPOS” | grep “/[Bb]in/\| [Bb]in/” | wc -w`
if [ $BINF -gt $ZERO ];
then
echo ” ” >&2
echo “***** ERROR *****” >&2
echo “You are attempting to commit a bin folder, which is a sin.” >&2
echo “You need to preform the Undo Add to the bin folder under SVN.” >&2
echo ” ” >&2
echo `/usr/bin/svnlook changed -t “$TXN” “$REPOS” | grep “/[Bb]in/\| [Bb]in/” ` >&2
echo “***** End Of Error Message *****” >&2
exit 1
fi

# Make Sure Obj Folder Is Not Being Added
OBJF=`/usr/bin/svnlook changed -t “$TXN” “$REPOS” | grep “/[Oo]bj/\| [Oo]bj/” | wc -w`
if [ $OBJF -gt $ZERO ];
then
echo ” ” >&2
echo “***** ERROR *****” >&2
echo “You are attempting to commit a obj folder, which is a sin.” >&2
echo “You need to preform the Undo Add to the obj folder under SVN.” >&2
echo ” ” >&2
echo `/usr/bin/svnlook changed -t “$TXN” “$REPOS” | grep “/[Oo]bj/\| [Oo]bj/” ` >&2
echo “***** End Of Error Message *****” >&2
exit 1
fi

# Make Sure .suo File Is Not Being Added
SUOF=`/usr/bin/svnlook changed -t “$TXN” “$REPOS” | grep “suo\$” | wc -w`
if [ $SUOF -gt $ZERO ];
then
echo ” ” >&2
echo “***** ERROR *****” >&2
echo “You are attempting to commit a suo file, which is a sin.” >&2
echo “You need to preform the Undo Add to the suo file under SVN.” >&2
echo ” ” >&2
echo `/usr/bin/svnlook changed -t “$TXN” “$REPOS” | grep “suo\$” ` >&2
echo “***** End Of Error Message *****” >&2
exit 1
fi

# Make Sure .user File Is Not Being Added
USERF=`/usr/bin/svnlook changed -t “$TXN” “$REPOS” | grep “user\$” | wc -w`
if [ $USERF -gt $ZERO ];
then
echo ” ” >&2
echo “***** ERROR *****” >&2
echo “You are attempting to commit a user file, which is a sin.” >&2
echo “You need to preform the Undo Add to the user file under SVN.” >&2
echo ” ” >&2
echo `/usr/bin/svnlook changed -t “$TXN” “$REPOS” | grep “user\$” ` >&2
echo “***** End Of Error Message *****” >&2
exit 1
fi

# Make sure that the log message contains word minimum
LOG=`/usr/bin/svnlook log -t “$TXN” “$REPOS”`
WORDCOUNT=`echo $LOG | wc -w`
if [ $WORDMIN -gt  $WORDCOUNT ];
then
echo “Commit log message must be at least 4 words long.” >&2
exit 1
fi

# All checks passed, so allow the commit.
exit 0

Leave a Reply

Your email address will not be published. Required fields are marked *


*