Class: Rlt::Commands::Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/rlt/commands/switch.rb

Constant Summary collapse

CONF_BRANCH_NAME_TEMPLATE =
'branch_name_template'

Class Method Summary collapse

Class Method Details

.apply_stash_if_any(branch_name) ⇒ Object



29
30
31
32
33
34
# File 'lib/rlt/commands/switch.rb', line 29

def self.apply_stash_if_any(branch_name)
  stash_name = Utils::GitUtil.latest_stash_name(branch_name)
  return if stash_name.nil?
  Utils::GitUtil.apply_stash(stash_name, print_info: true)
  Utils::GitUtil.drop_stash(stash_name, print_info: true)
end

.change_branch_name(config, branch_name) ⇒ Object



17
18
19
20
21
22
# File 'lib/rlt/commands/switch.rb', line 17

def self.change_branch_name(config, branch_name)
  return branch_name if dont_change_branch_name?(config, branch_name)
  branch_name_template = config[CONF_BRANCH_NAME_TEMPLATE]
  return branch_name if branch_name_template.nil?
  ERB.new(branch_name_template).result binding
end

.checkout(branch_name) ⇒ Object



45
46
47
48
49
# File 'lib/rlt/commands/switch.rb', line 45

def self.checkout(branch_name)
  result = Utils::GitUtil.silently_try_checkout(branch_name)
  Utils::Logger.info "Switched to '#{branch_name}'." if result
  result
end

.create_and_checkout(branch_name) ⇒ Object



51
52
53
54
# File 'lib/rlt/commands/switch.rb', line 51

def self.create_and_checkout(branch_name)
  Utils::GitUtil.silently_create_and_checkout(branch_name)
  Utils::Logger.info "Created & Switched to '#{branch_name}'."
end

.dont_change_branch_name?(config, branch_name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/rlt/commands/switch.rb', line 36

def self.dont_change_branch_name?(config, branch_name)
  list = %w[master develop] + (config['dont_change_names'] || [])
  list.include? branch_name
end

.run(config, branch_name) ⇒ Object



10
11
12
13
14
15
# File 'lib/rlt/commands/switch.rb', line 10

def self.run(config, branch_name)
  modified_branch_name = change_branch_name(config, branch_name)
  save_stash_if_any
  switch(modified_branch_name)
  apply_stash_if_any(modified_branch_name)
end

.save_stash_if_anyObject



24
25
26
27
# File 'lib/rlt/commands/switch.rb', line 24

def self.save_stash_if_any
  return unless Utils::GitUtil.uncommitted_change?
  Utils::GitUtil.save_stash('Auto stash', print_info: true)
end

.switch(branch_name) ⇒ Object



41
42
43
# File 'lib/rlt/commands/switch.rb', line 41

def self.switch(branch_name)
  create_and_checkout(branch_name) unless checkout(branch_name)
end