Find out if changes were pushed to origin in gitpython
Asked Answered
H

1

7

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?

Hurley answered 27/3, 2017 at 4:9 Comment(1)
I too am interested in the answer to this question, if it is possible to achieve.Lohman
A
3

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))
Absinthism answered 11/10, 2018 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.