在shell中我说'git status'并获得输出:
Your branch is up-to-date with 'origin/master'.
或
Your branch is ahead of 'origin/master' by 3 commits.
在GitPython中,我怎样才能知道是否还没有将更改推送到远程?
答案 0 :(得分:0)
似乎还没有针对该功能的包装器(我自己搜索了一个,偶然发现了您的答案)。但是您可以做的是use git directly。
根据您希望解决方案的复杂程度,一种快速的方法可能是执行以下操作:
import re
from git import Repo
a_repo = Repo("/path/to/your/repo")
ahead = 0
behind = 0
# Porcelain v2 is easier to parse, branch shows ahead/behind
repo_status = a_repo.git.status(porcelain="v2", branch=True)
ahead_behind_match = re.search(r"#\sbranch\.ab\s\+(\d+)\s-(\d+)", repo_status)
# If no remotes exist or the HEAD is detached, there is no ahead/behind info
if ahead_behind_match:
ahead = int(ahead_behind_match.group(1))
behind = int(ahead_behind_match.group(2))
print("Repo is {} commits ahead and {} behind.".format(ahead, behind))