-
Notifications
You must be signed in to change notification settings - Fork 280
/
yaml_lint.rb
39 lines (33 loc) · 971 Bytes
/
yaml_lint.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
# frozen_string_literal: true
module Overcommit::Hook::PreCommit
# Runs `YAMLLint` against any modified YAML files.
#
# @see https://github.com/adrienverge/yamllint
class YamlLint < Base
MESSAGE_REGEX = /
^(?<file>.+)
:(?<line>\d+)
:(?<col>\d+)
:\s\[(?<type>\w+)\]
\s(?<msg>.+)$
/x.freeze
def run
result = execute(command, args: applicable_files)
parse_messages(result.stdout)
end
private
def parse_messages(output)
repo_root = Overcommit::Utils.repo_root
output.scan(MESSAGE_REGEX).map do |file, line, col, type, msg|
line = line.to_i
type = type.to_sym
# Obtain the path relative to the root of the repository
# for nicer output:
relpath = file.dup
relpath.slice!("#{repo_root}/")
text = "#{relpath}:#{line}:#{col}:#{type} #{msg}"
Overcommit::Hook::Message.new(type, file, line, text)
end
end
end
end