Skip to content

Commit 044e30e

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

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

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, 'file')
545+
opt_values.insert(1, 'file')
546+
return a.diff(b, *opt_values)
545547

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

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

test/test_diff.py

+76
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,68 @@
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+
Blob 1 line 2
116+
Common footer of the file
117+
"""
118+
119+
TEXT_BLOB2 = """Common header of the file
120+
Blob 2 line 1
121+
Common middle line 1
122+
Common middle line 2
123+
Common middle line 3
124+
Blob 2 line 2
125+
Common footer of the file
126+
"""
127+
128+
PATCH_BLOBS_DEFAULT = """diff --git a/file b/file
129+
index 0b5ac93..ddfdbcc 100644
130+
--- a/file
131+
+++ b/file
132+
@@ -1,7 +1,7 @@
133+
Common header of the file
134+
-Blob 1 line 1
135+
+Blob 2 line 1
136+
Common middle line 1
137+
Common middle line 2
138+
Common middle line 3
139+
-Blob 1 line 2
140+
+Blob 2 line 2
141+
Common footer of the file
142+
"""
143+
144+
PATCH_BLOBS_NO_LEEWAY = """diff --git a/file b/file
145+
index 0b5ac93..ddfdbcc 100644
146+
--- a/file
147+
+++ b/file
148+
@@ -2 +2 @@ Common header of the file
149+
-Blob 1 line 1
150+
+Blob 2 line 1
151+
@@ -6 +6 @@ Common middle line 3
152+
-Blob 1 line 2
153+
+Blob 2 line 2
154+
"""
155+
156+
PATCH_BLOBS_ONE_CONTEXT_LINE = """diff --git a/file b/file
157+
index 0b5ac93..ddfdbcc 100644
158+
--- a/file
159+
+++ b/file
160+
@@ -1,3 +1,3 @@
161+
Common header of the file
162+
-Blob 1 line 1
163+
+Blob 2 line 1
164+
Common middle line 1
165+
@@ -5,3 +5,3 @@ Common middle line 2
166+
Common middle line 3
167+
-Blob 1 line 2
168+
+Blob 2 line 2
169+
Common footer of the file
170+
"""
171+
110172

111173
def test_diff_empty_index(dirtyrepo):
112174
repo = dirtyrepo
@@ -382,3 +444,17 @@ def test_parse_diff_bad():
382444
)
383445
with pytest.raises(pygit2.GitError):
384446
pygit2.Diff.parse_diff(diff)
447+
448+
449+
def test_diff_blobs(emptyrepo):
450+
repo = emptyrepo
451+
blob1 = repo.create_blob(TEXT_BLOB1.encode())
452+
blob2 = repo.create_blob(TEXT_BLOB2.encode())
453+
diff_default = repo.diff(blob1, blob2)
454+
assert diff_default.text == PATCH_BLOBS_DEFAULT
455+
diff_no_leeway = repo.diff(blob1, blob2, context_lines=0)
456+
assert diff_no_leeway.text == PATCH_BLOBS_NO_LEEWAY
457+
diff_one_context_line = repo.diff(blob1, blob2, context_lines=1)
458+
assert diff_one_context_line.text == PATCH_BLOBS_ONE_CONTEXT_LINE
459+
diff_all_together = repo.diff(blob1, blob2, context_lines=1, interhunk_lines=1)
460+
assert diff_all_together.text == PATCH_BLOBS_DEFAULT

0 commit comments

Comments
 (0)