Class: Git::Worktrees
- Inherits:
-
Object
- Object
- Git::Worktrees
- 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.
Instance Method Summary collapse
-
#[](worktree_name) ⇒ Git::Worktree?
Returns the worktree with the given path.
-
#each
Iterates over every worktree in the collection.
-
#initialize(base)
constructor
Creates a new Worktrees collection populated from the given repository.
-
#prune ⇒ String
Removes stale administrative files for worktrees that no longer exist.
-
#size ⇒ Integer
Returns the number of worktrees in the collection.
-
#to_s ⇒ String
Returns a string listing all worktrees, one per line.
Constructor Details
#initialize(base)
Creates a new Worktrees collection populated from the given repository
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).
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 |
#each ⇒ Enumerator<Git::Worktree> #each {|worktree| ... } ⇒ Array<Git::Worktree>
Iterates over every worktree in the collection
77 78 79 |
# File 'lib/git/worktrees.rb', line 77 def each(&) @worktrees.values.each(&) end |
#prune ⇒ String
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.
127 128 129 |
# File 'lib/git/worktrees.rb', line 127 def prune worktree_repository.worktree_prune end |
#size ⇒ Integer
Returns the number of worktrees in the collection
51 52 53 |
# File 'lib/git/worktrees.rb', line 51 def size @worktrees.size end |
#to_s ⇒ String
Returns a string listing all worktrees, one per line
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 |