Skip to content

Commit 3277587

Browse files
committed
Add basic support for binary diffs
1 parent 8c3ae63 commit 3277587

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

diff/diff_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,76 @@ func TestParseMultiFileDiffHeaders(t *testing.T) {
388388
},
389389
},
390390
},
391+
{
392+
filename: "sample_binary_inline.diff",
393+
wantDiffs: []*FileDiff{
394+
{
395+
OrigName: "a/logo-old.png",
396+
OrigTime: nil,
397+
NewName: "/dev/null",
398+
NewTime: nil,
399+
Extended: []string{
400+
"diff --git a/logo-old.png b/logo-old.png",
401+
"deleted file mode 100644",
402+
"index d29d0e9757e0d9b854a8ed58f170bcb454cc1ae3..0000000000000000000000000000000000000000",
403+
"GIT binary patch",
404+
"literal 0",
405+
"HcmV?d00001",
406+
"",
407+
"literal 0",
408+
"HcmV?d00001",
409+
"",
410+
},
411+
},
412+
{
413+
OrigName: "a/logo-old.png",
414+
OrigTime: nil,
415+
NewName: "b/logo-old.png",
416+
NewTime: nil,
417+
Extended: []string{
418+
"diff --git a/logo-old.png b/logo-old.png",
419+
"index ff82e793467f2050d731d75b4968d2e6b9c5d33b..d29d0e9757e0d9b854a8ed58f170bcb454cc1ae3 100644",
420+
"GIT binary patch",
421+
"literal 0",
422+
"HcmV?d00001",
423+
"",
424+
"literal 0",
425+
"HcmV?d00001",
426+
"",
427+
},
428+
},
429+
{
430+
OrigName: "a/logo.png",
431+
OrigTime: nil,
432+
NewName: "b/logo-old.png",
433+
NewTime: nil,
434+
Extended: []string{
435+
"diff --git a/logo.png b/logo-old.png",
436+
"similarity index 100%",
437+
"rename from logo.png",
438+
"rename to logo-old.png",
439+
},
440+
},
441+
{
442+
OrigName: "/dev/null",
443+
OrigTime: nil,
444+
NewName: "b/logo.png",
445+
NewTime: nil,
446+
Extended: []string{
447+
"diff --git a/logo.png b/logo.png",
448+
"new file mode 100644",
449+
"index 0000000000000000000000000000000000000000..ff82e793467f2050d731d75b4968d2e6b9c5d33b",
450+
"GIT binary patch",
451+
"literal 0",
452+
"HcmV?d00001",
453+
"",
454+
"literal 0",
455+
"HcmV?d00001",
456+
"",
457+
},
458+
},
459+
},
460+
},
391461
{
392462
filename: "sample_multi_file_new_win.diff",
393463
wantDiffs: []*FileDiff{

diff/parse.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func (r *FileDiffReader) ReadExtendedHeaders() ([]string, error) {
341341
func handleEmpty(fd *FileDiff) (wasEmpty bool) {
342342
var err error
343343
switch {
344-
case (len(fd.Extended) == 3 || len(fd.Extended) == 4 && strings.HasPrefix(fd.Extended[3], "Binary files ")) &&
344+
case (len(fd.Extended) == 3 || len(fd.Extended) == 4 && strings.HasPrefix(fd.Extended[3], "Binary files ") || len(fd.Extended) > 4 && strings.HasPrefix(fd.Extended[3], "GIT binary patch")) &&
345345
strings.HasPrefix(fd.Extended[1], "new file mode ") && strings.HasPrefix(fd.Extended[0], "diff --git "):
346346

347347
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
@@ -351,7 +351,7 @@ func handleEmpty(fd *FileDiff) (wasEmpty bool) {
351351
fd.NewName = names[1]
352352
}
353353
return true
354-
case (len(fd.Extended) == 3 || len(fd.Extended) == 4 && strings.HasPrefix(fd.Extended[3], "Binary files ")) &&
354+
case (len(fd.Extended) == 3 || len(fd.Extended) == 4 && strings.HasPrefix(fd.Extended[3], "Binary files ") || len(fd.Extended) > 4 && strings.HasPrefix(fd.Extended[3], "GIT binary patch")) &&
355355
strings.HasPrefix(fd.Extended[1], "deleted file mode ") && strings.HasPrefix(fd.Extended[0], "diff --git "):
356356

357357
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
@@ -372,7 +372,7 @@ func handleEmpty(fd *FileDiff) (wasEmpty bool) {
372372
fd.NewName = names[1]
373373
}
374374
return true
375-
case len(fd.Extended) == 3 && strings.HasPrefix(fd.Extended[2], "Binary files ") && strings.HasPrefix(fd.Extended[0], "diff --git "):
375+
case (len(fd.Extended) == 3 && strings.HasPrefix(fd.Extended[2], "Binary files ") || len(fd.Extended) > 3 && strings.HasPrefix(fd.Extended[2], "GIT binary patch")) && strings.HasPrefix(fd.Extended[0], "diff --git "):
376376
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
377377
fd.OrigName, err = strconv.Unquote(names[0])
378378
if err != nil {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
diff --git a/logo-old.png b/logo-old.png
2+
deleted file mode 100644
3+
index d29d0e9757e0d9b854a8ed58f170bcb454cc1ae3..0000000000000000000000000000000000000000
4+
GIT binary patch
5+
literal 0
6+
HcmV?d00001
7+
8+
literal 0
9+
HcmV?d00001
10+
11+
diff --git a/logo-old.png b/logo-old.png
12+
index ff82e793467f2050d731d75b4968d2e6b9c5d33b..d29d0e9757e0d9b854a8ed58f170bcb454cc1ae3 100644
13+
GIT binary patch
14+
literal 0
15+
HcmV?d00001
16+
17+
literal 0
18+
HcmV?d00001
19+
20+
diff --git a/logo.png b/logo-old.png
21+
similarity index 100%
22+
rename from logo.png
23+
rename to logo-old.png
24+
diff --git a/logo.png b/logo.png
25+
new file mode 100644
26+
index 0000000000000000000000000000000000000000..ff82e793467f2050d731d75b4968d2e6b9c5d33b
27+
GIT binary patch
28+
literal 0
29+
HcmV?d00001
30+
31+
literal 0
32+
HcmV?d00001
33+

0 commit comments

Comments
 (0)