首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pygit2 blob历史记录

pygit2 blob历史记录
EN

Stack Overflow用户
提问于 2012-11-09 00:17:48
回答 2查看 1.5K关注 0票数 7

我正在尝试使用pygit2在git bare存储库中做等同于git log filename的工作。文档只解释了如何执行这样的git log

代码语言:javascript
复制
from pygit2 import GIT_SORT_TIME
for commit in repo.walk(oid, GIT_SORT_TIME):
    print(commit.hex)

你有什么想法吗?

谢谢

编辑:

我现在有这样的东西,或多或少准确地说:

代码语言:javascript
复制
from pygit2 import GIT_SORT_TIME, Repository


repo = Repository('/path/to/repo')

def iter_commits(name):
    last_commit = None
    last_oid = None

    # loops through all the commits
    for commit in repo.walk(repo.head.oid, GIT_SORT_TIME):

        # checks if the file exists
        if name in commit.tree:
            # has it changed since last commit?
            # let's compare it's sha with the previous found sha
            oid = commit.tree[name].oid
            has_changed = (oid != last_oid and last_oid)

            if has_changed:
                yield last_commit

            last_oid = oid
        else:
            last_oid = None

        last_commit = commit

    if last_oid:
        yield last_commit


for commit in iter_commits("AUTHORS"):
    print(commit.message, commit.author.name, commit.commit_time)
EN

回答 2

Stack Overflow用户

发布于 2013-08-06 16:56:47

我建议您只使用git的命令行界面,它可以提供格式化良好的输出,使用Python进行解析非常容易。例如,要获取给定文件的作者姓名、日志消息和提交散列:

代码语言:javascript
复制
import subprocess
subprocess.check_output(['git','log','--pretty="%H,%cn%n----%B----"','some_git_file.py'])

有关可以传递给--pretty的格式说明符的完整列表,请查看git日志https://www.kernel.org/pub/software/scm/git/docs/git-log.html的文档

票数 1
EN

Stack Overflow用户

发布于 2015-01-06 16:32:06

另一种解决方案是,懒惰地从给定的提交中生成文件的修订版。由于它是递归的,如果历史记录太大,它可能会崩溃。

代码语言:javascript
复制
def revisions(commit, file, last=None):
    try:
        entry = commit.tree[file]
    except KeyError:
        return
    if entry != last:
        yield entry
        last = entry
    for parent in commit.parents:
        for rev in revisions(parent, file, last):
            yield rev
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13293052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档