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