forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspell_check.rb
43 lines (34 loc) · 1.18 KB
/
spell_check.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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