views:

4583

answers:

11

We've got VisualSVN set up as our Subversion server on Windows, and we use Ankhsvn + TortoiseSVN on our client workstations.

How can you configure the server to require commit messages to be non-empty?

+2  A: 

I believe you'll have to setup a pre-commit hook that will check for the message.

Indeed just by googling the first result I got was a perl pre-commit script to do exactly what you intended.

Perl pre-commit hook example (untested)

thismat
+14  A: 

SVN uses a number of hooks to accomplish tasks like this.

  • start-commit - run before commit transaction begins, can be used to do special permission checking
  • pre-commit - run at the end of the transaction, but before commit. Often used to validate things such as a non zero length log message.
  • post-commit - runs after the transaction has been committed. Can be used for sending emails, or backing up repository.
  • pre-revprop-change - runs before a revision property change. Can be used to check permissions.
  • post-revprop-change - runs after a revision property change. Can be used to email or backup these changes.

You want the pre-commit hook. You can write it yourself in just about any language your platform supports, but there are a number of scripts on the web. Googling "svn precommit hook to require comment" I found a couple that looked like they would fit the bill:

Perl Script

Python Script

Jason Jackson
Ok, so if you edit one of those hooks, what is it!
CoffeeAddict
what's the code to require x chars on the pre-commit comments?
CoffeeAddict
Using Visual SVN Server
CoffeeAddict
You will have to decide what language you wish to use if you are writing your own hook. I would just go find a pre-written hook to accomplish whatever your needs are and try to find it in a language with which you are comfortable. You may need to modify the code, which shouldn't be to difficult in this case.
Jason Jackson
writing the script is the problem. I too can't just magically come up with a script that tells SVN to force comments. I too have struggled with this. I'm using Tortoise and Visual SVN server. It's not that easy when you have no clue even when looking at existing hooks.
CoffeeAddict
@coffeeaddict, I don't get what you mean. Do you mean you don't know how to write a script, or you aren't sure where to put it, or what? I just googled "pre-commit svn require comment" (http://www.google.com/search?q=pre-commit+svn+require+comment) and found all kinds of scripts. Here is the SVN book entry on the hook: http://svnbook.red-bean.com/en/1.4/svn.ref.reposhooks.pre-commit.html. Here is another part of the SVN book referring to installing hooks: http://svnbook.red-bean.com/en/1.4/svn.reposadmin.create.html
Jason Jackson
+12  A: 

The technical answers to your question have already been given. I'd like to add the social answer, which is: "By establishing commit message standards with your team and getting them to agree (or accept) reasons why one would need expressive commit messages"

I've seen so many commit messages that said "patch", "typo", "fix" or similar that I've lost count.

Really - make it clear to everybody why you'd need them.

Examples for reasons are:

  • Generated Changenotes (well - this'd actually make a nice automatic tool to enforce good messages if I know that they will be (with my name) publically visible - if only for the team)
  • License issues: You might need to know the origin of code later, e.g. should you want to change the license to your code (Some organizations even have standards for commit message formatting - well, you could automate the checking for this, but you'd not necessarily get good commit messages with this)
  • Interoperability with other tools, e.g. bugtrackers/issue management systems that interface with your version control and extract information from the commit messages.

Hope that helps, additionally to the technical answers about precommit hooks.

Olaf
I've worked for an organization that even went so far as to systematically roll back commits without acceptable commit messages, especially as the release approached.
Warren Pena
+2  A: 

What VisualSVN offers you to enter as hooks are "Windows NT command scripts", which are basically batch files.

Writing if-then-else in batch files is very ugly and probably very hard to debug.

It will look something like the following (search for pre-commit.bat) (not tested):

SVNLOOK.exe log -t "%2" "%1" | grep.exe "[a-zA-Z0-9]" > nul || GOTO ERROR
GOTO OK
:ERROR
ECHO "Please enter comment and then retry commit!"
exit 1
:OK
exit 0

You need a grep.exe on the path, %1 is the the path to this repository, %2 the name of the txn about to be committed. Also have a look at the pre-commit.tmpl in the hooks directory of your repository.

Ansgar
gbjbaanb
sorry, not an NT command script guru and don't have time to hack one up and hit my head learning it. Any chance you can tell us how to check the comment length here?
CoffeeAddict
A: 

Good question and good solution! By the way, how can we modify commited files in start commit or pre-comit hook?

You can't change a transaction in pre or start commit. The client assumes the file is committed unmodified, so even if this was possible it would break the working copy on the client.You should deny commits that are not valid (let the user fix it) or post commit fix it with an extra commit.
Bert Huijben
A: 

Prior to adding commit hooks to my server, I just distributed svnprops to the tortoisesvn clients.

So, as an alternative:

In tortoiseSVN -> Properties property name - add/set tsvn:logminsize appropriately.

This of course is no guarantee on the server as clients/users can opt not to do it, but you can distribute svnprops files if you like. This way users don't have to set their own values - you can provide them to all users.

This also works for things like bugtraq: settings to link issue tracking stuff in the logs.

Tim
+2  A: 

Use this on Windows:

svnlook log -t "%2" "%1" | c:\tools\grep -c "[a-zA-z0-9]" > nul
if %ERRORLEVEL% NEQ 1 exit 0

echo Please enter a check-in comment 1>&2
exit 1

You'll need a copy of grep, I recommend the gnu tools version.

gbjbaanb
what about a comment length?
CoffeeAddict
the gnu tools link you gave does not work. The download is not available.
irperez
its on SF! http://downloads.sourceforge.net/project/unxutils/unxutils/current/UnxUtils.zip?use_mirror=kent
gbjbaanb
A: 

tried this but no luck:

svnlook log -t "%2" "%1" | c:\tools\grep -c "[a-zA-z0-9]" > nul
if %ERRORLEVEL% NEQ 1 exit 0

echo Please enter a check-in comment 1>&2
exit 1

REPOS="$1"
TXN="$2"

# Make sure that the log message contains some text.
SVNLOOK=/usr/local/bin/svnlook
$SVNLOOK log -t "$TXN" "$REPOS" | \
   grep "[a-zA-Z0-9]" > /dev/null || exit 1

# Check that the author of this commit has the rights to perform
# the commit on the files and directories being modified.
commit-access-control.pl "$REPOS" "$TXN" commit-access-control.cfg || exit 1


# All checks passed, so allow the commit.
exit 0

also, how to check for comment length is more important than no comment at all!

CoffeeAddict
just the first bit is needed, as described in my answer. That's what I have in my hook. If you're using perl in your scripts, use a bit of perl to determine length. the length() call is what you want.
gbjbaanb
+2  A: 

Here's a Windows Shell JScript that you can use by specifying the hook as:

%SystemRoot%\System32\CScript.exe //nologo <..path..to..script> %1 %2

It's pretty easy-to-read, so go ahead an experiment.

BTW the reason to do this in JScript is that it does not rely on any other tools (Perl, CygWin, etc) to be installed.

if (WScript.Arguments.Length < 2) 
{   
    WScript.StdErr.WriteLine("Repository Hook Error: Missing parameters. Should be REPOS_PATH then TXN_NAME, e.g. %1 %2 in pre-commit hook");
    WScript.Quit(-1);
}

var oShell = new ActiveXObject("WScript.Shell");
var oFSO = new ActiveXObject("Scripting.FileSystemObject");

var preCommitStdOut = oShell.ExpandEnvironmentStrings("%TEMP%\\PRE-COMMIT." + WScript.Arguments(1) + ".stdout");
var preCommitStdErr = oShell.ExpandEnvironmentStrings("%TEMP%\\PRE-COMMIT." + WScript.Arguments(1) + ".stderr");

var commandLine = "%COMSPEC% /C \"C:\\Program Files\\VisualSVN Server\\bin\\SVNLook.exe\" log -t ";

commandLine += WScript.Arguments(1);
commandLine += " ";
commandLine += WScript.Arguments(0);
commandLine += "> " + preCommitStdOut + " 2> " + preCommitStdErr;


// Run Synchronously, don't show a window
// WScript.Echo("About to run: " + commandLine);
var exitCode = oShell.Run(commandLine, 0, true);

var fsOUT = oFSO.GetFile(preCommitStdOut).OpenAsTextStream(1);
var fsERR = oFSO.GetFile(preCommitStdErr).OpenAsTextStream(1);

var stdout = fsOUT && !fsOUT.AtEndOfStream ? fsOUT.ReadAll() : "";
var stderr = fsERR && !fsERR.AtEndOfStream ? fsERR.ReadAll() : "";

if (stderr.length > 0)
{
WScript.StdErr.WriteLine("Error with SVNLook: " + stderr);
WScript.Quit(-2);
}

// To catch naught commiters who write 'blah' as their commit message

if (stdout.length < 5)
{
WScript.StdErr.WriteLine("Please provide a commit message that describes why you've made these changes.");
WScript.Quit(-3);
}

WScript.Quit(0);
JBRWilkinson
This works great.
irperez
The only issue I have is that it still accepts "blah"... weird.
irperez
+7  A: 

This is ours. No extra tools required.

I'm glad you asked this question

pre-commit:

setlocal 

set REPOS=%1
set TXN=%2

set SVNLOOK="C:\Program Files\VisualSVN Server\bin\svnlook.exe"

REM Make sure that the log message contains some text.
FOR /F "usebackq delims==" %%g IN (`%SVNLOOK% log -t %TXN% %REPOS% FINDSTR /R /C:......`) DO goto NORMAL_EXIT



:ERROR_TOO_SHORT
echo "Commit note must be at least 6 letters" >&2
goto ERROR_EXIT

:ERROR_EXIT
exit /b 1

REM All checks passed, so allow the commit.
:NORMAL_EXIT
exit 0
sylvanaar
this is awesome!
Sander Versluys
i've got a little improvement which is usefull in 64bit version of windows 2008: instead of using "C:\Program Files\Vis..." use Windows Environment Variables like "%PROGRAMFILES%\Vis...."
Sander Versluys
It's good practive to use environment variable VISUALSVN_SERVER to discover location of svnlook. I.e.:set SVNLOOK="%VISUALSVN_SERVER%\bin\svnlook.exe"
Ivan Zhakov
@ivan Even better! ;-)
Sander Versluys
A: 

Hi I've installed VisualSVN Server 2.1.3 on my computer (XP Pro SP3) and the server is running correctly My Subversion client is TortoiseSVN 1.6.10 However, I'd like to use a pre commit hook to check empty comment in the commit dialog window. But It didn't work at all: here is a snapshot of my server structure: maquette architecture serveur I made right click on "Main"-->"Properties"-->"Hooks"-->"Pre-commit hook"

here is the batch script that I put in the proper window:


setlocal

set REPOS=%1 set TXN=%2

set SVNLOOK="C:\Program Files\VisualSVN Server\bin\svnlook.exe"

REM Faire en sorte que les Developpeurs Bancs de Test laissent un commentaire avant chaque "commit".

FOR /F "usebackq delims==" %%g IN (%SVNLOOK% log -t %TXN% %REPOS% FINDSTR /R /C:......) DO goto NORMAL_EXIT

:ERROR_TOO_SHORT echo "Un commentaire d'au moins six caracteres est demandé. Merci" >&2 goto ERROR_EXIT

:ERROR_EXIT exit /b 1

REM verification effectuée, livraison en cours.

:NORMAL_EXIT

exit 0


But once in tortoiseSVN, without any comment, the commit is always allowed and files are updated.

What's wrong with my setting up ?

Sources :http://stackoverflow.com/questions/247888/how-to-require-commit-messages-in-visualsvn-server

Cordially Ben

Baron Ben