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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // +build !gogit
  7. package git
  8. import (
  9. "bufio"
  10. "bytes"
  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()
  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()
  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. // GetBranches 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) GetBranches(skip, limit int) ([]string, int, error) {
  55. return callShowRef(repo.Path, BranchPrefix, "--heads", skip, limit)
  56. }
  57. // callShowRef return refs, if limit = 0 it will not limit
  58. func callShowRef(repoPath, prefix, arg string, skip, limit int) (branchNames []string, countAll int, err error) {
  59. stdoutReader, stdoutWriter := io.Pipe()
  60. defer func() {
  61. _ = stdoutReader.Close()
  62. _ = stdoutWriter.Close()
  63. }()
  64. go func() {
  65. stderrBuilder := &strings.Builder{}
  66. err := NewCommand("show-ref", arg).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder)
  67. if err != nil {
  68. if stderrBuilder.Len() == 0 {
  69. _ = stdoutWriter.Close()
  70. return
  71. }
  72. _ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String()))
  73. } else {
  74. _ = stdoutWriter.Close()
  75. }
  76. }()
  77. i := 0
  78. bufReader := bufio.NewReader(stdoutReader)
  79. for i < skip {
  80. _, isPrefix, err := bufReader.ReadLine()
  81. if err == io.EOF {
  82. return branchNames, i, nil
  83. }
  84. if err != nil {
  85. return nil, 0, err
  86. }
  87. if !isPrefix {
  88. i++
  89. }
  90. }
  91. for limit == 0 || i < skip+limit {
  92. // The output of show-ref is simply a list:
  93. // <sha> SP <ref> LF
  94. _, err := bufReader.ReadSlice(' ')
  95. for err == bufio.ErrBufferFull {
  96. // This shouldn't happen but we'll tolerate it for the sake of peace
  97. _, err = bufReader.ReadSlice(' ')
  98. }
  99. if err == io.EOF {
  100. return branchNames, i, nil
  101. }
  102. if err != nil {
  103. return nil, 0, err
  104. }
  105. branchName, err := bufReader.ReadString('\n')
  106. if err == io.EOF {
  107. // This shouldn't happen... but we'll tolerate it for the sake of peace
  108. return branchNames, i, nil
  109. }
  110. if err != nil {
  111. return nil, i, err
  112. }
  113. branchName = strings.TrimPrefix(branchName, prefix)
  114. if len(branchName) > 0 {
  115. branchName = branchName[:len(branchName)-1]
  116. }
  117. branchNames = append(branchNames, branchName)
  118. i++
  119. }
  120. // count all refs
  121. for limit != 0 {
  122. _, isPrefix, err := bufReader.ReadLine()
  123. if err == io.EOF {
  124. return branchNames, i, nil
  125. }
  126. if err != nil {
  127. return nil, 0, err
  128. }
  129. if !isPrefix {
  130. i++
  131. }
  132. }
  133. return branchNames, i, nil
  134. }