I am trying to use pygit2 library.
seems I got stuck on the first step. its documentation doesn't explain how to create a blob and add it to a tree. It is mostly around how to work with an existing git repository but I want to create one and add blobs, commits, ... to my repo. Is it possible to create a blob from a file directly or should I read the file contents and set blob.data?
from pygit2 import Repository
from pygit2 import init_repository
bare = False
repo = init_repository('test', bare)
How can I create and add blobs or trees to the repository?
The python bindings don't let you create a blob from a file directly, so you'll have to read in the file to memory and use Repository.write(pygit2.GIT_OBJ_BLOB, filecontents)
to create the blob.
You can then create trees with the TreeBuilder
, for example, like
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With