Check that git meets version requirements (#1373)

This commit is contained in:
Dustin Decker 2023-06-01 09:41:06 -07:00 committed by GitHub
parent 8b7c50825e
commit 183037ab34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 8 deletions

View file

@ -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)
}
}

View file

@ -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