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.

220 lines
5.4 KiB

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