Skip to content

Commit 14da3d3

Browse files
authoredJan 8, 2025··
Parse binary headers with file paths (#55)
Some patches may include one or more file paths as part of the binary header when there is no binary data. Git accounts for this by only checking the prefix and suffix of the line, but I missed that logic when implementing this originally.
1 parent 8584cd5 commit 14da3d3

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed
 

‎gitdiff/binary.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ func (p *parser) ParseBinaryFragments(f *File) (n int, err error) {
5050
}
5151

5252
func (p *parser) ParseBinaryMarker() (isBinary bool, hasData bool, err error) {
53-
switch p.Line(0) {
54-
case "GIT binary patch\n":
53+
line := p.Line(0)
54+
switch {
55+
case line == "GIT binary patch\n":
5556
hasData = true
56-
case "Binary files differ\n":
57-
case "Files differ\n":
57+
case isBinaryNoDataMarker(line):
5858
default:
5959
return false, false, nil
6060
}
@@ -65,6 +65,13 @@ func (p *parser) ParseBinaryMarker() (isBinary bool, hasData bool, err error) {
6565
return true, hasData, nil
6666
}
6767

68+
func isBinaryNoDataMarker(line string) bool {
69+
if strings.HasSuffix(line, " differ\n") {
70+
return strings.HasPrefix(line, "Binary files ") || strings.HasPrefix(line, "Files ")
71+
}
72+
return false
73+
}
74+
6875
func (p *parser) ParseBinaryFragmentHeader() (*BinaryFragment, error) {
6976
parts := strings.SplitN(strings.TrimSuffix(p.Line(0), "\n"), " ", 2)
7077
if len(parts) < 2 {

‎gitdiff/binary_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ func TestParseBinaryMarker(t *testing.T) {
2525
IsBinary: true,
2626
HasData: false,
2727
},
28+
"binaryFileNoPatchPaths": {
29+
Input: "Binary files a/foo.bin and b/foo.bin differ\n",
30+
IsBinary: true,
31+
HasData: false,
32+
},
33+
"fileNoPatch": {
34+
Input: "Files differ\n",
35+
IsBinary: true,
36+
HasData: false,
37+
},
2838
"textFile": {
2939
Input: "@@ -10,14 +22,31 @@\n",
3040
IsBinary: false,

0 commit comments

Comments
 (0)
Please sign in to comment.