ticks.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package plumbing
  2. import (
  3. "log"
  4. "time"
  5. "gopkg.in/src-d/go-git.v4"
  6. "gopkg.in/src-d/go-git.v4/plumbing"
  7. "gopkg.in/src-d/go-git.v4/plumbing/object"
  8. "gopkg.in/src-d/hercules.v10/internal/core"
  9. )
  10. // TicksSinceStart provides relative tick information for every commit.
  11. // It is a PipelineItem.
  12. type TicksSinceStart struct {
  13. core.NoopMerger
  14. TickSize time.Duration
  15. remote string
  16. tick0 *time.Time
  17. previousTick int
  18. commits map[int][]plumbing.Hash
  19. }
  20. const (
  21. // DependencyTick is the name of the dependency which DaysSinceStart provides - the number
  22. // of ticks since the first commit in the analysed sequence.
  23. DependencyTick = "tick"
  24. // FactCommitsByTick contains the mapping between day indices and the corresponding commits.
  25. FactCommitsByTick = "TicksSinceStart.Commits"
  26. // FactTickSize contains the time.Duration of each tick.
  27. FactTickSize = "TicksSinceStart.TickSize"
  28. // ConfigTicksSinceStartTickSize sets the size of each 'tick' in hours.
  29. ConfigTicksSinceStartTickSize = "TicksSinceStart.TickSize"
  30. // DefaultTicksSinceStartTickSize is the default number of hours in each 'tick' (24*hour = 1day).
  31. DefaultTicksSinceStartTickSize = 24
  32. )
  33. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  34. func (ticks *TicksSinceStart) Name() string {
  35. return "TicksSinceStart"
  36. }
  37. // Provides returns the list of names of entities which are produced by this PipelineItem.
  38. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  39. // to this list. Also used by core.Registry to build the global map of providers.
  40. func (ticks *TicksSinceStart) Provides() []string {
  41. arr := [...]string{DependencyTick}
  42. return arr[:]
  43. }
  44. // Requires returns the list of names of entities which are needed by this PipelineItem.
  45. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  46. // entities are Provides() upstream.
  47. func (ticks *TicksSinceStart) Requires() []string {
  48. return []string{}
  49. }
  50. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  51. func (ticks *TicksSinceStart) ListConfigurationOptions() []core.ConfigurationOption {
  52. return []core.ConfigurationOption{{
  53. Name: ConfigTicksSinceStartTickSize,
  54. Description: "How long each 'tick' represents in hours.",
  55. Flag: "tick-size",
  56. Type: core.IntConfigurationOption,
  57. Default: DefaultTicksSinceStartTickSize},
  58. }
  59. }
  60. // Configure sets the properties previously published by ListConfigurationOptions().
  61. func (ticks *TicksSinceStart) Configure(facts map[string]interface{}) error {
  62. if val, exists := facts[ConfigTicksSinceStartTickSize].(int); exists {
  63. ticks.TickSize = time.Duration(val) * time.Hour
  64. } else {
  65. ticks.TickSize = DefaultTicksSinceStartTickSize * time.Hour
  66. }
  67. if ticks.commits == nil {
  68. ticks.commits = map[int][]plumbing.Hash{}
  69. }
  70. facts[FactCommitsByTick] = ticks.commits
  71. facts[FactTickSize] = ticks.TickSize
  72. return nil
  73. }
  74. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  75. // calls. The repository which is going to be analysed is supplied as an argument.
  76. func (ticks *TicksSinceStart) Initialize(repository *git.Repository) error {
  77. if ticks.TickSize == 0 {
  78. ticks.TickSize = DefaultTicksSinceStartTickSize * time.Hour
  79. }
  80. ticks.tick0 = &time.Time{}
  81. ticks.previousTick = 0
  82. if len(ticks.commits) > 0 {
  83. keys := make([]int, len(ticks.commits))
  84. for key := range ticks.commits {
  85. keys = append(keys, key)
  86. }
  87. for _, key := range keys {
  88. delete(ticks.commits, key)
  89. }
  90. }
  91. if r, err := repository.Remotes(); err == nil && len(r) > 0 {
  92. ticks.remote = r[0].Config().URLs[0]
  93. }
  94. return nil
  95. }
  96. // Consume runs this PipelineItem on the next commit data.
  97. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  98. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  99. // This function returns the mapping with analysis results. The keys must be the same as
  100. // in Provides(). If there was an error, nil is returned.
  101. func (ticks *TicksSinceStart) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  102. commit := deps[core.DependencyCommit].(*object.Commit)
  103. index := deps[core.DependencyIndex].(int)
  104. if index == 0 {
  105. // first iteration - initialize the file objects from the tree
  106. // our precision is 1 day
  107. *ticks.tick0 = commit.Committer.When
  108. if ticks.tick0.Unix() < 631152000 { // 01.01.1990, that was 30 years ago
  109. log.Println()
  110. log.Printf("Warning: suspicious committer timestamp in %s > %s: %d",
  111. ticks.remote, commit.Hash.String(), ticks.tick0.Unix())
  112. }
  113. }
  114. tick := int(commit.Committer.When.Sub(*ticks.tick0) / ticks.TickSize)
  115. if tick < ticks.previousTick {
  116. // rebase works miracles, but we need the monotonous time
  117. tick = ticks.previousTick
  118. }
  119. ticks.previousTick = tick
  120. tickCommits := ticks.commits[tick]
  121. if tickCommits == nil {
  122. tickCommits = []plumbing.Hash{}
  123. }
  124. exists := false
  125. if commit.NumParents() > 0 {
  126. for i := range tickCommits {
  127. if tickCommits[len(tickCommits)-i-1] == commit.Hash {
  128. exists = true
  129. break
  130. }
  131. }
  132. }
  133. if !exists {
  134. ticks.commits[tick] = append(tickCommits, commit.Hash)
  135. }
  136. return map[string]interface{}{DependencyTick: tick}, nil
  137. }
  138. // Fork clones this PipelineItem.
  139. func (ticks *TicksSinceStart) Fork(n int) []core.PipelineItem {
  140. return core.ForkCopyPipelineItem(ticks, n)
  141. }
  142. func init() {
  143. core.Registry.Register(&TicksSinceStart{})
  144. }