Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add blobs or trees to a created repository using pygit2 (libgit2)?

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?

like image 743
Peqi Hash Avatar asked May 02 '12 07:05

Peqi Hash


1 Answers

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()
like image 143
Carlos Martín Nieto Avatar answered Sep 19 '22 16:09

Carlos Martín Nieto