Skip to content

Commit 464ab5f

Browse files
authored
Merge pull request #1 from k0ral/master
fix: Support for more kinds of binary headers
2 parents 566f41b + da30780 commit 464ab5f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

gitdiff/binary.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import (
66
"fmt"
77
"io"
88
"io/ioutil"
9+
"regexp"
910
"strconv"
1011
"strings"
1112
)
1213

14+
var binaryRegexp = regexp.MustCompile(`^Binary files (/dev/null|a/(.+)|"a/(.+)") and (/dev/null|b/(.+)|"b/(.+)") differ\s*$`)
15+
1316
func (p *parser) ParseBinaryFragments(f *File) (n int, err error) {
1417
isBinary, hasData, err := p.ParseBinaryMarker()
1518
if err != nil || !isBinary {
@@ -56,7 +59,9 @@ func (p *parser) ParseBinaryMarker() (isBinary bool, hasData bool, err error) {
5659
case "Binary files differ\n":
5760
case "Files differ\n":
5861
default:
59-
return false, false, nil
62+
if !binaryRegexp.MatchString(p.Line(0)) {
63+
return false, false, nil
64+
}
6065
}
6166

6267
if err = p.Next(); err != nil && err != io.EOF {

gitdiff/binary_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ func TestParseBinaryMarker(t *testing.T) {
3030
IsBinary: false,
3131
HasData: false,
3232
},
33+
"binaryPatchCreated": {
34+
Input: "Binary files /dev/null and b/path/to/file.ext differ\n",
35+
IsBinary: true,
36+
HasData: false,
37+
},
38+
"binaryPatchModified": {
39+
Input: "Binary files a/path/to/file.ext and b/path/to/file.ext differ\n",
40+
IsBinary: true,
41+
HasData: false,
42+
},
43+
"binaryPatchModifiedQuoted": {
44+
Input: "Binary files \"a/path/to/file.ext\" and \"b/path/to/file.ext\" differ\n",
45+
IsBinary: true,
46+
HasData: false,
47+
},
48+
"binaryPatchDeleted": {
49+
Input: "Binary files a/path/to/file.ext and /dev/null differ\n",
50+
IsBinary: true,
51+
HasData: false,
52+
},
3353
}
3454

3555
for name, test := range tests {

0 commit comments

Comments
 (0)