From 7e0be1240ad5b5b8d7ded6a9083e90cb89b528ff Mon Sep 17 00:00:00 2001 From: Bill Rich Date: Thu, 26 Oct 2023 10:07:02 -0700 Subject: [PATCH] Fix binary handling (#1999) --- pkg/gitparse/gitparse.go | 16 ++++++++++------ pkg/gitparse/gitparse_test.go | 20 +++++++++++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/pkg/gitparse/gitparse.go b/pkg/gitparse/gitparse.go index 2813178a2b45..39a72d399c04 100644 --- a/pkg/gitparse/gitparse.go +++ b/pkg/gitparse/gitparse.go @@ -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() } @@ -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 @@ -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 @@ -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{ @@ -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 @@ -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 { diff --git a/pkg/gitparse/gitparse_test.go b/pkg/gitparse/gitparse_test.go index ce8427fd9a9b..02fbbd209e31 100644 --- a/pkg/gitparse/gitparse_test.go +++ b/pkg/gitparse/gitparse_test.go @@ -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, @@ -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++ } @@ -1715,14 +1719,24 @@ protos: Author: "John Smith ", 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 ", 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",