git shortlog 是否有 python 接口?

Is there a python interface to git shortlog?

我正在尝试将日志信息从 git 中获取到 python 中。我查看了 pygit2 和 gitpython,但它们似乎都没有提供类似于 git shortlog 的高级界面。是否有提供此类接口的 python 库,或者我应该调用 git 可执行文件?

我假设您的意思是 pygit2 是您查看的图书馆之一。这是一个较低级别的库,可让您在此之上编写更高级别的库(它已经足够高级了,3 行代码获取基本日志输出是不是太多了?)。做你想做的事甚至都不难 - 阅读 relevant documentation 你可以想出类似的东西:

>>> from pygit2 import Repository
>>> from pygit2 import GIT_SORT_TIME
>>> from pygit2 import GIT_SORT_REVERSE
>>> repo = Repository('/path/to/repo')
>>> rev = repo.revparse_single('HEAD').hex
>>> iterator = repo.walk(rev, GIT_SORT_TIME | GIT_SORT_REVERSE)
>>> results = [(commit.committer.name, commit.message.splitlines()[0])
...     for commit in iterator]
>>> results
[(u'User', u'first commit.'), (u'User', u'demo file.'), ... (u'User', u'okay?')]

如果您想将输出分组为与 git shortlog 相同的结果,那也不难,您需要的所有数据都已在迭代器中,因此只需使用它将数据放入您想要的格式即可需要。