Skip to content

Commit

Permalink
smarter checking of git version
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Sep 18, 2020
1 parent 3a66801 commit 307d051
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
29 changes: 22 additions & 7 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -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) {
Expand Down
44 changes: 44 additions & 0 deletions pkg/app/app_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit 307d051

Please sign in to comment.