class Chandler::Git

Uses the shell to execute git commands against a given .git directory.

Constants

Error

Attributes

path[R]
tag_mapper[R]

Public Class Methods

new(path:, tag_mapper:) click to toggle source

Initializes the Git object with the path to the `.git` directory of the desired git repository.

Chandler::Git.new(:path => “/path/to/my/project/.git”)

# File lib/chandler/git.rb, line 17
def initialize(path:, tag_mapper:)
  @path = path
  @tag_mapper = tag_mapper
end

Public Instance Methods

origin_remote() click to toggle source

Uses `git remote -v` to list the remotes and returns the URL of the first one labeled “origin”.

origin_remote # => “git@github.com:mattbrictson/chandler.git”

# File lib/chandler/git.rb, line 40
def origin_remote
  origin = git("remote", "-v").lines.grep(/^origin\s/).first
  origin && origin.split[1]
end
version_tags() click to toggle source

Uses `git tag -l` to obtain the list of tags, then returns the subset of those tags that appear to be version numbers.

version_tags # => [“v0.0.1”, “v0.2.0”, “v0.2.1”, “v0.3.0”]

# File lib/chandler/git.rb, line 27
def version_tags
  tags = git("tag", "-l").lines.map(&:strip).select do |tag|
    version_part = tag_mapper.call(tag)
    version_part && version_part.version?
  end
  tags.sort_by { |t| Gem::Version.new(tag_mapper.call(t).version_number) }
end

Private Instance Methods

capture(*args) click to toggle source
# File lib/chandler/git.rb, line 51
def capture(*args)
  out, err, status = Open3.capture3(*args)
  return out if status.success?

  message = "Failed to execute: #{args.join(' ')}"
  message << "\n#{err}" unless err.nil?
  raise Error, message
end
git(*args) click to toggle source
# File lib/chandler/git.rb, line 47
def git(*args)
  capture("git", "--git-dir", path, *args)
end