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_all(from: GT::Git.current_branch) ⇒ Object

Build the full linear stack containing ‘from`, from root to tip. Walks UP from `from` to find the root, then DOWN to find any children.



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

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

  # Walk UP from `from` to root (reuse build)
  result = build(from: managed.include?(from) ? from : managed.first)

  # Walk DOWN from the tip following child pointers
  loop do
    child = managed.find { |b| GT::Git.gt_parent(b) == result.last }
    break unless child

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


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

def self.print(from: GT::Git.current_branch, output: $stdout)
  current = GT::Git.current_branch
  branches = build_all
  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