Class: Git::ExecutionContext::Repository Private

Inherits:
Git::ExecutionContext show all
Defined in:
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.

Execution context for repository-bound git commands

Manages the git environment for commands that operate within an existing repository — setting GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE, and GIT_SSH — and prepending --git-dir / --work-tree to every git invocation via #global_opts.

Construction

Prefer the factory class methods over new when building from a Base object or a hash:

context = Git::ExecutionContext::Repository.from_base(base) context = Git::ExecutionContext::Repository.from_hash(repository: '/repo/.git', ...)

Examples:

Build from a Git::Base object

context = Git::ExecutionContext::Repository.from_base(git)

Build from a configuration hash

context = Git::ExecutionContext::Repository.from_hash(
  repository: '/path/to/.git',
  working_directory: '/path/to'
)

Constant Summary

Constants inherited from Git::ExecutionContext

COMMAND_CAPTURING_ARG_DEFAULTS, COMMAND_STREAMING_ARG_DEFAULTS, STATIC_GLOBAL_OPTS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Git::ExecutionContext

#binary_path, #command_capturing, #command_streaming, #git_ssh, #git_version

Constructor Details

#initialize(git_dir:, git_work_dir: nil, git_index_file: nil, base_object: nil, binary_path: :use_global_config, git_ssh: :use_global_config, logger: nil) ⇒ Repository

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 repository execution context

Examples:

Create with required git_dir

Git::ExecutionContext::Repository.new(git_dir: '/path/to/.git')

Parameters:

  • git_dir (String, nil)

    path to the .git directory

  • git_work_dir (String, nil) (defaults to: nil)

    path to the working tree

  • git_index_file (String, nil) (defaults to: nil)

    path to the index file

  • base_object (Git::Base, nil) (defaults to: nil)

    originating base object

  • binary_path (String, :use_global_config) (defaults to: :use_global_config)

    path to the git binary

    Give :use_global_config (the default) to use Git::Base.config.binary_path.

    Passing nil raises ArgumentError — there is no "unset the binary" semantic.

  • git_ssh (String, nil, :use_global_config) (defaults to: :use_global_config)

    the SSH wrapper path

    Give nil to unset GIT_SSH, or :use_global_config (default) to use Git::Base.config.git_ssh.

  • logger (Logger, nil) (defaults to: nil)

    the logger to use in the CommandLine layer

    Give nil to use a null logger (Logger.new(nil)).

Raises:

  • (ArgumentError)

    if binary_path is nil



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/git/execution_context/repository.rb', line 118

def initialize( # rubocop:disable Metrics/ParameterLists
  git_dir:,
  git_work_dir: nil,
  git_index_file: nil,
  base_object: nil,
  binary_path: :use_global_config,
  git_ssh: :use_global_config,
  logger: nil
)
  super(binary_path: binary_path, git_ssh: git_ssh, logger: logger)
  @git_dir = git_dir
  @git_work_dir = git_work_dir
  @git_index_file = git_index_file
  @base_object = base_object
end

Instance Attribute Details

#base_objectGit::Base? (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 originating base object.

Returns:

  • (Git::Base, nil)

    originating base object



144
145
146
# File 'lib/git/execution_context/repository.rb', line 144

def base_object
  @base_object
end

#git_dirString? (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 path to the .git directory.

Returns:

  • (String, nil)

    path to the .git directory



135
136
137
# File 'lib/git/execution_context/repository.rb', line 135

def git_dir
  @git_dir
end

#git_index_fileString? (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 path to the index file.

Returns:

  • (String, nil)

    path to the index file



141
142
143
# File 'lib/git/execution_context/repository.rb', line 141

def git_index_file
  @git_index_file
end

#git_work_dirString? (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 path to the working tree.

Returns:

  • (String, nil)

    path to the working tree



138
139
140
# File 'lib/git/execution_context/repository.rb', line 138

def git_work_dir
  @git_work_dir
end

Class Method Details

.from_base(base_object, logger: nil) ⇒ Git::ExecutionContext::Repository

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 Repository context from a Base instance

Examples:

Build from a base object

git = Git.open('/path/to/repo')
context = Git::ExecutionContext::Repository.from_base(git)

Parameters:

  • base_object (Git::Base)

    the base Git object to derive context from

  • logger (Logger, nil) (defaults to: nil)

    logger forwarded to the CommandLine layer; nil uses a null logger (see Git::ExecutionContext#initialize)

Returns:



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/git/execution_context/repository.rb', line 47

def self.from_base(base_object, logger: nil)
  new(
    git_dir: base_object.repo.to_s,
    git_index_file: base_object.index&.to_s,
    git_work_dir: base_object.dir&.to_s,
    base_object: base_object,
    git_ssh: base_object.git_ssh,
    binary_path: base_object.binary_path,
    logger: logger
  )
end

.from_hash(base_hash, logger: nil) ⇒ Git::ExecutionContext::Repository

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 Repository context from a Hash

Expected keys: :repository, :working_directory, :index, :git_ssh, :binary_path

Examples:

Build from a configuration hash

context = Git::ExecutionContext::Repository.from_hash(
  repository: '/path/to/.git',
  working_directory: '/path/to'
)

Parameters:

  • base_hash (Hash)

    the hash of repository configuration values

  • logger (Logger, nil) (defaults to: nil)

    logger forwarded to the CommandLine layer; nil uses a null logger (see Git::ExecutionContext#initialize)

Returns:



77
78
79
80
81
82
83
84
85
86
# File 'lib/git/execution_context/repository.rb', line 77

def self.from_hash(base_hash, logger: nil)
  new(
    git_dir: base_hash[:repository],
    git_index_file: base_hash[:index],
    git_work_dir: base_hash[:working_directory],
    git_ssh: base_hash.key?(:git_ssh) ? base_hash[:git_ssh] : :use_global_config,
    binary_path: base_hash.key?(:binary_path) ? base_hash[:binary_path] : :use_global_config,
    logger: logger
  )
end