Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/overcommit/hook/commit_msg/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/overcommit/hook/commit_msg/capitalized_subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions lib/overcommit/hook/commit_msg/hard_tabs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions lib/overcommit/hook/commit_msg/single_line_subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/overcommit/hook/commit_msg/text_width.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/overcommit/hook/commit_msg/trailing_period.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/overcommit/hook_context/commit_msg.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/overcommit/hook/commit_msg/capitalized_subject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/overcommit/hook/commit_msg/hard_tabs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/overcommit/hook/commit_msg/single_line_subject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/overcommit/hook/commit_msg/text_width_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/overcommit/hook/commit_msg/trailing_period_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions spec/overcommit/hook_context/commit_msg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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