如何使用 GitPython 获取回购的提交次数?

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

我是 GitPython 的新手,我想获取回购协议的提交次数。 我正在寻找替代 GitPython 中的“git rev-list --count HEAD”,是否有特定的功能可以做到这一点?

我试图获取要显示的回购的每个提交的列表,然后显示它的大小,但只显示最后一次提交。 感谢帮助, 问候。

试试代码:

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)

我不熟悉 Python 3.x。 Python 2.x 和 3.x.

之间可能存在差异

经过一些研究,我发现我们可以直接调用 git rev-list --count HEAD

import git

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

注意命令名中的-在代码中应该是_

您可以使用 iter_commits() 获取所有提交的列表。迭代它并计算提交数

from git import Repo

repo = Repo()

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