Skip to content

Commit ad88bf7

Browse files
committed
Flatten YAML configuration as per review comments
1 parent a359629 commit ad88bf7

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -683,21 +683,26 @@ with Overcommit without writing any Ruby code in a similar way as
683683

684684
These special line-aware command hooks behave and are configured the same way
685685
as the Git ones, except only file arguments get passed to them.
686-
Also they must have the `ad_hoc` option, so that, using the command output:
686+
Also to enable the feature, they must use at least one of the following options,
687+
so that, using the command output:
687688
- differentiating between warnings and errors becomes possible
688689
- modified lines can be detected and acted upon as defined by
689690
the `problem_on_unmodified_line`, `requires_files`, `include` and `exclude`
690691
[hook options](#hook-options)
691692

692693
**Warning**: Only the command's standard output stream is considered for now,
693694
*not* its standard error stream.
695+
If you do not need to change the default values for any other option,
696+
then the `extract_messages_from` option has to be specified.
697+
Its value does not matter for now, but it should be set to `stdout`
698+
to avoid problems in the future.
694699

695700
To differentiate between warning and error messages,
696-
the `warning_message_type_pattern` suboption may be specified:
701+
the `warning_message_type_pattern` option may be specified:
697702
the `type` field of the `message_pattern` regexp below must then include
698703
the `warning_message_type_pattern` option's text.
699704

700-
The `message_pattern` suboption specifies the format of the command's messages.
705+
The `message_pattern` option specifies the format of the command's messages.
701706
It is a optional [(Ruby) regexp][RubyRE], which if present must at least define
702707
a `file` [named capture group][RubyRENCG].
703708
The only other allowed ones are `line` and `type`, which when specified
@@ -714,9 +719,8 @@ PreCommit:
714719
CustomScript:
715720
enabled: true
716721
command: './bin/custom-script'
717-
ad_hoc:
718-
message_pattern: !ruby/regexp /^(?<file>[^:]+):(?<line>[0-9]+):(?<type>[^ ]+)/
719-
warning_message_type_pattern: warning
722+
message_pattern: !ruby/regexp /^(?<file>[^:]+):(?<line>[0-9]+):(?<type>[^ ]+)/
723+
warning_message_type_pattern: warning
720724
```
721725

722726
**Tip**: To get the syntax of the regexps right, a Ruby interpreter like `irb`

lib/overcommit/hook_loader/base.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,34 @@ def load_hooks
2626
AD_HOC_HOOK_DEFAULT_MESSAGE_PATTERN =
2727
/^(?<file>(?:\w:)?[^:]+):(?<line>\d+):[^ ]* (?<type>[^ ]+)/.freeze
2828

29-
def create_line_aware_command_hook_class(hook_base)
29+
def is_hook_line_aware(hook_config)
30+
hook_config['extract_messages_from'] ||
31+
hook_config['message_pattern'] ||
32+
hook_config['warning_message_type_pattern']
33+
end
34+
35+
def create_line_aware_command_hook_class(hook_base) # rubocop:disable Metrics/MethodLength
3036
Class.new(hook_base) do
37+
def line_aware_config
38+
{
39+
message_pattern: @config['message_pattern'],
40+
warning_message_type_pattern: @config['warning_message_type_pattern']
41+
}
42+
end
43+
3144
def run
3245
result = execute(command, args: applicable_files)
3346

3447
return :pass if result.success?
3548

36-
extract_messages(@config['ad_hoc'], result)
49+
extract_messages(line_aware_config, result)
3750
end
3851

39-
def extract_messages(ad_hoc_config, result)
40-
warning_message_type_pattern = ad_hoc_config['warning_message_type_pattern']
52+
def extract_messages(line_aware_config, result)
53+
warning_message_type_pattern = line_aware_config[:warning_message_type_pattern]
4154
Overcommit::Utils::MessagesUtils.extract_messages(
4255
result.stdout.split("\n"),
43-
ad_hoc_config['message_pattern'] ||
56+
line_aware_config[:message_pattern] ||
4457
AD_HOC_HOOK_DEFAULT_MESSAGE_PATTERN,
4558
Overcommit::Utils::MessagesUtils.create_type_categorizer(
4659
warning_message_type_pattern

lib/overcommit/hook_loader/plugin_hook_loader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def create_ad_hoc_hook(hook_name)
9595

9696
hook_config = @config.for_hook(hook_name, @context.hook_class_name)
9797
hook_class =
98-
if hook_config['ad_hoc']
98+
if is_hook_line_aware(hook_config)
9999
create_line_aware_command_hook_class(hook_base)
100100
else
101101
create_git_hook_class(hook_base)

spec/overcommit/hook_loader/plugin_hook_loader_spec.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,22 @@
7373
FooLint:
7474
enabled: true
7575
command: ["foo", "lint"]
76-
ad_hoc:
77-
message_pattern: !ruby/regexp /^(?<file>[^:]+):(?<line>[0-9]+):(?<type>[^ ]+)/
78-
warning_message_type_pattern: warning
76+
message_pattern: !ruby/regexp /^(?<file>[^:]+):(?<line>[0-9]+):(?<type>[^ ]+)/
77+
warning_message_type_pattern: warning
7978
flags:
8079
- "--format=emacs"
8180
include: '**/*.foo'
8281
FooLintDefault:
8382
enabled: true
8483
command: ["foo", "lint"]
85-
ad_hoc:
86-
warning_message_type_pattern: warning
84+
warning_message_type_pattern: warning
8785
flags:
8886
- "--format=emacs"
8987
include: '**/*.foo'
9088
FooLintDefaultNoWarnings:
9189
enabled: true
9290
command: ["foo", "lint"]
93-
ad_hoc:
91+
extract_messages_from: stdout
9492
flags:
9593
- "--format=emacs"
9694
include: '**/*.foo'

0 commit comments

Comments
 (0)