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 5.3KB

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