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.
[xml]
@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
[/xml]
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.”
Awsome!!! works great appreciate!!
[…] firemanworld Leave a comment Go to comments I have seen many similar posts like this, like here and […]
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
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.
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 ?
I assume VBscript would work within a .bat file although I have never tried doing it, but fundamentally it should work.
Thank you sur. 🙂
Hi DBA_Othman,
Appreciate if you can share some code-snippets ans steps on how did you implement the commit comment-format. I am also looking for the same.
..
Hi,
Have you ever worked with MYLYN on eclipse ?
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.
I’ve just integrated it with Eclipse + SVN and soon Mantis
Could you please share you knowlege about mylyn ?
very nice article indeed
Hi DBA_Othman,
Appreciate if you can share some code-snippets ans steps on how did you implement the commit comment-format. I am also looking for the same.
Hi Anuj
I need to write a similar script in my project but unfortunately we are not using SVN but we use FISHEYE .Could you please suggest me if you know how to write a similar script in FISHEYE.
Your response is much appreciated.
Thanks
Sravamo
Hi Sravamo,
I do not have hands-on experience with Fisheye but I think the script would be the same if Fisheye supports post-commit hooks. Sorry, can’t be of more help here.
Hi Anuj
Could you help me write a pre-commit script in CVS? Let me know if this is possible?
thanks in advance
Hi Sravz, I have actually never used CVS. One of the things I never got a chance to work with. So, can’t help you much in that area.
Thanks for the *nix version!
Great script, i’m gonna use it. Thanks =3
Hi Anuj, thanks for posting this script. Anyone can really make use of them. You are very helpful when it comes to web developing. 😀
When it comes to this concern, the best way to force comments is to have good feedback towards a certain topic that was given.Though, thanks a lot for the useful tips that you have shared here.
Thanks for the tip Anuj,
In my SVN server system, there are many project folders. I want to restrict svn commit comments as mandatory for one specific folder and its subfolders.
Is there anyway to do this?
Thanks a lott….
how to force svn comments only for cpp files during commit?
Great post. I was checking constantly this blog and I am inspired! Very helpful information particularly the final phase 🙂 I take care of such information much. I used to be seeking this particular info for a very long time. Thank you and best of luck.
hello sir..
i m adding this file to pre-commit.tmpl .. but its not work ..?? still it force commit with no comments.??
which OS are you on?
i m using windows 7 32 bit..
Sagar, read the comments above in the post. Other people have the same issues. the file should be called .bat instead of .tmpl and then it should work.
but there is no .bat file in my /hooks
???..
you need to create one…
Hello yes it run good on windows .. …thnx
but in linux where i add this command.??