我正在尝试使用 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 绑定(bind)不允许您直接从文件创建 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()
关于python - 使用 pygit2 (libgit2) 将 blob 或树添加到创建的存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10409378/