day.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package hercules
  2. import (
  3. "time"
  4. "gopkg.in/src-d/go-git.v4"
  5. "gopkg.in/src-d/go-git.v4/plumbing"
  6. "gopkg.in/src-d/go-git.v4/plumbing/object"
  7. )
  8. // DaysSinceStart provides the relative date information for every commit.
  9. // It is a PipelineItem.
  10. type DaysSinceStart struct {
  11. day0 time.Time
  12. previousDay int
  13. commits map[int][]plumbing.Hash
  14. }
  15. const (
  16. // DependencyDay is the name of the dependency which DaysSinceStart provides - the number
  17. // of days since the first commit in the analysed sequence.
  18. DependencyDay = "day"
  19. // FactCommitsByDay contains the mapping between day indices and the corresponding commits.
  20. FactCommitsByDay = "DaysSinceStart.Commits"
  21. )
  22. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  23. func (days *DaysSinceStart) Name() string {
  24. return "DaysSinceStart"
  25. }
  26. // Provides returns the list of names of entities which are produced by this PipelineItem.
  27. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  28. // to this list. Also used by hercules.Registry to build the global map of providers.
  29. func (days *DaysSinceStart) Provides() []string {
  30. arr := [...]string{DependencyDay}
  31. return arr[:]
  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 (days *DaysSinceStart) Requires() []string {
  37. return []string{}
  38. }
  39. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  40. func (days *DaysSinceStart) ListConfigurationOptions() []ConfigurationOption {
  41. return []ConfigurationOption{}
  42. }
  43. // Configure sets the properties previously published by ListConfigurationOptions().
  44. func (days *DaysSinceStart) Configure(facts map[string]interface{}) {
  45. if days.commits == nil {
  46. days.commits = map[int][]plumbing.Hash{}
  47. }
  48. facts[FactCommitsByDay] = days.commits
  49. }
  50. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  51. // calls. The repository which is going to be analysed is supplied as an argument.
  52. func (days *DaysSinceStart) Initialize(repository *git.Repository) {
  53. days.day0 = time.Time{}
  54. days.previousDay = 0
  55. if len(days.commits) > 0 {
  56. keys := make([]int, len(days.commits))
  57. for key := range days.commits {
  58. keys = append(keys, key)
  59. }
  60. for _, key := range keys {
  61. delete(days.commits, key)
  62. }
  63. }
  64. }
  65. // Consume runs this PipelineItem on the next commit data.
  66. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  67. // Additionally, "commit" is always present there and represents the analysed *object.Commit.
  68. // This function returns the mapping with analysis results. The keys must be the same as
  69. // in Provides(). If there was an error, nil is returned.
  70. func (days *DaysSinceStart) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  71. commit := deps["commit"].(*object.Commit)
  72. index := deps["index"].(int)
  73. if index == 0 {
  74. // first iteration - initialize the file objects from the tree
  75. days.day0 = commit.Author.When
  76. // our precision is 1 day
  77. days.day0 = days.day0.Truncate(24 * time.Hour)
  78. }
  79. day := int(commit.Author.When.Sub(days.day0).Hours() / 24)
  80. if day < days.previousDay {
  81. // rebase works miracles, but we need the monotonous time
  82. day = days.previousDay
  83. }
  84. days.previousDay = day
  85. dayCommits := days.commits[day]
  86. if dayCommits == nil {
  87. dayCommits = []plumbing.Hash{}
  88. }
  89. days.commits[day] = append(dayCommits, commit.Hash)
  90. return map[string]interface{}{DependencyDay: day}, nil
  91. }
  92. func init() {
  93. Registry.Register(&DaysSinceStart{})
  94. }