2

我找到了一个示例 python 脚本并对其进行了修改,以便在提交前检查评论。我的问题是 python 解析的注释文本总是以空白结尾。

  • 环境:Windows XP
  • SVN 版本:svn,版本 1.5.6 (r36142) 编译于 2009 年 3 月 6 日,14:54:47
  • 蟒蛇 2.7

预提交.bat

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

预提交.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])

我将变量的打印输出添加到错误消息中以进行调试。

令人讨厌的是,如果我使用 bat 文件命令,事情似乎工作正常:

pre-commit.bat 仅检查空白提交消息:

@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

我究竟做错了什么?

4

1 回答 1

3

当您通过 Python 运行它时,Python 设置的错误代码在批处理文件中被忽略;你会想做这样的事情:

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

引用您的值也是一个好主意,以确保它们完整地通过 Python:

C:\Python27\python "%1\hooks\pre-commit.py" "%1" "%2"
exit %ERRORLEVEL%
于 2011-05-25T03:08:12.983 回答