mirror of
https://github.com/jesseduffield/lazygit.git
synced 2024-12-12 11:15:00 +02:00
63dc07fded
By constructing an arg vector manually, we no longer need to quote arguments Mandate that args must be passed when building a command Now you need to provide an args array when building a command. There are a handful of places where we need to deal with a string, such as with user-defined custom commands, and for those we now require that at the callsite they use str.ToArgv to do that. I don't want to provide a method out of the box for it because I want to discourage its use. For some reason we were invoking a command through a shell when amending a commit, and I don't believe we needed to do that as there was nothing user- supplied about the command. So I've switched to using a regular command out- side the shell there
72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
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(NewGitCmd("--version").ToArgv()).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
|
|
}
|
|
|
|
func (v *GitVersion) IsOlderThanVersion(version *GitVersion) bool {
|
|
return v.IsOlderThan(version.Major, version.Minor, version.Patch)
|
|
}
|