Class: Mj::Git::Commands::CheckoutCommandHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/mj/git/commands/checkout_command_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(stdout:, command_executer: CommandExecuter.new) ⇒ CheckoutCommandHandler

Returns a new instance of CheckoutCommandHandler.



10
11
12
13
# File 'lib/mj/git/commands/checkout_command_handler.rb', line 10

def initialize(stdout:, command_executer: CommandExecuter.new)
  @stdout = stdout
  @command_executer = command_executer
end

Instance Method Details

#handle(command) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mj/git/commands/checkout_command_handler.rb', line 15

def handle(command)
  branches = @command_executer.execute("git branch -a")
  branches = Git::Branches.from_branch_names(branches).matching(command.branch)

  if branches.to_local.uniq.length > 1
    warn_multiple_matches(branches)
  end

  winner = branches.min_by(&:length)

  if winner.nil?
    puts("No branche found matching #{command.branch}", color: :red)
    exit(1)
  end

  puts(winner.checkout_command, color: :green)

  if command.dry_run?
    return
  end

  @command_executer.execute(winner.checkout_command)
end