Module: Git::Repository::ContextHelpers Private
- Included in:
- Git::Repository
- Defined in:
- lib/git/repository/context_helpers.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Facade methods for block-based directory and index context helpers
The with_* helpers build a second Git::Repository bound to a different
index file or working directory, yield it, and leave the receiver bound to
its original index and working directory. Nothing on the receiver changes,
so nothing on it is restored when the block exits. The working directory
variants change the process working directory for the duration of the
block and change it back afterward. The temporary variants remove the
scratch directory they created, even if the block raises an exception.
The second repository is built with self.class.new(execution_context:),
so a subclass of Git::Repository must accept that constructor call for
the helpers to yield an instance of the subclass.
Each with_* helper requires a block that declares a positional parameter
for the yielded repository and raises ArgumentError otherwise. A block with no
positional parameter is the v5.x form, which relied on the receiver being
rebound; failing fast keeps that form from running against the original
index or working directory.
#set_index and #set_working are deprecated and warn on every call through Deprecation; UPGRADING.md explains why and shows the replacements.
Included by Git::Repository.
Instance Method Summary collapse
-
#chdir {|dir| ... } ⇒ Object
private
Changes the current working directory to the repository working directory for the duration of the block.
-
#set_index(index_file, must_exist: true)
deprecated
private
Deprecated.
Use
Git.open(dir, index: path)orGit.bare(git_dir, index: path)to open a repository bound to the index, or #with_index to bind a copy for the duration of a block. Will be removed in a future major release. -
#set_working(work_dir, must_exist: true)
deprecated
private
Deprecated.
Use
Git.open(work_dir, repository: git_dir)to open a repository bound to the working tree, addingindex:when the receiver's index was custom, or #with_working to bind a copy for the duration of a block. Will be removed in a future major release. -
#with_index(new_index) {|repo| ... } ⇒ Object
private
Yields a repository bound to
new_indexin place of the receiver's index. -
#with_temp_index {|repo| ... } ⇒ Object
private
Yields a repository bound to a new temporary index file, then removes the file.
-
#with_temp_working {|repo| ... } ⇒ Object
private
Yields a repository bound to a new temporary working directory, then removes the directory and its contents.
-
#with_working(work_dir) {|repo| ... } ⇒ Object
private
Yields a repository bound to
work_dirin place of the receiver's working directory.
Instance Method Details
#chdir {|dir| ... } ⇒ Object
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.
Changes the current working directory to the repository working directory for the duration of the block
78 79 80 81 82 |
# File 'lib/git/repository/context_helpers.rb', line 78 def chdir raise ArgumentError, 'cannot chdir: repository has no working directory (bare repository)' if dir.nil? context_helpers_chdir(dir.to_s) { yield dir } end |
#set_index(index_file, must_exist: true)
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.
Use Git.open(dir, index: path) or
Git.bare(git_dir, index: path) to open a repository bound to the
index, or #with_index to bind a copy for the duration of a block.
Will be removed in a future major release.
This method returns an undefined value.
Sets the git index to index_file and rebuilds the execution context
By default raises if index_file does not exist. Pass must_exist: false to skip the existence check (useful when the index will be
created by git later).
Every call warns through Deprecation; see Git::Repository::ContextHelpers.
293 294 295 296 297 298 |
# File 'lib/git/repository/context_helpers.rb', line 293 def set_index(index_file, must_exist: true) Git::Deprecation.warn(SET_INDEX_DEPRECATION) new_path = context_helpers_validate_path(index_file, must_exist) @execution_context = @execution_context.dup_with(git_index_file: new_path.to_s) nil end |
#set_working(work_dir, must_exist: true)
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.
Use Git.open(work_dir, repository: git_dir) to open a
repository bound to the working tree, adding index: when the
receiver's index was custom, or #with_working to bind a copy for
the duration of a block. Will be removed in a future major release.
This method returns an undefined value.
Sets the git working directory to work_dir and rebuilds the execution
context
By default raises if work_dir does not exist. Pass must_exist: false to skip the existence check.
Every call warns through Deprecation; see Git::Repository::ContextHelpers.
set_working keeps the receiver's index. Git.open with :repository
alone resolves the index to <git_dir>/index, so pass :index as well
whenever the receiver's index was custom.
337 338 339 340 341 342 |
# File 'lib/git/repository/context_helpers.rb', line 337 def set_working(work_dir, must_exist: true) Git::Deprecation.warn(SET_WORKING_DEPRECATION) new_path = context_helpers_validate_path(work_dir, must_exist) @execution_context = @execution_context.dup_with(git_work_dir: new_path.to_s) nil end |
#with_index(new_index) {|repo| ... } ⇒ Object
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.
Yields a repository bound to new_index in place of the receiver's index
Yields a second repository bound to new_index; its working directory
and git directory are the receiver's. The receiver is not changed, so
calls on it inside the block still use the original index.
111 112 113 114 115 |
# File 'lib/git/repository/context_helpers.rb', line 111 def with_index(new_index, &block) context_helpers_require_repository_block(block) new_path = context_helpers_validate_path(new_index, false) yield context_helpers_derived_repository(git_index_file: new_path.to_s) end |
#with_temp_index {|repo| ... } ⇒ Object
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 yielded repository stays bound to the temporary index path after the directory that held it is removed. Do not let it, or an object that holds it and later runs commands against the index, escape the block. Git treats the missing index file, even with its directory gone, as an empty index, so reads such as StatusOperations#ls_files return nothing and StatusOperations#status_info reports every tracked file both as deleted and as untracked; writes such as Staging#add raise Error. Reads from the object database, such as Object::AbstractObject#contents, still work.
Yields a repository bound to a new temporary index file, then removes the file
The temporary index file does not exist until git creates it on first write. A unique temporary directory is created to hold the index path, avoiding the risk of presenting an empty file to git (which git would reject as a corrupt index). The directory, and any files inside it, are removed unconditionally after the block exits, even if the block raises an exception. The yielded repository is bound as #with_index binds it.
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/git/repository/context_helpers.rb', line 159 def with_temp_index(&block) context_helpers_require_repository_block(block) # Use a unique temp directory so the index file path is collision-free # and does not exist until git writes it. An existing empty file would # be treated as a corrupt index by git. temp_dir = context_helpers_mktmpdir('git-temp-index-') begin with_index(File.join(temp_dir, 'index'), &block) ensure FileUtils.remove_entry(temp_dir, true) end end |
#with_temp_working {|repo| ... } ⇒ Object
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 yielded repository stays bound to the temporary working directory after it is removed. Do not let it, or an object that holds it and later runs commands against the working tree, escape the block: calls that read or write the working tree then raise Error. Reads from the object database, such as Object::AbstractObject#contents, still work.
Yields a repository bound to a new temporary working directory, then removes the directory and its contents
The process working directory is changed to the temporary directory for the duration of the block, as #with_working does, so this method is not safe to call from more than one thread at a time. The directory is removed unconditionally after the block exits, even if the block raises an exception. The yielded repository is bound as #with_working binds it.
254 255 256 257 258 259 |
# File 'lib/git/repository/context_helpers.rb', line 254 def with_temp_working(&block) context_helpers_require_repository_block(block) Git::SystemCallGuard.call('Failed to create or remove a temporary directory') do |guard| Dir.mktmpdir('temp-workdir') { |temp_dir| guard.unguarded { with_working(temp_dir, &block) } } end end |
#with_working(work_dir) {|repo| ... } ⇒ Object
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.
Yields a repository bound to work_dir in place of the receiver's
working directory
Yields a second repository bound to work_dir. It shares the
receiver's index and git directory, so staging through it changes what
the receiver sees as staged. The process working directory is changed
to work_dir via Dir.chdir for the duration of the block and changed
back afterward. The receiver is not changed, so calls on it inside the
block still use the original working directory.
Dir.chdir is process-global, so this method is not safe to call from
more than one thread at a time.
209 210 211 212 213 214 |
# File 'lib/git/repository/context_helpers.rb', line 209 def with_working(work_dir, &block) context_helpers_require_repository_block(block) new_path = context_helpers_validate_path(work_dir, true) repo = context_helpers_derived_repository(git_work_dir: new_path.to_s) context_helpers_chdir(new_path.to_s) { yield repo } end |