file_history.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.v8/internal/core"
  13. "gopkg.in/src-d/hercules.v8/internal/pb"
  14. items "gopkg.in/src-d/hercules.v8/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{}) error {
  58. return nil
  59. }
  60. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  61. // calls. The repository which is going to be analysed is supplied as an argument.
  62. func (history *FileHistory) Initialize(repository *git.Repository) error {
  63. history.files = map[string][]plumbing.Hash{}
  64. history.OneShotMergeProcessor.Initialize()
  65. return nil
  66. }
  67. // Consume runs this PipelineItem on the next commit data.
  68. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  69. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  70. // This function returns the mapping with analysis results. The keys must be the same as
  71. // in Provides(). If there was an error, nil is returned.
  72. func (history *FileHistory) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  73. if !history.ShouldConsumeCommit(deps) {
  74. return nil, nil
  75. }
  76. commit := deps[core.DependencyCommit].(*object.Commit).Hash
  77. changes := deps[items.DependencyTreeChanges].(object.Changes)
  78. for _, change := range changes {
  79. action, _ := change.Action()
  80. switch action {
  81. case merkletrie.Insert:
  82. hashes := make([]plumbing.Hash, 1)
  83. hashes[0] = commit
  84. history.files[change.To.Name] = hashes
  85. case merkletrie.Delete:
  86. delete(history.files, change.From.Name)
  87. case merkletrie.Modify:
  88. hashes := history.files[change.From.Name]
  89. if change.From.Name != change.To.Name {
  90. delete(history.files, change.From.Name)
  91. }
  92. hashes = append(hashes, commit)
  93. history.files[change.To.Name] = hashes
  94. }
  95. }
  96. return nil, nil
  97. }
  98. // Finalize returns the result of the analysis. Further Consume() calls are not expected.
  99. func (history *FileHistory) Finalize() interface{} {
  100. return FileHistoryResult{Files: history.files}
  101. }
  102. // Fork clones this PipelineItem.
  103. func (history *FileHistory) Fork(n int) []core.PipelineItem {
  104. return core.ForkSamePipelineItem(history, n)
  105. }
  106. // Serialize converts the analysis result as returned by Finalize() to text or bytes.
  107. // The text format is YAML and the bytes format is Protocol Buffers.
  108. func (history *FileHistory) Serialize(result interface{}, binary bool, writer io.Writer) error {
  109. historyResult := result.(FileHistoryResult)
  110. if binary {
  111. return history.serializeBinary(&historyResult, writer)
  112. }
  113. history.serializeText(&historyResult, writer)
  114. return nil
  115. }
  116. func (history *FileHistory) serializeText(result *FileHistoryResult, writer io.Writer) {
  117. keys := make([]string, len(result.Files))
  118. i := 0
  119. for key := range result.Files {
  120. keys[i] = key
  121. i++
  122. }
  123. sort.Strings(keys)
  124. for _, key := range keys {
  125. hashes := result.Files[key]
  126. strhashes := make([]string, len(hashes))
  127. for i, hash := range hashes {
  128. strhashes[i] = "\"" + hash.String() + "\""
  129. }
  130. fmt.Fprintf(writer, " - %s: [%s]\n", key, strings.Join(strhashes, ","))
  131. }
  132. }
  133. func (history *FileHistory) serializeBinary(result *FileHistoryResult, writer io.Writer) error {
  134. message := pb.FileHistoryResultMessage{
  135. Files: map[string]*pb.FileHistory{},
  136. }
  137. for key, vals := range result.Files {
  138. hashes := &pb.FileHistory{
  139. Commits: make([]string, len(vals)),
  140. }
  141. for i, hash := range vals {
  142. hashes.Commits[i] = hash.String()
  143. }
  144. message.Files[key] = hashes
  145. }
  146. serialized, err := proto.Marshal(&message)
  147. if err != nil {
  148. return err
  149. }
  150. _, err = writer.Write(serialized)
  151. return err
  152. }
  153. func init() {
  154. core.Registry.Register(&FileHistory{})
  155. }