tree_entry.go 5.6 KB

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