repo_tag.go 845 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package database
  2. import (
  3. "fmt"
  4. "github.com/gogs/git-module"
  5. )
  6. type Tag struct {
  7. RepoPath string
  8. Name string
  9. IsProtected bool
  10. Commit *git.Commit
  11. }
  12. func (ta *Tag) GetCommit() (*git.Commit, error) {
  13. gitRepo, err := git.Open(ta.RepoPath)
  14. if err != nil {
  15. return nil, fmt.Errorf("open repository: %v", err)
  16. }
  17. return gitRepo.TagCommit(ta.Name)
  18. }
  19. func GetTagsByPath(path string) ([]*Tag, error) {
  20. gitRepo, err := git.Open(path)
  21. if err != nil {
  22. return nil, fmt.Errorf("open repository: %v", err)
  23. }
  24. names, err := gitRepo.Tags()
  25. if err != nil {
  26. return nil, fmt.Errorf("list tags")
  27. }
  28. tags := make([]*Tag, len(names))
  29. for i := range names {
  30. tags[i] = &Tag{
  31. RepoPath: path,
  32. Name: names[i],
  33. }
  34. }
  35. return tags, nil
  36. }
  37. func (r *Repository) GetTags() ([]*Tag, error) {
  38. return GetTagsByPath(r.RepoPath())
  39. }