From d60d9cf9857d1e7a7915b62851b254a5304b6cdc Mon Sep 17 00:00:00 2001 From: Devops <943927833@qq.com> Date: Tue, 14 Dec 2021 20:41:12 +0800 Subject: api: support listing repository tags (#6656) Co-authored-by: zhouzhibo Co-authored-by: Joe Chen --- internal/db/repo_tag.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 internal/db/repo_tag.go (limited to 'internal/db') diff --git a/internal/db/repo_tag.go b/internal/db/repo_tag.go new file mode 100644 index 00000000..cc6721fc --- /dev/null +++ b/internal/db/repo_tag.go @@ -0,0 +1,52 @@ +// Copyright 2021 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 db + +import ( + "fmt" + + "github.com/gogs/git-module" +) + +type Tag struct { + RepoPath string + Name string + + IsProtected bool + Commit *git.Commit +} + +func (ta *Tag) GetCommit() (*git.Commit, error) { + gitRepo, err := git.Open(ta.RepoPath) + if err != nil { + return nil, fmt.Errorf("open repository: %v", err) + } + return gitRepo.TagCommit(ta.Name) +} + +func GetTagsByPath(path string) ([]*Tag, error) { + gitRepo, err := git.Open(path) + if err != nil { + return nil, fmt.Errorf("open repository: %v", err) + } + + names, err := gitRepo.Tags() + if err != nil { + return nil, fmt.Errorf("list tags") + } + + tags := make([]*Tag, len(names)) + for i := range names { + tags[i] = &Tag{ + RepoPath: path, + Name: names[i], + } + } + return tags, nil +} + +func (repo *Repository) GetTags() ([]*Tag, error) { + return GetTagsByPath(repo.RepoPath()) +} -- cgit v1.2.3