Windows Pre-commit hook for comment length Subversion

30,315

Solution 1

This is a .bat file to require there is a comment. It checks for the existence of at least one character in the comment.

 @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           

 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 enter a comment. 1>&2  
 echo Write a log message describing the changes made and try again. 1>&2
 echo Thanks 1>&2
 exit 1

This file sits in the /hooks folder of the repository, named pre-commit.bat. If you need a minimum amount of characters, the line to modify is

svnlook log %REPOS% -t %TXN% | findstr . > nul

So if you wanted a minimum of 10 characters, you need to have 10 .'s rather than just one

svnlook log %REPOS% -t %TXN% | findstr .......... > nul

More advanced options for the findstr command will let you do fancier checks (certain character sets, ect)

Solution 2

I use SubversionNotify, it probably does more than what you need, but is pretty simple to set up.

Solution 3

I have a pre-commit hook that can do exactly what you want. Plus a lot more.

  • You can specify a minimum length of commit comment.
  • You can match the commit comment against a regular expression. Not only can you specify a length, but you can also specify certain parameters. For example, does the commit comment contain a bug number that your defect tracking system uses, so you can trace back the change to a particular defect?

It also allows you to do the following:

  • Set various commit permissions against particular files or directories:
    • read-write: User can checkout and commit these items.
    • read-only: User can checkout this item, but can't commit changes.
    • add-only: User can add a directory via svn cp, but not commit any changes. This is perfect for the /tags directory where you are allowed to make a tag, but not modify the tag.
    • no-delete: Users can commit changes and add new files, but not delete these files.
    • no-add: Users can only commit changes, and not add or delete files in a commit.

And, it also allows you to do this:

  • Ban certain file names via regular expressions of globbing,
  • Require certain files or directories have a particular property set to a particular value. Very useful for things like making sure Unix shell scripts, Unix Makefiles, and Windows Batch files have the correct line ending, or svn:ignore is set, so users don't accidentally commit in files they shouldn't commit.
  • Require certain revisions properties to be set with certain values. This is how you check commit messages, but saying that svn:log must match certain regular expressions.

This pre-commit script is written in Perl. By default, Perl comes with Unix, Mac, and Linux servers. Unfortunately, it isn't included on Windows computers. Fortunately, there are several open source, free, and easy to install Perl packages for the PC such as ActivePerl and Strawberry Perl

Solution 4

On Windows, you can use the VisualSVNServerHooks.exe check-logmessage pre-commit hook that comes with VisualSVN Server and is located in the %VISUALSVN_SERVER%bin directory. This simple tool will help you define the minimum allowed number of characters in the log message.

See the article KB140: Validating commit log messages in VisualSVN Server for instructions.

Share:
30,315
PositiveGuy
Author by

PositiveGuy

Updated on September 14, 2020

Comments

  • PositiveGuy
    PositiveGuy over 3 years

    I seem to be getting nowhere with this. Either searching the web for a script, etc. Anyone got a script that you can just edit the out-of-box pre-commit.tmpl in a Windows environment that requires x chars to be entered in for a comment on commit in Tortoise Subversion globally so that all members on the team are required whereas this requirement is pushed down to the clients from SVN server?

    I don't know the scripting language and this should be something pretty damn simple without me taking the time to figure out scripting for the next 3 hours.

  • PositiveGuy
    PositiveGuy almost 15 years
    I've seen that but it's not easy to modify when you don't know the syntax.
  • PositiveGuy
    PositiveGuy almost 15 years
    even tried putting that in the hook for pre-commit (server-side) and my client was not forced to enter a comment anyway
  • Corey Downie
    Corey Downie almost 15 years
    the key is in the svnlook line of the code. You will need to ensure you have a proper path to the svnlook command for this script to work.
  • PositiveGuy
    PositiveGuy almost 15 years
    Looked at this, but don't see a way to limit comments or any regex syntax that shows you how in reference to this application.
  • PositiveGuy
    PositiveGuy almost 15 years
    so if I want 100 chars I have to add 100 dots! are you serious?
  • PositiveGuy
    PositiveGuy almost 15 years
    I don't know what you mean by proper path to the svnlook
  • PositiveGuy
    PositiveGuy almost 15 years
    you paste the script in above to the pre-commit on server-side in that repository I'm working with and it still doesn't require comments to be entered...
  • PositiveGuy
    PositiveGuy almost 15 years
    so what transaction ID do you specify or is that a placeholder subversion fills once it runs this hook?
  • PositiveGuy
    PositiveGuy almost 15 years
    also, do you create the .bat and the .tmpl are for another language then and they don't run or what?
  • Corey Downie
    Corey Downie almost 15 years
    Yes, 100 characters = 100 dots with that script. But you only have to do it once... You need to make sure that on the svn server, when you goto a command prompt and type 'svnlook' something happens. svnlook is a command line tool and you might need to have a path like 'c:\Program Files\...\svnlook' for the script to work depending on how your system's PATH variable is set up. 1% and %2 are reading in the arguments that are passed to the hook from svn. We are assigning it to the REPOS, TSN variable for later use.
  • jpierson
    jpierson almost 13 years
    As a followup, what do you think a reasonable restriction would be fore size of comment. Previously I was using the default one character check but I think I would like to avoid people entering comments like "updated", don't laugh that was an actual comment in our system. I was thinking 50 but I relaxed it to 25.
  • fifth
    fifth over 9 years
    @CoreyDownie, is your solution only running on server? I tried it at client side, always got error message "svnlook: E720003: Can't open file 'C:\Users\me\AppData\Local\Temp\svnFF1C.tmp\format': The system cannot find the path specified." The command line was "svnlook.exe log "C:\Users\me\AppData\Local\Temp\svnFF1C.tmp" -t "3"", weird
  • Corey Downie
    Corey Downie over 9 years
    @fifth Yes I had this running on the server side, svnlook is only available on the server where the repo sits. You may be able to modify the script using 'svn log' instead on the client side but I haven't tried it.
  • Stewart
    Stewart about 3 years
    This applies to VisualSVN 3.9 and up. If you are older than 3.9 you'll need to find something else.