day.go 4.0 KB

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