line_stats.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. return []string{DependencyLineStats}
  39. }
  40. // Requires returns the list of names of entities which are needed by this PipelineItem.
  41. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  42. // entities are Provides() upstream.
  43. func (lsc *LinesStatsCalculator) Requires() []string {
  44. return []string{DependencyTreeChanges, DependencyBlobCache, DependencyFileDiff}
  45. }
  46. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  47. func (lsc *LinesStatsCalculator) ListConfigurationOptions() []core.ConfigurationOption {
  48. return nil
  49. }
  50. // Configure sets the properties previously published by ListConfigurationOptions().
  51. func (lsc *LinesStatsCalculator) Configure(facts map[string]interface{}) error {
  52. if l, exists := facts[core.ConfigLogger].(core.Logger); exists {
  53. lsc.l = l
  54. }
  55. return nil
  56. }
  57. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  58. // calls. The repository which is going to be analysed is supplied as an argument.
  59. func (lsc *LinesStatsCalculator) Initialize(repository *git.Repository) error {
  60. lsc.l = core.NewLogger()
  61. return nil
  62. }
  63. // Consume runs this PipelineItem on the next commit data.
  64. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  65. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  66. // This function returns the mapping with analysis results. The keys must be the same as
  67. // in Provides(). If there was an error, nil is returned.
  68. func (lsc *LinesStatsCalculator) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  69. result := map[object.ChangeEntry]LineStats{}
  70. if deps[core.DependencyIsMerge].(bool) {
  71. // we ignore merge commit diffs
  72. // TODO(vmarkovtsev): handle them better
  73. return map[string]interface{}{DependencyLineStats: result}, nil
  74. }
  75. treeDiff := deps[DependencyTreeChanges].(object.Changes)
  76. cache := deps[DependencyBlobCache].(map[plumbing.Hash]*CachedBlob)
  77. fileDiffs := deps[DependencyFileDiff].(map[string]FileDiffData)
  78. for _, change := range treeDiff {
  79. action, err := change.Action()
  80. if err != nil {
  81. return nil, err
  82. }
  83. switch action {
  84. case merkletrie.Insert:
  85. blob := cache[change.To.TreeEntry.Hash]
  86. lines, err := blob.CountLines()
  87. if err != nil {
  88. // binary
  89. continue
  90. }
  91. result[change.To] = LineStats{
  92. Added: lines,
  93. Removed: 0,
  94. Changed: 0,
  95. }
  96. case merkletrie.Delete:
  97. blob := cache[change.From.TreeEntry.Hash]
  98. lines, err := blob.CountLines()
  99. if err != nil {
  100. // binary
  101. continue
  102. }
  103. result[change.From] = LineStats{
  104. Added: 0,
  105. Removed: lines,
  106. Changed: 0,
  107. }
  108. case merkletrie.Modify:
  109. thisDiffs := fileDiffs[change.To.Name]
  110. var added, removed, changed, removedPending int
  111. for _, edit := range thisDiffs.Diffs {
  112. switch edit.Type {
  113. case diffmatchpatch.DiffEqual:
  114. if removedPending > 0 {
  115. removed += removedPending
  116. }
  117. removedPending = 0
  118. case diffmatchpatch.DiffInsert:
  119. delta := utf8.RuneCountInString(edit.Text)
  120. if removedPending > delta {
  121. changed += delta
  122. removed += removedPending - delta
  123. } else {
  124. changed += removedPending
  125. added += delta - removedPending
  126. }
  127. removedPending = 0
  128. case diffmatchpatch.DiffDelete:
  129. removedPending = utf8.RuneCountInString(edit.Text)
  130. }
  131. }
  132. if removedPending > 0 {
  133. removed += removedPending
  134. }
  135. result[change.To] = LineStats{
  136. Added: added,
  137. Removed: removed,
  138. Changed: changed,
  139. }
  140. }
  141. }
  142. return map[string]interface{}{DependencyLineStats: result}, nil
  143. }
  144. // Fork clones this PipelineItem.
  145. func (lsc *LinesStatsCalculator) Fork(n int) []core.PipelineItem {
  146. return core.ForkSamePipelineItem(lsc, n)
  147. }
  148. func init() {
  149. core.Registry.Register(&LinesStatsCalculator{})
  150. }