2016-11-03 22:16:01 +00:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 12:17:27 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-11-03 22:16:01 +00:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-11-30 20:06:32 +00:00
|
|
|
"context"
|
2019-06-08 14:31:11 +00:00
|
|
|
"fmt"
|
2016-11-03 22:16:01 +00:00
|
|
|
"strings"
|
2021-06-25 16:54:08 +00:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-08-12 12:43:08 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2016-11-03 22:16:01 +00:00
|
|
|
)
|
|
|
|
|
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.
|
2021-11-30 20:06:32 +00:00
|
|
|
func IsTagExist(ctx context.Context, repoPath, name string) bool {
|
|
|
|
return IsReferenceExist(ctx, repoPath, TagPrefix+name)
|
2016-11-03 22:16:01 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-11-30 20:06:32 +00:00
|
|
|
_, err := NewCommandContext(repo.Ctx, "tag", "--", name, revision).RunInDir(repo.Path)
|
2016-11-03 22:16:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:31:11 +00:00
|
|
|
// CreateAnnotatedTag create one annotated tag in the repository
|
|
|
|
func (repo *Repository) CreateAnnotatedTag(name, message, revision string) error {
|
2021-11-30 20:06:32 +00:00
|
|
|
_, err := NewCommandContext(repo.Ctx, "tag", "-a", "-m", message, "--", name, revision).RunInDir(repo.Path)
|
2019-06-08 14:31:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-19 22:30:39 +00:00
|
|
|
func (repo *Repository) getTag(tagID SHA1, name string) (*Tag, error) {
|
|
|
|
t, ok := repo.tagCache.Get(tagID.String())
|
2016-11-03 22:16:01 +00:00
|
|
|
if ok {
|
2021-06-25 16:54:08 +00:00
|
|
|
log.Debug("Hit cache: %s", tagID)
|
2019-06-08 14:31:11 +00:00
|
|
|
tagClone := *t.(*Tag)
|
2020-12-19 22:30:39 +00:00
|
|
|
tagClone.Name = name // This is necessary because lightweight tags may have same id
|
2019-06-08 14:31:11 +00:00
|
|
|
return &tagClone, nil
|
2016-11-03 22:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 22:30:39 +00:00
|
|
|
tp, err := repo.GetTagType(tagID)
|
2016-11-03 22:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:31:11 +00:00
|
|
|
// Get the commit ID and tag ID (may be different for annotated tag) for the returned tag object
|
|
|
|
commitIDStr, err := repo.GetTagCommitID(name)
|
|
|
|
if err != nil {
|
|
|
|
// every tag should have a commit ID so return all errors
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commitID, err := NewIDFromString(commitIDStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If type is "commit, the tag is a lightweight tag
|
2016-12-22 09:30:52 +00:00
|
|
|
if ObjectType(tp) == ObjectCommit {
|
2020-12-19 22:30:39 +00:00
|
|
|
commit, err := repo.GetCommit(commitIDStr)
|
2019-06-08 14:31:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
tag := &Tag{
|
2019-06-08 14:31:11 +00:00
|
|
|
Name: name,
|
|
|
|
ID: tagID,
|
|
|
|
Object: commitID,
|
2020-12-19 22:30:39 +00:00
|
|
|
Type: tp,
|
2019-06-08 14:31:11 +00:00
|
|
|
Tagger: commit.Committer,
|
|
|
|
Message: commit.Message(),
|
2016-11-03 22:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 22:30:39 +00:00
|
|
|
repo.tagCache.Set(tagID.String(), tag)
|
2016-11-03 22:16:01 +00:00
|
|
|
return tag, nil
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:31:11 +00:00
|
|
|
// The tag is an annotated tag with a message.
|
2021-11-30 20:06:32 +00:00
|
|
|
data, err := NewCommandContext(repo.Ctx, "cat-file", "-p", tagID.String()).RunInDirBytes(repo.Path)
|
2016-11-03 22:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tag, err := parseTagData(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:31:11 +00:00
|
|
|
tag.Name = name
|
2020-12-19 22:30:39 +00:00
|
|
|
tag.ID = tagID
|
2019-06-08 14:31:11 +00:00
|
|
|
tag.Type = tp
|
2016-11-03 22:16:01 +00:00
|
|
|
|
2020-12-19 22:30:39 +00:00
|
|
|
repo.tagCache.Set(tagID.String(), tag)
|
2016-11-03 22:16:01 +00:00
|
|
|
return tag, nil
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:31:11 +00:00
|
|
|
// GetTagNameBySHA returns the name of a tag from its tag object SHA or commit SHA
|
|
|
|
func (repo *Repository) GetTagNameBySHA(sha string) (string, error) {
|
|
|
|
if len(sha) < 5 {
|
|
|
|
return "", fmt.Errorf("SHA is too short: %s", sha)
|
|
|
|
}
|
|
|
|
|
2021-11-30 20:06:32 +00:00
|
|
|
stdout, err := NewCommandContext(repo.Ctx, "show-ref", "--tags", "-d").RunInDir(repo.Path)
|
2019-06-08 14:31:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tagRefs := strings.Split(stdout, "\n")
|
|
|
|
for _, tagRef := range tagRefs {
|
|
|
|
if len(strings.TrimSpace(tagRef)) > 0 {
|
|
|
|
fields := strings.Fields(tagRef)
|
|
|
|
if strings.HasPrefix(fields[0], sha) && strings.HasPrefix(fields[1], TagPrefix) {
|
|
|
|
name := fields[1][len(TagPrefix):]
|
2019-06-29 10:44:17 +00:00
|
|
|
// annotated tags show up twice, we should only return if is not the ^{} ref
|
|
|
|
if !strings.HasSuffix(name, "^{}") {
|
|
|
|
return name, nil
|
|
|
|
}
|
2019-06-08 14:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", ErrNotExist{ID: sha}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTagID returns the object ID for a tag (annotated tags have both an object SHA AND a commit SHA)
|
|
|
|
func (repo *Repository) GetTagID(name string) (string, error) {
|
2021-11-30 20:06:32 +00:00
|
|
|
stdout, err := NewCommandContext(repo.Ctx, "show-ref", "--tags", "--", name).RunInDir(repo.Path)
|
2019-06-08 14:31:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-09-04 05:48:17 +00:00
|
|
|
// Make sure exact match is used: "v1" != "release/v1"
|
|
|
|
for _, line := range strings.Split(stdout, "\n") {
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
if len(fields) == 2 && fields[1] == "refs/tags/"+name {
|
|
|
|
return fields[0], nil
|
|
|
|
}
|
2019-06-08 14:31:11 +00:00
|
|
|
}
|
2019-09-04 05:48:17 +00:00
|
|
|
return "", ErrNotExist{ID: name}
|
2019-06-08 14:31:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 22:16:01 +00:00
|
|
|
// GetTag returns a Git tag by given name.
|
|
|
|
func (repo *Repository) GetTag(name string) (*Tag, error) {
|
2019-06-08 14:31:11 +00:00
|
|
|
idStr, err := repo.GetTagID(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
|
|
|
|
}
|
|
|
|
|
2020-12-19 22:30:39 +00:00
|
|
|
tag, err := repo.getTag(id, name)
|
2016-11-03 22:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tag, nil
|
|
|
|
}
|
|
|
|
|
2017-01-06 01:51:15 +00:00
|
|
|
// GetTagInfos returns all tag infos of the repository.
|
2021-08-12 12:43:08 +00:00
|
|
|
func (repo *Repository) GetTagInfos(page, pageSize int) ([]*Tag, int, error) {
|
2017-06-06 17:36:48 +00:00
|
|
|
// TODO this a slow implementation, makes one git command per tag
|
2021-11-30 20:06:32 +00:00
|
|
|
stdout, err := NewCommandContext(repo.Ctx, "tag").RunInDir(repo.Path)
|
2017-01-06 01:51:15 +00:00
|
|
|
if err != nil {
|
2021-08-12 12:43:08 +00:00
|
|
|
return nil, 0, err
|
2017-01-06 01:51:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:31:11 +00:00
|
|
|
tagNames := strings.Split(strings.TrimRight(stdout, "\n"), "\n")
|
2021-08-12 12:43:08 +00:00
|
|
|
tagsTotal := len(tagNames)
|
2020-02-25 19:07:07 +00:00
|
|
|
|
|
|
|
if page != 0 {
|
2021-08-12 12:43:08 +00:00
|
|
|
tagNames = util.PaginateSlice(tagNames, page, pageSize).([]string)
|
2020-02-25 19:07:07 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 17:46:10 +00:00
|
|
|
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 {
|
2021-08-12 12:43:08 +00:00
|
|
|
return nil, tagsTotal, err
|
2017-01-06 01:51:15 +00:00
|
|
|
}
|
2019-06-08 14:31:11 +00:00
|
|
|
tag.Name = tagName
|
2019-02-07 12:00:52 +00:00
|
|
|
tags = append(tags, tag)
|
2017-01-06 01:51:15 +00:00
|
|
|
}
|
|
|
|
sortTagsByTime(tags)
|
2021-08-12 12:43:08 +00:00
|
|
|
return tags, tagsTotal, nil
|
2017-01-06 01:51:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:31:11 +00:00
|
|
|
// GetTagType gets the type of the tag, either commit (simple) or tag (annotated)
|
|
|
|
func (repo *Repository) GetTagType(id SHA1) (string, error) {
|
|
|
|
// Get tag type
|
2021-11-30 20:06:32 +00:00
|
|
|
stdout, err := NewCommandContext(repo.Ctx, "cat-file", "-t", id.String()).RunInDir(repo.Path)
|
2019-06-08 14:31:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(stdout) == 0 {
|
|
|
|
return "", ErrNotExist{ID: id.String()}
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(stdout), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAnnotatedTag returns a Git tag by its SHA, must be an annotated tag
|
|
|
|
func (repo *Repository) GetAnnotatedTag(sha string) (*Tag, error) {
|
|
|
|
id, err := NewIDFromString(sha)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tag type must be "tag" (annotated) and not a "commit" (lightweight) tag
|
|
|
|
if tagType, err := repo.GetTagType(id); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if ObjectType(tagType) != ObjectTag {
|
|
|
|
// not an annotated tag
|
|
|
|
return nil, ErrNotExist{ID: id.String()}
|
|
|
|
}
|
|
|
|
|
2020-12-19 22:30:39 +00:00
|
|
|
// Get tag name
|
|
|
|
name, err := repo.GetTagNameBySHA(id.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tag, err := repo.getTag(id, name)
|
2019-06-08 14:31:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tag, nil
|
|
|
|
}
|