From c8944825dea4422c084a2317dfb50cb0879d5cef Mon Sep 17 00:00:00 2001 From: Dustin Decker Date: Fri, 26 May 2023 15:23:08 -0700 Subject: [PATCH] Surface missing git as an error during initialization (#1362) --- pkg/sources/git/git.go | 13 +++++++++---- pkg/sources/github/github.go | 5 +++++ pkg/sources/gitlab/gitlab.go | 5 +++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.go index 20d33fe0b2f8..3840e355ad98 100644 --- a/pkg/sources/git/git.go +++ b/pkg/sources/git/git.go @@ -102,6 +102,11 @@ func (s *Source) Init(aCtx context.Context, name string, jobId, sourceId int64, concurrency = runtime.NumCPU() } + err := GitCmdCheck() + if err != nil { + return err + } + s.git = NewGit(s.Type(), s.jobId, s.sourceId, s.name, s.verify, concurrency, func(file, email, commit, timestamp, repository string, line int64) *source_metadatapb.MetaData { return &source_metadatapb.MetaData{ @@ -256,7 +261,7 @@ func gitURLParse(gitURL string) (*url.URL, error) { } func CloneRepo(ctx context.Context, userInfo *url.Userinfo, gitUrl string, args ...string) (string, *git.Repository, error) { - if err := gitCmdCheck(); err != nil { + if err := GitCmdCheck(); err != nil { return "", nil, err } clonePath, err := os.MkdirTemp(os.TempDir(), "trufflehog") @@ -332,8 +337,8 @@ func CloneRepoUsingSSH(ctx context.Context, gitUrl string, args ...string) (stri return CloneRepo(ctx, userInfo, gitUrl, args...) } -// gitCmdCheck checks if git is installed. -func gitCmdCheck() error { +// GitCmdCheck checks if git is installed. +func GitCmdCheck() error { if errors.Is(exec.Command("git").Run(), exec.ErrNotFound) { return fmt.Errorf("'git' command not found in $PATH. Make sure git is installed and included in $PATH") } @@ -341,7 +346,7 @@ func gitCmdCheck() error { } func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string, scanOptions *ScanOptions, chunksChan chan *sources.Chunk) error { - if err := gitCmdCheck(); err != nil { + if err := GitCmdCheck(); err != nil { return err } diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go index 8b7eaee54cb7..9f413b3c824c 100644 --- a/pkg/sources/github/github.go +++ b/pkg/sources/github/github.go @@ -220,6 +220,11 @@ func (s *Source) Init(aCtx context.Context, name string, jobID, sourceID int64, return fmt.Errorf("cannot specify head or base with multiple repositories") } + err = git.GitCmdCheck() + if err != nil { + return err + } + s.publicMap = map[string]source_metadatapb.Visibility{} s.git = git.NewGit(s.Type(), s.JobID(), s.SourceID(), s.name, s.verify, runtime.NumCPU(), diff --git a/pkg/sources/gitlab/gitlab.go b/pkg/sources/gitlab/gitlab.go index 73ddcf0e4a2b..7fb96fdeaebb 100644 --- a/pkg/sources/gitlab/gitlab.go +++ b/pkg/sources/gitlab/gitlab.go @@ -111,6 +111,11 @@ func (s *Source) Init(_ context.Context, name string, jobId, sourceId int64, ver s.url = "https://gitlab.com/" } + err = git.GitCmdCheck() + if err != nil { + return err + } + s.git = git.NewGit(s.Type(), s.JobID(), s.SourceID(), s.name, s.verify, runtime.NumCPU(), func(file, email, commit, timestamp, repository string, line int64) *source_metadatapb.MetaData { return &source_metadatapb.MetaData{