mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-10-20 16:26:08 +00:00
889a8c268c
Strangely #19038 appears to relate to an issue whereby a tag appears to be listed in `git show-ref --tags` but then does not appear when `git show-ref --tags -- short_name` is called. As a solution though I propose to stop the second call as it is unnecessary and only likely to cause problems. I've also noticed that the tags calls are wildly inefficient and aren't using the common cat-files - so these have been added. I've also noticed that the git commit-graph is not being written on mirroring - so I've also added writing this to the migration which should improve mirror rendering somewhat. Fix #19038 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de>
172 lines
4.5 KiB
Go
172 lines
4.5 KiB
Go
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
// Copyright 2019 The Gitea 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 (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
)
|
|
|
|
// TagPrefix tags prefix path on the repository
|
|
const TagPrefix = "refs/tags/"
|
|
|
|
// IsTagExist returns true if given tag exists in the repository.
|
|
func IsTagExist(ctx context.Context, repoPath, name string) bool {
|
|
return IsReferenceExist(ctx, repoPath, TagPrefix+name)
|
|
}
|
|
|
|
// CreateTag create one tag in the repository
|
|
func (repo *Repository) CreateTag(name, revision string) error {
|
|
_, err := NewCommand(repo.Ctx, "tag", "--", name, revision).RunInDir(repo.Path)
|
|
return err
|
|
}
|
|
|
|
// CreateAnnotatedTag create one annotated tag in the repository
|
|
func (repo *Repository) CreateAnnotatedTag(name, message, revision string) error {
|
|
_, err := NewCommand(repo.Ctx, "tag", "-a", "-m", message, "--", name, revision).RunInDir(repo.Path)
|
|
return err
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
stdout, err := NewCommand(repo.Ctx, "show-ref", "--tags", "-d").RunInDir(repo.Path)
|
|
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):]
|
|
// annotated tags show up twice, we should only return if is not the ^{} ref
|
|
if !strings.HasSuffix(name, "^{}") {
|
|
return name, nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
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) {
|
|
stdout, err := NewCommand(repo.Ctx, "show-ref", "--tags", "--", name).RunInDir(repo.Path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
// 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
|
|
}
|
|
}
|
|
return "", ErrNotExist{ID: name}
|
|
}
|
|
|
|
// GetTag returns a Git tag by given name.
|
|
func (repo *Repository) GetTag(name string) (*Tag, error) {
|
|
idStr, err := repo.GetTagID(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
id, err := NewIDFromString(idStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tag, err := repo.getTag(id, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return tag, nil
|
|
}
|
|
|
|
// GetTagWithID returns a Git tag by given name and ID
|
|
func (repo *Repository) GetTagWithID(idStr, name string) (*Tag, error) {
|
|
id, err := NewIDFromString(idStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tag, err := repo.getTag(id, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return tag, nil
|
|
}
|
|
|
|
// GetTagInfos returns all tag infos of the repository.
|
|
func (repo *Repository) GetTagInfos(page, pageSize int) ([]*Tag, int, error) {
|
|
// TODO this a slow implementation, makes one git command per tag
|
|
stdout, err := NewCommand(repo.Ctx, "tag").RunInDir(repo.Path)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
tagNames := strings.Split(strings.TrimRight(stdout, "\n"), "\n")
|
|
tagsTotal := len(tagNames)
|
|
|
|
if page != 0 {
|
|
tagNames = util.PaginateSlice(tagNames, page, pageSize).([]string)
|
|
}
|
|
|
|
tags := make([]*Tag, 0, len(tagNames))
|
|
for _, tagName := range tagNames {
|
|
tagName = strings.TrimSpace(tagName)
|
|
if len(tagName) == 0 {
|
|
continue
|
|
}
|
|
|
|
tag, err := repo.GetTag(tagName)
|
|
if err != nil {
|
|
return nil, tagsTotal, err
|
|
}
|
|
tag.Name = tagName
|
|
tags = append(tags, tag)
|
|
}
|
|
sortTagsByTime(tags)
|
|
return tags, tagsTotal, 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()}
|
|
}
|
|
|
|
// Get tag name
|
|
name, err := repo.GetTagNameBySHA(id.String())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tag, err := repo.getTag(id, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return tag, nil
|
|
}
|