From dea00cb0d64ed392f0a55df59727a65f1510a9de Mon Sep 17 00:00:00 2001 From: steeeve Date: Tue, 7 Jun 2022 18:35:04 -0400 Subject: [PATCH 1/2] Cleanup individual repositories after scanning --- pkg/sources/git/git.go | 56 +++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.go index 28b431cee963..aeb365c07abd 100644 --- a/pkg/sources/git/git.go +++ b/pkg/sources/git/git.go @@ -136,15 +136,19 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) err if len(repoURI) == 0 { continue } - path, repo, err := CloneRepoUsingToken(token, repoURI, user) - defer os.RemoveAll(path) - if err != nil { - return err - } - err = s.git.ScanRepo(ctx, repo, path, NewScanOptions(), chunksChan) - if err != nil { - return err - } + func(repoURI string) { + path, repo, err := CloneRepoUsingToken(token, repoURI, user) + defer os.RemoveAll(path) + if err != nil { + log.WithError(err).Errorf("Unable to clone repo using token: %q", repoURI) + return + } + err = s.git.ScanRepo(ctx, repo, path, NewScanOptions(), chunksChan) + if err != nil { + log.WithError(err).Errorf("Unable to scan repo using token: %q", repoURI) + return + } + }(repoURI) } case *sourcespb.Git_Unauthenticated: for i, repoURI := range s.conn.Repositories { @@ -152,15 +156,19 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) err if len(repoURI) == 0 { continue } - path, repo, err := CloneRepoUsingUnauthenticated(repoURI) - defer os.RemoveAll(path) - if err != nil { - return err - } - err = s.git.ScanRepo(ctx, repo, path, NewScanOptions(), chunksChan) - if err != nil { - return err - } + func(repoURI string) { + path, repo, err := CloneRepoUsingUnauthenticated(repoURI) + defer os.RemoveAll(path) + if err != nil { + log.WithError(err).Errorf("Unable to clone repo unauthenticated: %q", repoURI) + return + } + err = s.git.ScanRepo(ctx, repo, path, NewScanOptions(), chunksChan) + if err != nil { + log.WithError(err).Errorf("Unable to scan repo unauthenticated: %q", repoURI) + return + } + }(repoURI) } default: return errors.New("invalid connection type for git source") @@ -178,14 +186,16 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) err if err != nil { return err } - if strings.HasPrefix(u, filepath.Join(os.TempDir(), "trufflehog")) { - defer os.RemoveAll(u) - } - err = s.git.ScanRepo(ctx, repo, u, NewScanOptions(), chunksChan) + err = func(repoPath string) error { + if strings.HasPrefix(repoPath, filepath.Join(os.TempDir(), "trufflehog")) { + defer os.RemoveAll(repoPath) + } + + return s.git.ScanRepo(ctx, repo, repoPath, NewScanOptions(), chunksChan) + }(u) if err != nil { return err - } } From bcb77ba2dca3c6bb500db2affe3839f933a1186f Mon Sep 17 00:00:00 2001 From: steeeve Date: Wed, 8 Jun 2022 09:04:00 -0400 Subject: [PATCH 2/2] fixup - preserve original behavior --- pkg/sources/git/git.go | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.go index aeb365c07abd..2a176d21a702 100644 --- a/pkg/sources/git/git.go +++ b/pkg/sources/git/git.go @@ -136,19 +136,17 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) err if len(repoURI) == 0 { continue } - func(repoURI string) { + err := func(repoURI string) error { path, repo, err := CloneRepoUsingToken(token, repoURI, user) defer os.RemoveAll(path) if err != nil { - log.WithError(err).Errorf("Unable to clone repo using token: %q", repoURI) - return - } - err = s.git.ScanRepo(ctx, repo, path, NewScanOptions(), chunksChan) - if err != nil { - log.WithError(err).Errorf("Unable to scan repo using token: %q", repoURI) - return + return err } + return s.git.ScanRepo(ctx, repo, path, NewScanOptions(), chunksChan) }(repoURI) + if err != nil { + return err + } } case *sourcespb.Git_Unauthenticated: for i, repoURI := range s.conn.Repositories { @@ -156,19 +154,17 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) err if len(repoURI) == 0 { continue } - func(repoURI string) { + err := func(repoURI string) error { path, repo, err := CloneRepoUsingUnauthenticated(repoURI) defer os.RemoveAll(path) if err != nil { - log.WithError(err).Errorf("Unable to clone repo unauthenticated: %q", repoURI) - return - } - err = s.git.ScanRepo(ctx, repo, path, NewScanOptions(), chunksChan) - if err != nil { - log.WithError(err).Errorf("Unable to scan repo unauthenticated: %q", repoURI) - return + return err } + return s.git.ScanRepo(ctx, repo, path, NewScanOptions(), chunksChan) }(repoURI) + if err != nil { + return err + } } default: return errors.New("invalid connection type for git source")