line_stats.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package plumbing
  2. import (
  3. "unicode/utf8"
  4. "github.com/sergi/go-diff/diffmatchpatch"
  5. "gopkg.in/src-d/go-git.v4"
  6. "gopkg.in/src-d/go-git.v4/plumbing"
  7. "gopkg.in/src-d/go-git.v4/plumbing/object"
  8. "gopkg.in/src-d/go-git.v4/utils/merkletrie"
  9. "gopkg.in/src-d/hercules.v10/internal/core"
  10. )
  11. // LinesStatsCalculator measures line statistics for each text file in the commit.
  12. type LinesStatsCalculator struct {
  13. core.NoopMerger
  14. l core.Logger
  15. }
  16. // LineStats holds the numbers of inserted, deleted and changed lines.
  17. type LineStats struct {
  18. // Added is the number of added lines by a particular developer in a particular day.
  19. Added int
  20. // Removed is the number of removed lines by a particular developer in a particular day.
  21. Removed int
  22. // Changed is the number of changed lines by a particular developer in a particular day.
  23. Changed int
  24. }
  25. const (
  26. // DependencyLineStats is the identifier of the data provided by LinesStatsCalculator - line
  27. // statistics for each file in the commit.
  28. DependencyLineStats = "line_stats"
  29. )
  30. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  31. func (lsc *LinesStatsCalculator) Name() string {
  32. return "LinesStats"
  33. }
  34. // Provides returns the list of names of entities which are produced by this PipelineItem.
  35. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  36. // to this list. Also used by core.Registry to build the global map of providers.
  37. func (lsc *LinesStatsCalculator) Provides() []string {
  38. arr := [...]string{DependencyLineStats}
  39. return arr[:]
  40. }
  41. // Requires returns the list of names of entities which are needed by this PipelineItem.
  42. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  43. // entities are Provides() upstream.
  44. func (lsc *LinesStatsCalculator) Requires() []string {
  45. arr := [...]string{DependencyTreeChanges, DependencyBlobCache, DependencyFileDiff}
  46. return arr[:]
  47. }
  48. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  49. func (lsc *LinesStatsCalculator) ListConfigurationOptions() []core.ConfigurationOption {
  50. return nil
  51. }
  52. // Configure sets the properties previously published by ListConfigurationOptions().
  53. func (lsc *LinesStatsCalculator) Configure(facts map[string]interface{}) error {
  54. if l, exists := facts[core.ConfigLogger].(core.Logger); exists {
  55. lsc.l = l
  56. }
  57. return nil
  58. }
  59. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  60. // calls. The repository which is going to be analysed is supplied as an argument.
  61. func (lsc *LinesStatsCalculator) Initialize(repository *git.Repository) error {
  62. lsc.l = core.NewLogger()
  63. return nil
  64. }
  65. // Consume runs this PipelineItem on the next commit data.
  66. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  67. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  68. // This function returns the mapping with analysis results. The keys must be the same as
  69. // in Provides(). If there was an error, nil is returned.
  70. func (lsc *LinesStatsCalculator) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  71. result := map[object.ChangeEntry]LineStats{}
  72. if deps[core.DependencyIsMerge].(bool) {
  73. // we ignore merge commit diffs
  74. // TODO(vmarkovtsev): handle them better
  75. return map[string]interface{}{DependencyLineStats: result}, nil
  76. }
  77. treeDiff := deps[DependencyTreeChanges].(object.Changes)
  78. cache := deps[DependencyBlobCache].(map[plumbing.Hash]*CachedBlob)
  79. fileDiffs := deps[DependencyFileDiff].(map[string]FileDiffData)
  80. for _, change := range treeDiff {
  81. action, err := change.Action()
  82. if err != nil {
  83. return nil, err
  84. }
  85. switch action {
  86. case merkletrie.Insert:
  87. blob := cache[change.To.TreeEntry.Hash]
  88. lines, err := blob.CountLines()
  89. if err != nil {
  90. // binary
  91. continue
  92. }
  93. result[change.To] = LineStats{
  94. Added: lines,
  95. Removed: 0,
  96. Changed: 0,
  97. }
  98. case merkletrie.Delete:
  99. blob := cache[change.From.TreeEntry.Hash]
  100. lines, err := blob.CountLines()
  101. if err != nil {
  102. // binary
  103. continue
  104. }
  105. result[change.From] = LineStats{
  106. Added: 0,
  107. Removed: lines,
  108. Changed: 0,
  109. }
  110. case merkletrie.Modify:
  111. thisDiffs := fileDiffs[change.To.Name]
  112. var added, removed, changed, removedPending int
  113. for _, edit := range thisDiffs.Diffs {
  114. switch edit.Type {
  115. case diffmatchpatch.DiffEqual:
  116. if removedPending > 0 {
  117. removed += removedPending
  118. }
  119. removedPending = 0
  120. case diffmatchpatch.DiffInsert:
  121. delta := utf8.RuneCountInString(edit.Text)
  122. if removedPending > delta {
  123. changed += delta
  124. removed += removedPending - delta
  125. } else {
  126. changed += removedPending
  127. added += delta - removedPending
  128. }
  129. removedPending = 0
  130. case diffmatchpatch.DiffDelete:
  131. removedPending = utf8.RuneCountInString(edit.Text)
  132. }
  133. }
  134. if removedPending > 0 {
  135. removed += removedPending
  136. }
  137. result[change.To] = LineStats{
  138. Added: added,
  139. Removed: removed,
  140. Changed: changed,
  141. }
  142. }
  143. }
  144. return map[string]interface{}{DependencyLineStats: result}, nil
  145. }
  146. // Fork clones this PipelineItem.
  147. func (lsc *LinesStatsCalculator) Fork(n int) []core.PipelineItem {
  148. return core.ForkSamePipelineItem(lsc, n)
  149. }
  150. func init() {
  151. core.Registry.Register(&LinesStatsCalculator{})
  152. }