是否有 git Shortlog 的 python 接口?

And*_*ler 2 python git gitpython

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

met*_*ter 5

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

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

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