If you are using Subversion, you might have come across this issue. Multiple developers working on a set of files and committing them without any comments. If you decide to force comments on every commit, here is a little pre-commit hook script that will do the job. Just to be clear, a pre-commit script is fired when the user hits the ‘OK’ button on the commit dialog box.
This script would need to be under /{repository-location}/hooks/. There are some template scripts already in there and this one is probably a slight modification of one of those samples in there.
Here it is.
@echo off :: :: Stops commits that have empty log messages. :: @echo off setlocal rem Subversion sends through the path to the repository and transaction id set REPOS=%1 set TXN=%2 rem check for an empty log message svnlook log %REPOS% -t %TXN% | findstr . > nul if %errorlevel% gtr 0 (goto err) else exit 0 :err 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
#1 by marc esher on February 14, 2008 - 2:48 pm
Quote
Thanks for the post, Anuj! This is exactly what I needed, when I needed it. Fate….
#2 by Anuj Gakhar on February 14, 2008 - 3:04 pm
Quote
Good to know that Marc. It takes time to get used to comment before commit but definitely useful in the long run or when you are looking at commit stats etc..
#3 by pin7 on February 14, 2008 - 3:30 pm
Quote
Great one!
This is very usefull, thanks.
#4 by Lars Pohlmann on February 18, 2008 - 1:08 pm
Quote
thx. that’s really useful!
#5 by Jeroen Leenarts on February 18, 2008 - 1:47 pm
Quote
Here come the single dot “.” commit messages.
In my opinion, nice, but not at all effective I’m afraid.
#6 by Anuj Gakhar on February 18, 2008 - 1:58 pm
Quote
@Jeroen, I agree to that. Rules are always broken and always will be. But that doesnt mean you dont apply rules. Its just one of those things.
On a second thought, you could probably actually check the comment length and force atleast 5 or 10 characters. Havent tried but probably possible.
#7 by Luke Pearce on April 24, 2008 - 11:58 am
Quote
Works a charm – thanks for this.
#8 by Ryan Stille on June 16, 2008 - 7:32 pm
Quote
Might want to mention that this is for windows only. I started to copy and paste it and place it into my hooks directory, then noticed the echo, rem, etc. statements.
Pingback: Forcing SVN Comments with TortoiseSVN | Coldfusion/Flex Blog
#9 by Shin on October 30, 2008 - 4:11 am
Quote
is there a pre-load event? i wanna pre-populate the comment, for example
Comment:
Issue No:
Pupose:
etc.
#10 by Anuj Gakhar on October 30, 2008 - 11:32 am
Quote
@shin,
not sure about custom variables but there are some inbuilt properties which you can use for keyword substitution.
http://svnbook.red-bean.com/en/1.0/ch07s02.html#svn-ch-7-sect-2.3.4
#11 by Ajay Jadhav on November 6, 2008 - 2:53 pm
Quote
Is there a similar hook script available for Linux based SVN.
#12 by Anuj Gakhar on November 9, 2008 - 12:14 pm
Quote
@Ajay, there must be one. When you install SVN, there is a default pre-commit.tmpl file in there which has some commented out code for comment verification. you might want to give that a go.
#13 by raj on November 24, 2008 - 9:54 pm
Quote
Hi Anuj,
I have pre-commit.bat to customize the commit process in svn . I am calling a perl script from this batch file. I have to pass the Repos and TXN into the perl script. How can I do that?
thanks
RJ
#14 by Anuj Gakhar on December 20, 2008 - 1:18 pm
Quote
@Raj, I am not sure how this can be done as I have never done this myself. There are some examples out there for integration with bug databases, I guess they must be doing the same thing, you might want to have a look.
Pingback: Kommentare beim Committen ins SVN erzwingen « mirabis Development Blog
#15 by Raj on March 25, 2009 - 2:10 am
Quote
I copied and paste the script from this website under /{repository-location}/hooks/ and named it force-commit.tmpl to make it similar to the other hooks. I am still able to checkin the empty messages. Any help would be appreciated.
#16 by Anuj Gakhar on March 25, 2009 - 9:58 am
Quote
@Raj, I think it should be called pre-commit.bat if I am not mistaken.
http://svnbook.red-bean.com/en/1.4/svn.ref.reposhooks.pre-commit.html
#17 by joan severance illicit behavior on April 12, 2009 - 12:56 pm
Quote
I have pre-commit.bat to customize the commit process in svn . I am calling a perl script from this batch file.
#18 by Senthil on April 20, 2009 - 6:26 pm
Quote
Great script. However developer training is the more essential part of the equation. Establish a good commenting standard & guideline and make sure they follow it. The script can then be enhanced to validate the standard (such as Defect id, defect type, etc)
#19 by Anuj Gakhar on April 20, 2009 - 8:36 pm
Quote
@Senthil, I totally agree to that. If someone wants to break this script, they can by entering blank spaces, whats important is the developer training.
#20 by KOGI on April 22, 2009 - 8:33 pm
Quote
Here is a simple *nix version that I use:
#!/bin/sh
REPOS=”$1″
TXN=”$2″
# Make sure that the log message contains some text.
SVNLOOK=/usr/local/bin/svnlook
$SVNLOOK log -t “$TXN” “$REPOS” | grep “.\{5,\}” && exit 0
# If we haven’t already exited, there was an error
echo “*** Your commit has been blocked because you didn’t give any log message or your log message was too short.” 1>&2
echo “Please write a log message describing the purpose of your changes and then try committing again.” 1>&2
exit 1
#21 by Anuj Gakhar on April 23, 2009 - 12:12 pm
Quote
@Kogi, thanks for posting that here. Much appreciated.
#22 by Anuj on May 10, 2009 - 6:16 am
Quote
Anuj,
I am having trouble, getting the script to fire.In other words, I can still get the commit to go ahead without any comments.
I have SVN running on a Windows XP machine. I restarted SVN after adding the script to the pre-commit.tmpl in the {hooks} directory.Any ideas??
Thanks,
John
#23 by Eric on May 11, 2009 - 3:51 pm
Quote
@ John –
For Windows the tmpl files must be renamed in order to execute for example pre-commit.bat
- Eric
#24 by Anonymous on May 15, 2009 - 3:23 pm
Quote
Thank you. And I hate to spoil the party but you really mean “comment” not “log message”. That message should make sense. Logs are not comments. It’s important for those error messages to make sense to everyone on the team. Nobody will know what the heck you’re talking about when you say log message.
#25 by Eric on May 18, 2009 - 6:12 pm
Quote
The people who wrote the subversion manuals will know “what the heck you’re talking about when you say log message” because that is what they call it. From page 27 of the svn-book .pdf: “When you commit a change, you need to supply a log
message describing your change. Your log message will be attached to the new revision you create.”
#26 by Joseph on April 15, 2010 - 10:59 am
Quote
Awsome!!! works great appreciate!!
Pingback: SVN force comments on Windows without Subversion Notify « Yet Another Blog
#27 by DBA_Othman on July 6, 2010 - 12:56 pm
Quote
Works perfectly. Anyway, how can I implement it to make sure that the user must write the comment in a specific format ?
Exemple ==>
Name : XXX
Social: YYY
..etc
Thank’s
#28 by Anuj Gakhar on July 7, 2010 - 7:42 pm
Quote
DBA_Otham, I would assume some kind of String / Regex comparison will need to be done. This would be specific to your needs and there is no generic way of doing it, as far as I know.
#29 by DBA_Othman on July 8, 2010 - 10:02 am
Quote
Thank’s for the answer.
I tried to edit a regex using findstr but it doesn’t work 100% as I want.
Which language can I use on that .bat file to write my prog ?
#30 by Anuj Gakhar on July 8, 2010 - 10:20 am
Quote
I assume VBscript would work within a .bat file although I have never tried doing it, but fundamentally it should work.
#31 by DBA_Othman on July 8, 2010 - 10:40 am
Quote
Thank you sur.
#32 by DBA_Othman on July 8, 2010 - 10:58 am
Quote
Hi,
Have you ever worked with MYLYN on eclipse ?
#33 by Anuj Gakhar on July 8, 2010 - 12:04 pm
Quote
I installed MYLYN once and never continued using it purely because there was no need to do so in my projects. But I have heard good things about it.
#34 by DBA_Othman on July 8, 2010 - 12:17 pm
Quote
I’ve just integrated it with Eclipse + SVN and soon Mantis
Could you please share you knowlege about mylyn ?
#35 by wqs on August 17, 2010 - 6:57 pm
Quote
very nice article indeed