2016-11-03 22:16:01 +00:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mcuadros/go-version"
|
|
|
|
)
|
|
|
|
|
2016-12-22 09:30:52 +00:00
|
|
|
// TagPrefix tags prefix path on the repository
|
|
|
|
const TagPrefix = "refs/tags/"
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
|
|
// IsTagExist returns true if given tag exists in the repository.
|
|
|
|
func IsTagExist(repoPath, name string) bool {
|
2016-12-22 09:30:52 +00:00
|
|
|
return IsReferenceExist(repoPath, TagPrefix+name)
|
2016-11-03 22:16:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 09:30:52 +00:00
|
|
|
// IsTagExist returns true if given tag exists in the repository.
|
2016-11-03 22:16:01 +00:00
|
|
|
func (repo *Repository) IsTagExist(name string) bool {
|
|
|
|
return IsTagExist(repo.Path, name)
|
|
|
|
}
|
|
|
|
|
2016-12-22 09:30:52 +00:00
|
|
|
// CreateTag create one tag in the repository
|
2016-11-03 22:16:01 +00:00
|
|
|
func (repo *Repository) CreateTag(name, revision string) error {
|
|
|
|
_, err := NewCommand("tag", name, revision).RunInDir(repo.Path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-22 09:30:52 +00:00
|
|
|
func (repo *Repository) getTag(id SHA1) (*Tag, error) {
|
2016-11-03 22:16:01 +00:00
|
|
|
t, ok := repo.tagCache.Get(id.String())
|
|
|
|
if ok {
|
|
|
|
log("Hit cache: %s", id)
|
|
|
|
return t.(*Tag), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get tag type
|
|
|
|
tp, err := NewCommand("cat-file", "-t", id.String()).RunInDir(repo.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tp = strings.TrimSpace(tp)
|
|
|
|
|
|
|
|
// Tag is a commit.
|
2016-12-22 09:30:52 +00:00
|
|
|
if ObjectType(tp) == ObjectCommit {
|
2016-11-03 22:16:01 +00:00
|
|
|
tag := &Tag{
|
|
|
|
ID: id,
|
|
|
|
Object: id,
|
2016-12-22 09:30:52 +00:00
|
|
|
Type: string(ObjectCommit),
|
2016-11-03 22:16:01 +00:00
|
|
|
repo: repo,
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.tagCache.Set(id.String(), tag)
|
|
|
|
return tag, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tag with message.
|
|
|
|
data, err := NewCommand("cat-file", "-p", id.String()).RunInDirBytes(repo.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tag, err := parseTagData(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tag.ID = id
|
|
|
|
tag.repo = repo
|
|
|
|
|
|
|
|
repo.tagCache.Set(id.String(), tag)
|
|
|
|
return tag, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTag returns a Git tag by given name.
|
|
|
|
func (repo *Repository) GetTag(name string) (*Tag, error) {
|
2019-03-11 03:44:58 +00:00
|
|
|
idStr, err := repo.GetTagCommitID(name)
|
2016-11-03 22:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-11 03:44:58 +00:00
|
|
|
id, err := NewIDFromString(idStr)
|
2016-11-03 22:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tag, err := repo.getTag(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tag.Name = name
|
|
|
|
return tag, nil
|
|
|
|
}
|
|
|
|
|
2017-01-06 01:51:15 +00:00
|
|
|
// GetTagInfos returns all tag infos of the repository.
|
2017-06-06 17:36:48 +00:00
|
|
|
func (repo *Repository) GetTagInfos() ([]*Tag, error) {
|
|
|
|
// TODO this a slow implementation, makes one git command per tag
|
|
|
|
stdout, err := NewCommand("tag").RunInDir(repo.Path)
|
2017-01-06 01:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-06 17:36:48 +00:00
|
|
|
tagNames := strings.Split(stdout, "\n")
|
2019-02-07 12:00:52 +00:00
|
|
|
var tags = make([]*Tag, 0, len(tagNames))
|
2017-06-06 17:36:48 +00:00
|
|
|
for _, tagName := range tagNames {
|
|
|
|
tagName = strings.TrimSpace(tagName)
|
|
|
|
if len(tagName) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-02-07 12:00:52 +00:00
|
|
|
|
|
|
|
tag, err := repo.GetTag(tagName)
|
2017-06-06 17:36:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-01-06 01:51:15 +00:00
|
|
|
}
|
2019-02-07 12:00:52 +00:00
|
|
|
tags = append(tags, tag)
|
2017-01-06 01:51:15 +00:00
|
|
|
}
|
|
|
|
sortTagsByTime(tags)
|
|
|
|
return tags, nil
|
|
|
|
}
|
|
|
|
|
2016-11-03 22:16:01 +00:00
|
|
|
// GetTags returns all tags of the repository.
|
|
|
|
func (repo *Repository) GetTags() ([]string, error) {
|
|
|
|
cmd := NewCommand("tag", "-l")
|
|
|
|
if version.Compare(gitVersion, "2.0.0", ">=") {
|
|
|
|
cmd.AddArguments("--sort=-v:refname")
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout, err := cmd.RunInDir(repo.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := strings.Split(stdout, "\n")
|
|
|
|
tags = tags[:len(tags)-1]
|
|
|
|
|
|
|
|
if version.Compare(gitVersion, "2.0.0", "<") {
|
|
|
|
version.Sort(tags)
|
|
|
|
|
|
|
|
// Reverse order
|
2016-11-15 15:24:08 +00:00
|
|
|
for i := 0; i < len(tags)/2; i++ {
|
2016-11-03 22:16:01 +00:00
|
|
|
j := len(tags) - i - 1
|
|
|
|
tags[i], tags[j] = tags[j], tags[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags, nil
|
|
|
|
}
|