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

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