Ioan S.
Ioan S.

Reputation: 164

How to get number of commits of a repo using GitPython?

I'm new with GitPython and I would like to get the nomber of commits of a repo. I am looking to get alternative to "git rev-list --count HEAD" in GitPython, is there a specific function to do that ?

I tried to get the list of every commits of the repo to display then it's size, but the last commit only appears. thanks for help, Regards.

Upvotes: 3

Views: 6005

Answers (2)

ElpieKay
ElpieKay

Reputation: 30868

Try the code:

import git

repo_path = 'foo'
repo = git.Repo(repo_path)
# get all commits reachable from "HEAD"
commits = list(repo.iter_commits('HEAD'))
# get the number of commits
count = len(commits)

I'm not familiar with Python 3.x. There may be errors due to differences between Python 2.x and 3.x.

After doing some research, I find we can just invoke git rev-list --count HEAD in a direct way.

import git

repo_path = 'foo'
repo = git.Repo(repo_path)
count = repo.git.rev_list('--count', 'HEAD')

Note that the - in the command name should be _ in the code.

Upvotes: 7

Adrian Krupa
Adrian Krupa

Reputation: 1937

You can get list of all commits with iter_commits(). Iterate over it and count commits

from git import Repo

repo = Repo()

print(len(list(repo.iter_commits())))

Upvotes: 0

Related Questions