Sha256: d4d427e05421b2ed8374bb266a1bf289a5ff26b89584137836928d042b057116

Contents?: true

Size: 1.26 KB

Versions: 5

Compression:

Stored size: 1.26 KB

Contents

# typed: strict
# frozen_string_literal: true

require_relative "git"

module Spoom
  class Timeline
    extend T::Sig

    sig { params(from: Time, to: Time, path: String).void }
    def initialize(from, to, path: ".")
      @from = from
      @to = to
      @path = path
    end

    # Return one commit for each month between `from` and `to`
    sig { returns(T::Array[Git::Commit]) }
    def ticks
      commits_for_dates(months)
    end

    # Return all months between `from` and `to`
    sig { returns(T::Array[Time]) }
    def months
      d = Date.new(@from.year, @from.month, 1)
      to = Date.new(@to.year, @to.month, 1)
      res = [d.to_time]
      while d < to
        d = d.next_month
        res << d.to_time
      end
      res
    end

    # Return one commit for each date in `dates`
    sig { params(dates: T::Array[Time]).returns(T::Array[Git::Commit]) }
    def commits_for_dates(dates)
      dates.map do |t|
        result = Spoom::Git.log(
          "--since='#{t}'",
          "--until='#{t.to_date.next_month}'",
          "--format='format:%h %at'",
          "--author-date-order",
          "-1",
          path: @path,
        )
        next if result.out.empty?

        Git.parse_commit(result.out.strip)
      end.compact.uniq(&:sha)
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
spoom-1.1.16 lib/spoom/timeline.rb
devcycle-ruby-server-sdk-2.0.0 vendor/bundle/ruby/3.0.0/gems/spoom-1.1.15/lib/spoom/timeline.rb
spoom-1.1.15 lib/spoom/timeline.rb
spoom-1.1.14 lib/spoom/timeline.rb
spoom-1.1.13 lib/spoom/timeline.rb