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

Overview

Runs a git command and returns the result

API:

  • public

Defined Under Namespace

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:

API:

  • private



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:

API:

  • private



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:

API:

  • private



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:

API:

  • private



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:

  • the git command and it arguments

  • (defaults to: nil)

    the path to the git repository

  • (defaults to: nil)

    the path to the working tree

  • options to pass to the command line runner

Returns:

  • the result of running the command

Raises:

  • if the command fails for any of the following reasons

  • if the command returns with non-zero exitstatus

  • if the command times out

  • if the command terminates due to an uncaught signal

  • if an exception is raised while collecting subprocess output

API:

  • public



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