Refactored Index.diff() into Index.diff_to_tree()/diff_to_workdir()

This commit is contained in:
Nico von Geyso
2013-05-18 15:29:28 +02:00
parent 196d0595b0
commit 0fc7a4fbad
5 changed files with 70 additions and 43 deletions

View File

@@ -180,7 +180,7 @@ class Repository(_Repository):
# Case 2: Index to workdir
elif a is None and b is None:
return self.index.diff()
return self.index.diff_to_workdir()
# Case 3: Diff tree to index or workdir
elif isinstance(a, Tree) and b is None:

View File

@@ -109,7 +109,8 @@ diff_get_patch_byindex(git_diff_list* list, size_t idx)
goto cleanup;
PyList_SetItem(py_hunk->lines, j,
Py_BuildValue("cO", line_origin,
Py_BuildValue("OO",
to_unicode_n(&line_origin, 1, NULL, NULL),
to_unicode_n(line, line_len, NULL, NULL)
)
);

View File

@@ -31,6 +31,7 @@
#include "types.h"
#include "utils.h"
#include "oid.h"
#include "diff.h"
#include "index.h"
extern PyTypeObject IndexType;
@@ -114,54 +115,59 @@ Index_clear(Index *self)
}
PyDoc_STRVAR(Index_diff__doc__,
"diff([tree]) -> Diff\n"
PyDoc_STRVAR(Index_diff_to_workdir__doc__,
"diff_to_workdir() -> Diff\n"
"\n"
"Return a :py:class:`~pygit2.Diff` object with the differences between the\n"
"index and the working copy. If a :py:class:`~pygit2.Tree` object is\n"
"passed, return the diferences between the index and the given tree.");
"index and the working copy.\n");
PyObject *
Index_diff(Index *self, PyObject *args)
Index_diff_to_workdir(Index *self, PyObject *args)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff_list *diff;
int err;
Diff *py_diff;
PyObject *py_obj = NULL;
if (!PyArg_ParseTuple(args, "|O", &py_obj))
if (!PyArg_ParseTuple(args, "|i", &opts.flags))
return NULL;
if (py_obj == NULL) {
err = git_diff_index_to_workdir(
&diff,
self->repo->repo,
self->index,
&opts);
} else if (PyObject_TypeCheck(py_obj, &TreeType)) {
err = git_diff_tree_to_index(
&diff,
self->repo->repo,
((Tree *)py_obj)->tree,
self->index,
&opts);
} else {
PyErr_SetObject(PyExc_TypeError, py_obj);
return NULL;
}
err = git_diff_index_to_workdir(
&diff,
self->repo->repo,
self->index,
&opts);
if (err < 0)
return Error_set(err);
py_diff = PyObject_New(Diff, &DiffType);
if (py_diff) {
Py_INCREF(self->repo);
py_diff->repo = self->repo;
py_diff->list = diff;
}
return wrap_diff(diff, self->repo);
}
return (PyObject*)py_diff;
PyDoc_STRVAR(Index_diff_to_tree__doc__,
"diff_to_tree(tree) -> Diff\n"
"\n"
"Return a :py:class:`~pygit2.Diff` object with the differences between the\n"
"index and the given tree.\n");
PyObject *
Index_diff_to_tree(Index *self, PyObject *args)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff_list *diff;
git_repository* repo;
int err;
Tree *py_tree = NULL;
if (!PyArg_ParseTuple(args, "O!|i", &TreeType, &py_tree, &opts.flags))
return NULL;
repo = self->repo->repo;
err = git_diff_tree_to_index(&diff, repo, py_tree->tree, self->index, &opts);
if (err < 0)
return Error_set(err);
return wrap_diff(diff, self->repo);
}
@@ -393,7 +399,8 @@ PyMethodDef Index_methods[] = {
METHOD(Index, add, METH_VARARGS),
METHOD(Index, remove, METH_VARARGS),
METHOD(Index, clear, METH_NOARGS),
METHOD(Index, diff, METH_VARARGS),
METHOD(Index, diff_to_workdir, METH_VARARGS),
METHOD(Index, diff_to_tree, METH_VARARGS),
METHOD(Index, _find, METH_O),
METHOD(Index, read, METH_NOARGS),
METHOD(Index, write, METH_NOARGS),

View File

@@ -317,7 +317,6 @@ Tree_diff_to_index(Tree *self, PyObject *args, PyObject *kwds)
repo = self->repo->repo;
err = git_diff_tree_to_index(&diff, repo, self->tree, py_idx->index, &opts);
if (err < 0)
return Error_set(err);

View File

@@ -61,7 +61,7 @@ index 297efb8..0000000
-c/d contents
"""
DIFF_INDEX_EXPECTED = [
DIFF_HEAD_TO_INDEX_EXPECTED = [
'staged_changes',
'staged_changes_file_deleted',
'staged_changes_file_modified',
@@ -72,7 +72,7 @@ DIFF_INDEX_EXPECTED = [
'staged_new_file_modified'
]
DIFF_WORKDIR_EXPECTED = [
DIFF_HEAD_TO_WORKDIR_EXPECTED = [
'file_deleted',
'modified_file',
'staged_changes',
@@ -84,6 +84,17 @@ DIFF_WORKDIR_EXPECTED = [
'subdir/modified_file'
]
DIFF_INDEX_TO_WORK_EXPECTED = [
'file_deleted',
'modified_file',
'staged_changes_file_deleted',
'staged_changes_file_modified',
'staged_new_file_deleted',
'staged_new_file_modified',
'subdir/deleted_file',
'subdir/modified_file'
]
HUNK_EXPECTED = """- a contents 2
+ a contents
"""
@@ -95,11 +106,11 @@ class DiffDirtyTest(utils.DirtyRepoTestCase):
head = repo[repo.lookup_reference('HEAD').resolve().target]
diff = head.tree.diff_to_index(repo.index)
files = [patch.new_file_path for patch in diff]
self.assertEqual(DIFF_INDEX_EXPECTED, files)
self.assertEqual(DIFF_HEAD_TO_INDEX_EXPECTED, files)
diff = repo.diff('HEAD', cached=True)
files = [patch.new_file_path for patch in diff]
self.assertEqual(DIFF_INDEX_EXPECTED, files)
self.assertEqual(DIFF_HEAD_TO_INDEX_EXPECTED, files)
def test_workdir_to_tree(self):
repo = self.repo
@@ -107,11 +118,16 @@ class DiffDirtyTest(utils.DirtyRepoTestCase):
diff = head.tree.diff_to_workdir()
files = [patch.new_file_path for patch in diff]
self.assertEqual(DIFF_WORKDIR_EXPECTED, files)
self.assertEqual(DIFF_HEAD_TO_WORKDIR_EXPECTED, files)
diff = repo.diff('HEAD')
files = [patch.new_file_path for patch in diff]
self.assertEqual(DIFF_WORKDIR_EXPECTED, files)
self.assertEqual(DIFF_HEAD_TO_WORKDIR_EXPECTED, files)
def test_index_to_workdir(self):
diff = self.repo.diff()
files = [patch.new_file_path for patch in diff]
self.assertEqual(DIFF_INDEX_TO_WORK_EXPECTED, files)
class DiffTest(utils.BareRepoTestCase):
@@ -126,6 +142,10 @@ class DiffTest(utils.BareRepoTestCase):
repo = self.repo
head = repo[repo.lookup_reference('HEAD').resolve().target]
diff = self.repo.index.diff_to_tree(head.tree)
files = [patch.new_file_path.split('/')[0] for patch in diff]
self.assertEqual([x.name for x in head.tree], files)
diff = head.tree.diff_to_index(repo.index)
files = [patch.new_file_path.split('/')[0] for patch in diff]
self.assertEqual([x.name for x in head.tree], files)