Class: Legion::CLI::Commit

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/commit_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/legion/cli/commit_command.rb', line 12

def self.exit_on_failure?
  true
end

Instance Method Details

#generateObject



27
28
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
72
73
74
75
# File 'lib/legion/cli/commit_command.rb', line 27

def generate
  out = formatter

  stage_all if options[:all]
  diff = staged_diff
  if diff.strip.empty?
    out.error('Nothing staged to commit. Use -a to stage all changes, or git add files first.')
    raise SystemExit, 1
  end

  stat = staged_stat
  log = recent_commits
  setup_connection

  out.header('Generating commit message...')
  message = generate_message(diff, stat, log)

  if options[:json]
    out.json({ message: message, stat: stat })
    return
  end

  puts
  puts out.colorize(message, :green)
  puts
  puts out.dim(stat)
  puts

  unless options[:yes]
    $stderr.print "#{out.colorize('Commit with this message?', :yellow)} [Y/n/e(dit)] "
    response = $stdin.gets&.strip&.downcase
    case response
    when 'n', 'no'
      out.warn('Commit aborted.')
      return
    when 'e', 'edit'
      message = edit_message(message)
      return out.warn('Commit aborted (empty message).') if message.strip.empty?
    end
  end

  run_commit(message, amend: options[:amend])
  out.success(options[:amend] ? 'Commit amended.' : 'Committed.')
rescue CLI::Error => e
  out.error(e.message)
  raise SystemExit, 1
ensure
  Connection.shutdown
end