Sha256: a7dae770c666751af8b01906a6ba77447380baa2ef067babaa6084da6fc1dc7c

Contents?: true

Size: 1.18 KB

Versions: 30

Compression:

Stored size: 1.18 KB

Contents

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

30 entries across 28 versions & 2 rubygems

Version Path
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/overcommit-0.46.0/lib/overcommit/hook/commit_msg/spell_check.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/overcommit-0.46.0/lib/overcommit/hook/commit_msg/spell_check.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/overcommit-0.46.0/lib/overcommit/hook/commit_msg/spell_check.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/overcommit-0.46.0/lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.46.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.45.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.44.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.43.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.42.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.41.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.40.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.39.1 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.39.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.38.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.37.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.36.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.35.0 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.34.2 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.34.1 lib/overcommit/hook/commit_msg/spell_check.rb
overcommit-0.34.0 lib/overcommit/hook/commit_msg/spell_check.rb