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

This commit is contained in:
Bill Rich 2022-02-28 20:25:24 -08:00 committed by Dustin Decker
parent 4948ae8617
commit 9f378b8cb3
3 changed files with 17 additions and 14 deletions

View file

@ -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.")
}

View file

@ -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{

View file

@ -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) {