Skip to content

Commit 5b5f9ea

Browse files
jawshooahsds
authored andcommittedFeb 23, 2015
Expose initial_commit? to post-commit hooks
Change-Id: I8a0105a499eb1c3ad648f5feec85c1cbc09c2731 Reviewed-on: http://gerrit.causes.com/46927 Reviewed-by: Shane da Silva <shane.dasilva@brigade.com> Tested-by: Shane da Silva <shane.dasilva@brigade.com>
1 parent d6f66ad commit 5b5f9ea

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed
 

‎lib/overcommit/hook/post_commit/base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ module Overcommit::Hook::PostCommit
55
class Base < Overcommit::Hook::Base
66
extend Forwardable
77

8-
def_delegators :@context, :modified_lines_in_file
8+
def_delegators :@context, :modified_lines_in_file, :initial_commit?
99
end
1010
end

‎lib/overcommit/hook_context/post_commit.rb

+4-6
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ def modified_lines_in_file(file)
1919
Overcommit::GitRepo.extract_modified_lines(file, subcmd: subcmd)
2020
end
2121

22-
private
23-
24-
# Returns whether a previous commit exists on the current git branch.
22+
# Returns whether the current git branch has only one commit.
2523
# @return [true,false]
26-
def previous_commit?
27-
return @previous_commit unless @previous_commit.nil?
28-
@previous_commit = Overcommit::Utils.execute(%w[git rev-parse HEAD~]).success?
24+
def initial_commit?
25+
return @initial_commit unless @initial_commit.nil?
26+
@initial_commit = !Overcommit::Utils.execute(%w[git rev-parse HEAD~]).success?
2927
end
3028
end
3129
end

‎spec/overcommit/hook_context/post_commit_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,31 @@
113113
it { should == Set.new(1..3) }
114114
end
115115
end
116+
117+
describe '#initial_commit?' do
118+
subject { context.initial_commit? }
119+
120+
context 'when a previous commit exists' do
121+
around do |example|
122+
repo do
123+
`git commit --allow-empty -m "Initial commit"`
124+
`git commit --allow-empty -m "Another commit"`
125+
example.run
126+
end
127+
end
128+
129+
it { should == false }
130+
end
131+
132+
context 'when no previous commit exists' do
133+
around do |example|
134+
repo do
135+
`git commit --allow-empty -m "Initial commit"`
136+
example.run
137+
end
138+
end
139+
140+
it { should == true }
141+
end
142+
end
116143
end

0 commit comments

Comments
 (0)
Please sign in to comment.