python svn pre-commit hook

evil tabby cat eviltabbycat at gmail.com
Mon Jul 2 09:12:07 EDT 2007


This is a python script which is fired off from a batchfile pre-
commit.bat on Windows2000 server.

Everything I've read says that it should work & each part does work
when tested individually.

But when it runs within subversion this statement always ends with
this log_msg being an empty string.
log_msg = os.popen(log_cmd, 'r').readline().rstrip('\n')

Help. :-)


rem pre-commit.bat
c:\python23\python c:\repository\hooks\check-comments.py %1 %2

if errorlevel 1 goto :ERROR

exit 0



:ERROR

echo Error found in commit 1>&2

exit 1



#check-comments.py
import sys, os, string, re

SVNLOOK='c:\\subversion\\bin\\svnlook.exe'

# return true or false if this passed string is a valid comment
def check(comment):
    #define regular expression
    p = re.compile('\A[iI][sS][sS][uU][eE] \d+ - \w+')
    return (p.match(comment) != None) #returns false if doesn't match

def result(r):
   if r == 1:
      sys.stderr.write ("Comments must have the format of 'Issue X -
Comment text' where X is the issue number.")
   sys.exit(r)

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

    if check(log_msg):
        result(0)
    else:
       result(1)

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])




More information about the Python-list mailing list