gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/git/gitcmd"
  13. "code.gitea.io/gitea/modules/log"
  14. )
  15. // IsObjectExist returns true if the given object exists in the repository.
  16. func (repo *Repository) IsObjectExist(name string) bool {
  17. if name == "" {
  18. return false
  19. }
  20. wr, rd, cancel, err := repo.CatFileBatchCheck(repo.Ctx)
  21. if err != nil {
  22. log.Debug("Error writing to CatFileBatchCheck %v", err)
  23. return false
  24. }
  25. defer cancel()
  26. _, err = wr.Write([]byte(name + "\n"))
  27. if err != nil {
  28. log.Debug("Error writing to CatFileBatchCheck %v", err)
  29. return false
  30. }
  31. sha, _, _, err := ReadBatchLine(rd)
  32. return err == nil && bytes.HasPrefix(sha, []byte(strings.TrimSpace(name)))
  33. }
  34. // IsReferenceExist returns true if given reference exists in the repository.
  35. func (repo *Repository) IsReferenceExist(name string) bool {
  36. if name == "" {
  37. return false
  38. }
  39. wr, rd, cancel, err := repo.CatFileBatchCheck(repo.Ctx)
  40. if err != nil {
  41. log.Debug("Error writing to CatFileBatchCheck %v", err)
  42. return false
  43. }
  44. defer cancel()
  45. _, err = wr.Write([]byte(name + "\n"))
  46. if err != nil {
  47. log.Debug("Error writing to CatFileBatchCheck %v", err)
  48. return false
  49. }
  50. _, _, _, err = ReadBatchLine(rd)
  51. return err == nil
  52. }
  53. // IsBranchExist returns true if given branch exists in current repository.
  54. func (repo *Repository) IsBranchExist(name string) bool {
  55. if repo == nil || name == "" {
  56. return false
  57. }
  58. return repo.IsReferenceExist(BranchPrefix + name)
  59. }
  60. // GetBranchNames returns branches from the repository, skipping "skip" initial branches and
  61. // returning at most "limit" branches, or all branches if "limit" is 0.
  62. func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) {
  63. return callShowRef(repo.Ctx, repo.Path, BranchPrefix, gitcmd.TrustedCmdArgs{BranchPrefix, "--sort=-committerdate"}, skip, limit)
  64. }
  65. // WalkReferences walks all the references from the repository
  66. // refType should be empty, ObjectTag or ObjectBranch. All other values are equivalent to empty.
  67. func (repo *Repository) WalkReferences(refType ObjectType, skip, limit int, walkfn func(sha1, refname string) error) (int, error) {
  68. var args gitcmd.TrustedCmdArgs
  69. switch refType {
  70. case ObjectTag:
  71. args = gitcmd.TrustedCmdArgs{TagPrefix, "--sort=-taggerdate"}
  72. case ObjectBranch:
  73. args = gitcmd.TrustedCmdArgs{BranchPrefix, "--sort=-committerdate"}
  74. }
  75. return WalkShowRef(repo.Ctx, repo.Path, args, skip, limit, walkfn)
  76. }
  77. // callShowRef return refs, if limit = 0 it will not limit
  78. func callShowRef(ctx context.Context, repoPath, trimPrefix string, extraArgs gitcmd.TrustedCmdArgs, skip, limit int) (branchNames []string, countAll int, err error) {
  79. countAll, err = WalkShowRef(ctx, repoPath, extraArgs, skip, limit, func(_, branchName string) error {
  80. branchName = strings.TrimPrefix(branchName, trimPrefix)
  81. branchNames = append(branchNames, branchName)
  82. return nil
  83. })
  84. return branchNames, countAll, err
  85. }
  86. func WalkShowRef(ctx context.Context, repoPath string, extraArgs gitcmd.TrustedCmdArgs, skip, limit int, walkfn func(sha1, refname string) error) (countAll int, err error) {
  87. stdoutReader, stdoutWriter := io.Pipe()
  88. defer func() {
  89. _ = stdoutReader.Close()
  90. _ = stdoutWriter.Close()
  91. }()
  92. go func() {
  93. stderrBuilder := &strings.Builder{}
  94. args := gitcmd.TrustedCmdArgs{"for-each-ref", "--format=%(objectname) %(refname)"}
  95. args = append(args, extraArgs...)
  96. err := gitcmd.NewCommand(args...).Run(ctx, &gitcmd.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(gitcmd.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. }
  170. // GetRefsBySha returns all references filtered with prefix that belong to a sha commit hash
  171. func (repo *Repository) GetRefsBySha(sha, prefix string) ([]string, error) {
  172. var revList []string
  173. _, err := WalkShowRef(repo.Ctx, repo.Path, nil, 0, 0, func(walkSha, refname string) error {
  174. if walkSha == sha && strings.HasPrefix(refname, prefix) {
  175. revList = append(revList, refname)
  176. }
  177. return nil
  178. })
  179. return revList, err
  180. }