How to force Comments on SVN Commit
Tagged Under : Subversion
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







Thanks for the post, Anuj! This is exactly what I needed, when I needed it. Fate….
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..
Great one!
This is very usefull, thanks.
thx. that’s really useful!
Here come the single dot “.” commit messages.
In my opinion, nice, but not at all effective I’m afraid.
@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.
Works a charm – thanks for this.
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.
[...] of my earlier posts about forcing SVN comments talks about doing a pre-commit hook on SVN before every commit is made. [...]
is there a pre-load event? i wanna pre-populate the comment, for example
Comment:
Issue No:
Pupose:
etc.
@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
Is there a similar hook script available for Linux based SVN.
@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.
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
@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.
[...] Hooks kann erzwungen werden, dass Kommentare gesetzt werden müssen. Dies ist in diesem Blog beschrieben. Der Hook ist für Windows Betriebssysteme geschrieben [...]
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.
@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
I have pre-commit.bat to customize the commit process in svn . I am calling a perl script from this batch file.
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)
@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.
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
@Kogi, thanks for posting that here. Much appreciated.
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
@ John –
For Windows the tmpl files must be renamed in order to execute for example pre-commit.bat
- Eric
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.
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.”