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_diverged_repos, #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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/git_bundle/commands/checkout.rb', line 42

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

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

#checkout_parallel(repositories, fallback_branch: nil) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/git_bundle/commands/checkout.rb', line 72

def checkout_parallel(repositories, fallback_branch: nil)
  parallel(repositories) do |repo|
    output = repo.execute_git(['checkout', @project.branch_config.branch(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
35
36
37
38
39
40
# File 'lib/git_bundle/commands/checkout.rb', line 11

def invoke
  @project.load_dependant_repositories
  remaining_args = @args.dup
  flag = remaining_args.first&.chars&.first == "-" ? remaining_args.shift : nil
  branch = remaining_args.shift

  if remaining_args.any? || (flag && branch.nil?)
    puts_error "Invalid arguments for checkout.  Usage: \n\tgitb checkout\n\tgitb checkout <branch>\n\tgitb checkout <remote/branch>\n\tgitb checkout -b <new branch>\n\tgitb checkout -a <force branch all repositories>"
    return
  end

  case
  when flag == "-a" || flag == "--all"
    if checkout(@project.main_repository, branch)
      @project.dependant_repositories.each { |r| checkout(r, branch) }
    end
    @project.branch_config.save if @project.branch_config.changed?
  when flag == "-b"
    if checkout(@project.main_repository, branch, create_new: true, force: true)
      @project.dependant_repositories.each { |r| checkout(r, branch, create_new: true) }
    end
    @project.branch_config.save if @project.branch_config.changed?
  when branch.nil?
    checkout_parallel(@project.dependant_repositories, fallback_branch: @project.main_repository.branch)
  else
    if checkout(@project.main_repository, branch)
      checkout_parallel(@project.dependant_repositories, fallback_branch: branch)
    end
  end
end