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_nogogit.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. "bufio"
  9. "bytes"
  10. "context"
  11. "io"
  12. "strings"
  13. "code.gitea.io/gitea/modules/log"
  14. )
  15. // IsObjectExist returns true if given reference exists in the repository.
  16. func (repo *Repository) IsObjectExist(name string) bool {
  17. if name == "" {
  18. return false
  19. }
  20. wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx)
  21. defer cancel()
  22. _, err := wr.Write([]byte(name + "\n"))
  23. if err != nil {
  24. log.Debug("Error writing to CatFileBatchCheck %v", err)
  25. return false
  26. }
  27. sha, _, _, err := ReadBatchLine(rd)
  28. return err == nil && bytes.HasPrefix(sha, []byte(strings.TrimSpace(name)))
  29. }
  30. // IsReferenceExist returns true if given reference exists in the repository.
  31. func (repo *Repository) IsReferenceExist(name string) bool {
  32. if name == "" {
  33. return false
  34. }
  35. wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx)
  36. defer cancel()
  37. _, err := wr.Write([]byte(name + "\n"))
  38. if err != nil {
  39. log.Debug("Error writing to CatFileBatchCheck %v", err)
  40. return false
  41. }
  42. _, _, _, err = ReadBatchLine(rd)
  43. return err == nil
  44. }
  45. // IsBranchExist returns true if given branch exists in current repository.
  46. func (repo *Repository) IsBranchExist(name string) bool {
  47. if name == "" {
  48. return false
  49. }
  50. return repo.IsReferenceExist(BranchPrefix + name)
  51. }
  52. // GetBranchNames returns branches from the repository, skipping skip initial branches and
  53. // returning at most limit branches, or all branches if limit is 0.
  54. func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) {
  55. return callShowRef(repo.Ctx, repo.Path, BranchPrefix, "--heads", skip, limit)
  56. }
  57. // WalkReferences walks all the references from the repository
  58. func WalkReferences(ctx context.Context, repoPath string, walkfn func(sha1, refname string) error) (int, error) {
  59. return walkShowRef(ctx, repoPath, "", 0, 0, walkfn)
  60. }
  61. // WalkReferences walks all the references from the repository
  62. // refType should be empty, ObjectTag or ObjectBranch. All other values are equivalent to empty.
  63. func (repo *Repository) WalkReferences(refType ObjectType, skip, limit int, walkfn func(sha1, refname string) error) (int, error) {
  64. var arg string
  65. switch refType {
  66. case ObjectTag:
  67. arg = "--tags"
  68. case ObjectBranch:
  69. arg = "--heads"
  70. default:
  71. arg = ""
  72. }
  73. return walkShowRef(repo.Ctx, repo.Path, arg, skip, limit, walkfn)
  74. }
  75. // callShowRef return refs, if limit = 0 it will not limit
  76. func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit int) (branchNames []string, countAll int, err error) {
  77. countAll, err = walkShowRef(ctx, repoPath, arg, skip, limit, func(_, branchName string) error {
  78. branchName = strings.TrimPrefix(branchName, prefix)
  79. branchNames = append(branchNames, branchName)
  80. return nil
  81. })
  82. return
  83. }
  84. func walkShowRef(ctx context.Context, repoPath, arg string, skip, limit int, walkfn func(sha1, refname string) error) (countAll int, err error) {
  85. stdoutReader, stdoutWriter := io.Pipe()
  86. defer func() {
  87. _ = stdoutReader.Close()
  88. _ = stdoutWriter.Close()
  89. }()
  90. go func() {
  91. stderrBuilder := &strings.Builder{}
  92. args := []string{"show-ref"}
  93. if arg != "" {
  94. args = append(args, arg)
  95. }
  96. err := NewCommand(ctx, args...).Run(&RunOpts{
  97. Dir: repoPath,
  98. Stdout: stdoutWriter,
  99. Stderr: stderrBuilder,
  100. })
  101. if err != nil {
  102. if stderrBuilder.Len() == 0 {
  103. _ = stdoutWriter.Close()
  104. return
  105. }
  106. _ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String()))
  107. } else {
  108. _ = stdoutWriter.Close()
  109. }
  110. }()
  111. i := 0
  112. bufReader := bufio.NewReader(stdoutReader)
  113. for i < skip {
  114. _, isPrefix, err := bufReader.ReadLine()
  115. if err == io.EOF {
  116. return i, nil
  117. }
  118. if err != nil {
  119. return 0, err
  120. }
  121. if !isPrefix {
  122. i++
  123. }
  124. }
  125. for limit == 0 || i < skip+limit {
  126. // The output of show-ref is simply a list:
  127. // <sha> SP <ref> LF
  128. sha, err := bufReader.ReadString(' ')
  129. if err == io.EOF {
  130. return i, nil
  131. }
  132. if err != nil {
  133. return 0, err
  134. }
  135. branchName, err := bufReader.ReadString('\n')
  136. if err == io.EOF {
  137. // This shouldn't happen... but we'll tolerate it for the sake of peace
  138. return i, nil
  139. }
  140. if err != nil {
  141. return i, err
  142. }
  143. if len(branchName) > 0 {
  144. branchName = branchName[:len(branchName)-1]
  145. }
  146. if len(sha) > 0 {
  147. sha = sha[:len(sha)-1]
  148. }
  149. err = walkfn(sha, branchName)
  150. if err != nil {
  151. return i, err
  152. }
  153. i++
  154. }
  155. // count all refs
  156. for limit != 0 {
  157. _, isPrefix, err := bufReader.ReadLine()
  158. if err == io.EOF {
  159. return i, nil
  160. }
  161. if err != nil {
  162. return 0, err
  163. }
  164. if !isPrefix {
  165. i++
  166. }
  167. }
  168. return i, nil
  169. }