modules/git: migrate tag read lookups to got
giteabot backport / giteabot (push) Skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Canceled after 0s
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Canceled after 0s
release-nightly / nightly-binary (push) Canceled after 0s
release-nightly / nightly-container (push) Canceled after 0s

IsTagExist uses got ref existence in both builds. GetTagID resolves the
tag ref via got, GetTagNameBySHA lists refs/tags/ via got ref -l. GetTagInfos
and annotated-tag body parsing still use git for-each-ref/cat-file.

Verified: GOTMPDIR=/home/lotherk/.cache/gotmp go test ./modules/got ./modules/git -count=1
This commit is contained in:
lotherk committed 2026-11-24 06:59:04 +00:00
1 parent 1e81fa4a14
commit f3298fb282
3 files changed
+55 -26

No files matched your search

+22 -23
View File
@@ -5,6 +5,7 @@
package git
import (
"errors"
"context"
"fmt"
"strconv"
@@ -13,6 +14,8 @@ import (
"gotea.dev/modules/git/foreachref"
"gotea.dev/modules/git/gitcmd"
"gotea.dev/modules/git/gitrepo"
"gotea.dev/modules/got"
"gotea.dev/modules/util"
)
@@ -40,42 +43,38 @@ func (repo *Repository) GetTagNameBySHA(ctx context.Context, sha string) (string
if len(sha) < 5 {
return "", fmt.Errorf("SHA is too short: %s", sha)
}
stdout, _, err := gitcmd.NewCommand("show-ref", "--tags", "-d").WithRepo(repo).RunStdString(ctx)
lines, err := got.ListRefs(ctx, gitrepo.RepoLocalPath(repo), "refs/tags/")
if err != nil {
return "", err
}
tagRefs := strings.SplitSeq(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
}
}
for _, line := range lines {
refName, id, ok := strings.Cut(line, ": ")
if !ok || !strings.HasPrefix(id, sha) {
continue
}
if !strings.HasPrefix(refName, TagPrefix) {
continue
}
name := strings.TrimPrefix(refName, TagPrefix)
if strings.HasSuffix(name, "^{}") {
continue
}
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(ctx context.Context, name string) (string, error) {
stdout, _, err := gitcmd.NewCommand("show-ref", "--tags").AddDashesAndList(name).WithRepo(repo).RunStdString(ctx)
id, err := got.ResolveRefID(ctx, gitrepo.RepoLocalPath(repo), TagPrefix+name)
if err != nil {
var notFound got.ErrRefNotFound
if errors.As(err, &notFound) {
return "", ErrNotExist{ID: name}
}
return "", err
}
// Make sure exact match is used: "v1" != "release/v1"
for line := range strings.SplitSeq(stdout, "\n") {
fields := strings.Fields(line)
if len(fields) == 2 && fields[1] == "refs/tags/"+name {
return fields[0], nil
}
}
return "", ErrNotExist{ID: name}
return id, nil
}
// GetTag returns a Git tag by given name.
+12 -3
View File
@@ -9,15 +9,24 @@ package git
import (
"context"
"gotea.dev/modules/git/gitrepo"
"gotea.dev/modules/got"
"gotea.dev/modules/log"
"github.com/go-git/go-git/v5/plumbing"
)
// IsTagExist returns true if given tag exists in the repository.
func (repo *Repository) IsTagExist(_ context.Context, name string) bool {
_, err := repo.gogitRepo.Reference(plumbing.ReferenceName(TagPrefix+name), true)
return err == nil
func (repo *Repository) IsTagExist(ctx context.Context, name string) bool {
if repo == nil || name == "" {
return false
}
exists, err := got.RefExists(ctx, gitrepo.RepoLocalPath(repo), TagPrefix+name)
if err != nil {
log.Error("IsTagExist %s: %v", name, err)
return false
}
return exists
}
// GetTagType gets the type of the tag, either commit (simple) or tag (annotated)
+21
View File
@@ -124,6 +124,27 @@ func ObjectExists(ctx context.Context, path, name string) (bool, error) {
return false, err
}
// ListRefs returns "<ref>: <object>" lines for refs matching prefix. An empty
// prefix lists all refs.
func ListRefs(ctx context.Context, path, prefix string) ([]string, error) {
args := []string{"ref", "-l", "-r", path}
if prefix != "" {
args = append(args, prefix)
}
out, _, err := run(ctx, args...)
if err != nil {
return nil, err
}
var lines []string
for _, line := range strings.Split(out, "\n") {
line = strings.TrimSpace(line)
if line != "" {
lines = append(lines, line)
}
}
return lines, nil
}
// ResolveRefID returns the object ID a reference points at. It accepts full
// ref names (refs/...) and HEAD. For annotated tags this is the tag object ID,
// matching git show-ref / ref hash semantics.