Added: support for context_lines and interhunk_lines for diffs
This commit is contained in:
@@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
# Import from the Standard Library
|
# Import from the Standard Library
|
||||||
from string import hexdigits
|
from string import hexdigits
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
# Import from pygit2
|
# Import from pygit2
|
||||||
from utils import text_type
|
from utils import text_type
|
||||||
@@ -134,12 +135,29 @@ class Repository(_Repository):
|
|||||||
#
|
#
|
||||||
# Diff
|
# Diff
|
||||||
#
|
#
|
||||||
def diff(self, a=None, b=None, cached=False, flags=0):
|
def diff(self, a=None, b=None, cached=False, flags=0, context_lines=3,
|
||||||
|
interhunk_lines=0):
|
||||||
"""
|
"""
|
||||||
Show changes between the working tree and the index or a tree,
|
Show changes between the working tree and the index or a tree,
|
||||||
changes between the index and a tree, changes between two trees, or
|
changes between the index and a tree, changes between two trees, or
|
||||||
changes between two blobs.
|
changes between two blobs.
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
|
||||||
|
cached
|
||||||
|
use staged changes instead of workdir
|
||||||
|
|
||||||
|
flag
|
||||||
|
a GIT_DIFF_* constant
|
||||||
|
|
||||||
|
context_lines
|
||||||
|
the number of unchanged lines that define the boundary
|
||||||
|
of a hunk (and to display before and after)\n"
|
||||||
|
|
||||||
|
interhunk_lines
|
||||||
|
the maximum number of unchanged lines between hunk
|
||||||
|
boundaries before the hunks will be merged into a one
|
||||||
|
|
||||||
Examples::
|
Examples::
|
||||||
|
|
||||||
# Changes in the working tree not yet staged for the next commit
|
# Changes in the working tree not yet staged for the next commit
|
||||||
@@ -174,20 +192,26 @@ class Repository(_Repository):
|
|||||||
a = treeish_to_tree(a) or a
|
a = treeish_to_tree(a) or a
|
||||||
b = treeish_to_tree(b) or b
|
b = treeish_to_tree(b) or b
|
||||||
|
|
||||||
|
opts = OrderedDict([
|
||||||
|
('flags', flags),
|
||||||
|
('context_lines', context_lines),
|
||||||
|
('interhunk_lines', interhunk_lines)
|
||||||
|
])
|
||||||
|
|
||||||
# Case 1: Diff tree to tree
|
# Case 1: Diff tree to tree
|
||||||
if isinstance(a, Tree) and isinstance(b, Tree):
|
if isinstance(a, Tree) and isinstance(b, Tree):
|
||||||
return a.diff_to_tree(b, flags=flags)
|
return a.diff_to_tree(b, **opts)
|
||||||
|
|
||||||
# Case 2: Index to workdir
|
# Case 2: Index to workdir
|
||||||
elif a is None and b is None:
|
elif a is None and b is None:
|
||||||
return self.index.diff_to_workdir()
|
return self.index.diff_to_workdir(*opts.values())
|
||||||
|
|
||||||
# Case 3: Diff tree to index or workdir
|
# Case 3: Diff tree to index or workdir
|
||||||
elif isinstance(a, Tree) and b is None:
|
elif isinstance(a, Tree) and b is None:
|
||||||
if cached:
|
if cached:
|
||||||
return a.diff_to_index(self.index, flags=flags)
|
return a.diff_to_index(self.index, **opts)
|
||||||
else:
|
else:
|
||||||
return a.diff_to_workdir(flags)
|
return a.diff_to_workdir(*opts.values())
|
||||||
|
|
||||||
# Case 4: Diff blob to blob
|
# Case 4: Diff blob to blob
|
||||||
if isinstance(a, Blob) and isinstance(b, Blob):
|
if isinstance(a, Blob) and isinstance(b, Blob):
|
||||||
|
36
src/index.c
36
src/index.c
@@ -116,10 +116,20 @@ Index_clear(Index *self)
|
|||||||
|
|
||||||
|
|
||||||
PyDoc_STRVAR(Index_diff_to_workdir__doc__,
|
PyDoc_STRVAR(Index_diff_to_workdir__doc__,
|
||||||
"diff_to_workdir() -> Diff\n"
|
"diff_to_workdir([flag, context_lines, interhunk_lines]) -> Diff\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Return a :py:class:`~pygit2.Diff` object with the differences between the\n"
|
"Return a :py:class:`~pygit2.Diff` object with the differences between the\n"
|
||||||
"index and the working copy.\n");
|
"index and the working copy.\n"
|
||||||
|
"\n"
|
||||||
|
"Arguments:\n"
|
||||||
|
"\n"
|
||||||
|
"flag: a GIT_DIFF_* constant.\n"
|
||||||
|
"\n"
|
||||||
|
"context_lines: the number of unchanged lines that define the boundary\n"
|
||||||
|
" of a hunk (and to display before and after)\n"
|
||||||
|
"\n"
|
||||||
|
"interhunk_lines: the maximum number of unchanged lines between hunk\n"
|
||||||
|
" boundaries before the hunks will be merged into a one.\n");
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
Index_diff_to_workdir(Index *self, PyObject *args)
|
Index_diff_to_workdir(Index *self, PyObject *args)
|
||||||
@@ -128,7 +138,8 @@ Index_diff_to_workdir(Index *self, PyObject *args)
|
|||||||
git_diff_list *diff;
|
git_diff_list *diff;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "|i", &opts.flags))
|
if (!PyArg_ParseTuple(args, "|IHH", &opts.flags, &opts.context_lines,
|
||||||
|
&opts.interhunk_lines))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
err = git_diff_index_to_workdir(
|
err = git_diff_index_to_workdir(
|
||||||
@@ -144,10 +155,22 @@ Index_diff_to_workdir(Index *self, PyObject *args)
|
|||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(Index_diff_to_tree__doc__,
|
PyDoc_STRVAR(Index_diff_to_tree__doc__,
|
||||||
"diff_to_tree(tree) -> Diff\n"
|
"diff_to_tree(tree [, flag, context_lines, interhunk_lines]) -> Diff\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Return a :py:class:`~pygit2.Diff` object with the differences between the\n"
|
"Return a :py:class:`~pygit2.Diff` object with the differences between the\n"
|
||||||
"index and the given tree.\n");
|
"index and the given tree.\n"
|
||||||
|
"\n"
|
||||||
|
"Arguments:\n"
|
||||||
|
"\n"
|
||||||
|
"tree: the tree to diff.\n"
|
||||||
|
"\n"
|
||||||
|
"flag: a GIT_DIFF_* constant.\n"
|
||||||
|
"\n"
|
||||||
|
"context_lines: the number of unchanged lines that define the boundary\n"
|
||||||
|
" of a hunk (and to display before and after)\n"
|
||||||
|
"\n"
|
||||||
|
"interhunk_lines: the maximum number of unchanged lines between hunk\n"
|
||||||
|
" boundaries before the hunks will be merged into a one.\n");
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
Index_diff_to_tree(Index *self, PyObject *args)
|
Index_diff_to_tree(Index *self, PyObject *args)
|
||||||
@@ -159,7 +182,8 @@ Index_diff_to_tree(Index *self, PyObject *args)
|
|||||||
|
|
||||||
Tree *py_tree = NULL;
|
Tree *py_tree = NULL;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O!|i", &TreeType, &py_tree, &opts.flags))
|
if (!PyArg_ParseTuple(args, "O!|IHH", &TreeType, &py_tree, &opts.flags,
|
||||||
|
&opts.context_lines, &opts.interhunk_lines))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
repo = git_tree_owner(py_tree->tree);
|
repo = git_tree_owner(py_tree->tree);
|
||||||
|
65
src/tree.c
65
src/tree.c
@@ -272,9 +272,19 @@ Tree_getitem(Tree *self, PyObject *value)
|
|||||||
|
|
||||||
|
|
||||||
PyDoc_STRVAR(Tree_diff_to_workdir__doc__,
|
PyDoc_STRVAR(Tree_diff_to_workdir__doc__,
|
||||||
"diff_to_workdir([flags]) -> Diff\n"
|
"diff_to_workdir([flags, context_lines, interhunk_lines]) -> Diff\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Show the changes between the tree and the workdir.\n");
|
"Show the changes between the :py:class:`~pygit2.Tree` and the workdir.\n"
|
||||||
|
"\n"
|
||||||
|
"Arguments:\n"
|
||||||
|
"\n"
|
||||||
|
"flag: a GIT_DIFF_* constant.\n"
|
||||||
|
"\n"
|
||||||
|
"context_lines: the number of unchanged lines that define the boundary\n"
|
||||||
|
" of a hunk (and to display before and after)\n"
|
||||||
|
"\n"
|
||||||
|
"interhunk_lines: the maximum number of unchanged lines between hunk\n"
|
||||||
|
" boundaries before the hunks will be merged into a one.\n");
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
Tree_diff_to_workdir(Tree *self, PyObject *args)
|
Tree_diff_to_workdir(Tree *self, PyObject *args)
|
||||||
@@ -287,7 +297,8 @@ Tree_diff_to_workdir(Tree *self, PyObject *args)
|
|||||||
Diff *py_diff;
|
Diff *py_diff;
|
||||||
PyObject *py_obj = NULL;
|
PyObject *py_obj = NULL;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "|i", &opts.flags))
|
if (!PyArg_ParseTuple(args, "|IHH", &opts.flags, &opts.context_lines,
|
||||||
|
&opts.interhunk_lines))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
repo = git_tree_owner(self->tree);
|
repo = git_tree_owner(self->tree);
|
||||||
@@ -301,9 +312,21 @@ Tree_diff_to_workdir(Tree *self, PyObject *args)
|
|||||||
|
|
||||||
|
|
||||||
PyDoc_STRVAR(Tree_diff_to_index__doc__,
|
PyDoc_STRVAR(Tree_diff_to_index__doc__,
|
||||||
"diff_to_index(index, [flags]) -> Diff\n"
|
"diff_to_index(index, [flags, context_lines, interhunk_lines]) -> Diff\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Show the changes between the index and a given tree.\n");
|
"Show the changes between the index and a given :py:class:`~pygit2.Tree`.\n"
|
||||||
|
"\n"
|
||||||
|
"Arguments:\n"
|
||||||
|
"\n"
|
||||||
|
"tree: the :py:class:`~pygit2.Tree` to diff.\n"
|
||||||
|
"\n"
|
||||||
|
"flag: a GIT_DIFF_* constant.\n"
|
||||||
|
"\n"
|
||||||
|
"context_lines: the number of unchanged lines that define the boundary\n"
|
||||||
|
" of a hunk (and to display before and after)\n"
|
||||||
|
"\n"
|
||||||
|
"interhunk_lines: the maximum number of unchanged lines between hunk\n"
|
||||||
|
" boundaries before the hunks will be merged into a one.\n");
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
Tree_diff_to_index(Tree *self, PyObject *args, PyObject *kwds)
|
Tree_diff_to_index(Tree *self, PyObject *args, PyObject *kwds)
|
||||||
@@ -318,7 +341,9 @@ Tree_diff_to_index(Tree *self, PyObject *args, PyObject *kwds)
|
|||||||
Diff *py_diff;
|
Diff *py_diff;
|
||||||
Index *py_idx = NULL;
|
Index *py_idx = NULL;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O!|i", &IndexType, &py_idx, &opts.flags))
|
if (!PyArg_ParseTuple(args, "O!|IHH", &IndexType, &py_idx, &opts.flags,
|
||||||
|
&opts.context_lines,
|
||||||
|
&opts.interhunk_lines))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
repo = git_tree_owner(self->tree);
|
repo = git_tree_owner(self->tree);
|
||||||
@@ -331,10 +356,24 @@ Tree_diff_to_index(Tree *self, PyObject *args, PyObject *kwds)
|
|||||||
|
|
||||||
|
|
||||||
PyDoc_STRVAR(Tree_diff_to_tree__doc__,
|
PyDoc_STRVAR(Tree_diff_to_tree__doc__,
|
||||||
"diff_to_tree([tree, flags, swap]) -> Diff\n"
|
"diff_to_tree([tree, flags, context_lines, interhunk_lines, swap]) -> Diff\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Show the changes between two trees. If no tree is given the empty tree will"
|
"Show the changes between two trees\n"
|
||||||
"be used instead.\n");
|
"\n"
|
||||||
|
"Arguments:\n"
|
||||||
|
"\n"
|
||||||
|
"tree: the :py:class:`~pygit2.Tree` to diff. If no tree is given the empty\n"
|
||||||
|
" tree will be used instead.\n"
|
||||||
|
"\n"
|
||||||
|
"flag: a GIT_DIFF_* constant.\n"
|
||||||
|
"\n"
|
||||||
|
"context_lines: the number of unchanged lines that define the boundary\n"
|
||||||
|
" of a hunk (and to display before and after)\n"
|
||||||
|
"\n"
|
||||||
|
"interhunk_lines: the maximum number of unchanged lines between hunk\n"
|
||||||
|
" boundaries before the hunks will be merged into a one.\n"
|
||||||
|
"\n"
|
||||||
|
"swap: instead of diffing a to b. Diff b to a.\n");
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
Tree_diff_to_tree(Tree *self, PyObject *args, PyObject *kwds)
|
Tree_diff_to_tree(Tree *self, PyObject *args, PyObject *kwds)
|
||||||
@@ -344,14 +383,16 @@ Tree_diff_to_tree(Tree *self, PyObject *args, PyObject *kwds)
|
|||||||
git_tree *from, *to, *tmp;
|
git_tree *from, *to, *tmp;
|
||||||
git_repository* repo;
|
git_repository* repo;
|
||||||
int err, swap = 0;
|
int err, swap = 0;
|
||||||
char *keywords[] = {"obj", "flags", "swap", NULL};
|
char *keywords[] = {"obj", "flags", "context_lines", "interhunk_lines",
|
||||||
|
"swap", NULL};
|
||||||
|
|
||||||
Diff *py_diff;
|
Diff *py_diff;
|
||||||
Tree *py_tree = NULL;
|
Tree *py_tree = NULL;
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O!ii", keywords,
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O!IHHi", keywords,
|
||||||
&TreeType, &py_tree, &opts.flags,
|
&TreeType, &py_tree, &opts.flags,
|
||||||
&swap))
|
&opts.context_lines,
|
||||||
|
&opts.interhunk_lines, &swap))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
repo = git_tree_owner(self->tree);
|
repo = git_tree_owner(self->tree);
|
||||||
|
Reference in New Issue
Block a user