tree_entry.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. type commitInfo struct {
  95. entryName string
  96. infos []interface{}
  97. err error
  98. }
  99. // GetCommitsInfo takes advantages of concurrency to speed up getting information
  100. // of all commits that are corresponding to these entries. This method will automatically
  101. // choose the right number of goroutine (concurrency) to use related of the host CPU.
  102. func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interface{}, error) {
  103. return tes.GetCommitsInfoWithCustomConcurrency(commit, treePath, 0)
  104. }
  105. // GetCommitsInfoWithCustomConcurrency takes advantages of concurrency to speed up getting information
  106. // of all commits that are corresponding to these entries. If the given maxConcurrency is negative or
  107. // equal to zero: the right number of goroutine (concurrency) to use will be choosen related of the
  108. // host CPU.
  109. func (tes Entries) GetCommitsInfoWithCustomConcurrency(commit *Commit, treePath string, maxConcurrency int) ([][]interface{}, error) {
  110. if len(tes) == 0 {
  111. return nil, nil
  112. }
  113. if maxConcurrency <= 0 {
  114. maxConcurrency = runtime.NumCPU()
  115. }
  116. // Length of taskChan determines how many goroutines (subprocesses) can run at the same time.
  117. // The length of revChan should be same as taskChan so goroutines whoever finished job can
  118. // exit as early as possible, only store data inside channel.
  119. taskChan := make(chan bool, maxConcurrency)
  120. revChan := make(chan commitInfo, maxConcurrency)
  121. doneChan := make(chan error)
  122. // Receive loop will exit when it collects same number of data pieces as tree entries.
  123. // It notifies doneChan before exits or notify early with possible error.
  124. infoMap := make(map[string][]interface{}, len(tes))
  125. go func() {
  126. i := 0
  127. for info := range revChan {
  128. if info.err != nil {
  129. doneChan <- info.err
  130. return
  131. }
  132. infoMap[info.entryName] = info.infos
  133. i++
  134. if i == len(tes) {
  135. break
  136. }
  137. }
  138. doneChan <- nil
  139. }()
  140. for i := range tes {
  141. // When taskChan is idle (or has empty slots), put operation will not block.
  142. // However when taskChan is full, code will block and wait any running goroutines to finish.
  143. taskChan <- true
  144. if tes[i].Type != OBJECT_COMMIT {
  145. go func(i int) {
  146. cinfo := commitInfo{entryName: tes[i].Name()}
  147. c, err := commit.GetCommitByPath(filepath.Join(treePath, tes[i].Name()))
  148. if err != nil {
  149. cinfo.err = fmt.Errorf("GetCommitByPath (%s/%s): %v", treePath, tes[i].Name(), err)
  150. } else {
  151. cinfo.infos = []interface{}{tes[i], c}
  152. }
  153. revChan <- cinfo
  154. <-taskChan // Clear one slot from taskChan to allow new goroutines to start.
  155. }(i)
  156. continue
  157. }
  158. // Handle submodule
  159. go func(i int) {
  160. cinfo := commitInfo{entryName: tes[i].Name()}
  161. sm, err := commit.GetSubModule(path.Join(treePath, tes[i].Name()))
  162. if err != nil && !IsErrNotExist(err) {
  163. cinfo.err = fmt.Errorf("GetSubModule (%s/%s): %v", treePath, tes[i].Name(), err)
  164. revChan <- cinfo
  165. return
  166. }
  167. smURL := ""
  168. if sm != nil {
  169. smURL = sm.URL
  170. }
  171. c, err := commit.GetCommitByPath(filepath.Join(treePath, tes[i].Name()))
  172. if err != nil {
  173. cinfo.err = fmt.Errorf("GetCommitByPath (%s/%s): %v", treePath, tes[i].Name(), err)
  174. } else {
  175. cinfo.infos = []interface{}{tes[i], NewSubModuleFile(c, smURL, tes[i].ID.String())}
  176. }
  177. revChan <- cinfo
  178. <-taskChan
  179. }(i)
  180. }
  181. if err := <-doneChan; err != nil {
  182. return nil, err
  183. }
  184. commitsInfo := make([][]interface{}, len(tes))
  185. for i := 0; i < len(tes); i++ {
  186. commitsInfo[i] = infoMap[tes[i].Name()]
  187. }
  188. return commitsInfo, nil
  189. }