tree_diff.go 5.5 KB

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