Class: CommandLine::SubCommands::FinishCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/git/contest/command_line/sub_commands/finish_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) ⇒ FinishCommand

Returns a new instance of FinishCommand.



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

def initialize(new_args, new_input_stream = STDIN)
  super
end

Instance Method Details

#define_optionsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/git/contest/command_line/sub_commands/finish_command.rb', line 19

def define_options
  opt_parser.on "--[no-]edit", "Use default commit message." do |v|
    options[:edit] = v
  end

  opt_parser.on "-k", "--keep", "Keep contest branch after merge." do
    options[:keep] = true
  end

  opt_parser.on "--rebase", "Use rebase instead of merge." do
    options[:rebase] = true
  end

  opt_parser.on "--force-delete", "Force delete contest branch after finish." do
    options[:force_delete] = true
  end

  opt_parser.on "-s", "--squash", "Use squash during merge." do
    options[:squash] = true
  end

  opt_parser.on "--fetch", "Fetch from origin before finish." do
    options[:fetch]
  end
end

#runObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/git/contest/command_line/sub_commands/finish_command.rb', line 54

def run
  expand_contest_branch
  Git.require_branch $BRANCH

  Git.require_clean_working_tree

  if Git.remote_branches().include?("#{$ORIGIN}/#{$BRANCH}")
    if options[:fetch]
      Git.do "fetch -q \"#{$ORIGIN}\" \"#{$BRANCH}\""
      Git.do "fetch -q \"#{$ORIGIN}\" \"#{$MASTER}\""
    end
  end

  if Git.remote_branches().include?("#{$ORIGIN}/#{$BRANCH}")
      Git.require_branches_equal $BRANCH, "#{$ORIGIN}/#{$BRANCH}"
  end

  if Git.remote_branches().include?("#{$ORIGIN}/#{$MASTER}")
      Git.require_branches_equal $MASTER, "#{$ORIGIN}/#{$MASTER}"
  end

  merge_options = ""
  if options[:edit]
    merge_options += " --no-edit"
  end

  if options[:rebase]
    ret = Git.do "contest rebase \"#{$NAME}\" \"#{$MASTER}\""
    exitcode = $?.to_i
    if ! $?
      puts "Finish was aborted due to conflicts during rebase."
      exit 1
    end
  end

  Git.do "checkout #{$MASTER}"
  if Git.do("rev-list -n2 \"#{$MASTER}..#{$BRANCH}\"").lines.to_a.length == 1
      Git.do "merge --ff \"#{$BRANCH}\" #{merge_options}"
  else
    if options[:squash]
      Git.do "merge --squash \"#{$BRANCH}\" #{merge_options}"
      unless options[:edit]
        Git.do "commit -m \"Squashed commit\""
      else
        Git.do "commit"
      end
      Git.do "merge \"#{$BRANCH}\" #{merge_options}"
    else
      Git.do "merge --no-ff \"#{$BRANCH}\" #{merge_options}"
    end
  end

  helper_finish_cleanup
end

#set_default_optionsObject



45
46
47
48
49
50
51
52
# File 'lib/git/contest/command_line/sub_commands/finish_command.rb', line 45

def set_default_options
  options[:edit] = true if options[:edit].nil?
  options[:keep] = false if options[:keep].nil?
  options[:rebase] = false if options[:rebase].nil?
  options[:force_delete] = false if options[:force_delete].nil?
  options[:squash] = false if options[:squash].nil?
  options[:fetch] = false if options[:fetch].nil?
end