|
 Thursday, April 26, 2007
« Good SharePoint HowTo Videos | Main | Flash & 64bit Internet Explorer dont pla... »

Forcing users to provide a message in SVN/TortoiseSVN (Including HOWTO)

I use Subversion (SVN) for my source control and I access SVN through TortoiseSVN so that its integrated into the Windows shell. The past few companies I've worked at have all utilized SVN in one way or another, but we've always had a problem with developers submitting code without attaching a message to the commit.

Why does this matter? If you, as a developer, check in 20 files that are related to project trunk X, without a message, how am I suppossed to know why you checked those into the repository? Did you fix a bug? Did you implement a new feature? Did you complete a task? Did you add a new project to the system? What did you do? Thats the problem if you do not provide a message for each commit, others have no idea of knowing what you did

By not providing the message we're not keeping track of what we did and why we did it. This comes heavily into play when you need to revert back to a certain revision, but you're not sure which revision to go back to. The only thing you remember is "I know it was when we implemented feature 'ABC' into the system." If I provided a comment, I could see in the Tortoise Log that on MM/DD/YYYY at XX:XX time developer "Bob" implemented the feature. Knowing that, I could find the version number and revert back to that revision. Without this information I might spend HOURS UPON HOURS searching through lines of code looking for a key piece that helps me decide if this is the version I want. This could have been avoided if a simple message was provided.

HOWTO: Requiring Messages

By default SVN does not require a message when committing. You can tap into the hooks of SVN and provide some custom code to force your developers to provide a message. The script to do this is below. Note: I cannot take credit for creating this script, as it was found on the net and I dont have the source. So, if you wrote this script, please let me know and I'll add your name here as the author. :) 

Steps:

  1. Create Script
  2. Save as pre-commit.bat
  3. Drop it into the hooks directory of your repository.
  4. DONE

The Script (also provided at the bottom of this post as a download)


@echo off
::
:: Stops commits that have empty log messages.
::

setlocal

set "REPOS=%~1"
set "TXN=%~2"
set "SVNLOOK=c:\progra~1\subversion\bin\svnlook.exe"

:: Make sure that the log message contains some text.
for /f "tokens=*" %%i in ('%SVNLOOK% log -t "%TXN%" "%REPOS%"') do set "LOGMSG=%%i"
if not "%LOGMSG%"=="" exit 0

echo. 1>&2
echo Your commit has been blocked because you didn't give any log message! 1>&2
echo Please write a log message describing the purpose of your changes and 1>&2
echo then try committing again. -- Thank you 1>&2
exit 1


Please note: This assumes that you have Subversion installed in the C:\Program Files\

Save this script as "pre-commit.bat".

Now drop this into your hooks directory of your actual repository. SVN will now execute this batch file each time a commit is taking place. It will check that a message exists. If one does not, it will throw an error message that is highlighted in dark red (above, in the script).

If you are using TortoiseSVN, this is what you will see:

Thats it. Now users cannot commit without a message. :)

 

Good Message Recommendations

Even if messages are required it still does not prevent a user from typing some garbage into the screen. Here's what I recommened to my team...

SVN Messages should contain:

  1. What you did. Give a quick explanation "altered file processing routine" or "created new project library" 
  2. Why you did it. This could be as simple as stating "Fixed Bug 456" or "added new library to handle file conversions."

Example messages:

"Fixed bug 456 by altering file processing routine"

"Added new library to project XYZ to handle file conversions".

There are also many other plug-ins that have been built for TortoiseSVN and SVN in general. Just search for them and you should be able to find something that matches your bug tracking system (such as Gemini).

Conclusion

Forcing the developers to provide a message has enabled the team to communicate better. No longer are members constantly asking the question of "when was this fix applied?" or "What version did this happen in?" The messages make the respository easier to manage and a respository thats easy to manage means we have a healthier code base. :)

Download

pre-commit.zip (.48 KB)

kick it on DotNetKicks.com

#    Comments [12] |
Thursday, April 26, 2007 9:56:42 AM (Eastern Standard Time, UTC-05:00)
The next step is to require users to enter a ticket number into their commit messages, at least if you're using something like Trac or JIRA which have Subversion integration. Quite nice, because then your ticket system is immediately also a source for finding which changes were related to which ticket. For Trac, there are example hooks available, and I'm sure Atlassian also has hooks available for that.
Tuesday, May 01, 2007 8:33:30 AM (Eastern Standard Time, UTC-05:00)
...or you could use the sv:minlogsize property.
Pete
Monday, May 07, 2007 12:57:56 PM (Eastern Standard Time, UTC-05:00)
Thanks for this entry... I was able to drop it into our environment and presto-chango it works.
RobH
Monday, May 07, 2007 1:43:28 PM (Eastern Standard Time, UTC-05:00)
No problem. I'm glad it worked for you. Its worked wonders for my team.
Thursday, May 10, 2007 10:18:00 AM (Eastern Standard Time, UTC-05:00)
One follow up question... we've encountered a problem when the log message includes double quotes. If, for example, the log message entered is:

Performance improvement: added nolock hints to "select association data from app" queries in an attempt to prevent blocking

the commit errors saying:
Error Commit failed (details follow):
Error MERGE request failed on '/svn/test/testme/trunk/docs'
Error 'pre-commit' hook failed with output:
Error association was unexpected at this time.

Is this a limitation of the hook being written as a batch file? It looks to me like the 1st double quote is terminating the results of the call to svnlook log. Have you seen this before? Do we just avoid double quotes?
RobH
Thursday, May 10, 2007 1:25:24 PM (Eastern Standard Time, UTC-05:00)
@RobH,
It looks like this line:

if not "%LOGMSG%"=="" exit 0

Is causing the error. When I have a free moment, I'll take a look at it. It definately has to do with double quotes. I need to escape the double quotes in the batch file. Once that is done double quotes should be ok. Thanks for bringing it up!
Tuesday, May 22, 2007 6:56:48 AM (Eastern Standard Time, UTC-05:00)
Hello RobH,

Thanks for this great script! I also ran into the same problem with double-quotes and found that a good work-around (well, at least it works so far) is to replace this line:

if not "%LOGMSG%"=="" exit 0

with:

if defined LOGMSG exit 0

This uses the DEFINED condition to evaluate whether the environment variable LOGMSG has been defined.

I'll be keeping an eye on this blog to see if anyone found problems with this or came up with something better ;-)
Tuesday, May 22, 2007 7:03:11 AM (Eastern Standard Time, UTC-05:00)
Woops, sorry Donn. I thanked the wrong person for this script :-) My comment was actually directed at you.

Chris
Tuesday, July 10, 2007 11:25:55 PM (Eastern Standard Time, UTC-05:00)
You might want to try Scmbug http://www.mkgnu.net/?q=scmbug

"Scmbug integrates software configuration management (SCM) with bug-tracking. It aims to solve the integration problem once and for all. It will glue any source code version control system (such as CVS, Subversion, Git) with any bug-tracking system (such as Bugzilla, Mantis, Request Tracker, Test Director)."


I currently use it to integrate Subversion (with Apache) and Bugzilla running on Windows 2003.

It is great!
Gary B
Friday, October 05, 2007 4:11:27 AM (Eastern Standard Time, UTC-05:00)
this ones great. I was searching for the exact thing...

Thanks a lot!!!
nikhil
Thursday, May 29, 2008 5:48:12 AM (Eastern Standard Time, UTC-05:00)
afaik tsvn:minlogsize only works for the tortoiseSVN client,
Patrik P
Thursday, May 29, 2008 11:37:45 AM (Eastern Standard Time, UTC-05:00)
here is a linux version

#!/bin/sh
#@file:pre-commit
#@author:Andrei McMillan
#@date:05-29-2008
#make sure that the user supply a comment
#

REPO=$1
REV=$2
SVNLOOK=/usr/bin/svnlook

#get the amount of words in the comment
comment=`$SVNLOOK info $REPO -t $REV | sed -n 3p`

if [ $comment != 0 ]
then
echo "OK" >&1
exit 0
else
echo "PLEASE ENTER A MESSAGE" >&2
exit 1
fi
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview