[chore] - Compile regex once (#2176)

* move regex compilation out of the fxn

* missed a spot

* merge main
This commit is contained in:
ahrav
2023-12-07 07:26:27 -08:00
committed by GitHub
parent f772fd8b44
commit b75991850a
4 changed files with 11 additions and 11 deletions
+7 -7
View File
@@ -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)
}
+2 -2
View File
@@ -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)
+1 -1
View File
@@ -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
}
+1 -1
View File
@@ -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
}