A few svn pre-commit hooks

I’ve recently been looking around for some pre-commit hooks for our subversion repositories. I figured that since I have them all up and running now, I might as well share them.
I can’t remember where I got all of these from, so if it’s one of yours, then I apologise. Feel free to contact me and I’ll give you credit for it.

Check for blank/empty commit messages and reject
This goes in hooks/pre-commit

REPOS="$1"
TXN="$2"

# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
SVNLOOKOK=1
$SVNLOOK log -t "$TXN" "$REPOS" | \
grep "[a-zA-Z0-9]" > /dev/null || SVNLOOKOK=0
if [ $SVNLOOKOK = 0 ]; then
echo Empty log messages are not allowed. Please provide a proper log message. 1>&2
exit 1
fi
exit 0

Check that the commit message has a reference to a JIRA issue
This goes in hooks/pre-commit, it also uses ‘check_log_message.sh’ which can be found down further.

REPOS="$1"
TXN="$2"

# Check log message for proper task/bug identification
if [ -x ${REPOS}/hooks/check_log_message.sh ]; then
${REPOS}/hooks/check_log_message.sh "${REPOS}" "${TXN}" 1>&2 || exit 1
fi
exit 0

check_log_message.sh

#!/bin/bash

REPOS="${1}"
TXN="${2}"

SVNLOOK=/usr/bin/svnlook

LOG_MSG_LINE1=`${SVNLOOK} log -t "${TXN}" "${REPOS}" | head -n1`

if (echo "${LOG_MSG_LINE1}" | egrep '^[a-zA-Z]+[-][1-9][0-9]*[:]?[\s]*.*$' > /dev/null;) \
|| (echo "${LOG_MSG_LINE1}" | egrep '^[nN][oO][jJ][iI][rR][aA][:]?[\s]*.*$' > /dev/null;) \
|| (echo "${LOG_MSG_LINE1}" | egrep '^\[maven-release-plugin\][\s]*.*$' > /dev/null;)
then
exit 0
else
echo ""
echo "Your log message does not contain a JIRA Issue identifier (or bad format used)"
echo "The JIRA Issue identifier must be the first item on the first line of the log message."
echo ""
echo "Proper JIRA format:  'AAA-000'"
echo "JIRA regex: '^[a-zA-Z]+[-][1-9][0-9]*[:]?[\s]*.*$'"
exit 1
fi

Check that commit message is more than 5 characters long
This goes in hooks/pre-commit

REPOS="$1"
TXN="$2"

SVNLOOK=/usr/bin/svnlook
LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS" | grep [a-zA-Z0-9] | wc -c)

if [ "$LOGMSG" -lt 5 ]; then
echo -e "Please provide a meaningful comment when committing changes." 1>&2
exit 1
fi
exit 0
Be Sociable, Share!

5 thoughts on “A few svn pre-commit hooks”

  1. Thanks for these suggestions.

    I added a check to reject the Tortoise default log messages

    LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS")

    if [ ${#LOGMSG} -lt 10 ]; then
    echo -e ">>>
    Your commit has been rejected.
    Commit again and provide a meaningful log comment: at least 10 characters!" 1>&2
    exit 1
    fi

    if [ "$LOGMSG" == "Moved remotely" -o "$LOGMSG" == "Copied remotely" -o "$LOGMSG" == "Renamed remotely" -o "$LOGMSG" == "Removed file/folder" ]; then
    echo -e ">>> Big sister is watching you …
    Your commit has been rejected.
    Commit again and provide a meaningful log comment: default messages like '$LOGMSG' are not accepted." 1>&2
    exit 1
    fi

  2. Thanks for these examples. Very useful.

    On my repository, I also reject Tortoise default log messages such as "Moved remotely".

    LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS")

    if [ ${#LOGMSG} -lt 10 ]; then
    echo -e ">>> Big brother is watching you …
    Your commit has been rejected.
    Commit again and provide a meaningful log comment: at least 10 characters!" 1>&2
    exit 1
    fi

    if [ "$LOGMSG" == "Moved remotely" -o "$LOGMSG" == "Copied remotely" -o "$LOGMSG" == "Renamed remotely" -o "$LOGMSG" == "Removed file/folder" ]; then
    echo -e ">>> Big sister is watching you …
    Your commit has been rejected.
    Commit again and provide a meaningful log comment: default messages like '$LOGMSG' are not accepted." 1>&2
    exit 1
    fi

  3. Hi,

    I have similar kind of requirement where I need to check if Issue # entered is present in defect tracking tool’s respective table to make sure that user enters correct issue number against every commit.

    I am new to hook scripts and did much research on it. It would be really helpful if anybody can help me how can I do that.. Thanks a lot in advance..

  4. Hi

    We need to check if Issue # entered is present in defect tracking tool (HP QC)to make sure that user enters correct issue number against every commit.

  5. I just wanted to point out to anyone who finds this blog that in order for these scripts to work in RHEL6 the following changes have to be made.

    Remove braces from check_log_messages.sh otherwise svnlook will have a bad file path.

    REPOS=”${1}”
    TXN=”${2}”

    Change these the below to 1>2& to handle standout/standerr
    >

Leave a Reply

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