我正在编写一个 Python 脚本来获取即将由git pull
操作应用的提交列表。优秀的GitPython 库是一个很好的入门基础,但 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()
本地存储库,将是一个很大的帮助。但我还没有在文档中找到如何做到这一点。我可以只为远程存储库获取一个 Repo 对象吗?
有没有另一种方法可以让我到达那里,我正在用锤子钉螺丝?