mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-21 12:16:54 +02:00
smarter checking of git version
This commit is contained in:
parent
3a668011fa
commit
307d051ec2
@ -8,6 +8,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -138,19 +139,33 @@ func (app *App) validateGitVersion() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return minVersionError
|
return minVersionError
|
||||||
}
|
}
|
||||||
// output should be something like: 'git version 2.23.0'
|
|
||||||
// first number in the string should be greater than 0
|
if isGitVersionValid(output) {
|
||||||
split := strings.Split(output, " ")
|
return nil
|
||||||
gitVersion := split[len(split)-1]
|
|
||||||
majorVersion, err := strconv.Atoi(gitVersion[0:1])
|
|
||||||
if err != nil {
|
|
||||||
return minVersionError
|
|
||||||
}
|
}
|
||||||
if majorVersion < 2 {
|
|
||||||
return minVersionError
|
return minVersionError
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
func isGitVersionValid(versionStr string) bool {
|
||||||
|
// output should be something like: 'git version 2.23.0 (blah)'
|
||||||
|
re := regexp.MustCompile(`[^\d]+([\d\.]+)`)
|
||||||
|
matches := re.FindStringSubmatch(versionStr)
|
||||||
|
|
||||||
|
if len(matches) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
gitVersion := matches[1]
|
||||||
|
majorVersion, err := strconv.Atoi(gitVersion[0:1])
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if majorVersion < 2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) setupRepo() (bool, error) {
|
func (app *App) setupRepo() (bool, error) {
|
||||||
|
44
pkg/app/app_test.go
Normal file
44
pkg/app/app_test.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIsGitVersionValid(t *testing.T) {
|
||||||
|
type scenario struct {
|
||||||
|
versionStr string
|
||||||
|
expectedResult bool
|
||||||
|
}
|
||||||
|
|
||||||
|
scenarios := []scenario{
|
||||||
|
{
|
||||||
|
"",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"git version 1.9.0",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"git version 1.9.0 (Apple Git-128)",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"git version 2.4.0",
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"git version 2.24.3 (Apple Git-128)",
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range scenarios {
|
||||||
|
t.Run(s.versionStr, func(t *testing.T) {
|
||||||
|
result := isGitVersionValid(s.versionStr)
|
||||||
|
assert.Equal(t, result, s.expectedResult)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user