File tree 5 files changed +26
-19
lines changed
lib/overcommit/hook/commit_msg
spec/overcommit/hook/commit_msg
5 files changed +26
-19
lines changed Original file line number Diff line number Diff line change 9
9
* Improve error message output when there is a problem processing messages
10
10
via ` extract_messages ` pre-commit hook helper
11
11
* 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
12
14
13
15
## 0.32.0.rc1
14
16
Original file line number Diff line number Diff line change @@ -378,12 +378,12 @@ follow [proper formatting guidelines](http://tbaggery.com/2008/04/19/a-note-abou
378
378
* [`*`EmptyMessage](lib/overcommit/hook/commit_msg/empty_message.rb)
379
379
* [GerritChangeId](lib/overcommit/hook/commit_msg/gerrit_change_id.rb)
380
380
* [HardTabs](lib/overcommit/hook/commit_msg/hard_tabs.rb)
381
+ * [MessageFormat](lib/overcommit/hook/commit_msg/message_format.rb)
381
382
* [RussianNovel](lib/overcommit/hook/commit_msg/russian_novel.rb)
382
383
* [`*`SingleLineSubject](lib/overcommit/hook/commit_msg/single_line_subject.rb)
383
384
* [SpellCheck](lib/overcommit/hook/commit_msg/spell_check.rb)
384
385
* [`*`TextWidth](lib/overcommit/hook/commit_msg/text_width.rb)
385
386
* [`*`TrailingPeriod](lib/overcommit/hook/commit_msg/trailing_period.rb)
386
- * [`*`MsgPattern](lib/overcommit/hook/commit_msg/msg_pattern.rb)
387
387
388
388
# ## PostCheckout
389
389
Original file line number Diff line number Diff line change @@ -78,6 +78,13 @@ CommitMsg:
78
78
enabled : false
79
79
description : ' Checking for hard tabs'
80
80
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
+
81
88
RussianNovel :
82
89
enabled : false
83
90
description : ' Checking length of commit message'
@@ -103,13 +110,6 @@ CommitMsg:
103
110
enabled : true
104
111
description : ' Checking for trailing periods in subject'
105
112
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
-
113
113
# Hooks that are run after `git commit` is executed, before the commit message
114
114
# editor is displayed. These hooks are ideal for syntax checkers, linters, and
115
115
# other checks that you want to run before you allow a commit object to be
Original file line number Diff line number Diff line change 1
1
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
4
4
def run
5
- return :fail , "Empty Message not allowed." if empty_message?
5
+ return :fail , "Empty message not allowed." if empty_message?
6
6
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
10
9
11
10
:pass
12
11
end
@@ -15,10 +14,12 @@ def run
15
14
16
15
def validate_pattern ( message )
17
16
pattern = config [ 'pattern' ]
18
- expected_pattern_message = config [ 'expected_pattern_message' ] || ''
19
- sample_message = config [ 'sample_message' ] || ''
20
17
return if pattern . empty?
21
- @errors << [
18
+
19
+ expected_pattern_message = config [ 'expected_pattern_message' ]
20
+ sample_message = config [ 'sample_message' ]
21
+
22
+ [
22
23
'Commit message pattern mismatch.' ,
23
24
"Expected : #{ expected_pattern_message } " ,
24
25
"Sample : #{ sample_message } "
Original file line number Diff line number Diff line change 1
1
require 'spec_helper'
2
2
3
- describe Overcommit ::Hook ::CommitMsg ::MsgPattern do
3
+ describe Overcommit ::Hook ::CommitMsg ::MessageFormat do
4
4
let ( :config ) { Overcommit ::ConfigurationLoader . default_configuration }
5
5
let ( :context ) { double ( 'context' ) }
6
6
subject { described_class . new ( config , context ) }
20
20
let ( :config ) do
21
21
super ( ) . merge ( Overcommit ::Configuration . new (
22
22
'CommitMsg' => {
23
- 'MsgPattern ' => {
23
+ 'MessageFormat ' => {
24
24
'pattern' => nil
25
25
}
26
26
}
27
27
) )
28
28
end
29
+
29
30
let ( :commit_msg ) { 'Some Message' }
31
+
30
32
it { should pass }
31
33
end
32
34
33
35
context 'when message does not match the pattern' do
34
36
let ( :commit_msg ) { 'Some Message' }
37
+
35
38
expected_message = [
36
39
'Commit message pattern mismatch.' ,
37
40
'Expected : <Issue Id> | <Commit Message Description> | <Developer(s)>' ,
38
41
'Sample : DEFECT-1234 | Refactored Onboarding flow | John Doe'
39
42
] . join ( "\n " )
43
+
40
44
it { should fail_hook expected_message }
41
45
end
42
46
end
You can’t perform that action at this time.
0 commit comments