forked from jesseduffield/lazygit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41222f0
commit cd91118
Showing
9 changed files
with
144 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters