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.
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
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).
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 |
#each ⇒ Enumerator<Git::Worktree> #each {|worktree| ... } ⇒ Array<Git::Worktree>
Iterates over every worktree in the collection
70 71 72 |
# File 'lib/git/worktrees.rb', line 70 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.
120 121 122 |
# File 'lib/git/worktrees.rb', line 120 def prune worktree_repository.worktree_prune end |
#size ⇒ Integer
Returns the number of worktrees in the collection
44 45 46 |
# File 'lib/git/worktrees.rb', line 44 def size @worktrees.size end |
#to_s ⇒ String
Returns a string listing all worktrees, one per line
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 |