In shell I say 'git status' and get output:
Your branch is up-to-date with 'origin/master'.
or
Your branch is ahead of 'origin/master' by 3 commits.
In GitPython how can I find out if changes have not been not pushed to remote yet?
In shell I say 'git status' and get output:
Your branch is up-to-date with 'origin/master'.
or
Your branch is ahead of 'origin/master' by 3 commits.
In GitPython how can I find out if changes have not been not pushed to remote yet?
It seems that there is no wrapper for this functionality yet (I searched for one myself and stumbled upon your answer). But what you can do is use git directly.
Depending on how complicated you want your solution to be, a quick way might be to do the following:
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))
© 2022 - 2024 — McMap. All rights reserved.