我正在編寫一個Python腳本以獲取將由git pull
操作應用的提交列表。優秀的GitPython library是一個很好的開始,但git的微妙的內部運作正在殺死我。現在,這裏是我的時刻(簡體和註釋的版本):GitPython:獲取尚未應用的遠程提交列表
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()
對本地回購會有幫助。但我還沒有在documentation找到如何做到這一點。我能否獲得遠程存儲庫的Repo對象?
是否有另一種方法可能讓我在那裏,我用錘子釘螺釘?