Sha256: f63fb824b1a5d758ee1cf1c6550bbda1b1189d6de8eba50ae285c4d366dfd42e

Contents?: true

Size: 1.21 KB

Versions: 19

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'tempfile'

module Overcommit::Hook::CommitMsg
  # Checks the commit message for potential misspellings with `hunspell`.
  #
  # @see http://hunspell.sourceforge.net/
  class SpellCheck < Base
    Misspelling = Struct.new(:word, :suggestions)

    MISSPELLING_REGEX = /^[&#]\s(?<word>\w+)(?:.+?:\s(?<suggestions>.*))?/

    def run
      result = execute(command + [uncommented_commit_msg_file])
      return [:fail, "Error running spellcheck: #{result.stderr.chomp}"] unless result.success?

      misspellings = parse_misspellings(result.stdout)
      return :pass if misspellings.empty?

      messages = misspellings.map do |misspelled|
        msg = "Potential misspelling: #{misspelled.word}."
        msg += " Suggestions: #{misspelled.suggestions}" unless misspelled.suggestions.nil?
        msg
      end

      [:warn, messages.join("\n")]
    end

    private

    def uncommented_commit_msg_file
      ::Tempfile.open('commit-msg') do |file|
        file.write(commit_message)
        file.path
      end
    end

    def parse_misspellings(output)
      output.scan(MISSPELLING_REGEX).map do |word, suggestions|
        Misspelling.new(word, suggestions)
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
overcommit-0.58.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.57.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.56.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.55.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.54.1 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.54.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-jeygeethanmedia-0.53.1.2 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-jeygeethanmedia-0.53.1.1 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-jeygeethanmedia-0.53.1 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.53.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.52.1 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.52.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.51.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.50.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.49.1 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.49.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.48.1 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.48.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.47.0 lib/overcommit/hook/commit_msg/spell_check.rb