Skip to content

Find, verify, and analyze leaked credentials

[chore] - Compile regex once (#2176)

ahravb75991850a86
diff --git a/pkg/sources/git/cmd_check.go b/pkg/sources/git/cmd_check.go
index 8cff00103..67df43563 100644
--- a/pkg/sources/git/cmd_check.go
+++ b/pkg/sources/git/cmd_check.go
@@ -10,8 +10,11 @@ import (
"github.com/go-errors/errors"
)
-// GitCmdCheck checks if git is installed and meets 2.20.0<=x<3.0.0 version requirements.
-func GitCmdCheck() error {
+// Extract the version string using a regex to find the version numbers
+var regex = regexp.MustCompile(`\d+\.\d+\.\d+`)
+
+// CmdCheck checks if git is installed and meets 2.20.0<=x<3.0.0 version requirements.
+func CmdCheck() 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")
}
@@ -22,9 +25,7 @@ func GitCmdCheck() error {
return fmt.Errorf("failed to check git version: %w", err)
}
- // Extract the version string using a regex to find the version numbers
- r := regexp.MustCompile(`\d+\.\d+\.\d+`)
- versionStr := r.FindString(string(out))
+ versionStr := regex.FindString(string(out))
versionParts := strings.Split(versionStr, ".")
// Parse version numbers
@@ -34,7 +35,6 @@ func GitCmdCheck() error {
// Compare with version 2.20.0<=x<3.0.0
if major == 2 && minor >= 20 {
return nil
- } else {
- return fmt.Errorf("git version is %s, but must be greater than or equal to 2.20.0, and less than 3.0.0", versionStr)
}
+ return fmt.Errorf("git version is %s, but must be greater than or equal to 2.20.0, and less than 3.0.0", versionStr)
}
diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.go
index 502f82b17..9b0271b55 100644
--- a/pkg/sources/git/git.go
+++ b/pkg/sources/git/git.go
@@ -157,7 +157,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobId sources.JobID, so
concurrency = runtime.NumCPU()
}
- if err = GitCmdCheck(); err != nil {
+ if err = CmdCheck(); err != nil {
return err
}
@@ -410,7 +410,7 @@ func executeClone(ctx context.Context, params cloneParams) (*git.Repository, err
//
// Pinging using other authentication methods is only unimplemented because there's been no pressing need for it yet.
func PingRepoUsingToken(ctx context.Context, token, gitUrl, user string) error {
- if err := GitCmdCheck(); err != nil {
+ if err := CmdCheck(); err != nil {
return err
}
lsUrl, err := GitURLParse(gitUrl)
diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go
index 898b3f04c..5a52ef0a8 100644
--- a/pkg/sources/github/github.go
+++ b/pkg/sources/github/github.go
@@ -253,7 +253,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobID sources.JobID, so
return fmt.Errorf("cannot specify head or base with multiple repositories")
}
- err = git.GitCmdCheck()
+ err = git.CmdCheck()
if err != nil {
return err
}
diff --git a/pkg/sources/gitlab/gitlab.go b/pkg/sources/gitlab/gitlab.go
index cbee1e8bc..7851e7c6f 100644
--- a/pkg/sources/gitlab/gitlab.go
+++ b/pkg/sources/gitlab/gitlab.go
@@ -116,7 +116,7 @@ func (s *Source) Init(_ context.Context, name string, jobId sources.JobID, sourc
s.url = "https://gitlab.com/"
}
- err = git.GitCmdCheck()
+ err = git.CmdCheck()
if err != nil {
return err
}