我正在编写一个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.iter_commits()
对于本地Repo做的那样,将是一个很大的帮助。但我在文档中没有找到如何做到这一点。我可以为Remote存储库获取一个Repo对象吗?
是否有其他方法可以帮助我达到目标,而我却在用锤子钉螺丝?