Sha256: a1db105fa048a6c4dc4c99d40ae5d0629c43e1bdf5f45fe24785c11d2d0eeb2c

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module Groundskeeper
  # A Git repository.
  class Repository
    MAJOR = "major"
    MINOR = "minor"
    PATCH = "patch"
    SEMANTIC_RELEASE_TYPE = { M: MAJOR, m: MINOR, p: PATCH }.freeze
    # git output matching
    ON_BRANCH_MASTER = "On branch master"
    UP_TO_DATE = "Your branch is up-to-date with 'origin/master'"
    UNSTAGED_CHANGES = "Changes not staged for commit"
    # git comment matching
    COMMIT_HASH = /^[^ ]+ /
    RELEASE_MESSAGE = "release version %s"

    attr_reader :git

    def initialize(git: nil)
      @git = git || Git.build
    end

    def clean_directory?
      on_branch_master? && up_to_date? && !unstaged_changes?
    end

    def on_branch_master?
      git.status.include? ON_BRANCH_MASTER
    end

    def up_to_date?
      git.status.include? UP_TO_DATE
    end

    def unstaged_changes?
      git.status.include? UNSTAGED_CHANGES
    end

    def status
      @status ||= begin
        git.fetch

        git.status
      end
    end

    def changes_since_latest_tag
      git
        .raw_changes_since_latest_tag
        .map { |c| c.gsub(COMMIT_HASH, "") }
        .reject { |c| c.match(/#{format(RELEASE_MESSAGE, "")}/) }
    end

    # rubocop:disable Metrics/MethodLength
    def bumped_semantic_version(type)
      major, minor, patch = git
                            .latest_tag_name_across_branches
                            .split(".")
                            .map(&:to_i)

      case SEMANTIC_RELEASE_TYPE[type.to_sym]
      when MAJOR
        "#{major + 1}.0.0"
      when MINOR
        "#{major}.#{minor + 1}.0"
      when PATCH
        "#{major}.#{minor}.#{patch + 1}"
      end
    end
    # rubocop:enable Metrics/MethodLength

    def name
      `pwd`.split("/").last.chop
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
groundskeeper-bitcore-0.2.0 lib/groundskeeper/repository.rb
groundskeeper-bitcore-0.1.1 lib/groundskeeper/repository.rb