Skip to content

Commit

Permalink
Surface missing git as an error during initialization (#1362)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin-decker committed May 26, 2023
1 parent cbfbf53 commit c894482
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/sources/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -332,16 +337,16 @@ 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")
}
return nil
}

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
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/sources/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
5 changes: 5 additions & 0 deletions pkg/sources/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit c894482

Please sign in to comment.