【发布时间】:2011-11-28 00:16:03
【问题描述】:
我正在编写一个 Python 脚本来获取即将由 git pull 操作应用的提交列表。优秀的GitPython library 是一个很好的开始,但是 git 的微妙内部运作让我很生气。现在,这是我目前拥有的(简化和注释版本):
repo = git.Repo(path) # get the local repo
local_commit = repo.commit() # latest local commit
remote = git.remote.Remote(repo, 'origin') # remote repo
info = remote.fetch()[0] # fetch changes
remote_commit = info.commit # latest remote commit
if local_commit.hexsha == remote_commit.hexsha: # local is updated; end
return
# for every remote commit
while remote_commit.hexsha != local_commit.hexsha:
authors.append(remote_commit.author.email) # note the author
remote_commit = remote_commit.parents[0] # navigate up to the parent
本质上,它会获取将在下一个git pull 中应用的所有提交的作者。这运行良好,但存在以下问题:
- 当本地提交在远程之前,我的代码只会将所有提交打印到第一个。
- 远程提交可以有多个父级,本地提交可以是第二个父级。这意味着我的代码永远不会在远程存储库中找到本地提交。
我可以处理位于本地存储库之后的远程存储库:只需同时查看另一个方向(本地到远程),代码会变得混乱但它可以工作。但是最后一个问题让我很生气:现在我需要导航一个(可能无限的)树来找到本地提交的匹配项。这不仅仅是理论上的:我最近的更改是一个 repo 合并,它提出了这个问题,所以我的脚本不起作用。
在远程存储库中获取提交的有序列表,例如 repo.iter_commits() 为本地存储库所做的,将有很大帮助。但我还没有在documentation 中找到如何做到这一点。我可以只为远程存储库获取一个 Repo 对象吗?
是否有另一种方法可以让我到达那里,我正在用锤子钉螺丝?
【问题讨论】: