class Schmersion::Repo

Attributes

path[R]
repo[R]

Public Class Methods

new(path) click to toggle source
# File lib/schmersion/repo.rb, line 18
def initialize(path)
  @path = path
  @repo = Git.open(path)
rescue ArgumentError => e
  if e.message =~ /path does not exist/
    raise Error, "No git repository found at #{path}"
  end

  raise
end

Public Instance Methods

commits(start_commit, end_commit, **options) click to toggle source
# File lib/schmersion/repo.rb, line 86
def commits(start_commit, end_commit, **options)
  parser = CommitParser.new(@repo, start_commit, end_commit, **options)
  parser.commits
end
config() click to toggle source
# File lib/schmersion/repo.rb, line 33
def config
  @config ||= load_config(['.schmersion.yaml', '.schmersion.yml'])
end
current_branch() click to toggle source
# File lib/schmersion/repo.rb, line 91
def current_branch
  @repo.branch.name
end
host() click to toggle source
# File lib/schmersion/repo.rb, line 41
def host
  return nil if origin.nil?

  Hosts.host_for_url(origin)
end
origin() click to toggle source
# File lib/schmersion/repo.rb, line 37
def origin
  @origin ||= @repo.remotes.find { |r| r.name == 'origin' }&.url
end
path_for(*join) click to toggle source
# File lib/schmersion/repo.rb, line 29
def path_for(*join)
  File.join(@path, *join)
end
pending_version(from: nil, to: 'HEAD', **options) click to toggle source

Get the pending version for the currently checked out branch for the repository.

# File lib/schmersion/repo.rb, line 49
def pending_version(from: nil, to: 'HEAD', **options)
  options[:version_options] ||= {}

  if from.nil?
    from_version = versions.last
  else
    from_version = versions.find { |v, _| v.to_s == from }
    if from_version.nil?
      raise Error, "Could not find existing version named #{from}"
    end
  end

  if from_version
    previous_version, previous_version_commit = from_version
  end

  parser = CommitParser.new(@repo, previous_version_commit&.ref || :start, to)
  if v = options[:override_version]
    begin
      next_version = Semantic::Version.new(v)
    rescue ArgumentError => e
      if e.message =~ /not a valid SemVer/
        raise Error, "'#{v}' is not a valid version"
      end

      raise
    end
  else
    next_version = parser.next_version_after(previous_version, **options[:version_options])
  end

  [
    previous_version,
    Version.new(self, next_version, parser)
  ]
end
version?(version) click to toggle source
# File lib/schmersion/repo.rb, line 95
def version?(version)
  @repo.tag(version.to_s).is_a?(Git::Object::Tag)
rescue Git::GitTagNameDoesNotExist
  false
end
versions() click to toggle source
# File lib/schmersion/repo.rb, line 101
def versions
  versions = @repo.tags.each_with_object([]) do |tag, array|
    commit = @repo.gcommit(tag.sha)
    version = Semantic::Version.new(tag.name)
    array << [version, Commit.new(commit)]
  rescue ArgumentError => e
    raise unless e.message =~ /not a valid SemVer/
  end
  versions.sort_by { |_, c| c.date }
end

Private Instance Methods

load_config(filenames) click to toggle source
# File lib/schmersion/repo.rb, line 114
def load_config(filenames)
  filenames.each do |filename|
    path = path_for(filename)
    if File.file?(path)
      return Config.new(::YAML.load_file(path))
    end
  end

  warn 'No config file was found, using defaults'
  Config.new({})
end