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.

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