Class: Git::CommandLine
- Inherits:
-
Object
- Object
- Git::CommandLine
- Defined in:
- lib/git/command_line.rb
Overview
Runs a git command and returns the result
Constant Summary collapse
- RUN_ARGS =
{ normalize: false, chomp: false, merge: false, out: nil, err: nil, chdir: nil, timeout: nil }.freeze
Instance Attribute Summary collapse
-
#binary_path ⇒ String
readonly
The path to the command line binary to run.
-
#env ⇒ Hash<String, String>
readonly
Variables to set (or unset) in the git command's environment.
-
#global_opts ⇒ Array<String>
readonly
The global options to pass to git.
-
#logger ⇒ Logger
readonly
The logger to use for logging git commands and results.
Instance Method Summary collapse
-
#initialize(env, binary_path, global_opts, logger) ⇒ CommandLine
constructor
Create a Git::CommandLine object.
-
#run(**options_hash) ⇒ Git::CommandLineResult
Execute a git command, wait for it to finish, and return the result.
-
#run_with_capture(*args, **options_hash) ⇒ Git::CommandLineResult
private
The result of running the command.
- #run_with_capture_options(**options_hash)
Constructor Details
#initialize(env, binary_path, global_opts, logger) ⇒ CommandLine
Create a Git::CommandLine object
28 29 30 31 32 33 |
# File 'lib/git/command_line.rb', line 28 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_path ⇒ String (readonly)
The path to the command line binary to run
62 63 64 |
# File 'lib/git/command_line.rb', line 62 def binary_path @binary_path end |
#env ⇒ Hash<String, String> (readonly)
Variables to set (or unset) in the git command's environment
49 50 51 |
# File 'lib/git/command_line.rb', line 49 def env @env end |
#global_opts ⇒ Array<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].
81 82 83 |
# File 'lib/git/command_line.rb', line 81 def global_opts @global_opts end |
#logger ⇒ Logger (readonly)
The logger to use for logging git commands and results
96 97 98 |
# File 'lib/git/command_line.rb', line 96 def logger @logger end |
Instance Method Details
#run(**options_hash) ⇒ Git::CommandLineResult
Execute a git command, wait for it to finish, and return the result
Non-option the command line arguements to pass to git. If you collect the command line arguments in an array, make sure you splat the array into the parameter list.
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 Git::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.
194 195 196 197 198 199 200 201 |
# File 'lib/git/command_line.rb', line 194 def run(*, **) = RUN_ARGS.merge() = .keys - RUN_ARGS.keys raise ArgumentError, "Unknown options: #{.join(', ')}" if .any? result = run_with_capture(*, **) process_result(result, [:normalize], [:chomp], [:timeout]) end |
#run_with_capture(*args, **options_hash) ⇒ Git::CommandLineResult
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.
Returns the result of running the command.
207 208 209 210 211 212 213 |
# File 'lib/git/command_line.rb', line 207 def run_with_capture(*args, **) git_cmd = build_git_cmd(args) = (**) ProcessExecuter.run_with_capture(env, *git_cmd, **) rescue ProcessExecuter::ProcessIOError => e raise Git::ProcessIOError.new(e.), cause: e.exception.cause end |
#run_with_capture_options(**options_hash)
215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/git/command_line.rb', line 215 def (**) chdir = [:chdir] || :not_set timeout_after = [:timeout] out = [:out] err = [:err] merge_output = [:merge] || false { chdir:, timeout_after:, merge_output:, raise_errors: false }.tap do || [:out] = out unless out.nil? [:err] = err unless err.nil? end end |