Module: RubyGit::CommandLine

Defined in:
lib/ruby_git/command_line.rb,
lib/ruby_git/command_line/result.rb,
lib/ruby_git/command_line/runner.rb,
lib/ruby_git/command_line/options.rb,
lib/ruby_git/command_line/encoding_normalizer.rb

Overview

Runs a git command and returns the result

Defined Under Namespace

Modules: EncodingNormalizer Classes: Options, Result, Runner

Class Method Summary collapse

Class Method Details

.binary_pathString

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 git binary

Returns:

  • (String)


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

def self.binary_path = RubyGit.binary_path

.envHash<String, String>

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 environment variables that will be set for all git commands

Returns:

  • (Hash<String, String>)


55
56
57
58
59
60
61
62
63
# File 'lib/ruby_git/command_line.rb', line 55

def self.env
  {
    'GIT_DIR' => nil,
    'GIT_WORK_TREE' => nil,
    'GIT_INDEX_FILE' => nil,
    # 'GIT_SSH' => Git::Base.config.git_ssh,
    'LC_ALL' => 'en_US.UTF-8'
  }
end

.global_options(repository_path:, worktree_path:) ⇒ Array<String>

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 that will be set for all git commands

Returns:

  • (Array<String>)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ruby_git/command_line.rb', line 73

def self.global_options(repository_path:, worktree_path:) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  [].tap do |global_opts|
    global_opts << "--git-dir=#{repository_path}" unless repository_path.nil?
    global_opts << "--work-tree=#{worktree_path}" unless worktree_path.nil?
    global_opts << '-c' << 'core.quotePath=true'
    global_opts << '-c' << 'color.ui=false'
    global_opts << '-c' << 'color.advice=false'
    global_opts << '-c' << 'color.diff=false'
    global_opts << '-c' << 'color.grep=false'
    global_opts << '-c' << 'color.push=false'
    global_opts << '-c' << 'color.remote=false'
    global_opts << '-c' << 'color.showBranch=false'
    global_opts << '-c' << 'color.status=false'
    global_opts << '-c' << 'color.transport=false'
  end
end

.loggerLogger

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

Returns:

  • (Logger)


93
# File 'lib/ruby_git/command_line.rb', line 93

def self.logger = RubyGit.logger

.run(*args, repository_path: nil, worktree_path: nil, **options) ⇒ RubyGit::CommandLine::Result

Run a git command

Examples:

A simple example

RubyGit::CommandLine.run('version') #=> outputs "git version 2.30.2\n" to stdout

Capture stdout

command = %w[version]
options = { out: StringIO.new }
result = RubyGit::CommandLine.run(*command, **options) #=> #<Process::Status: pid 21742 exit 0>
result.stdout #=> "git version 2.30.2\n"

A more complex example

command = %w[rev-parse --show-toplevel]
options = { chdir: worktree_path, chomp: true, out: StringIO.new, err: StringIO.new }
RubyGit::CommandLine.run(*command, **options).stdout #=> "/path/to/working/tree"

Parameters:

  • args (Array<String>)

    the git command and it arguments

  • repository_path (String) (defaults to: nil)

    the path to the git repository

  • worktree_path (String) (defaults to: nil)

    the path to the working tree

  • options (Hash<Symbol, Object>)

    options to pass to the command line runner

Returns:

Raises:



42
43
44
45
46
47
48
49
50
# File 'lib/ruby_git/command_line.rb', line 42

def self.run(*args, repository_path: nil, worktree_path: nil, **options)
  runner = RubyGit::CommandLine::Runner.new(
    env,
    binary_path,
    global_options(repository_path:, worktree_path:),
    logger
  )
  runner.call(*args, **options)
end