Class: Git::Worktrees

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/git/worktrees.rb

Overview

Collection of all Git worktrees in a repository

Wraps every linked and main worktree and provides enumeration and path-based lookup.

Accepts either a Repository (new form) or a Base (legacy form) as the base argument. The is_a?(Git::Base) guard routes git operations through the facade repository and will be removed when Base is deleted in Phase 4.

Examples:

Enumerate all worktrees

worktrees = repo.worktrees
worktrees.each { |wt| puts wt.dir }

Instance Method Summary collapse

Constructor Details

#initialize(base)

Creates a new Worktrees collection populated from the given repository

Parameters:

Raises:



34
35
36
37
38
39
40
41
42
# File 'lib/git/worktrees.rb', line 34

def initialize(base)
  @worktrees = {}

  @base = base

  worktree_repository.worktrees_all.each do |w|
    @worktrees[w[0]] = Git::Worktree.new(@base, w[0], w[1])
  end
end

Instance Method Details

#[](worktree_name) ⇒ Git::Worktree?

Returns the worktree with the given path

Supports lookup by the filesystem path of the worktree directory or by the full worktree descriptor (path and optional commitish).

Examples:

Look up a worktree by path

repo.worktrees['/path/to/linked-worktree']

Parameters:

  • worktree_name (#to_s)

    the path (or full descriptor) of the worktree to retrieve

Returns:

  • (Git::Worktree, nil)

    the matching worktree, or nil if not found



94
95
96
97
98
# File 'lib/git/worktrees.rb', line 94

def [](worktree_name)
  @worktrees.values.each_with_object(@worktrees) do |worktree, worktrees|
    worktrees[worktree.full] ||= worktree
  end[worktree_name.to_s]
end

#eachEnumerator<Git::Worktree> #each {|worktree| ... } ⇒ Array<Git::Worktree>

Iterates over every worktree in the collection

Overloads:

  • #eachEnumerator<Git::Worktree>

    Returns an enumerator over all worktrees.

    Examples:

    Get an enumerator over all worktrees

    enum = repo.worktrees.each

    Returns:

    • (Enumerator<Git::Worktree>)

      an enumerator over all worktrees

  • #each {|worktree| ... } ⇒ Array<Git::Worktree>

    Returns the full list of worktrees.

    Examples:

    Print every worktree path

    repo.worktrees.each { |wt| puts wt.dir }

    Yields:

    • (worktree)

      passes each worktree to the block

    Yield Parameters:

    Yield Returns:

    • (void)

    Returns:



77
78
79
# File 'lib/git/worktrees.rb', line 77

def each(&)
  @worktrees.values.each(&)
end

#pruneString

Removes stale administrative files for worktrees that no longer exist

Runs git worktree prune to clean up any lingering worktree metadata for linked worktrees whose directories have been deleted.

Examples:

Prune stale worktree metadata

repo.worktrees.prune

Returns:

  • (String)

    stdout from the git command (typically empty)

Raises:



127
128
129
# File 'lib/git/worktrees.rb', line 127

def prune
  worktree_repository.worktree_prune
end

#sizeInteger

Returns the number of worktrees in the collection

Examples:

Count all worktrees

repo.worktrees.size  # => 2

Returns:

  • (Integer)

    the total number of worktrees



51
52
53
# File 'lib/git/worktrees.rb', line 51

def size
  @worktrees.size
end

#to_sString

Returns a string listing all worktrees, one per line

Examples:

Display all worktrees

puts repo.worktrees.to_s

Returns:

  • (String)

    a newline-separated listing of worktree descriptors



107
108
109
110
111
112
113
# File 'lib/git/worktrees.rb', line 107

def to_s
  out = +''
  @worktrees.each_value do |b|
    out << b.to_s << "\n"
  end
  out
end