class FitCommit::Runner

Constants

DEFAULT_EDITOR
EXIT_CODE_ALLOW_COMMIT
EXIT_CODE_REJECT_COMMIT
StartOverOnEditException

Attributes

branch_name[RW]
message_path[RW]
stderr[RW]
stdin[RW]

Public Class Methods

new(message_path, branch_name, stderr = $stderr, stdin = $stdin) click to toggle source
# File lib/fit_commit/runner.rb, line 14
def initialize(message_path, branch_name, stderr = $stderr, stdin = $stdin)
  self.message_path = message_path
  self.branch_name = branch_name
  self.stderr = stderr
  self.stdin = stdin
end

Public Instance Methods

run() click to toggle source
# File lib/fit_commit/runner.rb, line 21
def run
  allow_commit = retry_on_user_edit do
    return EXIT_CODE_ALLOW_COMMIT if empty_commit?
    run_validators
    return EXIT_CODE_ALLOW_COMMIT if [errors, warnings].all?(&:empty?)
    print_results
    errors.empty? || ask_force_commit
  end

  if allow_commit
    stderr.print "\n"
    EXIT_CODE_ALLOW_COMMIT
  else
    EXIT_CODE_REJECT_COMMIT
  end
rescue Interrupt # Ctrl-c
  EXIT_CODE_REJECT_COMMIT
end

Private Instance Methods

ask_force_commit() click to toggle source
# File lib/fit_commit/runner.rb, line 53
def ask_force_commit
  return unless interactive?
  stderr.print "\nCommit anyway? [y/n/e] "
  input = stdin.gets
  fail StartOverOnEditException if input =~ /e/i
  input =~ /y/i
end
clear_lines() click to toggle source
# File lib/fit_commit/runner.rb, line 97
def clear_lines
  @lines = nil
end
edit_message() click to toggle source
# File lib/fit_commit/runner.rb, line 105
def edit_message
  system(editor, message_path)
end
editor() click to toggle source
# File lib/fit_commit/runner.rb, line 109
def editor
  editor = ENV["EDITOR"]
  editor = DEFAULT_EDITOR unless editor && editor != "none"
  editor
end
empty_commit?() click to toggle source
# File lib/fit_commit/runner.rb, line 101
def empty_commit?
  lines.all?(&:empty?)
end
interactive?() click to toggle source
# File lib/fit_commit/runner.rb, line 61
def interactive?
  stdin.tty?
end
lines() click to toggle source
# File lib/fit_commit/runner.rb, line 93
def lines
  @lines ||= FitCommit::MessageParser.new(message_path).lines
end
print_results() click to toggle source
retry_on_user_edit() { || ... } click to toggle source
# File lib/fit_commit/runner.rb, line 44
def retry_on_user_edit
  yield
rescue StartOverOnEditException
  clear_lines
  clear_errors
  clear_warnings
  edit_message && retry
end
run_validators() click to toggle source
# File lib/fit_commit/runner.rb, line 65
def run_validators
  validators.each do |validator|
    validator.validate(lines)
    merge_errors(validator.errors)
    merge_warnings(validator.warnings)
  end
end
validators() click to toggle source
# File lib/fit_commit/runner.rb, line 73
def validators
  FitCommit::ValidatorLoader.new(branch_name).validators
end