tree_entry.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "fmt"
  7. "path"
  8. "path/filepath"
  9. "runtime"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. )
  14. type EntryMode int
  15. // There are only a few file modes in Git. They look like unix file modes, but they can only be
  16. // one of these.
  17. const (
  18. EntryBlob EntryMode = 0100644
  19. EntryExec EntryMode = 0100755
  20. EntrySymlink EntryMode = 0120000
  21. EntryCommit EntryMode = 0160000
  22. EntryTree EntryMode = 0040000
  23. )
  24. type TreeEntry struct {
  25. ID sha1
  26. Type ObjectType
  27. mode EntryMode
  28. name string
  29. ptree *Tree
  30. commited bool
  31. size int64
  32. sized bool
  33. }
  34. func (te *TreeEntry) SetSize(size int64) {
  35. te.size = size
  36. te.sized = true
  37. }
  38. func (te *TreeEntry) Name() string {
  39. return te.name
  40. }
  41. func (te *TreeEntry) Size() int64 {
  42. if te.IsDir() {
  43. return 0
  44. } else if te.sized {
  45. return te.size
  46. }
  47. stdout, err := NewCommand("cat-file", "-s", te.ID.String()).RunInDir(te.ptree.repo.Path)
  48. if err != nil {
  49. return 0
  50. }
  51. te.sized = true
  52. te.size, _ = strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
  53. return te.size
  54. }
  55. func (te *TreeEntry) IsSubModule() bool {
  56. return te.mode == EntryCommit
  57. }
  58. func (te *TreeEntry) IsDir() bool {
  59. return te.mode == EntryTree
  60. }
  61. func (te *TreeEntry) IsLink() bool {
  62. return te.mode == EntrySymlink
  63. }
  64. func (te *TreeEntry) Blob() *Blob {
  65. return &Blob{
  66. repo: te.ptree.repo,
  67. TreeEntry: te,
  68. }
  69. }
  70. type Entries []*TreeEntry
  71. var sorter = []func(t1, t2 *TreeEntry) bool{
  72. func(t1, t2 *TreeEntry) bool {
  73. return (t1.IsDir() || t1.IsSubModule()) && !t2.IsDir() && !t2.IsSubModule()
  74. },
  75. func(t1, t2 *TreeEntry) bool {
  76. return t1.name < t2.name
  77. },
  78. }
  79. func (tes Entries) Len() int { return len(tes) }
  80. func (tes Entries) Swap(i, j int) { tes[i], tes[j] = tes[j], tes[i] }
  81. func (tes Entries) Less(i, j int) bool {
  82. t1, t2 := tes[i], tes[j]
  83. var k int
  84. for k = 0; k < len(sorter)-1; k++ {
  85. sort := sorter[k]
  86. switch {
  87. case sort(t1, t2):
  88. return true
  89. case sort(t2, t1):
  90. return false
  91. }
  92. }
  93. return sorter[k](t1, t2)
  94. }
  95. func (tes Entries) Sort() {
  96. sort.Sort(tes)
  97. }
  98. var defaultConcurrency = runtime.NumCPU()
  99. type commitInfo struct {
  100. entryName string
  101. infos []interface{}
  102. err error
  103. }
  104. // GetCommitsInfo takes advantages of concurrency to speed up getting information
  105. // of all commits that are corresponding to these entries. This method will automatically
  106. // choose the right number of goroutine (concurrency) to use related of the host CPU.
  107. func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interface{}, error) {
  108. return tes.GetCommitsInfoWithCustomConcurrency(commit, treePath, 0)
  109. }
  110. // GetCommitsInfoWithCustomConcurrency takes advantages of concurrency to speed up getting information
  111. // of all commits that are corresponding to these entries. If the given maxConcurrency is negative or
  112. // equal to zero: the right number of goroutine (concurrency) to use will be choosen related of the
  113. // host CPU.
  114. func (tes Entries) GetCommitsInfoWithCustomConcurrency(commit *Commit, treePath string, maxConcurrency int) ([][]interface{}, error) {
  115. if len(tes) == 0 {
  116. return nil, nil
  117. }
  118. if maxConcurrency <= 0 {
  119. maxConcurrency = defaultConcurrency
  120. }
  121. // Length of taskChan determines how many goroutines (subprocesses) can run at the same time.
  122. // The length of revChan should be same as taskChan so goroutines whoever finished job can
  123. // exit as early as possible, only store data inside channel.
  124. taskChan := make(chan bool, maxConcurrency)
  125. revChan := make(chan commitInfo, maxConcurrency)
  126. doneChan := make(chan error)
  127. // Receive loop will exit when it collects same number of data pieces as tree entries.
  128. // It notifies doneChan before exits or notify early with possible error.
  129. infoMap := make(map[string][]interface{}, len(tes))
  130. go func() {
  131. i := 0
  132. for info := range revChan {
  133. if info.err != nil {
  134. doneChan <- info.err
  135. return
  136. }
  137. infoMap[info.entryName] = info.infos
  138. i++
  139. if i == len(tes) {
  140. break
  141. }
  142. }
  143. doneChan <- nil
  144. }()
  145. for i := range tes {
  146. // When taskChan is idle (or has empty slots), put operation will not block.
  147. // However when taskChan is full, code will block and wait any running goroutines to finish.
  148. taskChan <- true
  149. if tes[i].Type != ObjectCommit {
  150. go func(i int) {
  151. cinfo := commitInfo{entryName: tes[i].Name()}
  152. c, err := commit.GetCommitByPath(filepath.Join(treePath, tes[i].Name()))
  153. if err != nil {
  154. cinfo.err = fmt.Errorf("GetCommitByPath (%s/%s): %v", treePath, tes[i].Name(), err)
  155. } else {
  156. cinfo.infos = []interface{}{tes[i], c}
  157. }
  158. revChan <- cinfo
  159. <-taskChan // Clear one slot from taskChan to allow new goroutines to start.
  160. }(i)
  161. continue
  162. }
  163. // Handle submodule
  164. go func(i int) {
  165. cinfo := commitInfo{entryName: tes[i].Name()}
  166. sm, err := commit.GetSubModule(path.Join(treePath, tes[i].Name()))
  167. if err != nil && !IsErrNotExist(err) {
  168. cinfo.err = fmt.Errorf("GetSubModule (%s/%s): %v", treePath, tes[i].Name(), err)
  169. revChan <- cinfo
  170. return
  171. }
  172. smURL := ""
  173. if sm != nil {
  174. smURL = sm.URL
  175. }
  176. c, err := commit.GetCommitByPath(filepath.Join(treePath, tes[i].Name()))
  177. if err != nil {
  178. cinfo.err = fmt.Errorf("GetCommitByPath (%s/%s): %v", treePath, tes[i].Name(), err)
  179. } else {
  180. cinfo.infos = []interface{}{tes[i], NewSubModuleFile(c, smURL, tes[i].ID.String())}
  181. }
  182. revChan <- cinfo
  183. <-taskChan
  184. }(i)
  185. }
  186. if err := <-doneChan; err != nil {
  187. return nil, err
  188. }
  189. commitsInfo := make([][]interface{}, len(tes))
  190. for i := 0; i < len(tes); i++ {
  191. commitsInfo[i] = infoMap[tes[i].Name()]
  192. }
  193. return commitsInfo, nil
  194. }