Check status of local Python relative to remote with GitPython

How can I use GitPython to determine whether:

  • My local branch is ahead of the remote (I can safely push)
  • My local branch is behind the remote (I can safely pull)
  • My local branch has diverged from the remote?

To check if the local and remote are the same, I'm doing this:

def local_and_remote_are_at_same_commit(repo, remote):
    local_commit = repo.commit()
    remote_commit = remote.fetch()[0].commit
    return local_commit.hexsha == remote_commit.hexsha

Answers


See https://stackoverflow.com/a/15862203/197789

E.g.

commits_behind = repo.iter_commits('master..origin/master')

and

commits_ahead = repo.iter_commits('origin/master..master')

Then you can use something like the following to go from iterator to a count:

count = sum(1 for c in commits_ahead)

This was last checked with GitPython 1.0.2.


Need Your Help

Greenspun's 10th rule in Perl?

perl programming-languages lisp

Greenspun's Tenth Rule of Programming is a common aphorism in computer programming and especially programming language circles. It states:

Rounding Datetime to nearest 15 minutes in SQL Server

sql-server datetime rounding

I have a problem in SQL Server with rounding datetime. I got datetime in column rec_datetime, but I want to round this datetime in a new column r_datetime, which has to be rounded to nearest 15 min...