How to force Comments on SVN Commit

On February 14, 2008, in Subversion, by Anuj Gakhar

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]

Tagged with:  

70 Responses to How to force Comments on SVN Commit

  1. marc esher says:

    Thanks for the post, Anuj! This is exactly what I needed, when I needed it. Fate….

  2. Anuj Gakhar says:

    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. pin7 says:

    Great one!
    This is very usefull, thanks.

  4. thx. that’s really useful!

  5. Here come the single dot “.” commit messages.

    In my opinion, nice, but not at all effective I’m afraid.

  6. Anuj Gakhar says:

    @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. Luke Pearce says:

    Works a charm – thanks for this.

  8. Ryan Stille says:

    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.

  9. […] of my earlier posts about forcing SVN comments talks about doing a pre-commit hook on SVN before every commit is made. […]

  10. Shin says:

    is there a pre-load event? i wanna pre-populate the comment, for example

    Comment:
    Issue No:
    Pupose:
    etc.

  11. Anuj Gakhar says:

    @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

  12. Ajay Jadhav says:

    Is there a similar hook script available for Linux based SVN.

  13. Anuj Gakhar says:

    @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.

  14. raj says:

    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

  15. Anuj Gakhar says:

    @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.

  16. […] Hooks kann erzwungen werden, dass Kommentare gesetzt werden müssen. Dies ist in diesem Blog beschrieben. Der Hook ist für Windows Betriebssysteme geschrieben […]

  17. Raj says:

    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.

  18. Anuj Gakhar says:

    @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

  19. I have pre-commit.bat to customize the commit process in svn . I am calling a perl script from this batch file.

  20. Senthil says:

    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)

  21. Anuj Gakhar says:

    @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.

  22. KOGI says:

    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

  23. Anuj Gakhar says:

    @Kogi, thanks for posting that here. Much appreciated.

  24. Anuj says:

    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

  25. Eric says:

    @ John –
    For Windows the tmpl files must be renamed in order to execute for example pre-commit.bat

    – Eric

  26. Anonymous says:

    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.

  27. Eric says:

    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.”

  28. Joseph says:

    Awsome!!! works great appreciate!!

  29. […] firemanworld Leave a comment Go to comments I have seen many similar posts like this, like here and […]

  30. DBA_Othman says:

    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

  31. Anuj Gakhar says:

    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.

  32. DBA_Othman says:

    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 ?

  33. Anuj Gakhar says:

    I assume VBscript would work within a .bat file although I have never tried doing it, but fundamentally it should work.

  34. DBA_Othman says:

    Thank you sur. 🙂

    • VC says:

      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.
      ..

  35. DBA_Othman says:

    Hi,
    Have you ever worked with MYLYN on eclipse ?

  36. Anuj Gakhar says:

    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.

  37. DBA_Othman says:

    I’ve just integrated it with Eclipse + SVN and soon Mantis
    Could you please share you knowlege about mylyn ?

  38. wqs says:

    very nice article indeed

  39. VC says:

    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.

  40. sravz says:

    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

  41. sravz says:

    Hi Anuj

    Could you help me write a pre-commit script in CVS? Let me know if this is possible?

    thanks in advance

  42. Tom says:

    Thanks for the *nix version!

  43. Eliel says:

    Great script, i’m gonna use it. Thanks =3

  44. Sean Franco says:

    Hi Anuj, thanks for posting this script. Anyone can really make use of them. You are very helpful when it comes to web developing. 😀

  45. Raluca Stone says:

    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.

  46. Vinay says:

    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….

  47. priya says:

    how to force svn comments only for cpp files during commit?

  48. 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.

  49. sagar says:

    hello sir..

    i m adding this file to pre-commit.tmpl .. but its not work ..?? still it force commit with no comments.??

  50. sagar says:

    i m using windows 7 32 bit..

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar
%d bloggers like this: