Skip to content
Closed
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
8 changes: 7 additions & 1 deletion lib/overcommit/hook_context/pre_push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def pushed_refs

PushedRef = Struct.new(:local_ref, :local_sha1, :remote_ref, :remote_sha1) do
def forced?
`git rev-list #{remote_sha1} ^#{local_sha1}`.chomp.any?
!(created? || deleted? || overwritten_commits.empty?)
end

def created?
Expand All @@ -33,6 +33,12 @@ def deleted?
def to_s
"#{local_ref} #{local_sha1} #{remote_ref} #{remote_sha1}"
end

private

def overwritten_commits
`git rev-list #{remote_sha1} ^#{local_sha1}`.split("\n")
end
end
end
end
88 changes: 88 additions & 0 deletions spec/overcommit/hook_context/pre_push_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,92 @@
end
end
end

describe Overcommit::HookContext::PrePush::PushedRef do
let(:local_ref) { 'refs/heads/master' }
let(:remote_ref) { 'refs/heads/master' }
let(:local_sha1) { random_hash }
let(:remote_sha1) { random_hash }
let(:pushed_ref) { described_class.new(local_ref, local_sha1, remote_ref, remote_sha1) }

describe '#forced?' do
subject { pushed_ref.forced? }

context 'when creating a ref' do
before do
pushed_ref.stub(created?: true, deleted?: false)
end

it { should == false }
end

context 'when deleting a ref' do
before do
pushed_ref.stub(created?: false, deleted?: true)
end

it { should == false }
end

context 'when remote commits are not overwritten' do
before do
pushed_ref.stub(created?: false,
deleted?: false,
overwritten_commits: [])
end

it { should == false }
end

context 'when remote commits are overwritten' do
before do
pushed_ref.stub(created?: false,
deleted?: false,
overwritten_commits: [random_hash])
end

it { should == true }
end
end

describe '#created?' do
subject { pushed_ref.created? }

context 'when creating a ref' do
before do
pushed_ref.stub(:remote_sha1).and_return('0' * 40)
end

it { should == true }
end

context 'when not creating a ref' do
before do
pushed_ref.stub(:remote_sha1).and_return(random_hash)
end

it { should == false }
end
end

describe '#deleted?' do
subject { pushed_ref.deleted? }

context 'when deleting a ref' do
before do
pushed_ref.stub(:local_sha1).and_return('0' * 40)
end

it { should == true }
end

context 'when not deleting a ref' do
before do
pushed_ref.stub(:local_sha1).and_return(random_hash)
end

it { should == false }
end
end
end
end