line_stats.go 5.2 KB

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