From 496f47d806df5f3d536b9668b0705c73b30be9e2 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Mon, 6 Apr 2015 00:31:21 -0400 Subject: [PATCH 1/2] Fix missing method error in PushedRef#forced? --- lib/overcommit/hook_context/pre_push.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/overcommit/hook_context/pre_push.rb b/lib/overcommit/hook_context/pre_push.rb index 6c255a48..a7c26bce 100644 --- a/lib/overcommit/hook_context/pre_push.rb +++ b/lib/overcommit/hook_context/pre_push.rb @@ -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? @@ -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 From 297bd0c7c947c69ec103aa41b378bc786331f046 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Mon, 6 Apr 2015 00:56:26 -0400 Subject: [PATCH 2/2] Add specs for PushedRef --- spec/overcommit/hook_context/pre_push_spec.rb | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/spec/overcommit/hook_context/pre_push_spec.rb b/spec/overcommit/hook_context/pre_push_spec.rb index da0f7831..8f182bb2 100644 --- a/spec/overcommit/hook_context/pre_push_spec.rb +++ b/spec/overcommit/hook_context/pre_push_spec.rb @@ -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