mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-16 23:12:43 +00:00
Work on #476
This commit is contained in:
parent
c1ceec45da
commit
0d9c41be7d
7 changed files with 78 additions and 17 deletions
|
@ -11,25 +11,24 @@ import (
|
|||
"github.com/Unknwon/com"
|
||||
)
|
||||
|
||||
var (
|
||||
// Cached Git version.
|
||||
gitVer *Version
|
||||
)
|
||||
|
||||
// Version represents version of Git.
|
||||
type Version struct {
|
||||
Major, Minor, Patch int
|
||||
}
|
||||
|
||||
// GetVersion returns current Git version installed.
|
||||
func GetVersion() (Version, error) {
|
||||
stdout, stderr, err := com.ExecCmd("git", "version")
|
||||
if err != nil {
|
||||
return Version{}, errors.New(stderr)
|
||||
}
|
||||
|
||||
infos := strings.Split(stdout, " ")
|
||||
func ParseVersion(verStr string) (*Version, error) {
|
||||
infos := strings.Split(verStr, ".")
|
||||
if len(infos) < 3 {
|
||||
return Version{}, errors.New("not enough output")
|
||||
return nil, errors.New("incorrect version input")
|
||||
}
|
||||
|
||||
v := Version{}
|
||||
for i, s := range strings.Split(strings.TrimSpace(infos[2]), ".") {
|
||||
v := &Version{}
|
||||
for i, s := range infos {
|
||||
switch i {
|
||||
case 0:
|
||||
v.Major, _ = com.StrTo(s).Int()
|
||||
|
@ -41,3 +40,52 @@ func GetVersion() (Version, error) {
|
|||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func MustParseVersion(verStr string) *Version {
|
||||
v, _ := ParseVersion(verStr)
|
||||
return v
|
||||
}
|
||||
|
||||
// Compare compares two versions,
|
||||
// it returns 1 if original is greater, 1 if original is smaller, 0 if equal.
|
||||
func (v *Version) Compare(that *Version) int {
|
||||
if v.Major > that.Major {
|
||||
return 1
|
||||
} else if v.Major < that.Major {
|
||||
return -1
|
||||
}
|
||||
|
||||
if v.Minor > that.Minor {
|
||||
return 1
|
||||
} else if v.Minor < that.Minor {
|
||||
return -1
|
||||
}
|
||||
|
||||
if v.Patch > that.Patch {
|
||||
return 1
|
||||
} else if v.Patch < that.Patch {
|
||||
return -1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetVersion returns current Git version installed.
|
||||
func GetVersion() (*Version, error) {
|
||||
if gitVer != nil {
|
||||
return gitVer, nil
|
||||
}
|
||||
|
||||
stdout, stderr, err := com.ExecCmd("git", "version")
|
||||
if err != nil {
|
||||
return nil, errors.New(stderr)
|
||||
}
|
||||
|
||||
infos := strings.Split(stdout, " ")
|
||||
if len(infos) < 3 {
|
||||
return nil, errors.New("not enough output")
|
||||
}
|
||||
|
||||
gitVer, err = ParseVersion(infos[2])
|
||||
return gitVer, err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue