Skip to content

Commit 181bf38

Browse files
authored
Merge pull request #6 from ramusus/master
support of git binary files
2 parents 31ffaf2 + 042773d commit 181bf38

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
diff --git a/project/media/i/asc.gif b/project/media/i/asc.gif
2+
new file mode 100755
3+
index 0000000..71e31ac
4+
Binary files /dev/null and b/project/media/i/asc.gif differ

tests/test_patch.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,23 @@ def test_git_header_long(self):
620620
results_main = wtp.patch.parse_header(text)
621621
self.assertEqual(results_main, expected)
622622

623+
def test_git_binary_files(self):
624+
with open('tests/casefiles/git-binary-files.diff') as f:
625+
text = f.read()
626+
627+
expected = wtp.patch.header(
628+
index_path = None,
629+
old_path = '/dev/null',
630+
old_version = '0000000',
631+
new_path = 'project/media/i/asc.gif',
632+
new_version = '71e31ac')
633+
634+
results = wtp.patch.parse_git_header(text)
635+
self.assertEqual(results, expected)
636+
637+
results_main = wtp.patch.parse_header(text)
638+
self.assertEqual(results_main, expected)
639+
623640
def test_svn_header(self):
624641
with open('tests/casefiles/svn-header.diff') as f:
625642
text = f.read()

whatthepatch/patch.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
git_header_old_line = re.compile('^--- (.+)$')
4646
git_header_new_line = re.compile('^\+\+\+ (.+)$')
4747
git_header_file_mode = re.compile('^(new|deleted) file mode \d{6}$')
48+
git_header_binary_file = re.compile('^Binary files (.+) and (.+) differ')
4849

4950
bzr_header_index = re.compile("=== (.+)")
5051
bzr_header_old_line = unified_header_old_line
@@ -89,7 +90,7 @@ def parse_patch(text):
8990
difftext = '\n'.join(diff) + '\n'
9091
h = parse_header(diff)
9192
d = parse_diff(diff)
92-
if d:
93+
if h or d:
9394
yield diffobj(header=h, changes=d, text=difftext)
9495

9596
def parse_header(text):
@@ -189,12 +190,11 @@ def parse_git_header(text):
189190
except AttributeError:
190191
lines = text
191192

192-
headers = findall_regex(lines, git_header_old_line)
193-
if len(headers) == 0:
194-
return None
193+
# headers = findall_regex(lines, git_header_old_line)
194+
# if len(headers) == 0:
195+
# return None
195196

196-
over = None
197-
nver = None
197+
over = nver = old_path = new_path = None
198198
while len(lines) > 1:
199199
g = git_header_index.match(lines[0])
200200
# git always has it's own special headers
@@ -210,18 +210,22 @@ def parse_git_header(text):
210210
if n:
211211
old_path = o.group(1)
212212
new_path = n.group(1)
213-
if old_path.startswith('a/'):
214-
old_path = old_path[2:]
215-
216-
if new_path.startswith('b/'):
217-
new_path = new_path[2:]
218-
219-
return header(
220-
index_path = None,
221-
old_path = old_path,
222-
old_version = over,
223-
new_path = new_path,
224-
new_version = nver)
213+
binary = git_header_binary_file.match(lines[0])
214+
if binary:
215+
old_path = binary.group(1)
216+
new_path = binary.group(2)
217+
if old_path and new_path:
218+
if old_path.startswith('a/'):
219+
old_path = old_path[2:]
220+
221+
if new_path.startswith('b/'):
222+
new_path = new_path[2:]
223+
return header(
224+
index_path = None,
225+
old_path = old_path,
226+
old_version = over,
227+
new_path = new_path,
228+
new_version = nver)
225229

226230
return None
227231

0 commit comments

Comments
 (0)