Skip to content

Commit

Permalink
[oc-147] - Add context to all git methods (#901)
Browse files Browse the repository at this point in the history
* Add context to all git methods.

* remove logrus.

* Add ctx.

* Address comments.

* Add error to clone failing.

* Return error.
  • Loading branch information
ahrav committed Nov 3, 2022
1 parent 3a143f0 commit dd141fb
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 88 deletions.
2 changes: 1 addition & 1 deletion hack/snifftest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func main() {
defer sem.Release(1)
defer wgChunkers.Done()
log.Infof("cloning %s", r)
path, repo, err := git.CloneRepoUsingUnauthenticated(r)
path, repo, err := git.CloneRepoUsingUnauthenticated(ctx, r)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func run(state overseer.State) {
var remote bool
switch cmd {
case gitScan.FullCommand():
repoPath, remote, err = git.PrepareRepoSinceCommit(*gitScanURI, *gitScanSinceCommit)
repoPath, remote, err = git.PrepareRepoSinceCommit(ctx, *gitScanURI, *gitScanSinceCommit)
if err != nil || repoPath == "" {
logrus.WithError(err).Fatal("error preparing git repo for scanning")
}
Expand Down Expand Up @@ -282,7 +282,7 @@ func run(state overseer.State) {

switch {
case *jsonLegacy:
output.PrintLegacyJSON(&r)
output.PrintLegacyJSON(ctx, &r)
case *jsonOut:
output.PrintJSON(&r)
default:
Expand Down
6 changes: 4 additions & 2 deletions pkg/engine/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type expResult struct {
}

func TestGitEngine(t *testing.T) {
ctx := context.Background()
repoUrl := "https://github.com/dustin-decker/secretsandstuff.git"
path, _, err := git.PrepareRepo(repoUrl)
path, _, err := git.PrepareRepo(ctx, repoUrl)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -89,8 +90,9 @@ func TestGitEngine(t *testing.T) {
}

func BenchmarkGitEngine(b *testing.B) {
ctx := context.Background()
repoUrl := "https://github.com/dustin-decker/secretsandstuff.git"
path, _, err := git.PrepareRepo(repoUrl)
path, _, err := git.PrepareRepo(ctx, repoUrl)
if err != nil {
b.Error(err)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/output/legacy_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/sirupsen/logrus"

"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/git"
)

func PrintLegacyJSON(r *detectors.ResultWithMetadata) {
func PrintLegacyJSON(ctx context.Context, r *detectors.ResultWithMetadata) {
var repo string
switch r.SourceType {
case sourcespb.SourceType_SOURCE_TYPE_GIT:
Expand All @@ -32,7 +34,7 @@ func PrintLegacyJSON(r *detectors.ResultWithMetadata) {
}

// cloning the repo again here is not great and only works with unauthed repos
repoPath, remote, err := git.PrepareRepo(repo)
repoPath, remote, err := git.PrepareRepo(ctx, repo)
if err != nil || repoPath == "" {
logrus.WithError(err).Fatal("error preparing git repo for scanning")
}
Expand Down
Loading

0 comments on commit dd141fb

Please sign in to comment.