Skip to content

Commit f7b52fa

Browse files
authored
Add parsing of yamllint output (sds#747)
Parse the yamllint output so Overcommit has per-line awareness of changes to support problem_on_unmodified_line option.
1 parent 192c84b commit f7b52fa

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

lib/overcommit/hook/pre_commit/yaml_lint.rb

+25-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,34 @@ module Overcommit::Hook::PreCommit
55
#
66
# @see https://github.com/adrienverge/yamllint
77
class YamlLint < Base
8+
MESSAGE_REGEX = /
9+
^(?<file>.+)
10+
:(?<line>\d+)
11+
:(?<col>\d+)
12+
:\s\[(?<type>\w+)\]
13+
\s(?<msg>.+)$
14+
/x
15+
816
def run
917
result = execute(command, args: applicable_files)
18+
parse_messages(result.stdout)
19+
end
20+
21+
private
22+
23+
def parse_messages(output)
24+
repo_root = Overcommit::Utils.repo_root
25+
26+
output.scan(MESSAGE_REGEX).map do |file, line, col, type, msg|
27+
line = line.to_i
28+
type = type.to_sym
29+
# Obtain the path relative to the root of the repository
30+
# for nicer output:
31+
relpath = file.dup
32+
relpath.slice!("#{repo_root}/")
1033

11-
if result.success?
12-
:pass
13-
elsif result.stdout.include?('error')
14-
[:fail, result.stdout]
15-
else
16-
[:warn, result.stdout]
34+
text = "#{relpath}:#{line}:#{col}:#{type} #{msg}"
35+
Overcommit::Hook::Message.new(type, file, line, text)
1736
end
1837
end
1938
end

0 commit comments

Comments
 (0)