From 85bd6dcd92f19c87734434e0398894e0b24bdf16 Mon Sep 17 00:00:00 2001 From: Richard Gomez Date: Mon, 11 Dec 2023 13:41:58 -0500 Subject: [PATCH] fix(gitparse): handle fromFileLine edge case --- pkg/gitparse/gitparse.go | 16 +++++--- pkg/gitparse/gitparse_test.go | 74 ++++++++++++++++++++++++++++++++++- pkg/sources/git/git.go | 23 ++++++----- 3 files changed, 97 insertions(+), 16 deletions(-) diff --git a/pkg/gitparse/gitparse.go b/pkg/gitparse/gitparse.go index f7b9124487d2..dcd544c85531 100644 --- a/pkg/gitparse/gitparse.go +++ b/pkg/gitparse/gitparse.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "fmt" + "github.com/go-logr/logr" "io" "os" "os/exec" @@ -92,6 +93,7 @@ func (state ParseState) String() string { "BinaryFileLine", "HunkLineNumberLine", "HunkContentLine", + "ParseFailure", }[state] } @@ -313,7 +315,7 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, commitChan ch case isMessageLine(isStaged, latestState, line): latestState = MessageLine - currentCommit.Message.Write(line[4:]) + currentCommit.Message.Write(line[4:]) // Messages are indented with 4 spaces. case isMessageEndLine(isStaged, latestState, line): latestState = MessageEndLine // NoOp @@ -425,13 +427,14 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, commitChan ch // Here be dragons... // Build an informative error message. - var err error + err := fmt.Errorf(`invalid line "%s" after state "%s"`, line, latestState) + var logger logr.Logger if currentCommit != nil && currentCommit.Hash != "" { - err = fmt.Errorf(`failed to parse line "%s" after state "%s" (commit=%s)`, line, latestState, currentCommit.Hash) + logger = ctx.Logger().WithValues("commit", currentCommit.Hash) } else { - err = fmt.Errorf(`failed to parse line "%s" after state "%s"`, line, latestState) + logger = ctx.Logger() } - ctx.Logger().V(2).Error(err, "Recovering at the latest commit or diff...\n") + logger.Error(err, "failed to parse Git input. Recovering at the latest commit or diff...") latestState = ParseFailure } @@ -612,8 +615,9 @@ func pathFromBinaryLine(line []byte) string { } // --- a/internal/addrs/move_endpoint_module.go +// --- /dev/null func isFromFileLine(isStaged bool, latestState ParseState, line []byte) bool { - if latestState != IndexLine { + if !(latestState == IndexLine || latestState == ModeLine) { return false } if len(line) >= 6 && bytes.Equal(line[:4], []byte("--- ")) { diff --git a/pkg/gitparse/gitparse_test.go b/pkg/gitparse/gitparse_test.go index 33d6e4483120..f343aead0011 100644 --- a/pkg/gitparse/gitparse_test.go +++ b/pkg/gitparse/gitparse_test.go @@ -323,6 +323,13 @@ func TestLineChecks(t *testing.T) { IndexLine, []byte("--- /dev/null"), }, + // New file (https://github.com/trufflesecurity/trufflehog/issues/2109) + // diff --git a/libs/Unfit-1.0 b/libs/Unfit-1.0 + // new file mode 160000 + { + ModeLine, + []byte("--- /dev/null"), + }, // Uncommon but valid prefixes. Will these ever show up? // https://stackoverflow.com/a/2530012 // https://git-scm.com/docs/git-config#Documentation/git-config.txt-diffmnemonicPrefix @@ -1148,7 +1155,45 @@ func TestMaxCommitSize(t *testing.T) { } -const commitLog = `commit 4727ffb7ad6dc5130bf4b4dd166e00705abdd018 (HEAD -> master) +const commitLog = `commit fd6e99e7a80199b76a694603be57c5ade1de18e7 +Author: Jaliborc +Date: Mon Apr 25 16:28:06 2011 +0100 + + Added Unusable coloring + +diff --git a/components/item.lua b/components/item.lua +index fc74534..f8d7d50 100755 +--- a/components/item.lua ++++ b/components/item.lua +@@ -9,6 +9,7 @@ ItemSlot:Hide() + Bagnon.ItemSlot = ItemSlot + + local ItemSearch = LibStub('LibItemSearch-1.0') ++local Unfit = LibStub('Unfit-1.0') + + local function hasBlizzQuestHighlight() + return GetContainerItemQuestInfo and true or false +diff --git a/embeds.xml b/embeds.xml +index d3f4e7c..0c2df69 100755 +--- a/embeds.xml ++++ b/embeds.xml +@@ -6,6 +6,7 @@ + + + ++