file_history.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. func (history *FileHistory) Name() string {
  24. return "FileHistory"
  25. }
  26. func (history *FileHistory) Provides() []string {
  27. return []string{}
  28. }
  29. func (history *FileHistory) Requires() []string {
  30. arr := [...]string{DependencyTreeChanges}
  31. return arr[:]
  32. }
  33. func (history *FileHistory) ListConfigurationOptions() []ConfigurationOption {
  34. return []ConfigurationOption{}
  35. }
  36. func (history *FileHistory) Flag() string {
  37. return "file-history"
  38. }
  39. func (history *FileHistory) Configure(facts map[string]interface{}) {
  40. }
  41. // Initialize resets the internal temporary data structures and prepares the object for Consume().
  42. func (history *FileHistory) Initialize(repository *git.Repository) {
  43. history.files = map[string][]plumbing.Hash{}
  44. }
  45. // Consume is called for every commit in the sequence.
  46. func (history *FileHistory) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  47. commit := deps["commit"].(*object.Commit).Hash
  48. changes := deps[DependencyTreeChanges].(object.Changes)
  49. for _, change := range changes {
  50. action, _ := change.Action()
  51. switch action {
  52. case merkletrie.Insert:
  53. hashes := make([]plumbing.Hash, 1)
  54. hashes[0] = commit
  55. history.files[change.To.Name] = hashes
  56. case merkletrie.Delete:
  57. delete(history.files, change.From.Name)
  58. case merkletrie.Modify:
  59. hashes := history.files[change.From.Name]
  60. if change.From.Name != change.To.Name {
  61. delete(history.files, change.From.Name)
  62. }
  63. hashes = append(hashes, commit)
  64. history.files[change.To.Name] = hashes
  65. }
  66. }
  67. return nil, nil
  68. }
  69. func (history *FileHistory) Finalize() interface{} {
  70. return FileHistoryResult{Files: history.files}
  71. }
  72. // Serialize converts the result from Finalize() to either Protocol Buffers or YAML.
  73. func (history *FileHistory) Serialize(result interface{}, binary bool, writer io.Writer) error {
  74. historyResult := result.(FileHistoryResult)
  75. if binary {
  76. return history.serializeBinary(&historyResult, writer)
  77. }
  78. history.serializeText(&historyResult, writer)
  79. return nil
  80. }
  81. func (history *FileHistory) serializeText(result *FileHistoryResult, writer io.Writer) {
  82. keys := make([]string, len(result.Files))
  83. i := 0
  84. for key := range result.Files {
  85. keys[i] = key
  86. i++
  87. }
  88. sort.Strings(keys)
  89. for _, key := range keys {
  90. hashes := result.Files[key]
  91. strhashes := make([]string, len(hashes))
  92. for i, hash := range hashes {
  93. strhashes[i] = "\"" + hash.String() + "\""
  94. }
  95. fmt.Fprintf(writer, " - %s: [%s]\n", key, strings.Join(strhashes, ","))
  96. }
  97. }
  98. func (history *FileHistory) serializeBinary(result *FileHistoryResult, writer io.Writer) error {
  99. message := pb.FileHistoryResultMessage{
  100. Files: map[string]*pb.FileHistory{},
  101. }
  102. for key, vals := range result.Files {
  103. hashes := &pb.FileHistory{
  104. Commits: make([]string, len(vals)),
  105. }
  106. for i, hash := range vals {
  107. hashes.Commits[i] = hash.String()
  108. }
  109. message.Files[key] = hashes
  110. }
  111. serialized, err := proto.Marshal(&message)
  112. if err != nil {
  113. return err
  114. }
  115. writer.Write(serialized)
  116. return nil
  117. }
  118. func init() {
  119. Registry.Register(&FileHistory{})
  120. }