summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2020-09-18 21:00:03 +1000
committerJesse Duffield <jessedduffield@gmail.com>2020-09-18 21:02:27 +1000
commit307d051ec2ec03a2fcd347ce94f205aa2dca75fc (patch)
treeefc09104c506582dc7dfae944259db1a17779779
parent3a668011fa61c243adf641ca9e0a7e151a30bf64 (diff)
smarter checking of git versionv0.22.9v0.22.8
-rw-r--r--pkg/app/app.go29
-rw-r--r--pkg/app/app_test.go44
2 files changed, 66 insertions, 7 deletions
diff --git a/pkg/app/app.go b/pkg/app/app.go
index 617e2dd0e..e9ba81f59 100644
--- a/pkg/app/app.go
+++ b/pkg/app/app.go
@@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
+ "regexp"
"strconv"
"strings"
@@ -138,19 +139,33 @@ func (app *App) validateGitVersion() error {
if err != nil {
return minVersionError
}
- // output should be something like: 'git version 2.23.0'
- // first number in the string should be greater than 0
- split := strings.Split(output, " ")
- gitVersion := split[len(split)-1]
+
+ if isGitVersionValid(output) {
+ return nil
+ }
+
+ return minVersionError
+}
+
+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 minVersionError
+ return false
}
if majorVersion < 2 {
- return minVersionError
+ return false
}
- return nil
+ return true
}
func (app *App) setupRepo() (bool, error) {
diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go
new file mode 100644
index 000000000..1ec46f0f7
--- /dev/null
+++ b/pkg/app/app_test.go
@@ -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)
+ })
+ }
+}