File tree 10 files changed +199
-16
lines changed
overcommit/hook/commit_msg
10 files changed +199
-16
lines changed Original file line number Diff line number Diff line change @@ -6,11 +6,13 @@ module Overcommit::Hook::CommitMsg
6
6
# you need to do it in a commit-msg hook. This is because the user could still
7
7
# edit the message after a prepare-commit-msg hook was run.
8
8
class GerritChangeId < Base
9
- SCRIPT_LOCATION = Overcommit ::Utils . script_path ( 'gerrit-change-id' )
10
-
11
9
def run
12
- command "#{ SCRIPT_LOCATION } #{ commit_message_file } "
13
- :good
10
+ result = command ( "#{ SCRIPT_LOCATION } #{ commit_message_file } " )
11
+ return ( result . success? ? :good : :bad ) , result . stdout
14
12
end
13
+
14
+ private
15
+
16
+ SCRIPT_LOCATION = Overcommit ::Utils . script_path ( 'gerrit-change-id' )
15
17
end
16
18
end
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ class RussianNovel < Base
4
4
RUSSIAN_NOVEL_LENGTH = 30
5
5
6
6
def run
7
- if commit_message . length > RUSSIAN_NOVEL_LENGTH
7
+ if commit_message . length >= RUSSIAN_NOVEL_LENGTH
8
8
return :warn , 'You seem to have authored a Russian novel; congratulations!'
9
9
end
10
10
Original file line number Diff line number Diff line change 1
1
module Overcommit ::HookContext
2
2
class CommitMsg < Base
3
- # User commit message stripped of comments and diff (from verbose output)
3
+ # User commit message stripped of comments and diff (from verbose output).
4
4
def commit_message
5
- @commit_message ||= raw_commit_message .
5
+ raw_commit_message .
6
6
reject { |line | line =~ /^#/ } .
7
7
take_while { |line | !line . start_with? ( 'diff --git' ) }
8
8
end
9
9
10
- private
11
-
12
- def raw_commit_message
13
- @raw_commit_message ||= ::IO . readlines ( commit_message_file )
10
+ def commit_message_file
11
+ @args [ 0 ]
14
12
end
15
13
16
- def commit_message_file
17
- unless @args [ 0 ] && ::File . exist? ( @args [ 0 ] )
18
- fail 'Not running in the context of a commit message'
19
- end
14
+ private
20
15
21
- @args [ 0 ]
16
+ def raw_commit_message
17
+ ::IO . readlines ( commit_message_file )
22
18
end
23
19
end
24
20
end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe Overcommit ::Hook ::CommitMsg ::GerritChangeId do
4
+ let ( :config ) { Overcommit ::ConfigurationLoader . default_configuration }
5
+ let ( :context ) { double ( 'context' ) }
6
+ let ( :subject ) { described_class . new ( config , context ) }
7
+ let ( :commit_msg_file ) { Tempfile . new ( 'commit-msg' ) }
8
+
9
+ before do
10
+ commit_msg_file . write ( commit_msg )
11
+ commit_msg_file . close
12
+ subject . stub ( :commit_message_file ) . and_return ( commit_msg_file . path )
13
+ end
14
+
15
+ context 'when the commit message contains no Change-Id' do
16
+ let ( :commit_msg ) { 'Add code to repo' }
17
+
18
+ it { should pass }
19
+
20
+ it 'adds a Change-Id to the commit message' do
21
+ subject . run
22
+ File . open ( commit_msg_file . path , 'r' ) . read . should =~ /Change-Id/
23
+ end
24
+ end
25
+
26
+ context 'when the commit message already contains a Change-Id' do
27
+ let ( :commit_msg ) { "Add code to repo\n \n Change-Id: I9f2b5528fa20ac91a55bbe9371e76a12dd1cce11" }
28
+
29
+ it { should pass }
30
+
31
+ it 'does nothing' do
32
+ before = File . open ( commit_msg_file . path , 'r' ) . read
33
+ subject . run
34
+ after = File . open ( commit_msg_file . path , 'r' ) . read
35
+ before . should == after
36
+ end
37
+ end
38
+ end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe Overcommit ::Hook ::CommitMsg ::HardTabs do
4
+ let ( :config ) { Overcommit ::ConfigurationLoader . default_configuration }
5
+ let ( :context ) { double ( 'context' ) }
6
+ let ( :subject ) { described_class . new ( config , context ) }
7
+
8
+ before do
9
+ subject . stub ( :commit_message ) . and_return ( commit_msg )
10
+ end
11
+
12
+ context 'when message contains hard tabs' do
13
+ let ( :commit_msg ) { [ "This is a hard-tab\t commit message" ] }
14
+
15
+ it { should warn }
16
+ end
17
+
18
+ context 'when message does not contain hard tabs' do
19
+ let ( :commit_msg ) { [ 'No hard tabs to be found' ] }
20
+
21
+ it { should pass }
22
+ end
23
+ end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe Overcommit ::Hook ::CommitMsg ::RussianNovel do
4
+ let ( :config ) { Overcommit ::ConfigurationLoader . default_configuration }
5
+ let ( :context ) { double ( 'context' ) }
6
+ let ( :subject ) { described_class . new ( config , context ) }
7
+
8
+ before do
9
+ subject . stub ( :commit_message ) . and_return ( commit_msg )
10
+ end
11
+
12
+ context 'when message contains fewer than 30 lines' do
13
+ let ( :commit_msg ) { [ 'A single line' ] * 10 }
14
+
15
+ it { should pass }
16
+ end
17
+
18
+ context 'when message contains at least 30 lines' do
19
+ let ( :commit_msg ) { [ 'A single line' ] * 30 }
20
+
21
+ it { should warn }
22
+ end
23
+ end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe Overcommit ::Hook ::CommitMsg ::SingleLineSubject do
4
+ let ( :config ) { Overcommit ::ConfigurationLoader . default_configuration }
5
+ let ( :context ) { double ( 'context' ) }
6
+ let ( :subject ) { described_class . new ( config , context ) }
7
+
8
+ before do
9
+ subject . stub ( :commit_message ) . and_return ( commit_msg . split ( "\n " ) )
10
+ end
11
+
12
+ context 'when subject is separated from body by a blank line' do
13
+ let ( :commit_msg ) { <<-MSG }
14
+ Initial commit
15
+
16
+ Mostly cats so far.
17
+ MSG
18
+
19
+ it { should pass }
20
+ end
21
+
22
+ context 'when subject is not kept to one line' do
23
+ let ( :commit_msg ) { <<-MSG }
24
+ Initial commit where I forget about commit message
25
+ standards and decide to hard-wrap my subject
26
+
27
+ Still mostly cats so far.
28
+ MSG
29
+
30
+ it { should warn }
31
+ end
32
+ end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe Overcommit ::Hook ::CommitMsg ::TextWidth do
4
+ let ( :config ) { Overcommit ::ConfigurationLoader . default_configuration }
5
+ let ( :context ) { double ( 'context' ) }
6
+ let ( :subject ) { described_class . new ( config , context ) }
7
+
8
+ before do
9
+ subject . stub ( :commit_message ) . and_return ( commit_msg . split ( "\n " ) )
10
+ end
11
+
12
+ context 'when subject is longer than 60 characters' do
13
+ let ( :commit_msg ) { 'A' * 61 }
14
+
15
+ it { should warn /subject/ }
16
+ end
17
+
18
+ context 'when subject is 60 characters or fewer' do
19
+ let ( :commit_msg ) { 'A' * 60 }
20
+
21
+ it { should pass }
22
+ end
23
+
24
+ context 'when a line in the message is longer than 72 characters' do
25
+ let ( :commit_msg ) { <<-MSG }
26
+ Some summary
27
+
28
+ This line is longer than 72 characters which is clearly be seen by count.
29
+ MSG
30
+
31
+ it { should warn /72 char/ }
32
+ end
33
+
34
+ context 'when all lines in the message are fewer than 72 characters' do
35
+ let ( :commit_msg ) { <<-MSG }
36
+ Some summary
37
+
38
+ A reasonable line.
39
+
40
+ Another reasonable line.
41
+ MSG
42
+
43
+ it { should pass }
44
+ end
45
+ end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe Overcommit ::Hook ::CommitMsg ::TrailingPeriod do
4
+ let ( :config ) { Overcommit ::ConfigurationLoader . default_configuration }
5
+ let ( :context ) { double ( 'context' ) }
6
+ let ( :subject ) { described_class . new ( config , context ) }
7
+
8
+ before do
9
+ subject . stub ( :commit_message ) . and_return ( commit_msg . split ( "\n " ) )
10
+ end
11
+
12
+ context 'when subject contains a trailing period' do
13
+ let ( :commit_msg ) { 'This subject has a period.' }
14
+
15
+ it { should warn }
16
+ end
17
+
18
+ context 'when subject does not contain a trailing period' do
19
+ let ( :commit_msg ) { 'This subject has no period' }
20
+
21
+ it { should pass }
22
+ end
23
+ end
Original file line number Diff line number Diff line change 1
1
require 'overcommit'
2
+ require 'tempfile'
2
3
3
4
hook_types = Dir [ File . join ( Overcommit ::OVERCOMMIT_HOME , 'lib/overcommit/hook/*' ) ] .
4
5
select { |f | File . directory? ( f ) } .
You can’t perform that action at this time.
0 commit comments