Class: Hiiro::Git::Worktrees

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worktrees) ⇒ Worktrees

Returns a new instance of Worktrees.



38
39
40
# File 'lib/hiiro/git/worktrees.rb', line 38

def initialize(worktrees)
  @worktrees = worktrees
end

Instance Attribute Details

#worktreesObject (readonly)

Returns the value of attribute worktrees.



36
37
38
# File 'lib/hiiro/git/worktrees.rb', line 36

def worktrees
  @worktrees
end

Class Method Details

.fetch(repo_path: nil) ⇒ Object



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

def self.fetch(repo_path: nil)
  output = if repo_path
    `git -C #{repo_path.shellescape} worktree list --porcelain 2>/dev/null`
  else
    `git worktree list --porcelain 2>/dev/null`
  end
  from_porcelain(output)
end

.from_porcelain(output) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hiiro/git/worktrees.rb', line 6

def self.from_porcelain(output)
  return new([]) if output.nil? || output.empty?

  blocks = []
  current_block = []

  output.each_line do |line|
    line = line.chomp
    if line.empty?
      blocks << current_block unless current_block.empty?
      current_block = []
    else
      current_block << line
    end
  end
  blocks << current_block unless current_block.empty?

  worktrees = blocks.map { |block| Worktree.from_porcelain_block(block) }.compact
  new(worktrees)
end

Instance Method Details

#detachedObject



66
67
68
# File 'lib/hiiro/git/worktrees.rb', line 66

def detached
  self.class.new(worktrees.select(&:detached?))
end

#each(&block) ⇒ Object



42
43
44
# File 'lib/hiiro/git/worktrees.rb', line 42

def each(&block)
  worktrees.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/hiiro/git/worktrees.rb', line 82

def empty?
  worktrees.empty?
end

#find_by_branch(branch) ⇒ Object



54
55
56
# File 'lib/hiiro/git/worktrees.rb', line 54

def find_by_branch(branch)
  worktrees.find { |wt| wt.branch == branch }
end

#find_by_name(name) ⇒ Object



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

def find_by_name(name)
  worktrees.find { |wt| wt.name == name }
end

#find_by_path(path) ⇒ Object



46
47
48
# File 'lib/hiiro/git/worktrees.rb', line 46

def find_by_path(path)
  worktrees.find { |wt| wt.path == path }
end

#matching(pwd) ⇒ Object



58
59
60
# File 'lib/hiiro/git/worktrees.rb', line 58

def matching(pwd)
  worktrees.find { |wt| wt.match?(pwd) }
end

#namesObject



74
75
76
# File 'lib/hiiro/git/worktrees.rb', line 74

def names
  worktrees.map(&:name)
end

#pathsObject



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

def paths
  worktrees.map(&:path)
end

#sizeObject Also known as: count, length



86
87
88
# File 'lib/hiiro/git/worktrees.rb', line 86

def size
  worktrees.size
end

#with_branchObject



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

def with_branch
  self.class.new(worktrees.reject(&:detached?))
end

#without_bareObject



62
63
64
# File 'lib/hiiro/git/worktrees.rb', line 62

def without_bare
  self.class.new(worktrees.reject(&:bare?))
end