Sha256: 6ca6be61bef9166e410b463864b73e23d64a18017354bf637335f52411addadb

Contents?: true

Size: 1.2 KB

Versions: 14

Compression:

Stored size: 1.2 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[String]) }
    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[String]) }
    def commits_for_dates(dates)
      dates.map do |t|
        out, _, _ = Spoom::Git.log(
          "--since='#{t}'",
          "--until='#{t.to_date.next_month}'",
          "--format='format:%h'",
          "--author-date-order",
          "-1",
          path: @path,
        )
        next if out.empty?
        out
      end.compact.uniq
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
spoom-1.1.8 lib/spoom/timeline.rb
spoom-1.1.7 lib/spoom/timeline.rb
spoom-1.1.6 lib/spoom/timeline.rb
spoom-1.1.5 lib/spoom/timeline.rb
spoom-1.1.4 lib/spoom/timeline.rb
spoom-1.1.3 lib/spoom/timeline.rb
spoom-1.1.2 lib/spoom/timeline.rb
spoom-1.1.1 lib/spoom/timeline.rb
spoom-1.1.0 lib/spoom/timeline.rb
spoom-1.0.9 lib/spoom/timeline.rb
spoom-1.0.8 lib/spoom/timeline.rb
spoom-1.0.7 lib/spoom/timeline.rb
spoom-1.0.6 lib/spoom/timeline.rb
spoom-1.0.5 lib/spoom/timeline.rb