Class: Toolshed::Commands::CreateBranch

Inherits:
Object
  • Object
show all
Defined in:
lib/toolshed/commands/create_branch.rb

Instance Method Summary collapse

Instance Method Details

#execute(args, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/toolshed/commands/create_branch.rb', line 4

def execute(args, options = {})
  begin
    branch_name = read_user_input_branch_name("Branch name:", options)
    branch_from = read_user_input_branch_from("Branch from:", options)

    puts "Branch name: #{branch_name}"
    puts "Branching from: #{branch_from}"

    git = Toolshed::Git::Base.new({
      from_remote_branch_name: branch_from,
      to_remote_branch_name: branch_name,
    })
    git.create_branch

    puts "Branch #{branch_name} has been created"
    return
  rescue Veto::InvalidEntity => e
    puts "Unable to create branch due to the following errors"
    e.message.each do |key, value|
      puts "#{key}: #{value}"
    end
    return
  end
end

#read_user_input_branch_from(message, options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/toolshed/commands/create_branch.rb', line 44

def read_user_input_branch_from(message, options)
  return options[:branch_from] if (options.has_key?(:branch_from))

  # if branch-name was supplied then default to master if not supplied
  if (options.has_key?(:branch_name))
    return Toolshed::Git::DEFAULT_BRANCH_FROM
  end

  puts message
  value = $stdin.gets.chomp

  until (!value.empty?)
    puts "Branch from cannot be empty"
    puts message
    value = $stdin.gets.chomp
  end

  value
end

#read_user_input_branch_name(message, options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/toolshed/commands/create_branch.rb', line 29

def read_user_input_branch_name(message, options)
  return options[:branch_name] if (options.has_key?(:branch_name))

  puts message
  value = $stdin.gets.chomp

  until (!value.empty?)
    puts "Branch name cannot be empty"
    puts message
    value = $stdin.gets.chomp
  end

  value
end