本站源代码
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

56 linhas
1.4KB

  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. "gopkg.in/src-d/go-git.v4"
  8. "gopkg.in/src-d/go-git.v4/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. }
上海开阖软件有限公司 沪ICP备12045867号-1