Class: RubyGit::GitBinary

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_git/git_binary.rb

Overview

Sets and tracks the path to a git executable and reports the version

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ GitBinary

Return a new GitBinary object

Examples:

GitBinary.new


14
15
16
# File 'lib/ruby_git/git_binary.rb', line 14

def initialize(path = nil)
  @path = Pathname.new(path) unless path.nil?
end

Class Method Details

.default_path(basename: 'git') ⇒ Pathname

Get the default path to to a git binary by searching the PATH

Examples:

Find the pathname to super_git

git = RubyGit::GitBinary.new
git.path = git.default_path(basename: 'super_git')

Parameters:

  • basename (String) (defaults to: 'git')

    The basename of the git command

Returns:

  • (Pathname)

    the path to the git binary found in the path

Raises:

  • (RuntimeError)

    if either PATH is not set or an executable file basename was not found on the path.



69
70
71
# File 'lib/ruby_git/git_binary.rb', line 69

def self.default_path(basename: 'git')
  RubyGit::FileHelpers.which(basename) || raise("Could not find '#{basename}' in the PATH.")
end

Instance Method Details

#pathPathname

Retrieve the path to the git binary

Examples:

Get the git found on the PATH

git = RubyGit::GitBinary.new
path = git.path

Returns:

  • (Pathname)

    the path to the git binary

Raises:

  • (RuntimeError)

    if path was not set via path= and either PATH is not set or git was not found on the path.



52
53
54
# File 'lib/ruby_git/git_binary.rb', line 52

def path
  @path || (@path = self.class.default_path)
end

#path=(path) ⇒ Pathname

Sets the path to the git binary

The given path must point to an executable file or a RuntimeError is raised.

Examples:

Setting the path to the git binary

git.path = '/usr/local/bin/git'

Parameters:

  • path (String)

    the path to a git executable

Returns:

  • (Pathname)

Raises:

  • (RuntimeError)

    A RuntimeError is raised when the path does not refer to an existing executable file.



32
33
34
35
36
37
38
39
# File 'lib/ruby_git/git_binary.rb', line 32

def path=(path)
  new_path = Pathname.new(path)
  raise "'#{new_path}' does not exist." unless new_path.exist?
  raise "'#{new_path}' is not a file." unless new_path.file?
  raise "'#{new_path}' is not executable." unless new_path.executable?

  @path = new_path
end

#to_sString

Return the path as a string

Examples:

git = RubyGit::GitBinary.new('/usr/bin/git')
git.to_s
 => '/usr/bin/git'

Returns:

  • (String)

    the path to the binary

Raises:

  • (RuntimeError)

    if path was not set via path= and either PATH is not set or git was not found on the path.



102
103
104
# File 'lib/ruby_git/git_binary.rb', line 102

def to_s
  path.to_s
end

#versionArray<Integer>

The version of git referred to by the path

Examples:

for version 2.28.0

git = RubyGit::GitBinary.new
puts git.version #=> [2,28,0]

Returns:

  • (Array<Integer>)

    an array of integers representing the version.

Raises:

  • (RuntimeError)

    if path was not set via path= and either PATH is not set or git was not found on the path.



84
85
86
87
88
# File 'lib/ruby_git/git_binary.rb', line 84

def version
  output = `#{path} --version`
  version = output[/\d+\.\d+(\.\d+)+/]
  version.split('.').collect(&:to_i)
end