【发布时间】:2013-08-29 05:11:16
【问题描述】:
我正在使用 Python 和 Git-Python 编写一个 git post-receive 钩子,它收集有关推送中包含的提交的信息,然后使用摘要更新我们的错误跟踪器和 IM。在推送创建分支的情况下(即 post-receive 的 fromrev 参数全为零)并且还跨越该分支上的多个提交,我遇到了麻烦。我正在从torev 提交向后遍历父列表,但我不知道如何判断哪个提交是分支中的第一个提交,即何时停止查找。
在命令行我可以做到
git rev-list this-branch ^not-that-branch ^master
这将给我准确的this-branch 中的提交列表,没有其他的。我尝试使用Commit.iter_parents 方法复制它,该方法被记录为采用与 git-rev-list 相同的参数,但据我所知,它不喜欢位置参数,而且我找不到集合有效的关键字参数。
我阅读了 Dulwich 的文档,但不清楚它是否会与 Git-Python 做任何不同的事情。
我的(简化的)代码如下所示。当推送启动一个新分支时,它目前只查看第一个提交然后停止:
import git
repo = git.Repo('.')
for line in input:
(fromrev, torev, refname) = line.rstrip().split(' ')
commit = repo.commit(torev)
maxdepth = 25 # just so we don't go too far back in the tree
if fromrev == ('0' * 40):
maxdepth = 1
depth = 0
while depth < maxdepth:
if commit.hexsha == fromrev:
# Reached the start of the push
break
print '{sha} by {name}: {msg}'.format(
sha = commit.hexsha[:7], user = commit.author.name, commit.summary)
commit = commit.parents[0]
depth += 1
【问题讨论】:
标签: python git git-post-receive gitpython dulwich