Sha256: 61cec563bea8fb853b9bdd3bb9bb4990faa7d87200db3ab2aba42afc68d3ea75

Contents?: true

Size: 1.23 KB

Versions: 13

Compression:

Stored size: 1.23 KB

Contents

# typed: strict
# frozen_string_literal: true

module Spoom
  class Timeline
    extend T::Sig

    sig { params(context: Context, from: Time, to: Time).void }
    def initialize(context, from, to)
      @context = context
      @from = from
      @to = to
    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 = @context.git_log(
          "--since='#{t}'",
          "--until='#{t.to_date.next_month}'",
          "--format='format:%h %at'",
          "--author-date-order",
          "-1",
        )
        next if result.out.empty?

        Spoom::Git::Commit.parse_line(result.out.strip)
      end.compact.uniq(&:sha)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
spoom-1.5.0 lib/spoom/timeline.rb
spoom-1.4.2 lib/spoom/timeline.rb
spoom-1.4.1 lib/spoom/timeline.rb
spoom-1.4.0 lib/spoom/timeline.rb
spoom-1.3.3 lib/spoom/timeline.rb
spoom-1.3.2 lib/spoom/timeline.rb
spoom-1.3.1 lib/spoom/timeline.rb
spoom-1.3.0 lib/spoom/timeline.rb
spoom-1.2.4 lib/spoom/timeline.rb
spoom-1.2.3 lib/spoom/timeline.rb
spoom-1.2.2 lib/spoom/timeline.rb
spoom-1.2.1 lib/spoom/timeline.rb
spoom-1.2.0 lib/spoom/timeline.rb