如何使用python(dulwich)获取指定文件的最后提交? [英] How to get last commit for specified file with python(dulwich)?

查看:44
本文介绍了如何使用python(dulwich)获取指定文件的最后提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用python指定文件的作者名和最后提交时间.目前,我正在尝试使用德威奇.

I need author name and last commit time for a specified file with python. Currentrly, I'm trying to use dulwich.

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

There're plenty of apis to retrieve objects for a specific SHA like:

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

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

But, how do i know the recent commit for the specific file? Is there a way to retrieve it like:

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

谢谢.

推荐答案

类似的方法似乎有效:

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

这篇关于如何使用python(dulwich)获取指定文件的最后提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆