Skip to content

Commit 8029765

Browse files
committed
Add support for GIT_DIFF_SHOW_BINARY
Adding the binary diff flag GIT_DIFF_SHOW_BINARY to pygit2. libgit2 0.23.0 already supports this constant to be used in diff flags and produces properly formated binary diffs.
1 parent ade211d commit 8029765

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

src/pygit2.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ moduleinit(PyObject* m)
323323
ADD_CONSTANT_INT(m, GIT_DIFF_IGNORE_CASE)
324324
ADD_CONSTANT_INT(m, GIT_DIFF_SHOW_UNTRACKED_CONTENT)
325325
ADD_CONSTANT_INT(m, GIT_DIFF_SKIP_BINARY_CHECK)
326+
ADD_CONSTANT_INT(m, GIT_DIFF_SHOW_BINARY)
326327
ADD_CONSTANT_INT(m, GIT_DIFF_INCLUDE_TYPECHANGE)
327328
ADD_CONSTANT_INT(m, GIT_DIFF_INCLUDE_TYPECHANGE_TREES)
328329
ADD_CONSTANT_INT(m, GIT_DIFF_RECURSE_IGNORED_DIRS)

test/data/binaryfilerepo.tar

90 KB
Binary file not shown.

test/test_diff.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import pygit2
3434
from pygit2 import GIT_DIFF_INCLUDE_UNMODIFIED
3535
from pygit2 import GIT_DIFF_IGNORE_WHITESPACE, GIT_DIFF_IGNORE_WHITESPACE_EOL
36+
from pygit2 import GIT_DIFF_SHOW_BINARY
3637
from pygit2 import GIT_DELTA_RENAMED
3738
from . import utils
3839
from itertools import chain
@@ -63,6 +64,21 @@
6364
-c/d contents
6465
"""
6566

67+
PATCH_BINARY = """diff --git a/binary_file b/binary_file
68+
index 86e5c10..b835d73 100644
69+
Binary files a/binary_file and b/binary_file differ
70+
"""
71+
72+
PATCH_BINARY_SHOW = """diff --git a/binary_file b/binary_file
73+
index 86e5c1008b5ce635d3e3fffa4434c5eccd8f00b6..b835d73543244b6694f36a8c5dfdffb71b153db7 100644
74+
GIT binary patch
75+
literal 8
76+
Pc${NM%FIhFs^kIy3n&7R
77+
78+
literal 8
79+
Pc${NM&PdElPvrst3ey5{
80+
"""
81+
6682
DIFF_HEAD_TO_INDEX_EXPECTED = [
6783
'staged_changes',
6884
'staged_changes_file_deleted',
@@ -308,5 +324,15 @@ def test_diff_stats(self):
308324
width=80)
309325
self.assertEqual(STATS_EXPECTED, formatted)
310326

327+
328+
class BinaryDiffTest(utils.BinaryFileRepoTestCase):
329+
def test_binary_diff(self):
330+
repo = self.repo
331+
diff = repo.diff('HEAD', 'HEAD^')
332+
self.assertEqual(PATCH_BINARY, diff.patch)
333+
diff = repo.diff('HEAD', 'HEAD^', flags=GIT_DIFF_SHOW_BINARY)
334+
self.assertEqual(PATCH_BINARY_SHOW, diff.patch)
335+
336+
311337
if __name__ == '__main__':
312338
unittest.main()

test/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ class EmptyRepoTestCase(AutoRepoTestCase):
160160

161161
repo_spec = 'tar', 'emptyrepo'
162162

163+
163164
class SubmoduleRepoTestCase(AutoRepoTestCase):
164165

165166
repo_spec = 'tar', 'submodulerepo'
167+
168+
169+
class BinaryFileRepoTestCase(AutoRepoTestCase):
170+
171+
repo_spec = 'tar', 'binaryfilerepo'

0 commit comments

Comments
 (0)