Class: FlightPlanCli::Commands::Checkout

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

Instance Method Summary collapse

Instance Method Details

#fetchObject



51
52
53
54
# File 'lib/flight_plan_cli/commands/checkout.rb', line 51

def fetch
  puts 'Fetching...'.green
  git.remotes.each { |remote| remote.fetch(credentials: ssh_agent) }
end

#gitObject



47
48
49
# File 'lib/flight_plan_cli/commands/checkout.rb', line 47

def git
  @git ||= Rugged::Repository.new(Dir.pwd)
end

#local_branch_for(issue) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/flight_plan_cli/commands/checkout.rb', line 13

def local_branch_for(issue)
  issue_branches = local_branches.map(&:name).grep(/##{issue}[^0-9]/)
  return false unless issue_branches.count == 1

  branch = issue_branches.first
  puts "Checking out local branch '#{branch}'".green
  git.checkout(branch)
  true
end

#local_branchesObject



39
40
41
# File 'lib/flight_plan_cli/commands/checkout.rb', line 39

def local_branches
  @local_branches ||= git.branches.each(:local)
end

#process(issue_no) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/flight_plan_cli/commands/checkout.rb', line 4

def process(issue_no)
  puts "Checking out branch for #{issue_no}"
  local_branch_for(issue_no) ||
    remote_branch_for(issue_no) ||
    new_branch_for(issue_no)
rescue Rugged::CheckoutError => e
  puts "Unable to checkout: #{e.message}".red
end

#remote_branch_for(issue) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/flight_plan_cli/commands/checkout.rb', line 23

def remote_branch_for(issue)
  fetch
  issue_branches = remote_branches.map(&:name).grep(/##{issue}[^0-9]/)
  return false unless issue_branches.count == 1

  remote_branch_name = issue_branches.first
  branch = remote_branches.find { |rb| rb.name == remote_branch_name }
  local_name = branch.name[branch.remote_name.size + 1..-1]

  puts "Checking out and tracking remote branch '#{local_name}'".green
  new_branch = git.branches.create(local_name, branch.name)
  new_branch.upstream = branch
  git.checkout(local_name)
  true
end

#remote_branchesObject



43
44
45
# File 'lib/flight_plan_cli/commands/checkout.rb', line 43

def remote_branches
  @remote_branches ||= git.branches.each(:remote)
end

#ssh_agentObject



56
57
58
# File 'lib/flight_plan_cli/commands/checkout.rb', line 56

def ssh_agent
  @ssh_agent ||= Rugged::Credentials::SshKeyFromAgent.new(username: 'git')
end