commits.go 6.8 KB

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