commits.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package leaves
  2. import (
  3. "fmt"
  4. "io"
  5. "github.com/gogo/protobuf/proto"
  6. git "gopkg.in/src-d/go-git.v4"
  7. "gopkg.in/src-d/go-git.v4/plumbing"
  8. "gopkg.in/src-d/go-git.v4/plumbing/object"
  9. "gopkg.in/src-d/hercules.v9/internal/core"
  10. "gopkg.in/src-d/hercules.v9/internal/pb"
  11. items "gopkg.in/src-d/hercules.v9/internal/plumbing"
  12. "gopkg.in/src-d/hercules.v9/internal/plumbing/identity"
  13. "gopkg.in/src-d/hercules.v9/internal/yaml"
  14. )
  15. // CommitsAnalysis extracts statistics for each commit
  16. type CommitsAnalysis struct {
  17. core.NoopMerger
  18. // commits stores statistics for each commit
  19. commits []*CommitStat
  20. // reversedPeopleDict references IdentityDetector.ReversedPeopleDict
  21. reversedPeopleDict []string
  22. }
  23. // CommitsResult is returned by CommitsAnalysis.Finalize() and carries the statistics
  24. // per commit.
  25. type CommitsResult struct {
  26. Commits []*CommitStat
  27. // reversedPeopleDict references IdentityDetector.ReversedPeopleDict
  28. reversedPeopleDict []string
  29. }
  30. // FileStat is the statistics for a file in a commit
  31. type FileStat struct {
  32. Name string
  33. Language string
  34. items.LineStats
  35. }
  36. // CommitStat is the statistics for a commit
  37. type CommitStat struct {
  38. Hash string
  39. When int64
  40. Author int
  41. Files []FileStat
  42. }
  43. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  44. func (ca *CommitsAnalysis) Name() string {
  45. return "CommitsStat"
  46. }
  47. // Provides returns the list of names of entities which are produced by this PipelineItem.
  48. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  49. // to this list. Also used by core.Registry to build the global map of providers.
  50. func (ca *CommitsAnalysis) Provides() []string {
  51. return []string{}
  52. }
  53. // Requires returns the list of names of entities which are needed by this PipelineItem.
  54. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  55. // entities are Provides() upstream.
  56. func (ca *CommitsAnalysis) Requires() []string {
  57. arr := [...]string{
  58. identity.DependencyAuthor, items.DependencyLanguages, items.DependencyLineStats}
  59. return arr[:]
  60. }
  61. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  62. func (ca *CommitsAnalysis) ListConfigurationOptions() []core.ConfigurationOption {
  63. return nil
  64. }
  65. // Configure sets the properties previously published by ListConfigurationOptions().
  66. func (ca *CommitsAnalysis) Configure(facts map[string]interface{}) error {
  67. if val, exists := facts[identity.FactIdentityDetectorReversedPeopleDict].([]string); exists {
  68. ca.reversedPeopleDict = val
  69. }
  70. return nil
  71. }
  72. // Flag for the command line switch which enables this analysis.
  73. func (ca *CommitsAnalysis) Flag() string {
  74. return "commits-stat"
  75. }
  76. // Description returns the text which explains what the analysis is doing.
  77. func (ca *CommitsAnalysis) Description() string {
  78. return "Extracts statistics for each commit. Identical to `git log --stat`"
  79. }
  80. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  81. // calls. The repository which is going to be analysed is supplied as an argument.
  82. func (ca *CommitsAnalysis) Initialize(repository *git.Repository) error {
  83. return nil
  84. }
  85. // Consume runs this PipelineItem on the next commit data.
  86. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  87. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  88. // This function returns the mapping with analysis results. The keys must be the same as
  89. // in Provides(). If there was an error, nil is returned.
  90. func (ca *CommitsAnalysis) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  91. if deps[core.DependencyIsMerge].(bool) {
  92. return nil, nil
  93. }
  94. commit := deps[core.DependencyCommit].(*object.Commit)
  95. author := deps[identity.DependencyAuthor].(int)
  96. lineStats := deps[items.DependencyLineStats].(map[object.ChangeEntry]items.LineStats)
  97. langs := deps[items.DependencyLanguages].(map[plumbing.Hash]string)
  98. cs := CommitStat{
  99. Hash: commit.Hash.String(),
  100. When: commit.Author.When.Unix(),
  101. Author: author,
  102. }
  103. for entry, stats := range lineStats {
  104. cs.Files = append(cs.Files, FileStat{
  105. Name: entry.Name,
  106. Language: langs[entry.TreeEntry.Hash],
  107. LineStats: stats,
  108. })
  109. }
  110. ca.commits = append(ca.commits, &cs)
  111. return nil, nil
  112. }
  113. // Finalize returns the result of the analysis. Further Consume() calls are not expected.
  114. func (ca *CommitsAnalysis) Finalize() interface{} {
  115. return CommitsResult{
  116. Commits: ca.commits,
  117. reversedPeopleDict: ca.reversedPeopleDict,
  118. }
  119. }
  120. // Fork clones this pipeline item.
  121. func (ca *CommitsAnalysis) Fork(n int) []core.PipelineItem {
  122. return core.ForkSamePipelineItem(ca, n)
  123. }
  124. // Serialize converts the analysis result as returned by Finalize() to text or bytes.
  125. // The text format is YAML and the bytes format is Protocol Buffers.
  126. func (ca *CommitsAnalysis) Serialize(result interface{}, binary bool, writer io.Writer) error {
  127. commitsResult := result.(CommitsResult)
  128. if binary {
  129. return ca.serializeBinary(&commitsResult, writer)
  130. }
  131. ca.serializeText(&commitsResult, writer)
  132. return nil
  133. }
  134. func (ca *CommitsAnalysis) serializeText(result *CommitsResult, writer io.Writer) {
  135. fmt.Fprintln(writer, " commits:")
  136. for _, c := range result.Commits {
  137. fmt.Fprintf(writer, " - hash: %s\n", c.Hash)
  138. fmt.Fprintf(writer, " when: %d\n", c.When)
  139. fmt.Fprintf(writer, " author: %d\n", c.Author)
  140. fmt.Fprintf(writer, " files:\n")
  141. for _, f := range c.Files {
  142. fmt.Fprintf(writer, " - name: %s\n", f.Name)
  143. fmt.Fprintf(writer, " language: %s\n", f.Language)
  144. fmt.Fprintf(writer, " stat: [%d, %d, %d]\n", f.Added, f.Changed, f.Removed)
  145. }
  146. }
  147. fmt.Fprintln(writer, " people:")
  148. for _, person := range result.reversedPeopleDict {
  149. fmt.Fprintf(writer, " - %s\n", yaml.SafeString(person))
  150. }
  151. }
  152. func (ca *CommitsAnalysis) serializeBinary(result *CommitsResult, writer io.Writer) error {
  153. message := pb.CommitsAnalysisResults{}
  154. message.AuthorIndex = result.reversedPeopleDict
  155. message.Commits = make([]*pb.Commit, len(result.Commits))
  156. for i, c := range result.Commits {
  157. files := make([]*pb.CommitFile, len(c.Files))
  158. for i, f := range c.Files {
  159. files[i] = &pb.CommitFile{
  160. Name: f.Name,
  161. Language: f.Language,
  162. Stats: &pb.LineStats{
  163. Added: int32(f.LineStats.Added),
  164. Changed: int32(f.LineStats.Changed),
  165. Removed: int32(f.LineStats.Removed),
  166. },
  167. }
  168. }
  169. message.Commits[i] = &pb.Commit{
  170. Hash: c.Hash,
  171. WhenUnixTime: c.When,
  172. Author: int32(c.Author),
  173. Files: files,
  174. }
  175. }
  176. serialized, err := proto.Marshal(&message)
  177. if err != nil {
  178. return err
  179. }
  180. _, err = writer.Write(serialized)
  181. return err
  182. }
  183. func init() {
  184. core.Registry.Register(&CommitsAnalysis{})
  185. }