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_gogit.go 1.3KB

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