Class: Chandler::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/chandler/git.rb

Overview

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

Constant Summary collapse

Error =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, tag_mapper:) ⇒ Git

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”)



17
18
19
20
# File 'lib/chandler/git.rb', line 17

def initialize(path:, tag_mapper:)
  @path = path
  @tag_mapper = tag_mapper
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/chandler/git.rb', line 10

def path
  @path
end

#tag_mapperObject (readonly)

Returns the value of attribute tag_mapper.



10
11
12
# File 'lib/chandler/git.rb', line 10

def tag_mapper
  @tag_mapper
end

Instance Method Details

#origin_remoteObject

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

origin_remote # => “[email protected]:mattbrictson/chandler.git”



40
41
42
43
# 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_tagsObject

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”]



27
28
29
30
31
32
33
# 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