ticks.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. remote string
  15. tickSize time.Duration
  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. // default to 1 day
  64. ticks.tickSize = 24 * time.Hour
  65. }
  66. if ticks.commits == nil {
  67. ticks.commits = map[int][]plumbing.Hash{}
  68. }
  69. facts[FactCommitsByTick] = ticks.commits
  70. return nil
  71. }
  72. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  73. // calls. The repository which is going to be analysed is supplied as an argument.
  74. func (ticks *TicksSinceStart) Initialize(repository *git.Repository) error {
  75. ticks.tick0 = &time.Time{}
  76. ticks.previousTick = 0
  77. if len(ticks.commits) > 0 {
  78. keys := make([]int, len(ticks.commits))
  79. for key := range ticks.commits {
  80. keys = append(keys, key)
  81. }
  82. for _, key := range keys {
  83. delete(ticks.commits, key)
  84. }
  85. }
  86. if r, err := repository.Remotes(); err == nil && len(r) > 0 {
  87. ticks.remote = r[0].Config().URLs[0]
  88. }
  89. return nil
  90. }
  91. // Consume runs this PipelineItem on the next commit data.
  92. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  93. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  94. // This function returns the mapping with analysis results. The keys must be the same as
  95. // in Provides(). If there was an error, nil is returned.
  96. func (ticks *TicksSinceStart) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  97. commit := deps[core.DependencyCommit].(*object.Commit)
  98. index := deps[core.DependencyIndex].(int)
  99. if index == 0 {
  100. // first iteration - initialize the file objects from the tree
  101. // our precision is 1 day
  102. *ticks.tick0 = commit.Committer.When
  103. if ticks.tick0.Unix() < 631152000 { // 01.01.1990, that was 30 years ago
  104. log.Println()
  105. log.Printf("Warning: suspicious committer timestamp in %s > %s: %d",
  106. ticks.remote, commit.Hash.String(), ticks.tick0.Unix())
  107. }
  108. }
  109. tick := int(commit.Committer.When.Sub(*ticks.tick0) / ticks.tickSize)
  110. if tick < ticks.previousTick {
  111. // rebase works miracles, but we need the monotonous time
  112. tick = ticks.previousTick
  113. }
  114. ticks.previousTick = tick
  115. tickCommits := ticks.commits[tick]
  116. if tickCommits == nil {
  117. tickCommits = []plumbing.Hash{}
  118. }
  119. exists := false
  120. if commit.NumParents() > 0 {
  121. for i := range tickCommits {
  122. if tickCommits[len(tickCommits)-i-1] == commit.Hash {
  123. exists = true
  124. }
  125. }
  126. }
  127. if !exists {
  128. ticks.commits[tick] = append(tickCommits, commit.Hash)
  129. }
  130. return map[string]interface{}{DependencyTick: tick}, nil
  131. }
  132. // Fork clones this PipelineItem.
  133. func (ticks *TicksSinceStart) Fork(n int) []core.PipelineItem {
  134. return core.ForkCopyPipelineItem(ticks, n)
  135. }
  136. func init() {
  137. core.Registry.Register(&TicksSinceStart{})
  138. }