GitPython 获取文件的提交记录

4 投票
4 回答
4771 浏览
提问于 2025-04-16 03:03

我想用gitpython来获取关于一个树的信息,比如文件什么时候被提交的,以及相关的日志。目前我得到的结果是

from git import *
repo = get_repo("/path/to/git/repo")
for item in repo.tree().items():
    print item[1]

这个结果只列出了像这样的内容

<git.Tree "ac1dcd90a3e9e0c0359626f222b99c1df1f11175">
<git.Blob "764192de68e293d2372b2b9cd0c6ef868c682116">
<git.Blob "39fb4ae33f07dee15008341e10d3c37760b48d63">
<git.Tree "c32394851edcff4bf7a452f12cfe010e0ed43739">
<git.Blob "6a8e9935334278e4f38f9ec70f982cdc4f42abf0">

我在git.Blog的文档里找不到获取这些数据的方法……我是不是走错方向了?

4 个回答

1

提交信息是在 commit object 里,而不是在 tree object 里。我想你可以用下面的方式获取它:

repo.heads[0].commit.message

(注意:我不懂Python。这是基于我对Git的了解和我花了一分钟阅读API文档的结果)

2

经过四个小时,我终于明白了。

repo = get_repo("/path/to/git/repo")

items = repo.tree().items()
items.sort()

for i in items:
    c = repo.commits(path=i[0], max_count=1)
    print i[0], c[0].author, c[0].authored_date, c[0].message
5

如果你现在想要做到这一点,可以这样做:

获取最近的100个提交,并按时间从新到旧排序:

repo.iter_commits('master', max_count=100)

你还可以使用 skip 来进行分页:

repo.iter_commits('master', max_count=10, skip=20)

参考链接: http://gitpython.readthedocs.org/en/stable/tutorial.html#the-commit-object

撰写回答