Skip to content

Commit a2adf24

Browse files
committed
repository - diff blobs: consider the values of context_lines and interhunk_lines
1 parent 212cae3 commit a2adf24

File tree

3 files changed

+106
-5
lines changed

3 files changed

+106
-5
lines changed

Diff for: pygit2/repository.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,9 @@ def diff(
541541

542542
# Case 4: Diff blob to blob
543543
if isinstance(a, Blob) and isinstance(b, Blob):
544-
return a.diff(b)
544+
opt_values.insert(1, '')
545+
opt_values.insert(1, '')
546+
return a.diff(b, *opt_values)
545547

546548
raise ValueError('Only blobs and treeish can be diffed')
547549

Diff for: src/blob.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ PyDoc_STRVAR(Blob_diff__doc__,
6060
" Treat old blob as if it had this filename.\n"
6161
"\n"
6262
"new_as_path : str\n"
63-
" Treat new blob as if it had this filename.\n");
63+
" Treat new blob as if it had this filename.\n"
64+
"\n"
65+
"context_lines: int\n"
66+
" Number of unchanged lines that define the boundary of a hunk\n"
67+
" (and to display before and after).\n"
68+
"\n"
69+
"interhunk_lines: int\n"
70+
" Maximum number of unchanged lines between hunk boundaries\n"
71+
" before the hunks will be merged into one.\n");
6472

6573
PyObject *
6674
Blob_diff(Blob *self, PyObject *args, PyObject *kwds)
@@ -70,11 +78,12 @@ Blob_diff(Blob *self, PyObject *args, PyObject *kwds)
7078
char *old_as_path = NULL, *new_as_path = NULL;
7179
Blob *other = NULL;
7280
int err;
73-
char *keywords[] = {"blob", "flag", "old_as_path", "new_as_path", NULL};
81+
char *keywords[] = {"blob", "flag", "old_as_path", "new_as_path", "context_lines", "interhunk_lines", NULL};
7482

75-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O!Iss", keywords,
83+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O!IssHH", keywords,
7684
&BlobType, &other, &opts.flags,
77-
&old_as_path, &new_as_path))
85+
&old_as_path, &new_as_path,
86+
&opts.context_lines, &opts.interhunk_lines))
7887
return NULL;
7988

8089
if (Object__load((Object*)self) == NULL) { return NULL; } // Lazy load

Diff for: test/test_diff.py

+90
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,84 @@
107107
delete mode 100644 c/d
108108
"""
109109

110+
TEXT_BLOB1 = """Common header of the file
111+
Blob 1 line 1
112+
Common middle line 1
113+
Common middle line 2
114+
Common middle line 3
115+
Common middle line 4
116+
Common middle line 5
117+
Common middle line 6
118+
Common middle line 7
119+
Blob 1 line 2
120+
Common footer of the file
121+
"""
122+
123+
TEXT_BLOB2 = """Common header of the file
124+
Blob 2 line 1
125+
Common middle line 1
126+
Common middle line 2
127+
Common middle line 3
128+
Common middle line 4
129+
Common middle line 5
130+
Common middle line 6
131+
Common middle line 7
132+
Blob 2 line 2
133+
Common footer of the file
134+
"""
135+
136+
PATCH_BLOBS_DEFAULT = """diff --git a/ b/
137+
index 183e154..7dcd86c 100644
138+
--- a/
139+
+++ b/
140+
@@ -1,5 +1,5 @@
141+
Common header of the file
142+
-Blob 1 line 1
143+
+Blob 2 line 1
144+
Common middle line 1
145+
Common middle line 2
146+
Common middle line 3
147+
@@ -7,5 +7,5 @@ Common middle line 4
148+
Common middle line 5
149+
Common middle line 6
150+
Common middle line 7
151+
-Blob 1 line 2
152+
+Blob 2 line 2
153+
Common footer of the file
154+
"""
155+
156+
PATCH_BLOBS_CONTEXT_LINES_0 = """diff --git a/ b/
157+
index 183e154..7dcd86c 100644
158+
--- a/
159+
+++ b/
160+
@@ -2 +2 @@ Common header of the file
161+
-Blob 1 line 1
162+
+Blob 2 line 1
163+
@@ -10 +10 @@ Common middle line 7
164+
-Blob 1 line 2
165+
+Blob 2 line 2
166+
"""
167+
168+
PATCH_BLOBS_INTERHUNK_LINES_1 = """diff --git a/ b/
169+
index 183e154..7dcd86c 100644
170+
--- a/
171+
+++ b/
172+
@@ -1,11 +1,11 @@
173+
Common header of the file
174+
-Blob 1 line 1
175+
+Blob 2 line 1
176+
Common middle line 1
177+
Common middle line 2
178+
Common middle line 3
179+
Common middle line 4
180+
Common middle line 5
181+
Common middle line 6
182+
Common middle line 7
183+
-Blob 1 line 2
184+
+Blob 2 line 2
185+
Common footer of the file
186+
"""
187+
110188

111189
def test_diff_empty_index(dirtyrepo):
112190
repo = dirtyrepo
@@ -382,3 +460,15 @@ def test_parse_diff_bad():
382460
)
383461
with pytest.raises(pygit2.GitError):
384462
pygit2.Diff.parse_diff(diff)
463+
464+
465+
def test_diff_blobs(emptyrepo):
466+
repo = emptyrepo
467+
blob1 = repo.create_blob(TEXT_BLOB1.encode())
468+
blob2 = repo.create_blob(TEXT_BLOB2.encode())
469+
diff_default = repo.diff(blob1, blob2)
470+
assert diff_default.text == PATCH_BLOBS_DEFAULT
471+
diff_context_lines = repo.diff(blob1, blob2, context_lines=0)
472+
assert diff_context_lines.text == PATCH_BLOBS_CONTEXT_LINES_0
473+
diff_inter_hunk = repo.diff(blob1, blob2, interhunk_lines=1)
474+
assert diff_inter_hunk.text == PATCH_BLOBS_INTERHUNK_LINES_1

0 commit comments

Comments
 (0)