Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements 'Repository.create_blob_fromiobase'. #490

Merged
merged 2 commits into from
Oct 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ them to the Git object database:

.. automethod:: pygit2.Repository.create_blob_fromworkdir
.. automethod:: pygit2.Repository.create_blob_fromdisk
.. automethod:: pygit2.Repository.create_blob_fromiobase

There are also some functions to calculate the id for a byte string without
creating the blob object:
Expand Down
73 changes: 73 additions & 0 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include "signature.h"
#include <git2/odb_backend.h>

extern PyTypeObject PyIOBase_Type;

extern PyObject *GitError;

extern PyTypeObject IndexType;
Expand Down Expand Up @@ -736,6 +738,76 @@ Repository_create_blob_fromdisk(Repository *self, PyObject *args)
}


PyDoc_STRVAR(Repository_create_blob_fromiobase__doc__,
"create_blob_fromiobase(io.IOBase) -> Oid\n"
"\n"
"Create a new blob from an IOBase object.");


int read_chunk(char *content, size_t max_length, void *payload)
{
PyObject *py_file;
PyObject *py_bytes;
char *bytes;
Py_ssize_t size;

py_file = (PyObject *)payload;
py_bytes = PyObject_CallMethod(py_file, "read", "i", max_length);
if (!py_bytes)
return -1;

size = 0;
if (py_bytes != Py_None) {
bytes = PyBytes_AsString(py_bytes);
size = PyBytes_Size(py_bytes);
memcpy(content, bytes, size);
}

Py_DECREF(py_bytes);
return size;
}

PyObject *
Repository_create_blob_fromiobase(Repository *self, PyObject *args)
{
git_oid oid;
PyObject *py_is_readable;
int is_readable;
PyObject *py_file;
int err;

if (!PyArg_ParseTuple(args, "O!", &PyIOBase_Type, &py_file))
return NULL;

py_is_readable = PyObject_CallMethod(py_file, "readable", NULL);
if (!py_is_readable) {
Py_DECREF(py_file);
return NULL;
}

is_readable = PyObject_IsTrue(py_is_readable);
Py_DECREF(py_is_readable);

if (!is_readable) {
Py_DECREF(py_file);
PyErr_SetString(PyExc_TypeError, "expected readable IO type");
return NULL;
}

err = git_blob_create_fromchunks(&oid,
self->repo,
NULL,
&read_chunk,
py_file);
Py_DECREF(py_file);

if (err < 0)
return Error_set(err);

return git_oid_to_python(&oid);
}


PyDoc_STRVAR(Repository_create_commit__doc__,
"create_commit(reference, author, committer, message, tree, parents[, encoding]) -> Oid\n"
"\n"
Expand Down Expand Up @@ -1386,6 +1458,7 @@ PyMethodDef Repository_methods[] = {
METHOD(Repository, create_blob, METH_VARARGS),
METHOD(Repository, create_blob_fromworkdir, METH_VARARGS),
METHOD(Repository, create_blob_fromdisk, METH_VARARGS),
METHOD(Repository, create_blob_fromiobase, METH_VARARGS),
METHOD(Repository, create_commit, METH_VARARGS),
METHOD(Repository, create_tag, METH_VARARGS),
METHOD(Repository, TreeBuilder, METH_VARARGS),
Expand Down
12 changes: 12 additions & 0 deletions test/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals
from os.path import dirname, join
import io
import unittest

import pygit2
Expand Down Expand Up @@ -112,6 +113,17 @@ def test_create_blob_fromdisk(self):
self.assertTrue(isinstance(blob, pygit2.Blob))
self.assertEqual(pygit2.GIT_OBJ_BLOB, blob.type)

def test_create_blob_fromiobase(self):
f = io.BytesIO(BLOB_CONTENT)
blob_oid = self.repo.create_blob_fromiobase(f)
blob = self.repo[blob_oid]

self.assertTrue(isinstance(blob, pygit2.Blob))
self.assertEqual(pygit2.GIT_OBJ_BLOB, blob.type)

self.assertEqual(blob_oid, blob.id)
self.assertEqual(BLOB_SHA, blob_oid.hex)

def test_diff_blob(self):
blob = self.repo[BLOB_SHA]
old_blob = self.repo['3b18e512dba79e4c8300dd08aeb37f8e728b8dad']
Expand Down