tree_diff.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package hercules
  2. import (
  3. "io"
  4. "gopkg.in/src-d/go-git.v4"
  5. "gopkg.in/src-d/go-git.v4/plumbing/object"
  6. )
  7. // TreeDiff generates the list of changes for a commit. A change can be either one or two blobs
  8. // under the same path: "before" and "after". If "before" is nil, the change is an addition.
  9. // If "after" is nil, the change is a removal. Otherwise, it is a modification.
  10. // TreeDiff is a PipelineItem.
  11. type TreeDiff struct {
  12. previousTree *object.Tree
  13. }
  14. const (
  15. // DependencyTreeChanges is the name of the dependency provided by TreeDiff.
  16. DependencyTreeChanges = "changes"
  17. )
  18. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  19. func (treediff *TreeDiff) Name() string {
  20. return "TreeDiff"
  21. }
  22. // Provides returns the list of names of entities which are produced by this PipelineItem.
  23. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  24. // to this list. Also used by hercules.Registry to build the global map of providers.
  25. func (treediff *TreeDiff) Provides() []string {
  26. arr := [...]string{DependencyTreeChanges}
  27. return arr[:]
  28. }
  29. // Requires returns the list of names of entities which are needed by this PipelineItem.
  30. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  31. // entities are Provides() upstream.
  32. func (treediff *TreeDiff) Requires() []string {
  33. return []string{}
  34. }
  35. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  36. func (treediff *TreeDiff) ListConfigurationOptions() []ConfigurationOption {
  37. return []ConfigurationOption{}
  38. }
  39. // Configure sets the properties previously published by ListConfigurationOptions().
  40. func (treediff *TreeDiff) Configure(facts map[string]interface{}) {}
  41. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  42. // calls. The repository which is going to be analysed is supplied as an argument.
  43. func (treediff *TreeDiff) Initialize(repository *git.Repository) {
  44. treediff.previousTree = nil
  45. }
  46. // Consume runs this PipelineItem on the next commit data.
  47. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  48. // Additionally, "commit" is always present there and represents the analysed *object.Commit.
  49. // This function returns the mapping with analysis results. The keys must be the same as
  50. // in Provides(). If there was an error, nil is returned.
  51. func (treediff *TreeDiff) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  52. commit := deps["commit"].(*object.Commit)
  53. tree, err := commit.Tree()
  54. if err != nil {
  55. return nil, err
  56. }
  57. var diff object.Changes
  58. if treediff.previousTree != nil {
  59. diff, err = object.DiffTree(treediff.previousTree, tree)
  60. if err != nil {
  61. return nil, err
  62. }
  63. } else {
  64. diff = []*object.Change{}
  65. err = func() error {
  66. fileIter := tree.Files()
  67. defer fileIter.Close()
  68. for {
  69. file, err := fileIter.Next()
  70. if err != nil {
  71. if err == io.EOF {
  72. break
  73. }
  74. return err
  75. }
  76. diff = append(diff, &object.Change{
  77. To: object.ChangeEntry{Name: file.Name, Tree: tree, TreeEntry: object.TreeEntry{
  78. Name: file.Name, Mode: file.Mode, Hash: file.Hash}}})
  79. }
  80. return nil
  81. }()
  82. if err != nil {
  83. return nil, err
  84. }
  85. }
  86. treediff.previousTree = tree
  87. return map[string]interface{}{DependencyTreeChanges: diff}, nil
  88. }
  89. func init() {
  90. Registry.Register(&TreeDiff{})
  91. }