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

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

Examples:

Write a file inside the repository working directory

repo.chdir do |dir|
  File.write('hello.txt', 'Hello, world!')
  repo.add('hello.txt')
end

Yields:

  • (dir) —

    the repository working directory

Yield Parameters:

  • dir (Pathname) —

    the working directory path

Yield Returns:

  • (Object) —

    returned as the method's return value

Returns:

  • (Object) —

    the value returned by the block

Raises:

  • (ArgumentError) —

    if the repository has no working directory (bare repository)

  • (Git::Error) —

    if the working directory cannot be entered



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.

Deprecated.

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.

Examples:

Set the index to a custom path

repo.set_index('/path/to/custom.index')

Open a repository bound to the index instead

repo = Git.open('/path/to/repo', index: '/path/to/custom.index')

Open a bare repository bound to the index instead

repo = Git.bare('/srv/repo.git', index: '/tmp/scratch.index')

Parameters:

  • index_file (String, Pathname) —

    path to the new index file

  • must_exist (Boolean) (defaults to: true) —

    when true (the default), raises ArgumentError if index_file does not exist on disk

Raises:

  • (ArgumentError) —

    if must_exist: true (the default) and index_file does not exist



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.

Deprecated.

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.

Examples:

Set the working directory to a custom path

repo.set_working('/path/to/working')

Open a repository bound to the working tree instead

repo = Git.open('/path/to/working', repository: '/path/to/repo/.git')

Open a repository bound to the working tree and a custom index

repo = Git.open('/path/to/working', repository: '/path/to/repo/.git',
                index: '/path/to/custom.index')

Parameters:

  • work_dir (String, Pathname) —

    path to the new working directory

  • must_exist (Boolean) (defaults to: true) —

    when true (the default), raises ArgumentError if work_dir does not exist on disk

Raises:

  • (ArgumentError) —

    if must_exist: true (the default) and work_dir does not exist



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.

Examples:

Read a tree into a custom index

repo.with_index('/tmp/custom.index') do |indexed|
  indexed.read_tree('HEAD')
end

Parameters:

  • new_index (String, Pathname) —

    path to the replacement index file

Yields:

  • (repo) —

    the repository bound to new_index

Yield Parameters:

  • repo (Git::Repository) —

    a new repository of the receiver's class bound to new_index

Yield Returns:

  • (Object) —

    returned as the method's return value

Returns:

  • (Object) —

    the value returned by the block

Raises:

  • (ArgumentError) —

    if no block is given or the block declares no positional parameter

  • (Git::Error) —

    if new_index cannot be expanded to an absolute path



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.

Note:

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.

Examples:

Write a tree from a temporary index

tree_sha = repo.with_temp_index do |indexed|
  indexed.read_tree('HEAD')
  indexed.add('generated.txt')
  indexed.write_tree
end

Yields:

  • (repo) —

    the repository bound to the temporary index

Yield Parameters:

  • repo (Git::Repository) —

    a new repository of the receiver's class bound to an index file inside the temporary directory

Yield Returns:

  • (Object) —

    returned as the method's return value

Returns:

  • (Object) —

    the value returned by the block

Raises:

  • (ArgumentError) —

    if no block is given or the block declares no positional parameter

  • (Git::Error) —

    if the temporary directory cannot be created



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.

Note:

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.

Examples:

Write files in an isolated temporary working directory

repo.with_temp_working do |scratch|
  File.write('scratch.txt', 'temporary content')
  scratch.untracked_files #=> ["scratch.txt"]
end

Yields:

  • (repo) —

    the repository bound to the temporary working directory

Yield Parameters:

  • repo (Git::Repository) —

    a new repository of the receiver's class bound to the temporary directory

Yield Returns:

  • (Object) —

    returned as the method's return value

Returns:

  • (Object) —

    the value returned by the block

Raises:

  • (ArgumentError) —

    if no block is given or the block declares no positional parameter

  • (Git::Error) —

    if the temporary directory cannot be created or removed



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.

Examples:

Commit changes from a different worktree path

repo.with_working('/path/to/worktree') do |worktree|
  worktree.add('.')
  worktree.commit('chore: automated update')
end

Parameters:

  • work_dir (String, Pathname) —

    path to the replacement working directory

Yields:

  • (repo) —

    the repository bound to work_dir

Yield Parameters:

  • repo (Git::Repository) —

    a new repository of the receiver's class bound to work_dir

Yield Returns:

  • (Object) —

    returned as the method's return value

Returns:

  • (Object) —

    the value returned by the block

Raises:

  • (ArgumentError) —

    if work_dir does not exist on disk, if no block is given, or if the block declares no positional parameter

  • (Git::Error) —

    if work_dir cannot be expanded to an absolute path or cannot be entered



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