Sha256: 753611a58627d8cdba82e7859a70e3ce47233cfe287bdc135654f1db9f4ed40a

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require "versionaire"

module Milestoner
  module Commits
    # Retrieves and categorizes Git repository commit tagged or untagged history.
    class Categorizer
      include Import[:git]

      def initialize expression: Regexp, **dependencies
        super(**dependencies)
        @expression = expression
      end

      def call configuration = Container[:configuration]
        prefixes = configuration.prefixes

        prefixes.reduce({}) { |group, prefix| group.merge prefix => [] }
                .merge("Unknown" => [])
                .then { |groups| group_by_prefix prefixes, groups }
                .each_value { |commits| commits.sort_by!(&:subject) }
                .values
                .flatten
                .uniq(&:subject)
      end

      private

      attr_reader :expression

      def group_by_prefix prefixes, groups
        computed_commits.each.with_object groups do |commit, collection|
          prefix = commit.subject[subject_pattern(prefixes)]
          key = collection.key?(prefix) ? prefix : "Unknown"
          collection[key] << commit
        end
      end

      def subject_pattern prefixes
        prefixes.empty? ? expression.new(//) : expression.union(prefixes)
      end

      def computed_commits = git.tagged? ? tagged_commits : saved_commits

      def tagged_commits
        git.tag_last
           .value_or(nil)
           .then { |tag| git.commits("#{tag}..HEAD") }
           .value_or([])
      end

      def saved_commits = git.commits.value_or([])
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
milestoner-15.2.0 lib/milestoner/commits/categorizer.rb
milestoner-15.1.0 lib/milestoner/commits/categorizer.rb
milestoner-15.0.0 lib/milestoner/commits/categorizer.rb