diff --git a/pkg/sources/git/cmd_check.go b/pkg/sources/git/cmd_check.go new file mode 100644 index 000000000..0c61df2ab --- /dev/null +++ b/pkg/sources/git/cmd_check.go @@ -0,0 +1,41 @@ +package git + +import ( + "fmt" + "os/exec" + "regexp" + "strconv" + "strings" + + "github.com/go-errors/errors" +) + +// GitCmdCheck checks if git is installed and meets 2.36.6<=x<3.0.0 version requirements. +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") + } + + // Check the version is greater than or equal to 2.36.6 + out, err := exec.Command("git", "--version").Output() + if err != nil { + 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)) + versionParts := strings.Split(versionStr, ".") + + // Parse version numbers + major, _ := strconv.Atoi(versionParts[0]) + minor, _ := strconv.Atoi(versionParts[1]) + patch, _ := strconv.Atoi(versionParts[2]) + + // Compare with version 2.36.6<=x<3.0.0 + if (major == 2 && minor > 36) || (major == 2 && minor == 36 && patch >= 6) { + return nil + } else { + return fmt.Errorf("git version is %s, but must be greater than or equal to 2.36.6, and less than 3.0.0", versionStr) + } +} diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.go index 3840e355a..4358819a5 100644 --- a/pkg/sources/git/git.go +++ b/pkg/sources/git/git.go @@ -337,14 +337,6 @@ 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 { - 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 { return err