gpt4 book ai didi

gitpython - 找出是否在 gitpython 中将更改推送到原点

转载 作者:行者123 更新时间:2023-12-01 14:18:02 26 4
gpt4 key购买 nike

在 shell 中,我说 'git status' 并得到输出:

Your branch is up-to-date with 'origin/master'.

或者
Your branch is ahead of 'origin/master' by 3 commits.

在 GitPython 中,如何确定更改是否尚未推送到远程?

最佳答案

似乎还没有此功能的包装器(我自己搜索了一个并偶然发现了您的答案)。但是你能做的就是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))

关于gitpython - 找出是否在 gitpython 中将更改推送到原点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43037807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com