Module: Git::Repository::WorktreeOperations Private

Included in:
Git::Repository
Defined in:
lib/git/repository/worktree_operations.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 worktree operations: listing, adding, removing, moving, locking, repairing, and pruning worktrees

Included by Git::Repository.

Instance Method Summary collapse

Instance Method Details

#worktree_add(dir, commitish = nil) ⇒ Git::WorktreeInfo

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:

When commitish is nil and git creates the branch, it does so before it creates the worktree, so a failure after that point leaves the new branch in place without a worktree. A branch that already existed is left as it was.

Create a new linked worktree at the given directory

Returns the entry that #worktree_list reports for the new worktree. Its path is the directory as git records it: absolute, with symlinks resolved, so /tmp/feature on macOS comes back as /private/tmp/feature.

Examples:

Create a worktree at a path (auto-creates a branch)

info = repo.worktree_add('/tmp/feature')
info.path    #=> "/private/tmp/feature"
info.branch  #=> "refs/heads/feature"

Create a worktree and check out an existing commitish

repo.worktree_add('/tmp/hotfix', 'main').head
#=> "4bef5ab0c8e7c19c6be2c0f55ccd45eec1f3d32a"

Parameters:

  • dir (String) —

    filesystem path for the new worktree, absolute or relative to the current directory

  • commitish (String, nil) (defaults to: nil) —

    branch, tag, or commit to check out

    When nil, git checks out the branch named after the final path component, creating it when no such branch exists

Returns:

Raises:

  • (Git::FailedError) —

    if git exits with a non-zero exit status

  • (Git::UnexpectedResultError) —

    if the worktree listing cannot be parsed or does not contain an entry for the new worktree's directory (for example, it was removed by another process before the lookup)

See Also:



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/git/repository/worktree_operations.rb', line 86

def worktree_add(dir, commitish = nil)
  args = [dir]
  args << commitish unless commitish.nil?

  Git::Commands::Worktree::Add.new(@execution_context).call(*args)

  # git records the path as given with symlinks resolved, so compare by
  # identity rather than by string: this covers symlinks, case-insensitive
  # filesystems, and Unicode normalization on macOS
  worktree_list.find { |worktree| File.identical?(worktree.path, dir) } ||
    raise(Git::UnexpectedResultError, "worktree was created but not found in the worktree list: #{dir}")
end

#worktree_list ⇒ Array<Git::WorktreeInfo>

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 every worktree attached to the repository

Lists the main worktree first, then each linked worktree, in the order git reports them. The main worktree of a bare repository is included with WorktreeInfo#bare? true and no head or branch.

Examples:

List all worktrees

repo.worktree_list.map(&:path)
#=> ["/path/to/main", "/tmp/feature"]

Find the worktree that has a branch checked out

info = repo.worktree_list.find { |w| w.branch == 'refs/heads/feature' }
info.path     #=> "/tmp/feature"
info.head     #=> "b8c63202c3c0ebd37b7e45fd0c22e6c20d5bead1"
info.locked?  #=> false

Returns:

Raises:

See Also:



42
43
44
45
# File 'lib/git/repository/worktree_operations.rb', line 42

def worktree_list
  result = Git::Commands::Worktree::List.new(@execution_context).call(porcelain: true)
  Git::Parsers::Worktree.parse_list(result.stdout)
end

#worktree_lock(worktree, opts = {}) ⇒ 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.

Lock a linked worktree so that git worktree prune leaves it alone

Lock a worktree whose directory is on removable media or a network share that is not always mounted.

Examples:

Lock a worktree

repo.worktree_lock('/tmp/feature')

Lock a worktree with a reason

repo.worktree_lock('/tmp/feature', reason: 'on an external drive')

Parameters:

  • worktree (String, Git::WorktreeInfo) —

    the path of the worktree to lock, or its entry from #worktree_list

  • opts (Hash) (defaults to: {}) —

    options for the lock

Options Hash (opts):

  • :reason (String, nil) — default: nil —

    an explanation stored with the lock and reported as WorktreeInfo#lock_reason

Returns:

  • (String) —

    the output from the git worktree lock command (typically empty)

Raises:

  • (ArgumentError) —

    if unsupported options are provided

  • (Git::FailedError) —

    if git exits with a non-zero exit status

See Also:



182
183
184
# File 'lib/git/repository/worktree_operations.rb', line 182

def worktree_lock(worktree, opts = {})
  Git::Commands::Worktree::Lock.new(@execution_context).call(worktree.to_s, **opts).stdout
end

#worktree_move(worktree, new_path, opts = {}) ⇒ 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.

Move a linked worktree to a new location

Examples:

Move a worktree

repo.worktree_move('/tmp/feature', '/tmp/feature-moved')

Move a locked worktree

repo.worktree_move('/tmp/feature', '/tmp/feature-moved', force: 2)

Parameters:

  • worktree (String, Git::WorktreeInfo) —

    the path of the worktree to move, or its entry from #worktree_list

  • new_path (String) —

    the destination path

  • opts (Hash) (defaults to: {}) —

    options for the move

Options Hash (opts):

  • :force (Boolean, Integer, nil) — default: nil —

    override git's safeguards; git refuses to move a locked worktree unless the flag is given twice, so pass 2 for that

Returns:

  • (String) —

    the output from the git worktree move command (typically empty)

Raises:

  • (ArgumentError) —

    if unsupported options are provided

  • (Git::FailedError) —

    if git exits with a non-zero exit status

See Also:



150
151
152
# File 'lib/git/repository/worktree_operations.rb', line 150

def worktree_move(worktree, new_path, opts = {})
  Git::Commands::Worktree::Move.new(@execution_context).call(worktree.to_s, new_path, **opts).stdout
end

#worktree_prune ⇒ 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.

Prune stale worktree administrative files

Removes stale administrative files from $GIT_DIR/worktrees. A worktree becomes stale when its directory no longer exists on disk.

Examples:

Prune stale worktrees

repo.worktree_prune

Returns:

  • (String) —

    the output from the git worktree prune command (typically empty)

Raises:

See Also:



247
248
249
# File 'lib/git/repository/worktree_operations.rb', line 247

def worktree_prune
  Git::Commands::Worktree::Prune.new(@execution_context).call.stdout
end

#worktree_remove(worktree) ⇒ 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.

Remove a linked worktree

Examples:

Remove a worktree by path

repo.worktree_remove('/tmp/feature')

Remove a worktree from the list

info = repo.worktree_list.find { |w| w.branch == 'refs/heads/feature' }
repo.worktree_remove(info)

Parameters:

Returns:

  • (String) —

    the output from the git worktree remove command (typically empty)

Raises:

See Also:



118
119
120
# File 'lib/git/repository/worktree_operations.rb', line 118

def worktree_remove(worktree)
  Git::Commands::Worktree::Remove.new(@execution_context).call(worktree.to_s).stdout
end

#worktree_repair(*paths) ⇒ 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.

Repair the links between the repository and its linked worktrees

With no paths, repairs the link from each linked worktree back to the repository, which is needed after the repository directory was moved. Given the current paths of linked worktrees that were moved without #worktree_move, also repairs the repository's links to them.

Examples:

Repair after the repository directory was moved

repo.worktree_repair

Repair after a linked worktree was moved by hand

repo.worktree_repair('/new/path/to/feature')

Parameters:

Returns:

  • (String) —

    the output from the git worktree repair command, which reports each repair made

Raises:

See Also:



228
229
230
# File 'lib/git/repository/worktree_operations.rb', line 228

def worktree_repair(*paths)
  Git::Commands::Worktree::Repair.new(@execution_context).call(*paths.map(&:to_s)).stdout
end

#worktree_unlock(worktree) ⇒ 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.

Unlock a linked worktree

Examples:

Unlock a worktree

repo.worktree_unlock('/tmp/feature')

Parameters:

Returns:

  • (String) —

    the output from the git worktree unlock command (typically empty)

Raises:

See Also:



201
202
203
# File 'lib/git/repository/worktree_operations.rb', line 201

def worktree_unlock(worktree)
  Git::Commands::Worktree::Unlock.new(@execution_context).call(worktree.to_s).stdout
end