【问题标题】:add blobs or trees to a created repository using pygit2 (libgit2)?使用 pygit2 (libgit2) 将 blob 或树添加到创建的存储库?
【发布时间】:2012-05-02 07:22:20
【问题描述】:

我正在尝试使用 pygit2 库。

似乎我卡在了第一步。它的文档没有解释如何创建 blob 并将其添加到树中。它主要是关于如何使用现有的 git 存储库,但我想创建一个并将 blob、提交等添加到我的存储库中。是否可以直接从文件创建 blob,还是应该读取文件内容并设置 blob.data?

from pygit2 import Repository
from pygit2 import init_repository

bare = False
repo = init_repository('test', bare)

如何创建 Blob 或树并将其添加到存储库?

【问题讨论】:

    标签: python git libgit2


    【解决方案1】:

    python 绑定不允许您直接从文件创建 blob,因此您必须将文件读入内存并使用 Repository.write(pygit2.GIT_OBJ_BLOB, filecontents) 来创建 blob。

    然后您可以使用TreeBuilder 创建树,例如,像

    import pygit2 as g
    
    repo = g.Repository('.')
    # grab the file from wherever and store in 'contents'
    oid = repo.write(g.GIT_OBJ_BLOB, contents)
    bld = repo.TreeBuilder()
    # attributes is whether it's a file or dir, 100644, 100755 or 040000
    bld.insert('file.txt', oid, attributes)
    treeoid = bld.write()
    

    【讨论】:

    • 非常感谢,我怎样才能了解更多关于 pygit2 的信息?你提到的都没有在 pygit2 文档中,你是如何学习如何使用它的?
    • @PeqiHash Carlos 是 libgit2 的开发者之一
    • 如果您更改了文件,您是否仍将整个文件作为 blob 添加到 repo 中,然后使用 TreeBuilder 通过插入引用该 oid ......或者这里有什么与补丁有关的吗?
    • 补丁存在于不同的级别。文件的每个版本的全部内容都包含在一个 blob 中。如果更新它,则需要更新 blob 以及从它到根的任何树。
    • 如果文件可能在子目录中(并且该目录可能存在也可能不存在),如何处理?好像写不出来bld.insert('path/to/file.txt', oid, attributes)
    猜你喜欢
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多