ticks.go 5.2 KB

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