From b7b9728115b5a054bbc908dedfff5208e6957348 Mon Sep 17 00:00:00 2001 From: Nico von Geyso Date: Mon, 6 May 2013 14:41:08 +0200 Subject: [PATCH] support for diffing the empty tree added missing support for diffing tree-to-tree with NULL as argument. (see issue #222) --- src/tree.c | 42 +++++++++++++++++++++--------------------- test/test_diff.py | 6 ++++++ 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/tree.c b/src/tree.c index 648b29d..f6a01cf 100644 --- a/src/tree.c +++ b/src/tree.c @@ -286,38 +286,38 @@ PyDoc_STRVAR(Tree_diff__doc__, " TODO"); PyObject * -Tree_diff(Tree *self, PyObject *args) +Tree_diff(Tree *self, PyObject *args, PyObject *kwds) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff_list *diff; - int err; + git_tree* tree = NULL; + git_index* index; + git_repository* repo; + int err, empty_tree = 0; + char *keywords[] = {"obj", "flags", "empty_tree", NULL}; Diff *py_diff; PyObject *py_obj = NULL; - if (!PyArg_ParseTuple(args, "|Oi", &py_obj, &opts.flags)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oii", keywords, + &py_obj, &opts.flags, &empty_tree)) return NULL; + repo = self->repo->repo; if (py_obj == NULL) { - err = git_diff_tree_to_workdir( - &diff, - self->repo->repo, - self->tree, - &opts); + if (empty_tree > 0) + err = git_diff_tree_to_tree(&diff, repo, self->tree, NULL, &opts); + else + err = git_diff_tree_to_workdir(&diff, repo, self->tree, &opts); + } else if (PyObject_TypeCheck(py_obj, &TreeType)) { - err = git_diff_tree_to_tree( - &diff, - self->repo->repo, - self->tree, - ((Tree *)py_obj)->tree, - &opts); + tree = ((Tree *)py_obj)->tree; + err = git_diff_tree_to_tree(&diff, repo, self->tree, tree, &opts); + } else if (PyObject_TypeCheck(py_obj, &IndexType)) { - err = git_diff_tree_to_index( - &diff, - self->repo->repo, - self->tree, - ((Index *)py_obj)->index, - &opts); + index = ((Index *)py_obj)->index; + err = git_diff_tree_to_index(&diff, repo, self->tree, index, &opts); + } else { PyErr_SetObject(PyExc_TypeError, py_obj); return NULL; @@ -355,7 +355,7 @@ PyMappingMethods Tree_as_mapping = { }; PyMethodDef Tree_methods[] = { - METHOD(Tree, diff, METH_VARARGS), + METHOD(Tree, diff, METH_VARARGS | METH_KEYWORDS), {NULL} }; diff --git a/test/test_diff.py b/test/test_diff.py index 71e15cd..c2f514e 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -142,6 +142,12 @@ class DiffTest(utils.BareRepoTestCase): self.assertEqual(patch.old_file_path, 'a') self.assertEqual(patch.new_file_path, 'a') + def test_diff_empty_tree(self): + commit_a = self.repo[COMMIT_SHA1_1] + diff = commit_a.tree.diff(empty_tree=True) + entries = [p.new_file_path for p in diff] + self.assertAll(lambda x: commit_a.tree[x], entries) + def test_diff_tree_opts(self): commit_c = self.repo[COMMIT_SHA1_3] commit_d = self.repo[COMMIT_SHA1_4]