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.

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:



27
28
29
30
31
32
33
34
35
# File 'lib/git/worktrees.rb', line 27

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



87
88
89
90
91
# File 'lib/git/worktrees.rb', line 87

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:



70
71
72
# File 'lib/git/worktrees.rb', line 70

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:



120
121
122
# File 'lib/git/worktrees.rb', line 120

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



44
45
46
# File 'lib/git/worktrees.rb', line 44

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



100
101
102
103
104
105
106
# File 'lib/git/worktrees.rb', line 100

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