1

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

4

1 回答 1

4

我假设您的意思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?')]

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

于 2015-03-30T02:06:29.570 回答