diff options
author | Ryooooooga <eial5q265e5@gmail.com> | 2022-12-26 23:43:08 +0900 |
---|---|---|
committer | Ryooooooga <eial5q265e5@gmail.com> | 2022-12-30 20:01:14 +0900 |
commit | cd9111837e3e331cc0da7e52f8a0be9af266c7f5 (patch) | |
tree | 1b33e6ec8b383d4936f988bd981c3b313e59e4f2 /pkg/commands | |
parent | 41222f07edc9d55dc43a0f5b4b8f865568271ae7 (diff) |
feat: add `GitVersion` struct
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/git.go | 5 | ||||
-rw-r--r-- | pkg/commands/git_commands/common.go | 3 | ||||
-rw-r--r-- | pkg/commands/git_commands/version.go | 67 | ||||
-rw-r--r-- | pkg/commands/git_commands/version_test.go | 47 | ||||
-rw-r--r-- | pkg/commands/git_test.go | 2 |
5 files changed, 123 insertions, 1 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index ee223a6d0..acfd8c934 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -53,6 +53,7 @@ type Loaders struct { func NewGitCommand( cmn *common.Common, + version *git_commands.GitVersion, osCommand *oscommands.OSCommand, gitConfig git_config.IGitConfig, syncMutex *deadlock.Mutex, @@ -73,6 +74,7 @@ func NewGitCommand( return NewGitCommandAux( cmn, + version, osCommand, gitConfig, dotGitDir, @@ -83,6 +85,7 @@ func NewGitCommand( func NewGitCommandAux( cmn *common.Common, + version *git_commands.GitVersion, osCommand *oscommands.OSCommand, gitConfig git_config.IGitConfig, dotGitDir string, @@ -100,7 +103,7 @@ func NewGitCommandAux( fileLoader := git_commands.NewFileLoader(cmn, cmd, configCommands) - gitCommon := git_commands.NewGitCommon(cmn, cmd, osCommand, dotGitDir, repo, configCommands, syncMutex) + gitCommon := git_commands.NewGitCommon(cmn, version, cmd, osCommand, dotGitDir, repo, configCommands, syncMutex) statusCommands := git_commands.NewStatusCommands(gitCommon) flowCommands := git_commands.NewFlowCommands(gitCommon) remoteCommands := git_commands.NewRemoteCommands(gitCommon) diff --git a/pkg/commands/git_commands/common.go b/pkg/commands/git_commands/common.go index 09694110d..f2f187bef 100644 --- a/pkg/commands/git_commands/common.go +++ b/pkg/commands/git_commands/common.go @@ -9,6 +9,7 @@ import ( type GitCommon struct { *common.Common + version *GitVersion cmd oscommands.ICmdObjBuilder os *oscommands.OSCommand dotGitDir string @@ -20,6 +21,7 @@ type GitCommon struct { func NewGitCommon( cmn *common.Common, + version *GitVersion, cmd oscommands.ICmdObjBuilder, osCommand *oscommands.OSCommand, dotGitDir string, @@ -29,6 +31,7 @@ func NewGitCommon( ) *GitCommon { return &GitCommon{ Common: cmn, + version: version, cmd: cmd, os: osCommand, dotGitDir: dotGitDir, diff --git a/pkg/commands/git_commands/version.go b/pkg/commands/git_commands/version.go new file mode 100644 index 000000000..0cf4b485c --- /dev/null +++ b/pkg/commands/git_commands/version.go @@ -0,0 +1,67 @@ +package git_commands + +import ( + "errors" + "regexp" + "strconv" + "strings" + + "github.com/jesseduffield/lazygit/pkg/commands/oscommands" +) + +type GitVersion struct { + Major, Minor, Patch int + Additional string +} + +func GetGitVersion(osCommand *oscommands.OSCommand) (*GitVersion, error) { + versionStr, _, err := osCommand.Cmd.New("git --version").RunWithOutputs() + if err != nil { + return nil, err + } + + version, err := ParseGitVersion(versionStr) + if err != nil { + return nil, err + } + + return version, nil +} + +func ParseGitVersion(versionStr string) (*GitVersion, error) { + // versionStr should be something like: + // git version 2.39.0 + // git version 2.37.1 (Apple Git-137.1) + re := regexp.MustCompile(`[^\d]+(\d+)(\.\d+)?(\.\d+)?(.*)`) + matches := re.FindStringSubmatch(versionStr) + + if len(matches) < 5 { + return nil, errors.New("unexpected git version format: " + versionStr) + } + + v := &GitVersion{} + var err error + + if v.Major, err = strconv.Atoi(matches[1]); err != nil { + return nil, err + } + if len(matches[2]) > 1 { + if v.Minor, err = strconv.Atoi(matches[2][1:]); err != nil { + return nil, err + } + } + if len(matches[3]) > 1 { + if v.Patch, err = strconv.Atoi(matches[3][1:]); err != nil { + return nil, err + } + } + v.Additional = strings.Trim(matches[4], " \r\n") + + return v, nil +} + +func (v *GitVersion) IsOlderThan(major, minor, patch int) bool { + actual := v.Major*1000*1000 + v.Minor*1000 + v.Patch + required := major*1000*1000 + minor*1000 + patch + return actual < required +} diff --git a/pkg/commands/git_commands/version_test.go b/pkg/commands/git_commands/version_test.go new file mode 100644 index 000000000..0c57813ef --- /dev/null +++ b/pkg/commands/git_commands/version_test.go @@ -0,0 +1,47 @@ +package git_commands + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseGitVersion(t *testing.T) { + scenarios := []struct { + input string + expected GitVersion + }{ + { + input: "git version 2.39.0", + expected: GitVersion{Major: 2, Minor: 39, Patch: 0, Additional: ""}, + }, + { + input: "git version 2.37.1 (Apple Git-137.1)", + expected: GitVersion{Major: 2, Minor: 37, Patch: 1, Additional: "(Apple Git-137.1)"}, + }, + { + input: "git version 2.37 (Apple Git-137.1)", + expected: GitVersion{Major: 2, Minor: 37, Patch: 0, Additional: "(Apple Git-137.1)"}, + }, + } + + for _, s := range scenarios { + actual, err := ParseGitVersion(s.input) + + assert.NoError(t, err) + assert.NotNil(t, actual) + assert.Equal(t, s.expected.Major, actual.Major) + assert.Equal(t, s.expected.Minor, actual.Minor) + assert.Equal(t, s.expected.Patch, actual.Patch) + assert.Equal(t, s.expected.Additional, actual.Additional) + } +} + +func TestGitVersionIsOlderThan(t *testing.T) { + assert.False(t, (&GitVersion{2, 0, 0, ""}).IsOlderThan(1, 99, 99)) + assert.False(t, (&GitVersion{2, 0, 0, ""}).IsOlderThan(2, 0, 0)) + assert.False(t, (&GitVersion{2, 1, 0, ""}).IsOlderThan(2, 0, 9)) + + assert.True(t, (&GitVersion{2, 0, 1, ""}).IsOlderThan(2, 1, 0)) + assert.True(t, (&GitVersion{2, 0, 1, ""}).IsOlderThan(3, 0, 0)) +} diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 3531f14ca..c0fab9dd7 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -8,6 +8,7 @@ import ( "github.com/go-errors/errors" gogit "github.com/jesseduffield/go-git/v5" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/utils" @@ -218,6 +219,7 @@ func TestNewGitCommand(t *testing.T) { s.setup() s.test( NewGitCommand(utils.NewDummyCommon(), + &git_commands.GitVersion{}, oscommands.NewDummyOSCommand(), git_config.NewFakeGitConfig(nil), &deadlock.Mutex{}, |