day.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package hercules
  2. import (
  3. "time"
  4. "gopkg.in/src-d/go-git.v4"
  5. "gopkg.in/src-d/go-git.v4/plumbing/object"
  6. )
  7. // DaysSinceStart provides the relative date information for every commit.
  8. // It is a PipelineItem.
  9. type DaysSinceStart struct {
  10. day0 time.Time
  11. previousDay int
  12. }
  13. const (
  14. // DependencyDay is the name of the dependency which DaysSinceStart provides - the number
  15. // of days since the first commit in the analysed sequence.
  16. DependencyDay = "day"
  17. )
  18. func (days *DaysSinceStart) Name() string {
  19. return "DaysSinceStart"
  20. }
  21. func (days *DaysSinceStart) Provides() []string {
  22. arr := [...]string{DependencyDay}
  23. return arr[:]
  24. }
  25. func (days *DaysSinceStart) Requires() []string {
  26. return []string{}
  27. }
  28. func (days *DaysSinceStart) ListConfigurationOptions() []ConfigurationOption {
  29. return []ConfigurationOption{}
  30. }
  31. func (days *DaysSinceStart) Configure(facts map[string]interface{}) {}
  32. func (days *DaysSinceStart) Initialize(repository *git.Repository) {
  33. days.day0 = time.Time{}
  34. days.previousDay = 0
  35. }
  36. func (days *DaysSinceStart) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  37. commit := deps["commit"].(*object.Commit)
  38. index := deps["index"].(int)
  39. if index == 0 {
  40. // first iteration - initialize the file objects from the tree
  41. days.day0 = commit.Author.When
  42. // our precision is 1 day
  43. days.day0 = days.day0.Truncate(24 * time.Hour)
  44. }
  45. day := int(commit.Author.When.Sub(days.day0).Hours() / 24)
  46. if day < days.previousDay {
  47. // rebase works miracles, but we need the monotonous time
  48. day = days.previousDay
  49. }
  50. days.previousDay = day
  51. return map[string]interface{}{DependencyDay: day}, nil
  52. }
  53. func init() {
  54. Registry.Register(&DaysSinceStart{})
  55. }