Skip to content

Commit

Permalink
Fix binary handling (trufflesecurity#1999)
Browse files Browse the repository at this point in the history
  • Loading branch information
bill-rich authored Oct 26, 2023
1 parent 4636dc0 commit 00a00ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
16 changes: 10 additions & 6 deletions pkg/gitparse/gitparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, commitChan ch
latestState = CommitLine

// If there is a currentDiff, add it to currentCommit.
if currentDiff.Content.Len() > 0 {
if currentDiff.Content.Len() > 0 || currentDiff.IsBinary {
currentCommit.Diffs = append(currentCommit.Diffs, currentDiff)
currentCommit.Size += currentDiff.Content.Len()
}
Expand Down Expand Up @@ -324,7 +324,7 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, commitChan ch
if currentCommit == nil {
currentCommit = &Commit{}
}
if currentDiff.Content.Len() > 0 {
if currentDiff.Content.Len() > 0 || currentDiff.IsBinary {
currentCommit.Diffs = append(currentCommit.Diffs, currentDiff)
// If the currentDiff is over 1GB, drop it into the channel so it isn't held in memory waiting for more commits.
totalSize := 0
Expand Down Expand Up @@ -356,8 +356,12 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, commitChan ch
case isBinaryLine(isStaged, latestState, line):
latestState = BinaryFileLine

currentDiff.IsBinary = true
currentDiff.PathB = pathFromBinaryLine(line)

// Don't do anything if the file is deleted. (pathA has file path, pathB is /dev/null)
if currentDiff.PathB != "" {
currentDiff.IsBinary = true
}
case isFromFileLine(isStaged, latestState, line):
latestState = FromFileLine
// NoOp
Expand All @@ -369,7 +373,7 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, commitChan ch
case isHunkLineNumberLine(isStaged, latestState, line):
latestState = HunkLineNumberLine

if currentDiff.Content.Len() > 0 {
if currentDiff.Content.Len() > 0 || currentDiff.IsBinary {
currentCommit.Diffs = append(currentCommit.Diffs, currentDiff)
}
currentDiff = Diff{
Expand Down Expand Up @@ -604,7 +608,7 @@ func pathFromBinaryLine(line []byte) string {
return ""
}
bRaw := sbytes[1]
return string(bRaw[:len(bRaw)-7]) // drop the "b/" and " differ"
return strings.TrimSpace(string(bRaw[:len(bRaw)-7])) // drop the "b/" and " differ"
}

// --- a/internal/addrs/move_endpoint_module.go
Expand Down Expand Up @@ -710,7 +714,7 @@ func isCommitSeparatorLine(isStaged bool, latestState ParseState, line []byte) b

func cleanupParse(currentCommit *Commit, currentDiff *Diff, commitChan chan Commit, totalLogSize *int) {
// Ignore empty or binary diffs (this condition may be redundant).
if currentDiff != nil && currentDiff.Content.Len() > 0 {
if currentDiff != nil && (currentDiff.Content.Len() > 0 || currentDiff.IsBinary) {
currentCommit.Diffs = append(currentCommit.Diffs, *currentDiff)
}
if currentCommit != nil {
Expand Down
20 changes: 17 additions & 3 deletions pkg/gitparse/gitparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,10 @@ func TestStagedDiffParsing(t *testing.T) {
Content: *bytes.NewBuffer([]byte("/**\n * This is usually used for command mode applications with a startup logic. The logic is executed inside\n * {@link QuarkusApplication#run} method before the main application exits.\n */\n")),
IsBinary: false,
},
{
PathB: "trufflehog_3.42.0_linux_arm64.tar.gz",
IsBinary: true,
},
{
PathB: "tzu",
LineStart: 11,
Expand Down Expand Up @@ -813,7 +817,7 @@ func TestStagedDiffParsing(t *testing.T) {
}

if !commit.Equal(&expected[i]) {
t.Errorf("Commit does not match.\nexpected: %+v\n\nactual : %+v\n", expected[i], commit)
t.Errorf("Commit does not match.\nexpected:\n%+v\n\nactual:\n%+v\n", expected[i], commit)
}
i++
}
Expand Down Expand Up @@ -1715,14 +1719,24 @@ protos:
Author: "John Smith <[email protected]>",
Date: newTime("Mon Jul 10 12:21:33 2023 -0400"),
Message: newStringBuilderValue("Change binary file\n"),
Diffs: []Diff{},
Diffs: []Diff{
{
PathB: "trufflehog_3.42.0_linux_arm64.tar.gz",
IsBinary: true,
},
},
},
{
Hash: "638595917417c5c8a956937b28c5127719023363",
Author: "John Smith <[email protected]>",
Date: newTime("Mon Jul 10 12:20:35 2023 -0400"),
Message: newStringBuilderValue("Add binary file\n"),
Diffs: []Diff{},
Diffs: []Diff{
{
PathB: "trufflehog_3.42.0_linux_arm64.tar.gz",
IsBinary: true,
},
},
},
{
Hash: "ce0f5d1fe0272f180ccb660196f439c0c2f4ec8e",
Expand Down

0 comments on commit 00a00ef

Please sign in to comment.