GitPython equivalent of "git remote show origin"?
Asked Answered
N

2

9

I'm trying to update a Python script that checks the status of a handful of local repositories against remotes from using subprocess to using GitPython. What is the equivalent command in GitPython for git remote show origin, or what is the better way to check that the local repo is fast-forwardable or out-of-date (etc.)?

$ git remote show origin
* remote origin
  Fetch URL: <url>
  Push  URL: <url>
  HEAD branch: master
  Remote branches:
    XYZ    tracked
    master tracked
  Local branches configured for 'git pull':
    XYZ    merges with remote XYZ
    master merges with remote master
  Local refs configured for 'git push':
    XYZ    pushes to XYZ    (up to date)
    master pushes to master (up to date)

The last two lines are my primary concern. It looks like this might be possible with GitPython by iterating over git.Repo.heads and git.Repo.remotes.origin.refs and comparing .master.commit (etc.) hashes. This seems a good deal more work than the above single native git command and will require even more work to tell which side(s) is/are outdated. I was expecting something like git.Repo.remotes.origin.status(). What is the proper way to determine this in GitPython?

Nardone answered 1/10, 2012 at 21:20 Comment(0)
M
4

You can use git.cmd.Git() if gitpython doesn't wrap wanted functionality. It directly calls git so it's quite convenient, although I guess that mostly it's just a subprocess' wrapper:

import git

g = git.cmd.Git("/path/to/git/repo")
print(g.execute("git remote show origin"))  # git remote show origin
print(g.execute(["git", "remote", "show", "origin"]))  # same as above
print(g.remote(verbose=True))  # git remote --verbose
Moisesmoishe answered 28/11, 2015 at 21:54 Comment(0)
B
0

I'm not aware of anything better than running git remote show origin as a subprocess if you need a concise report of every branch. If your interest is in a single branch, assuming you've done a fetch, you can check how many commits you are behind or ahead like this:

commits_behind = list(repo.iter_commits(
            '{branch}..{tracking_branch}'.format(
                branch=branch,
                tracking_branch=repo.heads[branch].tracking_branch())))

commits_ahead = list(repo.iter_commits(
            '{tracking_branch}..{branch}'.format(
                branch=branch,
                tracking_branch=repo.heads[branch].tracking_branch())))
Biometrics answered 17/4, 2015 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.