2

I have found an example python script and have modified it to do checks for comments in pre-commit. My problem is that the text of the comment as parsed by python always ends up blank.

  • Env: Windows XP
  • SVN version: svn, version 1.5.6 (r36142) compiled Mar 6 2009, 14:54:47
  • Python 2.7

pre-commit.bat

C:\Python27\python %1\hooks\pre-commit.py %1 %2

pre-commit.py

import sys, os, string, re

SVNLOOK='C:\\SVN\\bin\\svnlook.exe'


# return true or false if this passed string is a valid comment
def check(comment):
    #define regular expression
    print comment
    p = re.match("[bB][uU][gG]\s([0-9]+|NONE)+", comment)
    print p
    return (p != None) #returns false if doesn't match

def result(r, txn, repos, log_msg, log_cmd):
    if r == 1:
        sys.stderr.write ("File: " + repos + " Comment: " + txn + "\n" +\
                          "Log Msg: " + log_msg + "\n" +\
                          "Log Cmd: " + log_cmd + "\n" +\
                            "Comments must have the format of \n'Bug X' \n" +\
                          "'Comment text' where X is the issue number.\n" +\
                          "Use comma to separatemultiple bug id's\n" +\
                          "Example:\nBug 1234, 1235\nComment: Fixed things\n" +\
                          "Use 'NONE' if there is no bug ID")
        sys.exit(r)

def main(repos, txn):
    log_cmd = '%s log %s -t %s' % (SVNLOOK, txn, repos)
    log_msg = os.popen(log_cmd, 'r').readline().rstrip('\n')

    if check(log_msg):
        result(0, txn, repos, log_msg, log_cmd)
    else:
        result(1, txn, repos, log_msg, log_cmd)

if __name__ == '__main__':
    if len(sys.argv) < 3:
        sys.stderr.write("Usage: %s REPOS TXN\n" % (sys.argv[0]))
    else:
        main(sys.argv[1], sys.argv[2])

I Added print outs of the vars into the error message for debugging purposes.

What is anoying is that if I use bat file commands, things seem to work fine:

pre-commit.bat that only checks for blank commit msg:

@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 line below ensures at least one character ".", 5 characters require change to "....."
C:\SVN\bin\svnlook.exe 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

what am I doing wrong?

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Lang
  • 21
  • 2
  • See my similar question: [Wrapping an SVN Python hook in a Windows batch file](http://stackoverflow.com/questions/4989364/wrapping-an-svn-python-hook-in-a-windows-batch-file). Chris Morgan's answer covers it, though. – detly May 25 '11 at 03:19

1 Answers1

3

When you run it through Python, the error code which is set by Python is ignored in the batch file; you will want to do it something like this:

C:\Python27\python %1\hooks\pre-commit.py %1 %2
exit %ERRORLEVEL%

It's a good idea to quote your vaules, as well, to make sure that they get through to Python intact:

C:\Python27\python "%1\hooks\pre-commit.py" "%1" "%2"
exit %ERRORLEVEL%
Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • unfortunatly, that does not fix the problem. – Lang May 25 '11 at 11:54
  • Ah! Found it. txn and repos were being handed to svn look in the wrong order! it should be: log_cmd = '%s log %s -t %s' % (SVNLOOK, repos, txn) – Lang May 25 '11 at 12:02