Class: GitBundle::Commands::Checkout

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

Constant Summary collapse

BRANCH_CONFIG_FILE =
'.gitb.yml'

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.



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

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

Instance Method Details

#branch_configObject



64
65
66
67
# File 'lib/git_bundle/commands/checkout.rb', line 64

def branch_config
  return @branch_config if defined?(@branch_config)
  @branch_config ||= File.exists?(config_file) ? YAML.load_file(config_file) || {} : nil
end

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



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

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 create_new && success && branch_config
    branch_config[repo.name] = branch
    write_branch_config
  end
  success
end

#checkout_parallel(repositories, fallback_branch) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/git_bundle/commands/checkout.rb', line 56

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

#config_fileObject



69
70
71
# File 'lib/git_bundle/commands/checkout.rb', line 69

def config_file
  File.join(Dir.pwd, BRANCH_CONFIG_FILE)
end

#invokeObject



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

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
  else
    puts_error "Invalid arguments for checkout.  Usage: \n\tgitb checkout\n\tgitb checkout <branch>\n\tgitb checkout -b <new_branch>"
  end
end

#write_branch_configObject



73
74
75
# File 'lib/git_bundle/commands/checkout.rb', line 73

def write_branch_config
  File.open(File.join(Dir.pwd, BRANCH_CONFIG_FILE), 'w') {|file| file.write(branch_config.to_yaml.lines[1..-1].join)}
end