file_history.go 4.9 KB

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