如何使用python(dulwich)获取指定文件的最后一次提交?

cco*_*oom 1 python git dulwich

我需要使用python的指定文件的作者姓名和上次提交时间.Currentrly,我正在尝试使用德威.

有很多api可以检索特定SHA的对象,例如:

repo = Repo("myrepo")
head = repo.head()
object = repo.get_object(head)
author = object.author
time = object.commit_time
Run Code Online (Sandbox Code Playgroud)

但是,我怎么知道最近提交的特定文件?有没有办法检索它像:

repo = Repo("myrepo")
commit = repo.get_commit('a.txt')
author = commit.author
time = commit.commit_time
Run Code Online (Sandbox Code Playgroud)

要么

repo = Repo("myrepo")
sha = repo.get_sha_for('a.txt')
object = repo.get_object(sha)
author = object.author
time = object.commit_time
Run Code Online (Sandbox Code Playgroud)

谢谢.

jel*_*mer 5

一个较短的例子,使用Repo.get_walker:

r = Repo(".")
p = "the/file/to/look/for"

w = r.get_walker(paths=[p], max_entries=1)
try:
    c = iter(w).next().commit
except StopIteration:
     print "No file %s anywhere in history." % p
else:
    print "%s was last changed at %s by %s (commit %s)" % (
        p, time.ctime(c.author_time), c.author, c.id)
Run Code Online (Sandbox Code Playgroud)