Class: CommandLine::SubCommands::StartCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/git/contest/command_line/sub_commands/start_command.rb

Instance Attribute Summary

Attributes inherited from Command

#args, #input_stream, #opt_parser, #options, #terminal, #tokens

Instance Method Summary collapse

Methods inherited from Command

#init

Constructor Details

#initialize(new_args, new_input_stream = STDIN) ⇒ StartCommand

Returns a new instance of StartCommand.



15
16
17
# File 'lib/git/contest/command_line/sub_commands/start_command.rb', line 15

def initialize(new_args, new_input_stream = STDIN)
  super
end

Instance Method Details

#define_optionsObject



19
20
21
22
23
# File 'lib/git/contest/command_line/sub_commands/start_command.rb', line 19

def define_options
  opt_parser.on "-f", "--fetch", "fetch from origin before performing operation." do
    options[:fetch] = true
  end
end

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
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
71
# File 'lib/git/contest/command_line/sub_commands/start_command.rb', line 29

def run
  p options

  unless has_next_token?
    puts "Missing argument <name>"
    exit 1
  end

  base_branch_name = $MASTER
  # specify based branch
  if tokens.length == 2
    base_branch_name = tokens[1]
  end
  contest_branch_name = "#{tokens[0]}"
  contest_branch = "#{$PREFIX}/#{contest_branch_name}"
    Git.require_branch_absent contest_branch

  # fetch origin/master
  if options[:fetch]
    Git.do "fetch -q \"#{$ORIGIN}\""
  end

  # require equal
  if Git.branch_exists "#{$ORIGIN}/#{$MASTER}"
    Git.require_branches_equal "#{$MASTER}", "#{$ORIGIN}/#{$MASTER}"
  end

  # create branch
  if ! Git.do "checkout -b \"#{contest_branch}\" \"#{base_branch_name}\""
    abort "Could not create contest branch #{contest_branch}"
  end

  puts ""
  puts "Summary of actions:"
  puts "- A new branch \"#{contest_branch}\" was created, based on \"#{base_branch_name}\""
  puts "- You are now on branch \"#{contest_branch}\""
  puts ""
  puts "Now, start committing on your contest. When done, use:"
  puts ""
  puts "    git contest finish #{contest_branch_name}"
  puts ""

end

#set_default_optionsObject



25
26
27
# File 'lib/git/contest/command_line/sub_commands/start_command.rb', line 25

def set_default_options
  options[:fetch] = false if options[:fetch].nil?
end