Class: Git::ExecutionContext Private
- Inherits:
-
Object
- Object
- Git::ExecutionContext
- Defined in:
- lib/git/execution_context.rb,
lib/git/execution_context/global.rb,
lib/git/execution_context/repository.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.
Base class for execution contexts that run git commands
An execution context bundles three concerns that together describe how and where a git command runs:
Repository scope — the public accessors
git_dir,git_work_dir,git_index_file, andgit_sshidentify which repository git targets and which SSH wrapper to use. Their values are translated intoGIT_*environment variable overrides by the privateenv_overridesmethod. Anilvalue unsets the variable (seeProcess.spawnsemantics).CLI global options — the private
global_optsmethod returns the array of git flags prepended to every invocation:--git-dir/--work-treewhen those attributes are set, plus the static options in STATIC_GLOBAL_OPTS that ensure deterministic, script-friendly output.Execution defaults — COMMAND_CAPTURING_ARG_DEFAULTS and COMMAND_STREAMING_ARG_DEFAULTS define the default values for I/O, encoding, and behavioral options (
in:,out:,normalize:,timeout:, etc.) accepted by #command_capturing and #command_streaming.
Subclasses override the repository-scope accessors to supply context-specific
values. The env_overrides and global_opts methods are implemented here and
call those accessors, so subclasses do not need to override them directly.
Concrete subclasses:
- Repository — for repository-bound commands (
add,commit, …) - Global — for commands that do not require an existing repository
(
init,clone,version)
Direct Known Subclasses
Defined Under Namespace
Classes: Global, Repository
Constant Summary collapse
- COMMAND_CAPTURING_ARG_DEFAULTS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default keyword arguments accepted by #command_capturing.
Derived from CommandLine::Capturing::RUN_OPTION_DEFAULTS with two overrides:
normalize: trueandchomp: trueso callers receive clean UTF-8 strings by default. New options added to the CommandLine layer are automatically accepted here without requiring a coordinated edit.timeout: nilis intentional — the global timeout from Git.config is applied at call-time so that changes to the config are respected. Git::CommandLine::Capturing::RUN_OPTION_DEFAULTS .merge(normalize: true, chomp: true) .freeze
- COMMAND_STREAMING_ARG_DEFAULTS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default keyword arguments accepted by #command_streaming.
Identical to CommandLine::Streaming::RUN_OPTION_DEFAULTS. Defined here so callers interact with a stable constant on this class, and so that new options added to the CommandLine layer are automatically accepted.
Git::CommandLine::Streaming::RUN_OPTION_DEFAULTS.dup.freeze
- STATIC_GLOBAL_OPTS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Static git global options applied to every invocation.
These ensure deterministic, script-friendly output regardless of the user's local git configuration.
%w[ -c core.quotePath=true -c core.editor=false -c color.ui=false -c color.advice=false -c color.diff=false -c color.grep=false -c color.push=false -c color.remote=false -c color.showBranch=false -c color.status=false -c color.transport=false ].freeze
Instance Attribute Summary collapse
-
#logger ⇒ Logger
readonly
private
Returns the logger used by this context.
Instance Method Summary collapse
-
#binary_path ⇒ String
private
Returns the resolved git binary path for this context.
-
#command_capturing(*args, **options_hash) ⇒ Git::CommandLineResult
private
Runs a git command and returns the result.
-
#command_streaming(*args, **options_hash) ⇒ Git::CommandLineResult
private
Runs a git command using the streaming (non-capturing) execution path.
-
#env_overrides(**additional_overrides) ⇒ Hash<String, String|nil>
private
Returns a Hash of environment variable overrides for this context.
-
#git_dir ⇒ String?
private
Returns the
GIT_DIRpath for this context. -
#git_index_file ⇒ String?
private
Returns the
GIT_INDEX_FILEpath for this context. -
#git_ssh ⇒ String?
private
Returns the resolved
GIT_SSHwrapper path for this context. -
#git_version(timeout: nil) ⇒ Git::Version
private
Returns the installed git version.
-
#git_work_dir ⇒ String?
private
Returns the
GIT_WORK_TREEpath for this context. -
#initialize(binary_path: :use_global_config, git_ssh: :use_global_config, logger: nil) ⇒ ExecutionContext
constructor
private
Creates a new execution context.
Constructor Details
#initialize(binary_path: :use_global_config, git_ssh: :use_global_config, logger: nil) ⇒ ExecutionContext
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.
Creates a new execution context
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/git/execution_context.rb', line 110 def initialize(binary_path: :use_global_config, git_ssh: :use_global_config, logger: nil) if instance_of?(Git::ExecutionContext) raise NotImplementedError, 'Git::ExecutionContext is an abstract base class' end raise ArgumentError, 'binary_path must not be nil' if binary_path.nil? @binary_path = binary_path @git_ssh = git_ssh @logger = logger || Logger.new(nil) end |
Instance Attribute Details
#logger ⇒ Logger (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.
Returns the logger used by this context
216 217 218 |
# File 'lib/git/execution_context.rb', line 216 def logger @logger end |
Instance Method Details
#binary_path ⇒ 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.
Returns the resolved git binary path for this context
:use_global_config is resolved to Git::Config.instance.binary_path each time a
command method is called, so runtime changes to
Git.configure { |c| c.binary_path = ... }
are reflected per command invocation.
176 177 178 179 180 |
# File 'lib/git/execution_context.rb', line 176 def binary_path return Git::Config.instance.binary_path if @binary_path == :use_global_config @binary_path end |
#command_capturing(*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.
Individual command classes (under Commands) can selectively expose
:timeout and :env and other options to their callers by declaring them as
execution options in their Arguments DSL definition and forwarding them to
this method. See Commands::Clone#call for an example of a command that
exposes :timeout.
Runs a git command and returns the result
By default, raises FailedError if the command exits with a non-zero
status. Pass raise_on_failure: false to suppress this behavior.
311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/git/execution_context.rb', line 311 def command_capturing(*, **) = COMMAND_CAPTURING_ARG_DEFAULTS.merge() [:timeout] ||= Git.config.timeout = .keys - COMMAND_CAPTURING_ARG_DEFAULTS.keys raise ArgumentError, "Unknown options: #{.join(', ')}" if .any? env = .delete(:env) raise_on_failure = .delete(:raise_on_failure) command_line_capturing.run(*, raise_on_failure: raise_on_failure, env: env, **) end |
#command_streaming(*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.
Runs a git command using the streaming (non-capturing) execution path
Unlike #command_capturing, stdout is NOT buffered in memory. It is
written only to the IO object provided via the out: option. Stderr is
captured internally via a StringIO for error diagnostics.
Use this entry point when you want to stream large output (e.g. blob content from cat-file) without creating memory pressure.
398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/git/execution_context.rb', line 398 def command_streaming(*, **) = COMMAND_STREAMING_ARG_DEFAULTS.merge() [:timeout] ||= Git.config.timeout = .keys - COMMAND_STREAMING_ARG_DEFAULTS.keys raise ArgumentError, "Unknown options: #{.join(', ')}" if .any? env = .delete(:env) raise_on_failure = .delete(:raise_on_failure) command_line_streaming.run(*, raise_on_failure: raise_on_failure, env: env, **) end |
#env_overrides(**additional_overrides) ⇒ Hash<String, String|nil>
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 a Hash of environment variable overrides for this context
Builds the standard git environment from the public accessor methods
(#git_dir, #git_work_dir, #git_index_file, #git_ssh), then
merges any per-call additional_overrides on top.
Per Process.spawn semantics, a value of nil unsets the variable.
444 445 446 447 448 449 450 451 452 453 |
# File 'lib/git/execution_context.rb', line 444 def env_overrides(**additional_overrides) { 'GIT_DIR' => git_dir, 'GIT_WORK_TREE' => git_work_dir, 'GIT_INDEX_FILE' => git_index_file, 'GIT_SSH' => git_ssh, 'GIT_EDITOR' => 'true', 'LC_ALL' => 'en_US.UTF-8' }.merge(additional_overrides) end |
#git_dir ⇒ 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.
Returns the GIT_DIR path for this context
nil means GIT_DIR will be explicitly unset in the child process
(per Process.spawn semantics — unset is not the same as inherited).
Subclasses override this to supply a repository-specific path.
133 |
# File 'lib/git/execution_context.rb', line 133 def git_dir = nil |
#git_index_file ⇒ 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.
Returns the GIT_INDEX_FILE path for this context
nil means GIT_INDEX_FILE will be explicitly unset in the child process.
157 |
# File 'lib/git/execution_context.rb', line 157 def git_index_file = nil |
#git_ssh ⇒ 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.
Returns the resolved GIT_SSH wrapper path for this context
:use_global_config is resolved to Git::Config.instance.git_ssh each time a
command method is called, so runtime changes to
Git.configure { |c| c.git_ssh = ... }
are reflected per command invocation. nil means the variable will be
explicitly unset.
200 201 202 203 204 |
# File 'lib/git/execution_context.rb', line 200 def git_ssh return Git::Config.instance.git_ssh if @git_ssh == :use_global_config @git_ssh end |
#git_version(timeout: nil) ⇒ Git::Version
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 installed git version
The result is memoized per instance. Accepts an optional timeout used only when the version has not yet been fetched for this context.
423 424 425 426 427 428 |
# File 'lib/git/execution_context.rb', line 423 def git_version(timeout: nil) @git_version ||= begin call_opts = timeout.nil? ? {} : { timeout: timeout } Git::Version.parse(Git::Commands::Version.new(self).call(**call_opts).stdout) end end |
#git_work_dir ⇒ 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.
Returns the GIT_WORK_TREE path for this context
nil means GIT_WORK_TREE will be explicitly unset in the child process.
145 |
# File 'lib/git/execution_context.rb', line 145 def git_work_dir = nil |