]> source.dussan.org Git - gitea.git/commitdiff
If git >= 2.0, sort tags in descending order by version number
authorLinquize <linquize@yahoo.com.hk>
Fri, 3 Oct 2014 16:25:54 +0000 (00:25 +0800)
committerLinquize <linquize@yahoo.com.hk>
Sun, 5 Oct 2014 03:59:54 +0000 (11:59 +0800)
modules/git/repo_tag.go
modules/git/version.go

index 77ae3db0075ed94f86f93378dd43030b2bd941a7..ed994d48ae250db07dc17feee1c0f50d9001eade 100644 (file)
@@ -22,6 +22,9 @@ func (repo *Repository) IsTagExist(tagName string) bool {
 
 // GetTags returns all tags of given repository.
 func (repo *Repository) GetTags() ([]string, error) {
+       if gitVer.AtLeast(MustParseVersion("2.0.0")) {
+               return repo.getTagsReversed()
+       }
        stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "tag", "-l")
        if err != nil {
                return nil, errors.New(stderr)
@@ -30,6 +33,15 @@ func (repo *Repository) GetTags() ([]string, error) {
        return tags[:len(tags)-1], nil
 }
 
+func (repo *Repository) getTagsReversed() ([]string, error) {
+       stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "tag", "-l", "--sort=-v:refname")
+       if err != nil {
+               return nil, errors.New(stderr)
+       }
+       tags := strings.Split(stdout, "\n")
+       return tags[:len(tags)-1], nil
+}
+
 func (repo *Repository) CreateTag(tagName, idStr string) error {
        _, stderr, err := com.ExecCmdDir(repo.Path, "git", "tag", tagName, idStr)
        if err != nil {
index 9908d11e2013989f4e90083cfb675906999119dd..b535521ec447205b660a4e39110105f5242dd8e1 100644 (file)
@@ -74,6 +74,10 @@ func (v *Version) LessThan(that *Version) bool {
        return v.Compare(that) < 0
 }
 
+func (v *Version) AtLeast(that *Version) bool {
+       return v.Compare(that) >= 0
+}
+
 // GetVersion returns current Git version installed.
 func GetVersion() (*Version, error) {
        if gitVer != nil {