file_history.go 5.4 KB

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