我正在使用Python和Git-Python编写一个git post-receive hook,它收集有关push中包含的提交的信息,然后使用摘要更新我们的bug跟踪器和IM。我在推送创建分支的情况下遇到麻烦(即,后接收的fromrev
参数全为零)并且还跨越该分支上的多个提交。我正在从torev
提交向后查看父母列表,但我无法弄清楚如何判断哪个提交是分支中的第一个提交,即何时停止查找。
在命令行上,我可以
git rev-list this-branch ^not-that-branch ^master
这将完全给出this-branch
中的提交列表,而不是其他人。我试图使用Commit.iter_parents
方法复制这个,这个方法被记录为采用与git-rev-list相同的参数,但是就我所见,它不喜欢位置参数,我找不到一组有效的关键字参数。
我为Dulwich阅读了doco,但不清楚它是否会与Git-Python有很大不同。
我的(简化)代码如下所示。当push推出一个新的分支时,它目前只查看第一个提交然后停止:
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
答案 0 :(得分:5)
使用纯Git-Python,它也可以完成。我还没有找到一种方法来识别一组可以一次完成它的kwargs。但是可以简单地构造一组主分支的sha,然后在待检查的分支上使用iter_commits,以便找到第一个没有出现在父分支中的分支:
from git import *
repo_path = '.'
repo = Repo(repo_path)
parent_branch = repo.branches.master
examine_branch = repo.branches.test_feature_branch
other_shas = set()
for parent_commit in repo.iter_commits(rev=parent_branch):
other_shas.add(parent_commit.hexsha)
for commit in repo.iter_commits(rev=examine_branch):
if commit.hexsha not in other_shas:
first_commit = commit
print '%s by %s: %s' % (first_commit.hexsha[:7],
first_commit.author.name, first_commit.summary)
如果你真的想要确保排除所有其他分支上的所有提交,你可以将第一个for循环包装在另一个for循环中,而不是repo.branches:
other_shas = set()
for branch in repo.branches:
if branch != examine_branch:
for commit in repo.iter_commits(rev=branch):
other_shas.add(commit.hexsha)
答案 1 :(得分:2)
我刚刚玩过德威,也许还有更好的方法(使用内置助行器?)。假设只有一个新分支(或多个没有共同点的新分支):
#!/usr/bin/env python
import sys
from dulwich.repo import Repo
from dulwich.objects import ZERO_SHA
def walk(repo, sha, shas, callback=None, depth=100):
if not sha in shas and depth > 0:
shas.add(sha)
if callback:
callback(sha)
for parent in repo.commit(sha).parents:
walk(repo, parent, shas, callback, depth - 1)
def reachable_from_other_branches(repo, this_branch):
shas = set()
for branch in repo.refs.keys():
if branch.startswith("refs/heads") and branch != this_branch:
walk(repo, repo.refs[branch], shas)
return shas
def branch_commits(repo, fromrev, torev, branchname):
if fromrev == ZERO_SHA:
ends = reachable_from_other_branches(repo, branchname)
else:
ends = set([fromrev])
def print_callback(sha):
commit = repo.commit(sha)
msg = commit.message.split("\n")[0]
print('{sha} by {author}: {msg}'
.format(sha=sha[:7], author=commit.author, msg=msg))
print(branchname)
walk(repo, torev, ends, print_callback)
repo = Repo(".")
for line in sys.stdin:
fromrev, torev, refname = line.rstrip().split(' ')
branch_commits(repo, fromrev, torev, refname)
答案 2 :(得分:1)
这样的东西会找到第一个提交:
x = Repo('.')
print list(x.get_walker(include=[x.head()]))[-1].commit
(注意,这将使用O(n)内存用于大型存储库,使用迭代器来解决这个问题)