tree_entry.go 5.4 KB

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