tree_diff.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package hercules
  2. import (
  3. "io"
  4. "strings"
  5. "gopkg.in/src-d/go-git.v4"
  6. "gopkg.in/src-d/go-git.v4/plumbing/object"
  7. )
  8. // TreeDiff generates the list of changes for a commit. A change can be either one or two blobs
  9. // under the same path: "before" and "after". If "before" is nil, the change is an addition.
  10. // If "after" is nil, the change is a removal. Otherwise, it is a modification.
  11. // TreeDiff is a PipelineItem.
  12. type TreeDiff struct {
  13. SkipDirs []string
  14. previousTree *object.Tree
  15. }
  16. const (
  17. // DependencyTreeChanges is the name of the dependency provided by TreeDiff.
  18. DependencyTreeChanges = "changes"
  19. // ConfigTreeDiffEnableBlacklist is the name of the configuration option
  20. // (TreeDiff.Configure()) which allows to skip blacklisted directories.
  21. ConfigTreeDiffEnableBlacklist = "TreeDiff.EnableBlacklist"
  22. // ConfigTreeDiffBlacklistedDirs s the name of the configuration option
  23. // (TreeDiff.Configure()) which allows to set blacklisted directories.
  24. ConfigTreeDiffBlacklistedDirs = "TreeDiff.BlacklistedDirs"
  25. )
  26. var defaultBlacklistedDirs = []string{"vendor/", "vendors/", "node_modules/"}
  27. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  28. func (treediff *TreeDiff) Name() string {
  29. return "TreeDiff"
  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 hercules.Registry to build the global map of providers.
  34. func (treediff *TreeDiff) Provides() []string {
  35. arr := [...]string{DependencyTreeChanges}
  36. return arr[:]
  37. }
  38. // Requires returns the list of names of entities which are needed by this PipelineItem.
  39. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  40. // entities are Provides() upstream.
  41. func (treediff *TreeDiff) Requires() []string {
  42. return []string{}
  43. }
  44. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  45. func (treediff *TreeDiff) ListConfigurationOptions() []ConfigurationOption {
  46. options := [...]ConfigurationOption{{
  47. Name: ConfigTreeDiffEnableBlacklist,
  48. Description: "Skip blacklisted directories.",
  49. Flag: "skip-blacklist",
  50. Type: BoolConfigurationOption,
  51. Default: false}, {
  52. Name: ConfigTreeDiffBlacklistedDirs,
  53. Description: "List of blacklisted directories. Separated by comma \",\".",
  54. Flag: "blacklisted-dirs",
  55. Type: StringsConfigurationOption,
  56. Default: defaultBlacklistedDirs},
  57. }
  58. return options[:]
  59. }
  60. // Configure sets the properties previously published by ListConfigurationOptions().
  61. func (treediff *TreeDiff) Configure(facts map[string]interface{}) {
  62. if val, exist := facts[ConfigTreeDiffEnableBlacklist]; exist && val.(bool) {
  63. treediff.SkipDirs = facts[ConfigTreeDiffBlacklistedDirs].([]string)
  64. }
  65. }
  66. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  67. // calls. The repository which is going to be analysed is supplied as an argument.
  68. func (treediff *TreeDiff) Initialize(repository *git.Repository) {
  69. treediff.previousTree = nil
  70. }
  71. // Consume runs this PipelineItem on the next commit data.
  72. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  73. // Additionally, "commit" is always present there and represents the analysed *object.Commit.
  74. // This function returns the mapping with analysis results. The keys must be the same as
  75. // in Provides(). If there was an error, nil is returned.
  76. func (treediff *TreeDiff) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  77. commit := deps["commit"].(*object.Commit)
  78. tree, err := commit.Tree()
  79. if err != nil {
  80. return nil, err
  81. }
  82. var diff object.Changes
  83. if treediff.previousTree != nil {
  84. diff, err = object.DiffTree(treediff.previousTree, tree)
  85. if err != nil {
  86. return nil, err
  87. }
  88. } else {
  89. diff = []*object.Change{}
  90. err = func() error {
  91. fileIter := tree.Files()
  92. defer fileIter.Close()
  93. for {
  94. file, err := fileIter.Next()
  95. if err != nil {
  96. if err == io.EOF {
  97. break
  98. }
  99. return err
  100. }
  101. diff = append(diff, &object.Change{
  102. To: object.ChangeEntry{Name: file.Name, Tree: tree, TreeEntry: object.TreeEntry{
  103. Name: file.Name, Mode: file.Mode, Hash: file.Hash}}})
  104. }
  105. return nil
  106. }()
  107. if err != nil {
  108. return nil, err
  109. }
  110. }
  111. treediff.previousTree = tree
  112. if len(treediff.SkipDirs) > 0 {
  113. // filter without allocation
  114. filteredDiff := diff[:0]
  115. OUTER:
  116. for _, change := range diff {
  117. for _, dir := range treediff.SkipDirs {
  118. if strings.HasPrefix(change.To.Name, dir) || strings.HasPrefix(change.From.Name, dir) {
  119. continue OUTER
  120. }
  121. }
  122. filteredDiff = append(filteredDiff, change)
  123. }
  124. diff = filteredDiff
  125. }
  126. return map[string]interface{}{DependencyTreeChanges: diff}, nil
  127. }
  128. func init() {
  129. Registry.Register(&TreeDiff{})
  130. }