day.go 3.7 KB

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