You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
3.2 KiB
Go
150 lines
3.2 KiB
Go
8 years ago
|
// 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"
|
||
|
)
|
||
|
|
||
8 years ago
|
// TagPrefix tags prefix path on the repository
|
||
|
const TagPrefix = "refs/tags/"
|
||
8 years ago
|
|
||
|
// IsTagExist returns true if given tag exists in the repository.
|
||
|
func IsTagExist(repoPath, name string) bool {
|
||
8 years ago
|
return IsReferenceExist(repoPath, TagPrefix+name)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// IsTagExist returns true if given tag exists in the repository.
|
||
8 years ago
|
func (repo *Repository) IsTagExist(name string) bool {
|
||
|
return IsTagExist(repo.Path, name)
|
||
|
}
|
||
|
|
||
8 years ago
|
// CreateTag create one tag in the repository
|
||
8 years ago
|
func (repo *Repository) CreateTag(name, revision string) error {
|
||
|
_, err := NewCommand("tag", name, revision).RunInDir(repo.Path)
|
||
|
return err
|
||
|
}
|
||
|
|
||
8 years ago
|
func (repo *Repository) getTag(id SHA1) (*Tag, error) {
|
||
8 years ago
|
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.
|
||
8 years ago
|
if ObjectType(tp) == ObjectCommit {
|
||
8 years ago
|
tag := &Tag{
|
||
|
ID: id,
|
||
|
Object: id,
|
||
8 years ago
|
Type: string(ObjectCommit),
|
||
8 years ago
|
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) {
|
||
6 years ago
|
idStr, err := repo.GetTagCommitID(name)
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
6 years ago
|
id, err := NewIDFromString(idStr)
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
tag, err := repo.getTag(id)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
tag.Name = name
|
||
|
return tag, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
// GetTagInfos returns all tag infos of the repository.
|
||
7 years ago
|
func (repo *Repository) GetTagInfos() ([]*Tag, error) {
|
||
|
// TODO this a slow implementation, makes one git command per tag
|
||
|
stdout, err := NewCommand("tag").RunInDir(repo.Path)
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
7 years ago
|
tagNames := strings.Split(stdout, "\n")
|
||
6 years ago
|
var tags = make([]*Tag, 0, len(tagNames))
|
||
7 years ago
|
for _, tagName := range tagNames {
|
||
|
tagName = strings.TrimSpace(tagName)
|
||
|
if len(tagName) == 0 {
|
||
|
continue
|
||
|
}
|
||
6 years ago
|
|
||
|
tag, err := repo.GetTag(tagName)
|
||
7 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
8 years ago
|
}
|
||
6 years ago
|
tags = append(tags, tag)
|
||
8 years ago
|
}
|
||
|
sortTagsByTime(tags)
|
||
|
return tags, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
// 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
|
||
8 years ago
|
for i := 0; i < len(tags)/2; i++ {
|
||
8 years ago
|
j := len(tags) - i - 1
|
||
|
tags[i], tags[j] = tags[j], tags[i]
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return tags, nil
|
||
|
}
|