Class: GitBundle::Commands::Checkout

Inherits:
Object
  • Object
show all
Includes:
GitBundle::Console
Defined in:
lib/git_bundle/commands/checkout.rb

Constant Summary

Constants included from GitBundle::Console

GitBundle::Console::COLORS

Instance Method Summary collapse

Methods included from GitBundle::Console

#clear_line, #parallel, #puts_attention, #puts_error, #puts_heading, #puts_prompt, #puts_repo_heading, #puts_repo_heading_switch, #puts_stay_on_line, #puts_wait_line

Constructor Details

#initialize(project, args) ⇒ Checkout

Returns a new instance of Checkout.



6
7
8
9
# File 'lib/git_bundle/commands/checkout.rb', line 6

def initialize(project, args)
  @project = project
  @args = args
end

Instance Method Details

#checkout(repo, branch, create_new: false, force: false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/git_bundle/commands/checkout.rb', line 36

def checkout(repo, branch, create_new: false, force: false)
  if create_new
    unless force
      puts_repo_heading(repo)
      puts_prompt("Create #{branch}? (Y/N)")
      return unless STDIN.getch.upcase == 'Y'
    end
    args = ['checkout', '-b', branch]
  else
    args = ['checkout', branch]
  end

  output = repo.execute_git(args, color: true)
  success = $?.exitstatus == 0
  repo.refresh_branch
  puts_repo_heading(repo) unless create_new && !force
  success ? puts(output) : puts_error(output)
  if success && !repo.main && @project.branch_config.current && @project.branch_config.current[repo.name] != branch
    @project.branch_config.current[repo.name] = branch
  end
  success
end

#checkout_parallel(repositories, fallback_branch) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/git_bundle/commands/checkout.rb', line 59

def checkout_parallel(repositories, fallback_branch)
  parallel(repositories) do |repo|
    output = repo.execute_git(['checkout', @project.branch_config.current&.dig(repo.name) || fallback_branch], color: true)
    repo.refresh_branch
    ExecutionResult.new($?.exitstatus != 0, output)
  end
end

#invokeObject



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

def invoke
  @project.load_dependant_repositories

  if @args.empty?
    checkout_parallel(@project.dependant_repositories, @project.main_repository.branch)

  elsif @args.size == 1
    if checkout(@project.main_repository, @args.first)
      checkout_parallel(@project.dependant_repositories, @args.first)
    end
  elsif @args.size == 2 && @args.first == '-b'
    if checkout(@project.main_repository, @args.last, create_new: true, force: true)
      @project.dependant_repositories.each {|r| checkout(r, @args.last, create_new: true)}
    end
    @project.branch_config.save if @project.branch_config.changed?
  elsif @args.size == 2 && (@args.first == '-a' || @args.first == '--all')
    if checkout(@project.main_repository, @args.last)
      @project.dependant_repositories.each {|r| checkout(r, @args.last)}
    end
    @project.branch_config.save if @project.branch_config.changed?
  else
    puts_error "Invalid arguments for checkout.  Usage: \n\tgitb checkout\n\tgitb checkout <branch>\n\tgitb checkout -b <new branch>\n\tgitb checkout -a <force branch all repositories>"
  end
end