Skip to content

Commit

Permalink
Add static Diff.parse_diff
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonio21 committed Mar 3, 2018
1 parent f1e9c3b commit 711e9eb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ wrap_diff(git_diff *diff, Repository *repo)

py_diff = PyObject_New(Diff, &DiffType);
if (py_diff) {
Py_INCREF(repo);
Py_XINCREF(repo);
py_diff->repo = repo;
py_diff->diff = diff;
}
Expand Down Expand Up @@ -901,6 +901,32 @@ Diff_stats__get__(Diff *self)
return wrap_diff_stats(self->diff);
}

PyDoc_STRVAR(Diff_parse_diff__doc__,
"parse_diff(git_diff: str) -> Diff\n"
"\n"
"Parses a git unified diff into a diff object without a repository");

static PyObject *
Diff_parse_diff(PyObject *self, PyObject *args)
{
/* A wrapper around
* git_diff_from_buffer
*/
git_diff *diff;
const char *content = NULL;
Py_ssize_t content_len;
int err;

if (!PyArg_ParseTuple(args, "s#", &content, &content_len))
return NULL;

err = git_diff_from_buffer(&diff, content, content_len);
if (err < 0)
return Error_set(err);

return wrap_diff(diff, NULL);
}

static void
Diff_dealloc(Diff *self)
{
Expand All @@ -926,6 +952,8 @@ static PyMethodDef Diff_methods[] = {
METHOD(Diff, merge, METH_VARARGS),
METHOD(Diff, find_similar, METH_VARARGS | METH_KEYWORDS),
METHOD(Diff, from_c, METH_STATIC | METH_VARARGS),
{"parse_diff", (PyCFunction) Diff_parse_diff,
METH_VARARGS | METH_STATIC, Diff_parse_diff__doc__},
{NULL}
};

Expand Down
10 changes: 10 additions & 0 deletions test/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ def test_deltas(self):
# As explained in the libgit2 documentation, flags are not set
#self.assertEqual(delta.flags, patch_delta.flags)

def test_diff_parse(self):
diff = pygit2.Diff.parse_diff(PATCH)

stats = diff.stats
self.assertEqual(2, stats.deletions)
self.assertEqual(1, stats.insertions)
self.assertEqual(2, stats.files_changed)

deltas = list(diff.deltas)
self.assertEqual(2, len(deltas))

class BinaryDiffTest(utils.BinaryFileRepoTestCase):
def test_binary_diff(self):
Expand Down

0 comments on commit 711e9eb

Please sign in to comment.