3

我需要使用 python 指定文件的作者姓名和上次提交时间。目前,我正在尝试使用dulwich

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

repo = Repo("myrepo")
head = repo.head()
object = repo.get_object(head)
author = object.author
time = object.commit_time

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

repo = Repo("myrepo")
commit = repo.get_commit('a.txt')
author = commit.author
time = commit.commit_time

或者

repo = Repo("myrepo")
sha = repo.get_sha_for('a.txt')
object = repo.get_object(sha)
author = object.author
time = object.commit_time

谢谢你。

4

2 回答 2

6

一个较短的示例,使用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)
于 2013-12-07T14:27:34.587 回答
3

像这样的东西似乎有效:

from dulwich import repo, diff_tree

fn = 'a.txt'
r = repo.Repo('.')
prev = None
walker = r.get_graph_walker()
cset = walker.next()
while cset is not None:

    commit = r.get_object(cset)
    if prev is None:
        prev = commit.tree
        cset = walker.next()
        continue


    res = None
    delta = diff_tree.tree_changes(r, prev, commit.tree)
    for x in diff_tree.tree_changes(r, prev, commit.tree):
        if x.new.path == fn:
            res = cset
            break

    if res:
        break

    prev = commit.tree
    cset = walker.next()

print fn, res
于 2013-05-20T09:08:26.640 回答