[chore] - Compile regex once (#2176)
* move regex compilation out of the fxn * missed a spot * merge main
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user