day.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.v6/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{}) error {
  47. if days.commits == nil {
  48. days.commits = map[int][]plumbing.Hash{}
  49. }
  50. facts[FactCommitsByDay] = days.commits
  51. return nil
  52. }
  53. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  54. // calls. The repository which is going to be analysed is supplied as an argument.
  55. func (days *DaysSinceStart) Initialize(repository *git.Repository) error {
  56. days.day0 = &time.Time{}
  57. days.previousDay = 0
  58. if len(days.commits) > 0 {
  59. keys := make([]int, len(days.commits))
  60. for key := range days.commits {
  61. keys = append(keys, key)
  62. }
  63. for _, key := range keys {
  64. delete(days.commits, key)
  65. }
  66. }
  67. return nil
  68. }
  69. // Consume runs this PipelineItem on the next commit data.
  70. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  71. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  72. // This function returns the mapping with analysis results. The keys must be the same as
  73. // in Provides(). If there was an error, nil is returned.
  74. func (days *DaysSinceStart) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  75. commit := deps[core.DependencyCommit].(*object.Commit)
  76. index := deps[core.DependencyIndex].(int)
  77. if index == 0 {
  78. // first iteration - initialize the file objects from the tree
  79. *days.day0 = commit.Committer.When
  80. // our precision is 1 day
  81. *days.day0 = days.day0.Truncate(24 * time.Hour)
  82. }
  83. day := int(commit.Committer.When.Sub(*days.day0).Hours() / 24)
  84. if day < days.previousDay {
  85. // rebase works miracles, but we need the monotonous time
  86. day = days.previousDay
  87. }
  88. days.previousDay = day
  89. dayCommits := days.commits[day]
  90. if dayCommits == nil {
  91. dayCommits = []plumbing.Hash{}
  92. }
  93. exists := false
  94. if commit.NumParents() > 0 {
  95. for i := range dayCommits {
  96. if dayCommits[len(dayCommits)-i-1] == commit.Hash {
  97. exists = true
  98. }
  99. }
  100. }
  101. if !exists {
  102. days.commits[day] = append(dayCommits, commit.Hash)
  103. }
  104. return map[string]interface{}{DependencyDay: day}, nil
  105. }
  106. // Fork clones this PipelineItem.
  107. func (days *DaysSinceStart) Fork(n int) []core.PipelineItem {
  108. return core.ForkCopyPipelineItem(days, n)
  109. }
  110. func init() {
  111. core.Registry.Register(&DaysSinceStart{})
  112. }