GitPython equivalent to "git remote source show"?

I am trying to update a Python script that checks the status of several local repositories against remote objects from using a subprocess to using GitPython . What is the equivalent command in GitPython for git remote show origin, or the best way to verify that a local repo is advanced or deprecated (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 main problem. It seems like this is possible with GitPython , iterating over git.Repo.headsand git.Repo.remotes.origin.refsand comparing hashes .master.commit(etc.). This seems to be much bigger than the single git command above, and will require even more work to indicate which side is out of date. I was expecting something like git.Repo.remotes.origin.status(). What is the correct way to define this in GitPython ?

+5
source share
2 answers

I don't know anything better than running git remote show originas a subprocess if you need a brief report for each thread. If your interest is in one branch, if you made a selection, you can check how many of you have earned. or forward as follows:

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())))
+1

git.cmd.Git(), gitpython . git, , , :

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
0

All Articles