首页 / 知识

如何在Windows环境中用Trac和SVN实现提交后挂接?

2023-04-16 15:27:00

如何在Windows环境中用Trac和SVN实现提交后挂接?

How do I implement the Post Commit Hook with Trac & SVN in a Windows Environment?

我在带有Trac / SVN的Windows环境中运行,并且我希望提交到存储库以将其集成到Trac中并关闭SVN注释中提到的错误。

我知道有一些后提交钩子可以做到这一点,但是关于如何在Windows上进行操作的信息并不多。

有人成功完成了吗? 您要遵循的步骤是什么?

这是我需要在SVN中放置的钩子,但是我不确定在Windows环境中如何执行此操作。

Trac Post提交挂钩


本杰明的答案很接近,但是在Windows上,您需要为钩子脚本文件提供可执行扩展名,例如.bat或.cmd。我使用.cmd。您可以采用模板脚本(它们是unix shell脚本,shell脚本)并将其转换为.bat / .cmd语法。

但是要回答与Trac集成的问题,请遵循以下步骤。

  • 确保Python.exe在系统路径上。这将使您的生活更轻松。

  • 在\ hooks文件夹中创建post-commit.cmd。这是Subversion将在提交后事件上执行的实际钩子脚本。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    @ECHO OFF

    :: POST-COMMIT HOOK
    ::
    :: The post-commit hook is invoked after a commit.  Subversion runs
    :: this hook by invoking a program (script, executable, binary, etc.)
    :: named 'post-commit' (for which this file is a template) with the
    :: following ordered arguments:
    ::
    ::   [1] REPOS-PATH   (the path to this repository)
    ::   [2] REV          (the number of the revision just committed)
    ::
    :: The default working directory for the invocation is undefined, so
    :: the program should set one explicitly if it cares.
    ::
    :: Because the commit has already completed and cannot be undone,
    :: the exit code of the hook program is ignored.  The hook program
    :: can use the 'svnlook' utility to help it examine the
    :: newly-committed tree.
    ::
    :: On a Unix system, the normal procedure is to have 'post-commit'
    :: invoke other programs to do the real work, though it may do the
    :: work itself too.
    ::
    :: Note that 'post-commit' must be executable by the user(s) who will
    :: invoke it (typically the user httpd runs as), and that user must
    :: have filesystem-level permission to access the repository.
    ::
    :: On a Windows system, you should name the hook program
    :: 'post-commit.bat' or 'post-commit.exe',
    :: but the basic idea is the same.
    ::
    :: The hook program typically does not inherit the environment of
    :: its parent process.  For example, a common problem is for the
    :: PATH environment variable to not be set to its usual value, so
    :: that subprograms fail to launch unless invoked via absolute path.
    :: If you're having unexpected problems with a hook program, the
    :: culprit may be unusual (or missing) environment variables.
    ::
    :: Here is an example hook script, for a Unix /bin/sh interpreter.
    :: For more examples and pre-written hooks, see those in
    :: the Subversion repository at
    :: http://svn.collab.net/repos/svn/trunk/tools/hook-scripts/ and
    :: http://svn.collab.net/repos/svn/trunk/contrib/hook-scripts/

    setlocal

    :: Debugging setup
    :: 1. Make a copy of this file.
    :: 2. Enable the command below to call the copied file.
    :: 3. Remove all other commands
    ::call %~dp0post-commit-run.cmd %* > %1/hooks/post-commit.log 2>&1

    :: Call Trac post-commit hook
    call %~dp0trac-post-commit.cmd %* || exit 1

    endlocal
  • 在\ hooks文件夹中创建trac-post-commit.cmd:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    @ECHO OFF
    ::
    :: Trac post-commit-hook script for Windows
    ::
    :: Contributed by markus, modified by cboos.

    :: Usage:
    ::
    :: 1) Insert the following line in your post-commit.bat script
    ::
    :: call %~dp0\\trac-post-commit-hook.cmd %1 %2
    ::
    :: 2) Check the 'Modify paths' section below, be sure to set at least TRAC_ENV

    setlocal

    :: ----------------------------------------------------------
    :: Modify paths here:

    :: -- this one *must* be set
    SET TRAC_ENV=D:\\projects\\trac\\membershipdnn

    :: -- set if Python is not in the system path
    SET PYTHON_PATH=

    :: -- set to the folder containing trac/ if installed in a non-standard location
    SET TRAC_PATH=
    :: ----------------------------------------------------------

    :: Do not execute hook if trac environment does not exist
    IF NOT EXIST %TRAC_ENV% GOTO :EOF

    set PATH=%PYTHON_PATH%;%PATH%
    set PYTHONPATH=%TRAC_PATH%;%PYTHONPATH%

    SET REV=%2

    :: Resolve ticket references (fixes, closes, refs, etc.)
    Python"%~dp0trac-post-commit-resolve-ticket-ref.py" -p"%TRAC_ENV%" -r"%REV%"

    endlocal

  • 在\ hooks文件夹中创建trac-post-commit-resolve-ticket-ref.py。我使用了EdgeWall的相同脚本,只是我将其重命名以更好地阐明其用途。


  • 好了,现在我已经花了很多时间在弄清一切之后发布我的经验,并感谢Craig使我走上正确的道路。这是您需要做的(至少在SVN v1.4和Trac v0.10.3中):

  • 找到要为其启用"提交后挂接"的SVN存储库。
  • 在SVN存储库中,有一个名为hooks的目录,您将在其中放置发布后提交钩子。
  • 创建一个文件post-commit.bat(这是SVN post commit自动调用的批处理文件)。
  • 将以下代码放入post-commit.bat文件中(这将调用您的post commit cmd文件,并传入SVN自动传递的参数,其中%1是存储库,%2是已提交的修订版。
  • %?dp0 \ trac-post-commit-hook.cmd%1%2

  • 现在,按如下所示创建trac-post-commit-hook.cmd文件:
  • @ECHO OFF :: :: Trac
    post-commit-hook script for
    Windows :: :: Contributed by
    markus, modified by cboos. ::
    Usage: :: :: 1) Insert the
    following line in your post-commit.bat
    script :: :: call
    %~dp0\\trac-post-commit-hook.cmd %1
    %2 :: :: 2) Check the 'Modify
    paths' section below, be sure to set
    at least TRAC_ENV ::
    ---------------------------------------------------------- :: Modify paths here: :: --
    this one must be set SET
    TRAC_ENV=C:\\trac\\MySpecialProject
    :: -- set if Python is not in the
    system path :: SET
    PYTHON_PATH= :: -- set to the
    folder containing trac/ if installed
    in a non-standard location :: SET
    TRAC_PATH= ::
    ---------------------------------------------------------- :: Do not execute hook if trac
    environment does not exist IF NOT
    EXIST %TRAC_ENV% GOTO :EOF
    set PATH=%PYTHON_PATH%;%PATH% set
    PYTHONPATH=%TRAC_PATH%;%PYTHONPATH%
    SET REV=%2 :: GET THE
    AUTHOR AND THE LOG MESSAGE for /F
    %%A in ('svnlook author -r %REV% %1')
    do set AUTHOR=%%A for /F
    "delims==" %%B in ('svnlook log -r
    %REV% %1') do set LOG=%%B ::
    CALL THE PYTHON SCRIPT Python
    "%~dp0\\trac-post-commit-hook" -p
    "%TRAC_ENV%" -r"%REV%" -u"%AUTHOR%"
    -m"%LOG%"

    这里最重要的部分是设置TRAC_ENV,它是存储库根目录的路径(SET TRAC_ENV = C:\ trac \ MySpecialProject)

    该脚本中的下一个重要事项是执行以下操作:

    :: GET THE AUTHOR AND THE LOG
    MESSAGE for /F %%A in ('svnlook
    author -r %REV% %1') do set
    AUTHOR=%%A for /F"delims==" %%B
    in ('svnlook log -r %REV% %1') do set
    LOG=%%B

    如果您在上面的脚本文件中看到,我正在使用svnlook(这是SVN的命令行实用程序)来获取LOG消息和向存储库进行提交的作者。

    然后,脚本的下一行实际上是在调用Python代码来执行票证的关闭并解析日志消息。我必须修改它以传递日志消息和作者(我在Trac中使用的用户名与SVN中的用户名相匹配,这很容易)。

    CALL THE PYTHON SCRIPT Python
    "%~dp0\\trac-post-commit-hook" -p
    "%TRAC_ENV%" -r"%REV%" -u"%AUTHOR%"
    -m"%LOG%"

    脚本中的上述行将把Trac环境,修订版,进行提交的人员以及他们的注释传递到python脚本中。

    这是我使用的Python脚本。我在常规脚本之外做的一件事是,我们使用了一个自定义字段(fixed_in_ver),供我们的质量检查小组使用,它可以用来确定他们正在验证的修复程序是否在他们正在质量检查中测试的代码版本中。因此,我修改了python脚本中的代码以更新票证上的该字段。您可以删除不需要的代码,但这是一个很好的示例,说明了您也可以在Trac中更新自定义字段的方法。

    我这样做是通过让用户选择在他们的评论中包含以下内容:

    (version 2.1.2223.0)

    然后,我使用与python脚本用于正则??表达式相同的技术来获取信息。还算不错

    无论如何,这是我使用的python脚本,希望这是一个很好的教程,准确说明了我如何使其在Windows世界中正常工作,以便大家都可以在自己的商店中使用它。

    如果您不想处理我的其他代码来更新自定义字段,请从上面的Craig提到的位置(从Edgewall脚本)中获取基本脚本。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    #!/usr/bin/env python

    # trac-post-commit-hook
    # ----------------------------------------------------------------------------
    # Copyright (c) 2004 Stephen Hansen
    #
    # Permission is hereby granted, free of charge, to any person obtaining a copy
    # of this software and associated documentation files (the"Software"), to
    # deal in the Software without restriction, including without limitation the
    # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    # sell copies of the Software, and to permit persons to whom the Software is
    # furnished to do so, subject to the following conditions:
    #
    #   The above copyright notice and this permission notice shall be included in
    #   all copies or substantial portions of the Software.
    #
    # THE SOFTWARE IS PROVIDED"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    # IN THE SOFTWARE.
    # ----------------------------------------------------------------------------

    # This Subversion post-commit hook script is meant to interface to the
    # Trac (http://www.edgewall.com/products/trac/) issue tracking/wiki/etc
    # system.
    #
    # It should be called from the 'post-commit' script in Subversion, such as
    # via:
    #
    # REPOS="$1"
    # REV="$2"
    # LOG=`/usr/bin/svnlook log -r $REV $REPOS`
    # AUTHOR=`/usr/bin/svnlook author -r $REV $REPOS`
    # TRAC_ENV='/somewhere/trac/project/'
    # TRAC_URL='http://trac.mysite.com/project/'
    #
    # /usr/bin/python /usr/local/src/trac/contrib/trac-post-commit-hook \\
    #  -p"$TRAC_ENV"  \\
    #  -r"$REV"       \\
    #  -u"$AUTHOR"    \\
    #  -m"$LOG"       \\
    #  -s"$TRAC_URL"
    #
    # It searches commit messages for text in the form of:
    #   command #1
    #   command #1, #2
    #   command #1 & #2
    #   command #1 and #2
    #
    # You can have more then one command in a message. The following commands
    # are supported. There is more then one spelling for each command, to make
    # this as user-friendly as possible.
    #
    #   closes, fixes
    #     The specified issue numbers are closed with the contents of this
    #     commit message being added to it.
    #   references, refs, addresses, re
    #     The specified issue numbers are left in their current status, but
    #     the contents of this commit message are added to their notes.
    #
    # A fairly complicated example of what you can do is with a commit message
    # of:
    #
    #    Changed blah and foo to do this or that. Fixes #10 and #12, and refs #12.
    #
    # This will close #10 and #12, and add a note to #12.

    import re
    import os
    import sys
    import time

    from trac.env import open_environment
    from trac.ticket.notification import TicketNotifyEmail
    from trac.ticket import Ticket
    from trac.ticket.web_ui import TicketModule
    # TODO: move grouped_changelog_entries to model.py
    from trac.util.text import to_unicode
    from trac.web.href import Href

    try:
        from optparse import OptionParser
    except ImportError:
        try:
            from optik import OptionParser
        except ImportError:
            raise ImportError, 'Requires Python 2.3 or the Optik option parsing library.'

    parser = OptionParser()
    parser.add_option('-e', '--require-envelope', dest='env', default='',
                      help='Require commands to be enclosed in an envelope. If -e[], '
                           'then commands must be in the form of [closes #4]. Must '
                           'be two characters.')
    parser.add_option('-p', '--project', dest='project',
                      help='Path to the Trac project.')
    parser.add_option('-r', '--revision', dest='rev',
                      help='Repository revision number.')
    parser.add_option('-u', '--user', dest='user',
                      help='The user who is responsible for this action')
    parser.add_option('-m', '--msg', dest='msg',
                      help='The log message to search.')
    parser.add_option('-c', '--encoding', dest='encoding',
                      help='The encoding used by the log message.')
    parser.add_option('-s', '--siteurl', dest='url',
                      help='The base URL to the project\'s trac website (to which '
                           '/ticket/## is appended).  If this is not specified, '
                           'the project URL from trac.ini will be used.')

    (options, args) = parser.parse_args(sys.argv[1:])

    if options.env:
        leftEnv = '\\\' + options.env[0]
        rghtEnv = '\\\' + options.env[1]
    else:
        leftEnv = ''
        rghtEnv = ''

    commandPattern = re.compile(leftEnv + r'(?P[A-Za-z]*).?(?P<ticket>#[0-9]+(?:(?:[, &]*|[ ]?and[ ]?)#[0-9]+)*)' + rghtEnv)
    ticketPattern = re.compile(r'#([0-9]*)')
    versionPattern = re.compile(r"\\(version[ ]+(?P<version>([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+))\\)")

    class CommitHook:
        _supported_cmds = {'close':      '_cmdClose',
                           'closed':     '_cmdClose',
                           'closes':     '_cmdClose',
                           'fix':        '_cmdClose',
                           'fixed':      '_cmdClose',
                           'fixes':      '_cmdClose',
                           'addresses':  '_cmdRefs',
                           're':         '_cmdRefs',
                           'references': '_cmdRefs',
                           'refs':       '_cmdRefs',
                           'see':        '_cmdRefs'}

        def __init__(self, project=options.project, author=options.user,
                     rev=options.rev, msg=options.msg, url=options.url,
                     encoding=options.encoding):
            msg = to_unicode(msg, encoding)
            self.author = author
            self.rev = rev
            self.msg ="(In [%s]) %s" % (rev, msg)
            self.now = int(time.time())
            self.env = open_environment(project)
            if url is None:
                url = self.env.config.get('project', 'url')
            self.env.href = Href(url)
            self.env.abs_href = Href(url)

            cmdGroups = commandPattern.findall(msg)


            tickets = {}

            for cmd, tkts in cmdGroups:
                funcname = CommitHook._supported_cmds.get(cmd.lower(), '')

                if funcname:

                    for tkt_id in ticketPattern.findall(tkts):
                        func = getattr(self, funcname)
                        tickets.setdefault(tkt_id, []).append(func)

            for tkt_id, cmds in tickets.iteritems():
                try:
                    db = self.env.get_db_cnx()

                    ticket = Ticket(self.env, int(tkt_id), db)
                    for cmd in cmds:
                        cmd(ticket)

                    # determine sequence number...
                    cnum = 0
                    tm = TicketModule(self.env)
                    for change in tm.grouped_changelog_entries(ticket, db):
                        if change['permanent']:
                            cnum += 1

                    # get the version number from the checkin... and update the ticket with it.
                    version = versionPattern.search(msg)
                    if version != None and version.group("version") != None:
                        ticket['fixed_in_ver'] = version.group("version")

                    ticket.save_changes(self.author, self.msg, self.now, db, cnum+1)
                    db.commit()

                    tn = TicketNotifyEmail(self.env)
                    tn.notify(ticket, newticket=0, modtime=self.now)
                except Exception, e:
                    # import traceback
                    # traceback.print_exc(file=sys.stderr)
                    print>>sys.stderr, 'Unexpected error while processing ticket ' \\
                                       'ID %s: %s' % (tkt_id, e)


        def _cmdClose(self, ticket):
            ticket['status'] = 'closed'
            ticket['resolution'] = 'fixed'

        def _cmdRefs(self, ticket):
            pass


    if __name__ =="__main__":
        if len(sys.argv) < 5:
            print"For usage: %s --help" % (sys.argv[0])
        else:
            CommitHook()

    对于所有想要安装最新版本(0.11.5)的Windows用户:
    请按照Trac网站TracOnWindows上的说明进行操作。

    即使您拥有64位Windows,也请下载32位1.5 Python。
    注意:我在某处看到了有关如何编译Trac以在64位系统上本地工作的说明。

    当您安装所有必需的东西时,转到存储库文件夹。有文件夹挂钩。
    在其中放了代码猴子提到的文件,但不要像他那样创建" trac-post-commit-resolve-ticket-ref.py"。接受Quant Analyst的建议,就像他说的那样:

    "但是,根据您的trac版本,获取正确的python脚本很重要。要获取适当的版本,SVN请检查以下文件夹:
    http://svn.edgewall.com/repos/trac/branches/xxx-stable/contrib
    其中xxx对应于您使用的追踪版本,例如:0.11"

    从那里下载文件" trac-post-commit-hook",并将其放在hooks文件夹中。

    在trac-post-commit.cmd中编辑这些行

    SET PYTHON_PATH="Path to python installation folder"

    SET TRAC_ENV="Path to folder where you
    did tracd initenv"

    记住没有最后一个!

    我已经从最后一行-r"%REV%"删除了引号,成为-r%REV%,但是我不知道是否需要这样做。现在这将不起作用(至少在我的win 2008服务器上),因为钩子将失败(提交将正常)。这与权限有关。默认情况下,权限是受限制的,我们需要允许python或svn或trac(无论我不知道)更改trac信息。因此,转到您的trac文件夹,project文件夹,db文件夹,右键单击trac.db并选择属性。转到安全性标签并编辑权限,以允许所有人完全控制。这不是很安全,但是我整天都在安全问题上浪费了时间,我不想浪费另一个,只是为了寻找应该为哪个用户启用权限。

    希望这可以帮助....


    首先,非常感谢Code Monkey!

    但是,根据您的trac版本,获取正确的python脚本很重要。要获取适当的版本,SVN签出文件夹:

    http://svn.edgewall.com/repos/trac/branches/xxx-stable/contrib

    其中xxx对应于您所使用的trac版本,例如:0.11

    否则,您将收到如下所示的提交后错误:

    提交失败(详细信息如下):'/ svn / project / trunk / web / directory /'的合并:200 OK


    我要添加"代码猴子的答案是完美的"一件事-对此要小心(我的错误)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    :: Modify paths here:

    :: -- this one must be set
    SET TRAC_ENV=d:\\trac\\MySpecialProject

    :: -- set if Python is not in the system path
    :: SET PYTHON_PATH=**d:\\python**

    :: -- set to the folder containing trac/ if installed in a non-standard location
    :: SET TRAC_PATH=**d:\\python\\Lib\\site-packages\\trac**

    我没有设置非系统路径,花了我一段时间才能看到明显的:D

    只要匹配就可以确定没有其他人犯同样的错误!谢谢代码猴子! 1000000000点:D


    发布提交挂钩位于"钩子"目录中,而您所在的存储库都位于服务器端。我不知道您在环境中的什么位置,所以这只是一个例子

    例如(视窗):

    1
    2
    3
    C:\\Subversion\
    epositories\
    epo1\\hooks\\post-commit

    例如(llinux / unix):

    1
    /usr/local/subversion/repositories/repo1/hooks/post-commit


    运行提交集成注释

    最新内容

    相关内容

    猜你喜欢