repo_tag.go 1007 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2021 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "fmt"
  7. "github.com/gogs/git-module"
  8. )
  9. type Tag struct {
  10. RepoPath string
  11. Name string
  12. IsProtected bool
  13. Commit *git.Commit
  14. }
  15. func (ta *Tag) GetCommit() (*git.Commit, error) {
  16. gitRepo, err := git.Open(ta.RepoPath)
  17. if err != nil {
  18. return nil, fmt.Errorf("open repository: %v", err)
  19. }
  20. return gitRepo.TagCommit(ta.Name)
  21. }
  22. func GetTagsByPath(path string) ([]*Tag, error) {
  23. gitRepo, err := git.Open(path)
  24. if err != nil {
  25. return nil, fmt.Errorf("open repository: %v", err)
  26. }
  27. names, err := gitRepo.Tags()
  28. if err != nil {
  29. return nil, fmt.Errorf("list tags")
  30. }
  31. tags := make([]*Tag, len(names))
  32. for i := range names {
  33. tags[i] = &Tag{
  34. RepoPath: path,
  35. Name: names[i],
  36. }
  37. }
  38. return tags, nil
  39. }
  40. func (repo *Repository) GetTags() ([]*Tag, error) {
  41. return GetTagsByPath(repo.RepoPath())
  42. }