Class: GT::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/gt/stack.rb

Class Method Summary collapse

Class Method Details

.build(from: GT::Git.current_branch) ⇒ Object

Walk UP from a branch to its root, returns [root, …, from]



6
7
8
9
10
11
12
13
14
15
# File 'lib/gt/stack.rb', line 6

def self.build(from: GT::Git.current_branch)
  branches = []
  branch = from
  while (parent = GT::Git.gt_parent(branch))
    branches.unshift(branch)
    branch = parent
  end
  branches.unshift(branch)
  branches
end

.build_allObject

Build the full linear stack from root down, scanning all local branches. Returns [root, child, grandchild, …] regardless of current branch.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gt/stack.rb', line 19

def self.build_all
  all = GT::Git.all_branches
  managed = all.select { |b| GT::Git.gt_parent(b) }
  return [GT::Git.current_branch] if managed.empty?

  # Walk from any managed branch up to find root
  root = GT::Git.gt_parent(managed.first)
  root = GT::Git.gt_parent(root) while GT::Git.gt_parent(root)

  # Walk down from root following parent pointers
  result = [root]
  loop do
    child = managed.find { |b| GT::Git.gt_parent(b) == result.last }
    break unless child

    result << child
    managed.delete(child)
  end
  result
end


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gt/stack.rb', line 40

def self.print(from: GT::Git.current_branch, output: $stdout)
  current = GT::Git.current_branch
  branches = build(from: from)
  branches.each_with_index do |branch, i|
    prefix = i == 0 ? "" : ("  " * i) + "└─ "
    if branch == current
      output.puts GT::UI.render("#{prefix}{{green:#{branch} *}}")
    else
      output.puts "#{prefix}#{branch}"
    end
  end
end