如何使用 GitPython 获取存储库的提交数量?

Get*_*ept 3 git python-3.x gitpython

我是 GitPython 新手,我想获取存储库的提交数量。我正在寻找 GitPython 中“ git rev-list --count HEAD ”的替代方案,是否有特定的函数可以做到这一点?

我试图获取要显示的存储库的每个提交的列表,然后显示它的大小,但仅显示最后一个提交。感谢您的帮助,问候。

Elp*_*Kay 7

尝试一下代码:

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)
Run Code Online (Sandbox Code Playgroud)

我不熟悉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')
Run Code Online (Sandbox Code Playgroud)

请注意,-命令名称中的 应该_在代码中。