我刚刚在还拥有 Git 存储库的服务器上安装了 Trac 1.0.1。我希望能够通过在 Git 提交消息中包含诸如“修复 #3”之类的内容来关闭 Trac 票证。这应该很容易——通过在我的存储库中包含一个post-receive
钩子git push
,我可以在每个到服务器之后执行一些代码(例如 Python 脚本) 。但是要使用什么代码呢?
1 回答
在跑了一段时间后,遇到了几个死胡同(包括Trac Git 页面,在这个主题上含糊不清,以及Git 插件,在错误 #7301修复之前实际上不会工作(?!)),我找到了解决方案。
通过“设置 Trac 环境”</a> 中的步骤将您的 Git 存储库连接到 Trac 。
启用Commit Ticket Updater插件,通过 Trac 的“Admin”部分或编辑
trac.ini
.在Git 存储库
post-receive
的目录中创建一个名为的文件,其内容如下:hooks
#!/usr/bin/ruby ARGF.lines do |line| fields = line.split oldrev = fields[0] newrev = fields[1] refname = fields[2].chomp if oldrev =~ /^0+$/ revspec = newrev else revspec = oldrev + '..' + newrev end other_branches = `git for-each-ref --format='%(refname)' refs/heads/ | grep -F -v "#{refname}"` other_branches = other_branches.chomp.gsub /[\r\n]+/, ' ' commits = `git rev-parse --not #{other_branches} | git rev-list --stdin #{revspec}` commits.each_line do |commit| system "trac-admin .../trac changeset added '(default)' #{commit.chomp}" end end
当然,将“.../trac”替换为 Trac 安装的绝对路径。
我实际上是通过Virtualenv使用 Trac 。如果您也是,请将其添加到文件顶部:
require 'tempfile' def virtualenv_system(cmd) script = Tempfile.new('post_receive') script.write 'source .../virtualenvs/trac/bin/activate' script.write "\n" script.write cmd script.close system "bash #{script.path}" script.unlink end
并将
system
呼叫替换为virtualenv_system
.使该
post-receive
文件可执行。
这是受到存储库管理页面上给出的方法的启发,并结合了这个关于在接收后脚本中获取所有新提交的SO 答案。我相信这个脚本虽然很长,但当您推送多个提交和/或将提交推送到当前签出的分支以外的分支时,它的行为是正确的。(Repository Administration 页面上给出的脚本在这些情况下的行为不正确——它只查看来自 HEAD 的最新提交消息。)
在此设置过程之后,任何包含“fixes #7”之类的字符串的 Git 提交都将关闭 Trac 中的相应票证。您可以使用Commit Ticket Updater 页面上列出的选项进行一些配置。具体来说,您可能想要更改commit_ticket_update_envelope
; 这并不完全清楚,但我认为默认设置是为了让您必须将命令包含在方括号中,例如“重构的 MyAwesomeClass [修复 #42]”。