Class: Git::CommandLine::Base Abstract Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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)

    used to log git commands and their results



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

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)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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)


80
81
82
# File 'lib/git/command_line/base.rb', line 80

def binary_path
  @binary_path
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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:



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

def env
  @env
end

#global_optsArray<String> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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


95
96
97
# File 'lib/git/command_line/base.rb', line 95

def global_opts
  @global_opts
end

#loggerLogger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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)


106
107
108
# File 'lib/git/command_line/base.rb', line 106

def logger
  @logger
end

Instance Method Details

#run

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute a git command and return the result

Concrete subclasses must override this method.

Raises:

  • (NotImplementedError)

    always — must be implemented by subclasses



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

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