How to get count of unpublished commit with GitPython?
Asked Answered
G

2

10

With git status I can get information about count of unpublished commits:

» git status             
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean

I want to get unpublished commits (or count) with GitPython. I docs I found repo.git.status(), but this is not what I want.

Groundsill answered 6/4, 2013 at 10:16 Comment(0)
D
19

The command you are looking for is:

repo.iter_commits('BRANCH@{u}..BRANCH')

or if you want this as a list:

list(repo.iter_commits('BRANCH@{u}..BRANCH'))

The BRANCH@{u} syntax refers to the upstream branch of BRANCH.

Disengage answered 7/4, 2013 at 12:9 Comment(1)
This gave me an empty list, when I knew that I had unpushed changes. I found that I had to reverse the order of arguments, to find commits that I had made locally but not yet pushed, for example this worked: list(repo.iter_commits('master@{u}..master'))Transparency
E
1

Thanks to @Chronial and @Clare-Macrae for your feedback Using gitPython ==3.1.11, I did it like that:

branch = self.repo.active_branch
unpushed_symbol = '⇡' if list(self.repo.iter_commits(f'{branch}@{{u}}..{branch}')) else constants.NOTHING
unpulled_symbol = '⇣' if list(self.repo.iter_commits(f'{branch}..{branch}@{{u}}')) else constants.NOTHING
Extremely answered 2/1, 2021 at 1:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.