Class: Git::CommandLine::Base Abstract

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

Overview

This class is abstract.

Subclass and implement #run

Abstract base class for git command-line execution strategies

Concrete subclasses must implement #run to execute a git command and return a Result. Two implementations are provided:

  • Capturing — buffers stdout and stderr in memory
  • Streaming — streams stdout to a caller-supplied IO

Examples:

Instantiate a concrete subclass

env = { 'GIT_DIR' => '/path/to/git/dir' }
binary_path = '/usr/bin/git'
global_opts = %w[--git-dir /path/to/git/dir]
logger = Logger.new($stdout)
cli = Git::CommandLine::Capturing.new(env, binary_path, global_opts, logger)
cli.run('version') #=> #<Git::CommandLine::Result ...>

Direct Known Subclasses

Capturing, Streaming

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, binary_path, global_opts, logger) ⇒ Base

Create a Base (or subclass) object

Parameters:

  • env (Hash{String => String, nil})

    environment variables to set or unset. String values set the variable; nil values unset it.

  • binary_path (String)

    the path to the git binary

  • global_opts (Array<String>)

    global options to pass to git

  • logger (Logger)

    the logger to use



40
41
42
43
44
45
# File 'lib/git/command_line/base.rb', line 40

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

Instance Attribute Details

#binary_pathString (readonly)

The path to the command line binary to run

Examples:

cli = Git::CommandLine::Capturing.new({}, '/usr/bin/git', [], Logger.new(nil))
cli.binary_path #=> '/usr/bin/git'

Returns:

  • (String)


83
84
85
# File 'lib/git/command_line/base.rb', line 83

def binary_path
  @binary_path
end

#envHash{String => String, nil} (readonly)

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

Examples:

env = { 'GIT_DIR' => '/path/to/git/dir' }
cli = Git::CommandLine::Capturing.new(env, '/usr/bin/git', [], Logger.new(nil))
cli.env #=> { 'GIT_DIR' => '/path/to/git/dir' }

Returns:

  • (Hash{String => String, nil})

See Also:



71
72
73
# File 'lib/git/command_line/base.rb', line 71

def env
  @env
end

#global_optsArray<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:

global_opts = %w[--git-dir /path/to/git/dir]
cli = Git::CommandLine::Capturing.new({}, '/usr/bin/git', global_opts, Logger.new(nil))
cli.global_opts #=> %w[--git-dir /path/to/git/dir]

Returns:

  • (Array<String>)


100
101
102
# File 'lib/git/command_line/base.rb', line 100

def global_opts
  @global_opts
end

#loggerLogger (readonly)

The logger to use for logging git commands and results

Examples:

logger = Logger.new(nil)
cli = Git::CommandLine::Capturing.new({}, '/usr/bin/git', [], logger)
cli.logger == logger #=> true

Returns:

  • (Logger)


113
114
115
# File 'lib/git/command_line/base.rb', line 113

def logger
  @logger
end

Instance Method Details

#run

Execute a git command and return the result

Concrete subclasses must override this method.

Raises:

  • (NotImplementedError)

    always — must be implemented by subclasses



53
54
55
# File 'lib/git/command_line/base.rb', line 53

def run(*)
  raise NotImplementedError, "#{self.class}#run is not implemented"
end