You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_ref.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2018 The Gitea 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 git
  5. import (
  6. "strings"
  7. "github.com/go-git/go-git/v5"
  8. "github.com/go-git/go-git/v5/plumbing"
  9. )
  10. // GetRefs returns all references of the repository.
  11. func (repo *Repository) GetRefs() ([]*Reference, error) {
  12. return repo.GetRefsFiltered("")
  13. }
  14. // GetRefsFiltered returns all references of the repository that matches patterm exactly or starting with.
  15. func (repo *Repository) GetRefsFiltered(pattern string) ([]*Reference, error) {
  16. r, err := git.PlainOpen(repo.Path)
  17. if err != nil {
  18. return nil, err
  19. }
  20. refsIter, err := r.References()
  21. if err != nil {
  22. return nil, err
  23. }
  24. refs := make([]*Reference, 0)
  25. if err = refsIter.ForEach(func(ref *plumbing.Reference) error {
  26. if ref.Name() != plumbing.HEAD && !ref.Name().IsRemote() &&
  27. (pattern == "" || strings.HasPrefix(ref.Name().String(), pattern)) {
  28. refType := string(ObjectCommit)
  29. if ref.Name().IsTag() {
  30. // tags can be of type `commit` (lightweight) or `tag` (annotated)
  31. if tagType, _ := repo.GetTagType(ref.Hash()); err == nil {
  32. refType = tagType
  33. }
  34. }
  35. r := &Reference{
  36. Name: ref.Name().String(),
  37. Object: ref.Hash(),
  38. Type: refType,
  39. repo: repo,
  40. }
  41. refs = append(refs, r)
  42. }
  43. return nil
  44. }); err != nil {
  45. return nil, err
  46. }
  47. return refs, nil
  48. }