file_history.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. files map[string][]plumbing.Hash
  20. }
  21. // FileHistoryResult is returned by Finalize() and represents the analysis result.
  22. type FileHistoryResult struct {
  23. Files map[string][]plumbing.Hash
  24. }
  25. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  26. func (history *FileHistory) Name() string {
  27. return "FileHistory"
  28. }
  29. // Provides returns the list of names of entities which are produced by this PipelineItem.
  30. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  31. // to this list. Also used by core.Registry to build the global map of providers.
  32. func (history *FileHistory) Provides() []string {
  33. return []string{}
  34. }
  35. // Requires returns the list of names of entities which are needed by this PipelineItem.
  36. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  37. // entities are Provides() upstream.
  38. func (history *FileHistory) Requires() []string {
  39. arr := [...]string{items.DependencyTreeChanges}
  40. return arr[:]
  41. }
  42. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  43. func (history *FileHistory) ListConfigurationOptions() []core.ConfigurationOption {
  44. return []core.ConfigurationOption{}
  45. }
  46. // Flag for the command line switch which enables this analysis.
  47. func (history *FileHistory) Flag() string {
  48. return "file-history"
  49. }
  50. // Configure sets the properties previously published by ListConfigurationOptions().
  51. func (history *FileHistory) Configure(facts map[string]interface{}) {
  52. }
  53. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  54. // calls. The repository which is going to be analysed is supplied as an argument.
  55. func (history *FileHistory) Initialize(repository *git.Repository) {
  56. history.files = map[string][]plumbing.Hash{}
  57. }
  58. // Consume runs this PipelineItem on the next commit data.
  59. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  60. // Additionally, "commit" is always present there and represents the analysed *object.Commit.
  61. // This function returns the mapping with analysis results. The keys must be the same as
  62. // in Provides(). If there was an error, nil is returned.
  63. func (history *FileHistory) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  64. commit := deps["commit"].(*object.Commit).Hash
  65. changes := deps[items.DependencyTreeChanges].(object.Changes)
  66. for _, change := range changes {
  67. action, _ := change.Action()
  68. switch action {
  69. case merkletrie.Insert:
  70. hashes := make([]plumbing.Hash, 1)
  71. hashes[0] = commit
  72. history.files[change.To.Name] = hashes
  73. case merkletrie.Delete:
  74. delete(history.files, change.From.Name)
  75. case merkletrie.Modify:
  76. hashes := history.files[change.From.Name]
  77. if change.From.Name != change.To.Name {
  78. delete(history.files, change.From.Name)
  79. }
  80. hashes = append(hashes, commit)
  81. history.files[change.To.Name] = hashes
  82. }
  83. }
  84. return nil, nil
  85. }
  86. // Finalize returns the result of the analysis. Further Consume() calls are not expected.
  87. func (history *FileHistory) Finalize() interface{} {
  88. return FileHistoryResult{Files: history.files}
  89. }
  90. // Serialize converts the analysis result as returned by Finalize() to text or bytes.
  91. // The text format is YAML and the bytes format is Protocol Buffers.
  92. func (history *FileHistory) Serialize(result interface{}, binary bool, writer io.Writer) error {
  93. historyResult := result.(FileHistoryResult)
  94. if binary {
  95. return history.serializeBinary(&historyResult, writer)
  96. }
  97. history.serializeText(&historyResult, writer)
  98. return nil
  99. }
  100. func (history *FileHistory) serializeText(result *FileHistoryResult, writer io.Writer) {
  101. keys := make([]string, len(result.Files))
  102. i := 0
  103. for key := range result.Files {
  104. keys[i] = key
  105. i++
  106. }
  107. sort.Strings(keys)
  108. for _, key := range keys {
  109. hashes := result.Files[key]
  110. strhashes := make([]string, len(hashes))
  111. for i, hash := range hashes {
  112. strhashes[i] = "\"" + hash.String() + "\""
  113. }
  114. fmt.Fprintf(writer, " - %s: [%s]\n", key, strings.Join(strhashes, ","))
  115. }
  116. }
  117. func (history *FileHistory) serializeBinary(result *FileHistoryResult, writer io.Writer) error {
  118. message := pb.FileHistoryResultMessage{
  119. Files: map[string]*pb.FileHistory{},
  120. }
  121. for key, vals := range result.Files {
  122. hashes := &pb.FileHistory{
  123. Commits: make([]string, len(vals)),
  124. }
  125. for i, hash := range vals {
  126. hashes.Commits[i] = hash.String()
  127. }
  128. message.Files[key] = hashes
  129. }
  130. serialized, err := proto.Marshal(&message)
  131. if err != nil {
  132. return err
  133. }
  134. writer.Write(serialized)
  135. return nil
  136. }
  137. func init() {
  138. core.Registry.Register(&FileHistory{})
  139. }