day.go 4.4 KB

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