Skip to content

Find, verify, and analyze leaked credentials

Resolve a ref as arg for --since_commit (#57)

Bill Rich9f378b8cb3a3
diff --git a/main.go b/main.go
index e802fa462..5d70c47b1 100644
--- a/main.go
+++ b/main.go
@@ -14,7 +14,6 @@ import (
"github.com/felixge/fgprof"
- "github.com/go-git/go-git/v5/plumbing"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
@@ -122,8 +121,7 @@ func main() {
if remote {
defer os.RemoveAll(repoPath)
}
- sinceHash := plumbing.NewHash(*gitScanSinceCommit)
- err = e.ScanGit(ctx, repoPath, *gitScanBranch, "HEAD", &sinceHash, *gitScanMaxDepth, filter)
+ err = e.ScanGit(ctx, repoPath, *gitScanBranch, *gitScanSinceCommit, *gitScanMaxDepth, filter)
if err != nil {
logrus.WithError(err).Fatal("Failed to scan git.")
}
diff --git a/pkg/engine/git.go b/pkg/engine/git.go
index 7d733741e..0efe4628f 100644
--- a/pkg/engine/git.go
+++ b/pkg/engine/git.go
@@ -15,7 +15,7 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/git"
)
-func (e *Engine) ScanGit(ctx context.Context, repoPath, gitScanBranch, headRef string, sinceHash *plumbing.Hash, maxDepth int, filter *common.Filter) error {
+func (e *Engine) ScanGit(ctx context.Context, repoPath, headRef, baseRef string, maxDepth int, filter *common.Filter) error {
repo, err := gogit.PlainOpenWithOptions(repoPath, &gogit.PlainOpenOptions{DetectDotGit: true})
if err != nil {
return fmt.Errorf("could open repo: %s: %w", repoPath, err)
@@ -26,22 +26,29 @@ func (e *Engine) ScanGit(ctx context.Context, repoPath, gitScanBranch, headRef s
}
var sinceCommit, headCommit *object.Commit
- if !sinceHash.IsZero() {
- sinceCommit, err = repo.CommitObject(*sinceHash)
+ if len(baseRef) > 0 {
+ baseHash := plumbing.NewHash(baseRef)
+ if baseHash.IsZero() {
+ base, err := git.TryAdditionalBaseRefs(repo, baseRef)
+ if err == nil && !base.IsZero() {
+ baseHash = *base
+ }
+ }
+ sinceCommit, err = repo.CommitObject(baseHash)
if err != nil {
- return fmt.Errorf("unable to resolve commit %s: %s", sinceHash.String(), err)
+ return fmt.Errorf("unable to resolve commit %s: %s", baseRef, err)
}
}
- if gitScanBranch != "" {
- headHash, err := git.TryAdditionalBaseRefs(repo, gitScanBranch)
+ if headRef != "" {
+ headHash, err := git.TryAdditionalBaseRefs(repo, headRef)
if err != nil {
- return fmt.Errorf("could not parse revision: %q: %w", gitScanBranch, err)
+ return fmt.Errorf("could not parse revision: %q: %w", headRef, err)
}
headCommit, err = repo.CommitObject(*headHash)
if err != nil {
- return fmt.Errorf("could not find commit: %q: %w", gitScanBranch, err)
+ return fmt.Errorf("could not find commit: %q: %w", headRef, err)
}
logrus.WithFields(logrus.Fields{
diff --git a/pkg/engine/git_test.go b/pkg/engine/git_test.go
index fcc5d6b27..b0d749191 100644
--- a/pkg/engine/git_test.go
+++ b/pkg/engine/git_test.go
@@ -5,7 +5,6 @@ import (
"os"
"testing"
- "github.com/go-git/go-git/v5/plumbing"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/decoders"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
@@ -52,8 +51,7 @@ func TestGitEngine(t *testing.T) {
WithDecoders(decoders.DefaultDecoders()...),
WithDetectors(false, DefaultDetectors()...),
)
- base := plumbing.NewHash(tTest.base)
- e.ScanGit(ctx, path, tTest.branch, "HEAD", &base, tTest.maxDepth, tTest.filter)
+ e.ScanGit(ctx, path, tTest.branch, tTest.base, tTest.maxDepth, tTest.filter)
resultCount := 0
for result := range e.ResultsChan() {
switch meta := result.SourceMetadata.GetData().(type) {