Skip to content

Commit

Permalink
Parse binary headers with file paths (#55)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bluekeyes authored Jan 8, 2025
1 parent 8584cd5 commit 14da3d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 11 additions & 4 deletions gitdiff/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func (p *parser) ParseBinaryFragments(f *File) (n int, err error) {
}

func (p *parser) ParseBinaryMarker() (isBinary bool, hasData bool, err error) {
switch p.Line(0) {
case "GIT binary patch\n":
line := p.Line(0)
switch {
case line == "GIT binary patch\n":
hasData = true
case "Binary files differ\n":
case "Files differ\n":
case isBinaryNoDataMarker(line):
default:
return false, false, nil
}
Expand All @@ -65,6 +65,13 @@ func (p *parser) ParseBinaryMarker() (isBinary bool, hasData bool, err error) {
return true, hasData, nil
}

func isBinaryNoDataMarker(line string) bool {
if strings.HasSuffix(line, " differ\n") {
return strings.HasPrefix(line, "Binary files ") || strings.HasPrefix(line, "Files ")
}
return false
}

func (p *parser) ParseBinaryFragmentHeader() (*BinaryFragment, error) {
parts := strings.SplitN(strings.TrimSuffix(p.Line(0), "\n"), " ", 2)
if len(parts) < 2 {
Expand Down
10 changes: 10 additions & 0 deletions gitdiff/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ func TestParseBinaryMarker(t *testing.T) {
IsBinary: true,
HasData: false,
},
"binaryFileNoPatchPaths": {
Input: "Binary files a/foo.bin and b/foo.bin differ\n",
IsBinary: true,
HasData: false,
},
"fileNoPatch": {
Input: "Files differ\n",
IsBinary: true,
HasData: false,
},
"textFile": {
Input: "@@ -10,14 +22,31 @@\n",
IsBinary: false,
Expand Down

0 comments on commit 14da3d3

Please sign in to comment.