file_history.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package leaves
  2. import (
  3. "fmt"
  4. "io"
  5. "sort"
  6. "strings"
  7. "github.com/gogo/protobuf/proto"
  8. "gopkg.in/src-d/go-git.v4"
  9. "gopkg.in/src-d/go-git.v4/plumbing"
  10. "gopkg.in/src-d/go-git.v4/plumbing/object"
  11. "gopkg.in/src-d/go-git.v4/utils/merkletrie"
  12. "gopkg.in/src-d/hercules.v5/internal/core"
  13. "gopkg.in/src-d/hercules.v5/internal/pb"
  14. items "gopkg.in/src-d/hercules.v5/internal/plumbing"
  15. )
  16. // FileHistory contains the intermediate state which is mutated by Consume(). It should implement
  17. // LeafPipelineItem.
  18. type FileHistory struct {
  19. core.NoopMerger
  20. core.OneShotMergeProcessor
  21. files map[string][]plumbing.Hash
  22. }
  23. // FileHistoryResult is returned by Finalize() and represents the analysis result.
  24. type FileHistoryResult struct {
  25. Files map[string][]plumbing.Hash
  26. }
  27. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  28. func (history *FileHistory) Name() string {
  29. return "FileHistory"
  30. }
  31. // Provides returns the list of names of entities which are produced by this PipelineItem.
  32. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  33. // to this list. Also used by core.Registry to build the global map of providers.
  34. func (history *FileHistory) Provides() []string {
  35. return []string{}
  36. }
  37. // Requires returns the list of names of entities which are needed by this PipelineItem.
  38. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  39. // entities are Provides() upstream.
  40. func (history *FileHistory) Requires() []string {
  41. arr := [...]string{items.DependencyTreeChanges}
  42. return arr[:]
  43. }
  44. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  45. func (history *FileHistory) ListConfigurationOptions() []core.ConfigurationOption {
  46. return []core.ConfigurationOption{}
  47. }
  48. // Flag for the command line switch which enables this analysis.
  49. func (history *FileHistory) Flag() string {
  50. return "file-history"
  51. }
  52. // Description returns the text which explains what the analysis is doing.
  53. func (history *FileHistory) Description() string {
  54. return "Each file path is mapped to the list of commits which involve that file."
  55. }
  56. // Configure sets the properties previously published by ListConfigurationOptions().
  57. func (history *FileHistory) Configure(facts map[string]interface{}) {
  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 (history *FileHistory) Initialize(repository *git.Repository) {
  62. history.files = map[string][]plumbing.Hash{}
  63. history.OneShotMergeProcessor.Initialize()
  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 (history *FileHistory) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  71. if !history.ShouldConsumeCommit(deps) {
  72. return nil, nil
  73. }
  74. commit := deps[core.DependencyCommit].(*object.Commit).Hash
  75. changes := deps[items.DependencyTreeChanges].(object.Changes)
  76. for _, change := range changes {
  77. action, _ := change.Action()
  78. switch action {
  79. case merkletrie.Insert:
  80. hashes := make([]plumbing.Hash, 1)
  81. hashes[0] = commit
  82. history.files[change.To.Name] = hashes
  83. case merkletrie.Delete:
  84. delete(history.files, change.From.Name)
  85. case merkletrie.Modify:
  86. hashes := history.files[change.From.Name]
  87. if change.From.Name != change.To.Name {
  88. delete(history.files, change.From.Name)
  89. }
  90. hashes = append(hashes, commit)
  91. history.files[change.To.Name] = hashes
  92. }
  93. }
  94. return nil, nil
  95. }
  96. // Finalize returns the result of the analysis. Further Consume() calls are not expected.
  97. func (history *FileHistory) Finalize() interface{} {
  98. return FileHistoryResult{Files: history.files}
  99. }
  100. // Fork clones this PipelineItem.
  101. func (history *FileHistory) Fork(n int) []core.PipelineItem {
  102. return core.ForkSamePipelineItem(history, n)
  103. }
  104. // Serialize converts the analysis result as returned by Finalize() to text or bytes.
  105. // The text format is YAML and the bytes format is Protocol Buffers.
  106. func (history *FileHistory) Serialize(result interface{}, binary bool, writer io.Writer) error {
  107. historyResult := result.(FileHistoryResult)
  108. if binary {
  109. return history.serializeBinary(&historyResult, writer)
  110. }
  111. history.serializeText(&historyResult, writer)
  112. return nil
  113. }
  114. func (history *FileHistory) serializeText(result *FileHistoryResult, writer io.Writer) {
  115. keys := make([]string, len(result.Files))
  116. i := 0
  117. for key := range result.Files {
  118. keys[i] = key
  119. i++
  120. }
  121. sort.Strings(keys)
  122. for _, key := range keys {
  123. hashes := result.Files[key]
  124. strhashes := make([]string, len(hashes))
  125. for i, hash := range hashes {
  126. strhashes[i] = "\"" + hash.String() + "\""
  127. }
  128. fmt.Fprintf(writer, " - %s: [%s]\n", key, strings.Join(strhashes, ","))
  129. }
  130. }
  131. func (history *FileHistory) serializeBinary(result *FileHistoryResult, writer io.Writer) error {
  132. message := pb.FileHistoryResultMessage{
  133. Files: map[string]*pb.FileHistory{},
  134. }
  135. for key, vals := range result.Files {
  136. hashes := &pb.FileHistory{
  137. Commits: make([]string, len(vals)),
  138. }
  139. for i, hash := range vals {
  140. hashes.Commits[i] = hash.String()
  141. }
  142. message.Files[key] = hashes
  143. }
  144. serialized, err := proto.Marshal(&message)
  145. if err != nil {
  146. return err
  147. }
  148. writer.Write(serialized)
  149. return nil
  150. }
  151. func init() {
  152. core.Registry.Register(&FileHistory{})
  153. }