From 3b3fac5613ef43bc0103a056b5778e9116c33c52 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Sat, 2 May 2015 15:05:59 -0400 Subject: [PATCH 1/7] Add empty_message? method to CommitMsg hook context --- lib/overcommit/hook/commit_msg/base.rb | 5 +++-- lib/overcommit/hook_context/commit_msg.rb | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/overcommit/hook/commit_msg/base.rb b/lib/overcommit/hook/commit_msg/base.rb index 4cfcdd74..24241640 100644 --- a/lib/overcommit/hook/commit_msg/base.rb +++ b/lib/overcommit/hook/commit_msg/base.rb @@ -5,7 +5,8 @@ module Overcommit::Hook::CommitMsg class Base < Overcommit::Hook::Base extend Forwardable - def_delegators :@context, :commit_message, :update_commit_message, - :commit_message_lines, :commit_message_file + def_delegators :@context, :empty_message?, :commit_message, + :update_commit_message, :commit_message_lines, + :commit_message_file end end diff --git a/lib/overcommit/hook_context/commit_msg.rb b/lib/overcommit/hook_context/commit_msg.rb index db67ab64..b9f539f1 100644 --- a/lib/overcommit/hook_context/commit_msg.rb +++ b/lib/overcommit/hook_context/commit_msg.rb @@ -1,6 +1,10 @@ module Overcommit::HookContext # Contains helpers related to contextual information used by commit-msg hooks. class CommitMsg < Base + def empty_message? + commit_message.strip.empty? + end + # User commit message stripped of comments and diff (from verbose output). def commit_message commit_message_lines.join From 7b11e0a968ca4f2791df345568f9c665998e244f Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Sat, 2 May 2015 15:08:41 -0400 Subject: [PATCH 2/7] Pass CapitalizedSubject hook when commit message is empty --- lib/overcommit/hook/commit_msg/capitalized_subject.rb | 2 ++ .../overcommit/hook/commit_msg/capitalized_subject_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/overcommit/hook/commit_msg/capitalized_subject.rb b/lib/overcommit/hook/commit_msg/capitalized_subject.rb index c975cdef..bf720984 100644 --- a/lib/overcommit/hook/commit_msg/capitalized_subject.rb +++ b/lib/overcommit/hook/commit_msg/capitalized_subject.rb @@ -2,6 +2,8 @@ module Overcommit::Hook::CommitMsg # Ensures commit message subject lines start with a capital letter. class CapitalizedSubject < Base def run + return :pass if empty_message? + first_letter = commit_message_lines[0].to_s.match(/^[[:punct:]]*(.)/)[1] unless first_letter.match(/[[:upper:]]/) return :warn, 'Subject should start with a capital letter' diff --git a/spec/overcommit/hook/commit_msg/capitalized_subject_spec.rb b/spec/overcommit/hook/commit_msg/capitalized_subject_spec.rb index 6a1482d0..0775d818 100644 --- a/spec/overcommit/hook/commit_msg/capitalized_subject_spec.rb +++ b/spec/overcommit/hook/commit_msg/capitalized_subject_spec.rb @@ -8,6 +8,13 @@ before do subject.stub(:commit_message_lines).and_return(commit_msg.split("\n")) + subject.stub(:empty_message?).and_return(commit_msg.empty?) + end + + context 'when commit message is empty' do + let(:commit_msg) { '' } + + it { should pass } end context 'when subject starts with a capital letter' do From d0c37a0800ece92702ed9b63d785ae2c1c1c73ab Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Sat, 2 May 2015 15:09:01 -0400 Subject: [PATCH 3/7] Pass SingleLineSubject hook when commit message is empty --- lib/overcommit/hook/commit_msg/single_line_subject.rb | 2 ++ .../overcommit/hook/commit_msg/single_line_subject_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/overcommit/hook/commit_msg/single_line_subject.rb b/lib/overcommit/hook/commit_msg/single_line_subject.rb index 49e315ef..8e939536 100644 --- a/lib/overcommit/hook/commit_msg/single_line_subject.rb +++ b/lib/overcommit/hook/commit_msg/single_line_subject.rb @@ -2,6 +2,8 @@ module Overcommit::Hook::CommitMsg # Ensures commit message subject lines are followed by a blank line. class SingleLineSubject < Base def run + return :pass if empty_message? + unless commit_message_lines[1].to_s.strip.empty? return :warn, 'Subject should be one line and followed by a blank line' end diff --git a/spec/overcommit/hook/commit_msg/single_line_subject_spec.rb b/spec/overcommit/hook/commit_msg/single_line_subject_spec.rb index 7d83a388..be09f777 100644 --- a/spec/overcommit/hook/commit_msg/single_line_subject_spec.rb +++ b/spec/overcommit/hook/commit_msg/single_line_subject_spec.rb @@ -7,6 +7,13 @@ before do subject.stub(:commit_message_lines).and_return(commit_msg.split("\n")) + subject.stub(:empty_message?).and_return(commit_msg.empty?) + end + + context 'when commit message is empty' do + let(:commit_msg) { '' } + + it { should pass } end context 'when subject is separated from body by a blank line' do From e43669e3ba785f2c2d0e0dfec6f5028e824a79da Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Sat, 2 May 2015 15:09:17 -0400 Subject: [PATCH 4/7] Pass HardTabs hook when commit message is empty --- lib/overcommit/hook/commit_msg/hard_tabs.rb | 2 ++ spec/overcommit/hook/commit_msg/hard_tabs_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/overcommit/hook/commit_msg/hard_tabs.rb b/lib/overcommit/hook/commit_msg/hard_tabs.rb index e34ff6cf..dcd0356e 100644 --- a/lib/overcommit/hook/commit_msg/hard_tabs.rb +++ b/lib/overcommit/hook/commit_msg/hard_tabs.rb @@ -2,6 +2,8 @@ module Overcommit::Hook::CommitMsg # Checks for hard tabs in commit messages. class HardTabs < Base def run + return :pass if empty_message? + # Catches hard tabs entered by the user (not auto-generated) if commit_message.index(/\t/) return :warn, "Don't use hard tabs in commit messages" diff --git a/spec/overcommit/hook/commit_msg/hard_tabs_spec.rb b/spec/overcommit/hook/commit_msg/hard_tabs_spec.rb index db10d0ca..8da5db09 100644 --- a/spec/overcommit/hook/commit_msg/hard_tabs_spec.rb +++ b/spec/overcommit/hook/commit_msg/hard_tabs_spec.rb @@ -7,6 +7,13 @@ before do subject.stub(:commit_message).and_return(commit_msg) + subject.stub(:empty_message?).and_return(commit_msg.empty?) + end + + context 'when commit message is empty' do + let(:commit_msg) { '' } + + it { should pass } end context 'when message contains hard tabs' do From 852d1e85d8f21562c2a130b9e085b7a5b75dd302 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Sat, 2 May 2015 15:09:31 -0400 Subject: [PATCH 5/7] Pass TextWidth hook when commit message is empty --- lib/overcommit/hook/commit_msg/text_width.rb | 2 ++ spec/overcommit/hook/commit_msg/text_width_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/overcommit/hook/commit_msg/text_width.rb b/lib/overcommit/hook/commit_msg/text_width.rb index b56634da..b7bdfd53 100644 --- a/lib/overcommit/hook/commit_msg/text_width.rb +++ b/lib/overcommit/hook/commit_msg/text_width.rb @@ -3,6 +3,8 @@ module Overcommit::Hook::CommitMsg # under the preferred limits. class TextWidth < Base def run + return :pass if empty_message? + @errors = [] find_errors_in_subject(commit_message_lines.first) diff --git a/spec/overcommit/hook/commit_msg/text_width_spec.rb b/spec/overcommit/hook/commit_msg/text_width_spec.rb index e76be246..8ec9f97c 100644 --- a/spec/overcommit/hook/commit_msg/text_width_spec.rb +++ b/spec/overcommit/hook/commit_msg/text_width_spec.rb @@ -7,6 +7,13 @@ before do subject.stub(:commit_message_lines).and_return(commit_msg.split("\n")) + subject.stub(:empty_message?).and_return(commit_msg.empty?) + end + + context 'when commit message is empty' do + let(:commit_msg) { '' } + + it { should pass } end context 'when subject is longer than 60 characters' do From a4681bd92edcb0e29f61f0e98e22f4718efa1a76 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Sat, 2 May 2015 15:09:48 -0400 Subject: [PATCH 6/7] Pass TrailingPeriod hook when commit message is empty --- lib/overcommit/hook/commit_msg/trailing_period.rb | 2 ++ spec/overcommit/hook/commit_msg/trailing_period_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/overcommit/hook/commit_msg/trailing_period.rb b/lib/overcommit/hook/commit_msg/trailing_period.rb index ce7d444e..ea8725b8 100644 --- a/lib/overcommit/hook/commit_msg/trailing_period.rb +++ b/lib/overcommit/hook/commit_msg/trailing_period.rb @@ -2,6 +2,8 @@ module Overcommit::Hook::CommitMsg # Ensures commit message subject lines do not have a trailing period class TrailingPeriod < Base def run + return :pass if empty_message? + if commit_message_lines.first.rstrip.end_with?('.') return :warn, 'Please omit trailing period from commit message subject' end diff --git a/spec/overcommit/hook/commit_msg/trailing_period_spec.rb b/spec/overcommit/hook/commit_msg/trailing_period_spec.rb index 4a87e8c1..5d86248d 100644 --- a/spec/overcommit/hook/commit_msg/trailing_period_spec.rb +++ b/spec/overcommit/hook/commit_msg/trailing_period_spec.rb @@ -7,6 +7,13 @@ before do subject.stub(:commit_message_lines).and_return(commit_msg.split("\n")) + subject.stub(:empty_message?).and_return(commit_msg.empty?) + end + + context 'when commit message is empty' do + let(:commit_msg) { '' } + + it { should pass } end context 'when subject contains a trailing period' do From 70e2f3535f9baad156b50819723a0b261091d2c1 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Sat, 2 May 2015 15:29:35 -0400 Subject: [PATCH 7/7] Add spec for HookContext::CommitMsg#empty_message? --- .../hook_context/commit_msg_spec.rb | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/spec/overcommit/hook_context/commit_msg_spec.rb b/spec/overcommit/hook_context/commit_msg_spec.rb index 782de26b..6db67dcd 100644 --- a/spec/overcommit/hook_context/commit_msg_spec.rb +++ b/spec/overcommit/hook_context/commit_msg_spec.rb @@ -40,4 +40,26 @@ subject.should == ["Some commit message\n"] end end + + describe '#empty_message?' do + subject { context.empty_message? } + + context 'when commit message is empty' do + let(:commit_msg) { [] } + + it { should == true } + end + + context 'when commit message contains only whitespace' do + let(:commit_msg) { [' '] } + + it { should == true } + end + + context 'when commit message is not empty' do + let(:commit_msg) { ['Some commit message'] } + + it { should == false } + end + end end