diff --git a/modules/git/repo_tag.go b/modules/git/repo_tag.go index 63ae09f154..b457b1974e 100644 --- a/modules/git/repo_tag.go +++ b/modules/git/repo_tag.go @@ -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, ¬Found) { + 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. diff --git a/modules/git/repo_tag_gogit.go b/modules/git/repo_tag_gogit.go index cbea0f7966..e42c5ed603 100644 --- a/modules/git/repo_tag_gogit.go +++ b/modules/git/repo_tag_gogit.go @@ -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) diff --git a/modules/got/ref.go b/modules/got/ref.go index 90c0333777..ac0023ad0f 100644 --- a/modules/got/ref.go +++ b/modules/got/ref.go @@ -124,6 +124,27 @@ func ObjectExists(ctx context.Context, path, name string) (bool, error) { return false, err } +// ListRefs returns ": " 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.