Skip to content

Commit b68ebf1

Browse files
committed
Rename MsgPattern hook to MessageFormat
A bit nit-picky, but we've already established a convention of using "Message" instead of the "Msg" short form (CommitMsg doesn't count as it is a separate naming scheme brought over from Git). Also, I personally find "Format" a better descriptor than "Pattern", as the later implies some sort of repetition, where the former is simply "the way in which something is arranged or set out." (definition) I updated the implementation to not use instance variables (since there was no need).
1 parent 7fa0e12 commit b68ebf1

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* Improve error message output when there is a problem processing messages
1010
via `extract_messages` pre-commit hook helper
1111
* Fix `Minitest` pre-push hook to include all test files
12+
* Add `MessageFormat` commit-msg hook to validate commit messages against
13+
a regex pattern
1214

1315
## 0.32.0.rc1
1416

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,12 @@ follow [proper formatting guidelines](http://tbaggery.com/2008/04/19/a-note-abou
378378
* [`*`EmptyMessage](lib/overcommit/hook/commit_msg/empty_message.rb)
379379
* [GerritChangeId](lib/overcommit/hook/commit_msg/gerrit_change_id.rb)
380380
* [HardTabs](lib/overcommit/hook/commit_msg/hard_tabs.rb)
381+
* [MessageFormat](lib/overcommit/hook/commit_msg/message_format.rb)
381382
* [RussianNovel](lib/overcommit/hook/commit_msg/russian_novel.rb)
382383
* [`*`SingleLineSubject](lib/overcommit/hook/commit_msg/single_line_subject.rb)
383384
* [SpellCheck](lib/overcommit/hook/commit_msg/spell_check.rb)
384385
* [`*`TextWidth](lib/overcommit/hook/commit_msg/text_width.rb)
385386
* [`*`TrailingPeriod](lib/overcommit/hook/commit_msg/trailing_period.rb)
386-
* [`*`MsgPattern](lib/overcommit/hook/commit_msg/msg_pattern.rb)
387387

388388
### PostCheckout
389389

config/default.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ CommitMsg:
7878
enabled: false
7979
description: 'Checking for hard tabs'
8080

81+
MessageFormat:
82+
enabled: false
83+
description: 'Checking commit message matches expected pattern'
84+
pattern: '(.+)[|](.+)[|](.+)'
85+
expected_pattern_message: '<Issue Id> | <Commit Message Description> | <Developer(s)>'
86+
sample_message: 'DEFECT-1234 | Refactored Onboarding flow | John Doe'
87+
8188
RussianNovel:
8289
enabled: false
8390
description: 'Checking length of commit message'
@@ -103,13 +110,6 @@ CommitMsg:
103110
enabled: true
104111
description: 'Checking for trailing periods in subject'
105112

106-
MsgPattern:
107-
enabled: false
108-
description: 'Checking for message pattern'
109-
pattern: '(.+)[|](.+)[|](.+)'
110-
expected_pattern_message: '<Issue Id> | <Commit Message Description> | <Developer(s)>'
111-
sample_message: 'DEFECT-1234 | Refactored Onboarding flow | John Doe'
112-
113113
# Hooks that are run after `git commit` is executed, before the commit message
114114
# editor is displayed. These hooks are ideal for syntax checkers, linters, and
115115
# other checks that you want to run before you allow a commit object to be

lib/overcommit/hook/commit_msg/msg_pattern.rb lib/overcommit/hook/commit_msg/message_format.rb

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
module Overcommit::Hook::CommitMsg
2-
# Ensures the commit message follows a specific pattern for analytical purposes
3-
class MsgPattern < Base
2+
# Ensures the commit message follows a specific format.
3+
class MessageFormat < Base
44
def run
5-
return :fail, "Empty Message not allowed." if empty_message?
5+
return :fail, "Empty message not allowed." if empty_message?
66

7-
@errors = []
8-
validate_pattern(commit_message_lines.join("\n"))
9-
return :fail, @errors.join("\n") if @errors.any?
7+
error_msg = validate_pattern(commit_message_lines.join("\n"))
8+
return :fail, error_msg if error_msg
109

1110
:pass
1211
end
@@ -15,10 +14,12 @@ def run
1514

1615
def validate_pattern(message)
1716
pattern = config['pattern']
18-
expected_pattern_message = config['expected_pattern_message'] || ''
19-
sample_message = config['sample_message'] || ''
2017
return if pattern.empty?
21-
@errors << [
18+
19+
expected_pattern_message = config['expected_pattern_message']
20+
sample_message = config['sample_message']
21+
22+
[
2223
'Commit message pattern mismatch.',
2324
"Expected : #{expected_pattern_message}",
2425
"Sample : #{sample_message}"

spec/overcommit/hook/commit_msg/msg_pattern_spec.rb spec/overcommit/hook/commit_msg/message_format_spec.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
describe Overcommit::Hook::CommitMsg::MsgPattern do
3+
describe Overcommit::Hook::CommitMsg::MessageFormat do
44
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
55
let(:context) { double('context') }
66
subject { described_class.new(config, context) }
@@ -20,23 +20,27 @@
2020
let(:config) do
2121
super().merge(Overcommit::Configuration.new(
2222
'CommitMsg' => {
23-
'MsgPattern' => {
23+
'MessageFormat' => {
2424
'pattern' => nil
2525
}
2626
}
2727
))
2828
end
29+
2930
let(:commit_msg) { 'Some Message' }
31+
3032
it { should pass }
3133
end
3234

3335
context 'when message does not match the pattern' do
3436
let(:commit_msg) { 'Some Message' }
37+
3538
expected_message = [
3639
'Commit message pattern mismatch.',
3740
'Expected : <Issue Id> | <Commit Message Description> | <Developer(s)>',
3841
'Sample : DEFECT-1234 | Refactored Onboarding flow | John Doe'
3942
].join("\n")
43+
4044
it { should fail_hook expected_message }
4145
end
4246
end

0 commit comments

Comments
 (0)