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_branch_gogit.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. //go:build gogit
  6. package git
  7. import (
  8. "context"
  9. "strings"
  10. "github.com/go-git/go-git/v5/plumbing"
  11. "github.com/go-git/go-git/v5/plumbing/storer"
  12. )
  13. // IsObjectExist returns true if given reference exists in the repository.
  14. func (repo *Repository) IsObjectExist(name string) bool {
  15. if name == "" {
  16. return false
  17. }
  18. _, err := repo.gogitRepo.ResolveRevision(plumbing.Revision(name))
  19. return err == nil
  20. }
  21. // IsReferenceExist returns true if given reference exists in the repository.
  22. func (repo *Repository) IsReferenceExist(name string) bool {
  23. if name == "" {
  24. return false
  25. }
  26. reference, err := repo.gogitRepo.Reference(plumbing.ReferenceName(name), true)
  27. if err != nil {
  28. return false
  29. }
  30. return reference.Type() != plumbing.InvalidReference
  31. }
  32. // IsBranchExist returns true if given branch exists in current repository.
  33. func (repo *Repository) IsBranchExist(name string) bool {
  34. if name == "" {
  35. return false
  36. }
  37. reference, err := repo.gogitRepo.Reference(plumbing.ReferenceName(BranchPrefix+name), true)
  38. if err != nil {
  39. return false
  40. }
  41. return reference.Type() != plumbing.InvalidReference
  42. }
  43. // GetBranches returns branches from the repository, skipping skip initial branches and
  44. // returning at most limit branches, or all branches if limit is 0.
  45. func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) {
  46. var branchNames []string
  47. branches, err := repo.gogitRepo.Branches()
  48. if err != nil {
  49. return nil, 0, err
  50. }
  51. i := 0
  52. count := 0
  53. _ = branches.ForEach(func(branch *plumbing.Reference) error {
  54. count++
  55. if i < skip {
  56. i++
  57. return nil
  58. } else if limit != 0 && count > skip+limit {
  59. return nil
  60. }
  61. branchNames = append(branchNames, strings.TrimPrefix(branch.Name().String(), BranchPrefix))
  62. return nil
  63. })
  64. // TODO: Sort?
  65. return branchNames, count, nil
  66. }
  67. // WalkReferences walks all the references from the repository
  68. // refType should be empty, ObjectTag or ObjectBranch. All other values are equivalent to empty.
  69. func WalkReferences(ctx context.Context, repoPath string, walkfn func(sha1, refname string) error) (int, error) {
  70. repo := RepositoryFromContext(ctx, repoPath)
  71. if repo == nil {
  72. var err error
  73. repo, err = OpenRepository(ctx, repoPath)
  74. if err != nil {
  75. return 0, err
  76. }
  77. defer repo.Close()
  78. }
  79. i := 0
  80. iter, err := repo.gogitRepo.References()
  81. if err != nil {
  82. return i, err
  83. }
  84. defer iter.Close()
  85. err = iter.ForEach(func(ref *plumbing.Reference) error {
  86. err := walkfn(ref.Hash().String(), string(ref.Name()))
  87. i++
  88. return err
  89. })
  90. return i, err
  91. }
  92. // WalkReferences walks all the references from the repository
  93. func (repo *Repository) WalkReferences(arg ObjectType, skip, limit int, walkfn func(sha1, refname string) error) (int, error) {
  94. i := 0
  95. var iter storer.ReferenceIter
  96. var err error
  97. switch arg {
  98. case ObjectTag:
  99. iter, err = repo.gogitRepo.Tags()
  100. case ObjectBranch:
  101. iter, err = repo.gogitRepo.Branches()
  102. default:
  103. iter, err = repo.gogitRepo.References()
  104. }
  105. if err != nil {
  106. return i, err
  107. }
  108. defer iter.Close()
  109. err = iter.ForEach(func(ref *plumbing.Reference) error {
  110. if i < skip {
  111. i++
  112. return nil
  113. }
  114. err := walkfn(ref.Hash().String(), string(ref.Name()))
  115. i++
  116. if err != nil {
  117. return err
  118. }
  119. if limit != 0 && i >= skip+limit {
  120. return storer.ErrStop
  121. }
  122. return nil
  123. })
  124. return i, err
  125. }