Class: RubyGit::CommandLine::Runner

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

Overview

Runs the git command line and returns the result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, binary_path, global_options, logger) ⇒ Runner

Create a an object to run git commands via the command line

Examples:

env = { 'GIT_DIR' => '/path/to/git/dir' }
binary_path = '/usr/bin/git'
global_options = %w[--git-dir /path/to/git/dir]
logger = Logger.new(STDOUT)
cli = CommandLine.new(env, binary_path, global_options, logger)
cli.run('version') #=> #<RubyGit::CommandLineResult:0x00007f9b0c0b0e00

Parameters:

  • env (Hash<String, String>)

    environment variables to set

  • binary_path (String)

    the path to the git binary

  • global_options (Array<String>)

    global options to pass to git

  • logger (Logger)

    the logger to use



32
33
34
35
36
37
# File 'lib/ruby_git/command_line/runner.rb', line 32

def initialize(env, binary_path, global_options, logger)
  @env = env
  @binary_path = binary_path
  @global_options = global_options
  @logger = logger
end

Instance Attribute Details

#binary_pathString (readonly)

The path to the command line binary to run

Examples:

binary_path = '/usr/bin/git'
command_line = RubyGit::CommandLine.new({}, binary_path, ['version'], Logger.new(STDOUT))
command_line.binary_path #=> '/usr/bin/git'

Returns:

  • (String)


66
67
68
# File 'lib/ruby_git/command_line/runner.rb', line 66

def binary_path
  @binary_path
end

#envHash<String, String> (readonly)

Variables to set (or unset) in the git command's environment

Examples:

env = { 'GIT_DIR' => '/path/to/git/dir' }
command_line = RubyGit::CommandLine.new(env, '/usr/bin/git', [], Logger.new(STDOUT))
command_line.env #=> { 'GIT_DIR' => '/path/to/git/dir' }

Returns:

  • (Hash<String, String>)

See Also:



53
54
55
# File 'lib/ruby_git/command_line/runner.rb', line 53

def env
  @env
end

#global_optionsArray<String> (readonly)

The global options to pass to git

These are options that are passed to git before the command name and arguments. For example, in git --git-dir /path/to/git/dir version, the global options are %w[--git-dir /path/to/git/dir].

Examples:

env = {}
global_options = %w[--git-dir /path/to/git/dir]
logger = Logger.new(nil)
cli = CommandLine.new(env, '/usr/bin/git', global_options, logger)
cli.global_options #=> %w[--git-dir /path/to/git/dir]

Returns:

  • (Array<String>)


85
86
87
# File 'lib/ruby_git/command_line/runner.rb', line 85

def global_options
  @global_options
end

#loggerLogger (readonly)

The logger to use for logging git commands and results

Examples:

env = {}
global_options = %w[]
logger = Logger.new(STDOUT)
cli = CommandLine.new(env, '/usr/bin/git', global_options, logger)
cli.logger == logger #=> true

Returns:

  • (Logger)


100
101
102
# File 'lib/ruby_git/command_line/runner.rb', line 100

def logger
  @logger
end

Instance Method Details

#call(*args, **options_hash) ⇒ RubyGit::CommandLine::Result

Execute a git command, wait for it to finish, and return the result

NORMALIZATION

The command output is returned as a Unicde string containing the binary output from the command. If the binary output is not valid UTF-8, the output will cause problems because the encoding will be invalid.

Normalization is a process that trys to convert the binary output to a valid UTF-8 string. It uses the rchardet gem to detect the encoding of the binary output and then converts it to UTF-8.

Normalization is not enabled by default. Pass normalize: true to RubyGit::CommandLine#run to enable it. Normalization will only be performed on stdout and only if the out:` option is nil or is a StringIO object. If the out: option is set to a file or other IO object, the normalize option will be ignored.

Examples:

Run a command and return the output

cli.run('version') #=> "git version 2.39.1\n"

The args array should be splatted into the parameter list

args = %w[log -n 1 --oneline]
cli.run(*args) #=> "f5baa11 beginning of Ruby/Git project\n"

Run a command and return the chomped output

cli.run('version', chomp: true) #=> "git version 2.39.1"

Run a command and without normalizing the output

cli.run('version', normalize: false) #=> "git version 2.39.1\n"

Capture stdout in a temporary file

require 'tempfile'
tempfile = Tempfile.create('git') do |file|
  cli.run('version', out: file)
  file.rewind
  file.read #=> "git version 2.39.1\n"
end

Capture stderr in a StringIO object

require 'stringio'
stderr = StringIO.new
begin
  cli.run('log', 'nonexistent-branch', err: stderr)
rescue RubyGit::FailedError => e
  stderr.string #=> "unknown revision or path not in the working tree.\n"
end

Parameters:

  • args (Array<String>)

    the command line arguements to pass to git

    This array should be splatted into the parameter list.

  • options_hash (Hash)

    the options to initialize Options

Returns:

Raises:



167
168
169
170
171
# File 'lib/ruby_git/command_line/runner.rb', line 167

def call(*args, **options_hash)
  options = RubyGit::CommandLine::Options.new(logger: logger, **options_hash)
  result = run(*args, options)
  process_result(result, options)
end