Class: Branchtree::Commands::Checkout

Inherits:
Common
  • Object
show all
Defined in:
lib/branchtree/commands/checkout.rb

Instance Method Summary collapse

Methods inherited from Common

#load_situation, #load_tree, #pluralize

Methods included from Branchtree::Context

logger

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/branchtree/commands/checkout.rb', line 11

def execute
  super

  situation = load_situation
  tree = load_tree
  current_branch = tree.find_branch(situation.current_branch_name)

  choice = prompt.select("Choose a branch to check out:") do |menu|
    current_index = nil
    index = 1
    tree.depth_first do |level, branch|
      menu.choice "#{'  ' * level}#{branch.name}", branch

      current_index = index if branch == current_branch
      index += 1
    end
    menu.choice "Cancel", :cancel
    menu.default(current_index) unless current_index.nil?
  end

  if choice == :cancel
    logger.info "Goodbye!"
    exit 0
  end

  logger.debug "Checking out branch #{choice.name}."
  choice.checkout
  logger.success "Checkout successful."
end