From 0d9c41be7d7d4ae1d2a28931be5565c8f6d3f792 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 16 Sep 2014 10:10:33 -0400 Subject: Work on #476 --- modules/git/version.go | 70 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 11 deletions(-) (limited to 'modules/git/version.go') diff --git a/modules/git/version.go b/modules/git/version.go index 683e859b..653503c0 100644 --- a/modules/git/version.go +++ b/modules/git/version.go @@ -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 +} -- cgit v1.2.3 From 62f21ff3ed1b85a1d3a1eab73da354e4f6e8794a Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 16 Sep 2014 11:29:53 -0400 Subject: Work on #476 --- models/repo.go | 2 +- modules/git/repo_commit.go | 2 +- modules/git/version.go | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) (limited to 'modules/git/version.go') diff --git a/models/repo.go b/models/repo.go index a8e53dbc..2e162704 100644 --- a/models/repo.go +++ b/models/repo.go @@ -100,7 +100,7 @@ func NewRepoContext() { if err != nil { log.Fatal(4, "Fail to parse required Git version: %v", err) } - if ver.Compare(reqVer) == -1 { + if ver.LessThan(reqVer) { log.Fatal(4, "Gogs requires Git version greater or equal to 1.7.1") } diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index c9258927..cd0181c4 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -137,7 +137,7 @@ func (repo *Repository) GetCommit(commitId string) (*Commit, error) { } func (repo *Repository) commitsCount(id sha1) (int, error) { - if gitVer.Compare(MustParseVersion("1.8.0")) == -1 { + if gitVer.LessThan(MustParseVersion("1.8.0")) { stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", "--pretty=format:''", id.String()) if err != nil { return 0, errors.New(string(stderr)) diff --git a/modules/git/version.go b/modules/git/version.go index 653503c0..546397aa 100644 --- a/modules/git/version.go +++ b/modules/git/version.go @@ -47,7 +47,7 @@ func MustParseVersion(verStr string) *Version { } // Compare compares two versions, -// it returns 1 if original is greater, 1 if original is smaller, 0 if equal. +// 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 @@ -70,6 +70,10 @@ func (v *Version) Compare(that *Version) int { return 0 } +func (v *Version) LessThan(that *Version) bool { + return v.Compare(that) < 0 +} + // GetVersion returns current Git version installed. func GetVersion() (*Version, error) { if gitVer != nil { -- cgit v1.2.3 From ae3639868ee2d720dc50fc81be712072445ab6ed Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 16 Sep 2014 20:58:06 -0400 Subject: Quick fix on #476 --- modules/git/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/git/version.go') diff --git a/modules/git/version.go b/modules/git/version.go index 546397aa..9908d11e 100644 --- a/modules/git/version.go +++ b/modules/git/version.go @@ -35,7 +35,7 @@ func ParseVersion(verStr string) (*Version, error) { case 1: v.Minor, _ = com.StrTo(s).Int() case 2: - v.Patch, _ = com.StrTo(s).Int() + v.Patch, _ = com.StrTo(strings.TrimSpace(s)).Int() } } return v, nil -- cgit v1.2.3