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
14 changes: 13 additions & 1 deletion lib/overcommit/hook/pre_push/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ class Base < Overcommit::Hook::Base
def_delegators :@context, :remote_name, :remote_url, :pushed_refs

def skip?
super || exclude_remote_names.include?(remote_name)
super ||
exclude_remote_names.include?(remote_name) ||
skip_for_remote_branch_deletion?
end

private

def skip_for_remote_branch_deletion?
ignore_branch_deletions? && @context.remote_branch_deletion?
end

def ignore_branch_deletions?
@config['ignore_branch_deletions'] != false
end
end
end
9 changes: 9 additions & 0 deletions lib/overcommit/hook_context/pre_push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ def remote_url
@args[1]
end

def remote_branch_deletion?
return @remote_branch_deletion if defined? @remote_branch_deletion

@remote_branch_deletion ||= input_lines.
first.
split(' ').
first == '(deleted)'
end

def pushed_refs
input_lines.map do |line|
PushedRef.new(*line.split(' '))
Expand Down
86 changes: 82 additions & 4 deletions spec/overcommit/hook/pre_push/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

describe Overcommit::Hook::PrePush::Base do
let(:remote_name) { 'origin' }
let(:remote_branch_deletion?) { false }
let(:config) { double('config') }
let(:context) { double('context') }
let(:hook) { described_class.new(config, context) }
Expand All @@ -14,6 +15,7 @@

before do
allow(context).to receive(:remote_name).and_return(remote_name)
allow(context).to receive(:remote_branch_deletion?).and_return(remote_branch_deletion?)
allow(config).to receive(:for_hook).and_return(hook_config)
end

Expand Down Expand Up @@ -47,8 +49,6 @@
context 'skip is false and exclude_remote_names is nil' do
let(:skip) { false }
let(:exclude_remote_names) { nil }

it { subject.should == false }
end

context 'skip is true and matching exclude_remote_names is nil' do
Expand All @@ -61,8 +61,6 @@
context 'skip is false and matching exclude_remote_names is nil' do
let(:skip) { false }
let(:exclude_remote_names) { ['origin'] }

it { subject.should == true }
end

context 'skip is true and non-matching exclude_remote_names is nil' do
Expand All @@ -79,5 +77,85 @@
it { subject.should == false }
end
end

context 'with ignore_branch_deletions specified' do
let(:hook_config) do
{ 'skip' => skip, 'ignore_branch_deletions' => ignore_branch_deletions }
end
let(:remote_branch_deletion?) { false }
let(:ignore_branch_deletions) { false }

context(<<~DESC) do
skip is true and
remote_branch_deletion? is false and
ignore_branch_deletions false' do
DESC
let(:skip) { true }
let(:remote_branch_deletion?) { false }
let(:ignore_branch_deletions) { nil }

it { subject.should == true }
end

context(<<~DESC) do
skip is false and
remote_branch_deletion? is false and
ignore_branch_deletions false' do
DESC
let(:skip) { false }
let(:remote_branch_deletion?) { false }
let(:ignore_branch_deletions) { false }

it { subject.should == false }
end

context(<<~DESC) do
skip is false and
remote_branch_deletion? is true and
ignore_branch_deletions false' do
DESC
let(:skip) { false }
let(:remote_branch_deletion?) { true }
let(:ignore_branch_deletions) { false }

it { subject.should == false }
end

context(<<~DESC) do
skip is false and
remote_branch_deletion? is true and
ignore_branch_deletions true' do
DESC
let(:skip) { false }
let(:remote_branch_deletion?) { true }
let(:ignore_branch_deletions) { true }

it { subject.should == true }
end

context(<<~DESC) do
skip is false and
remote_branch_deletion? is false and
ignore_branch_deletions true' do
DESC
let(:skip) { false }
let(:remote_branch_deletion?) { false }
let(:ignore_branch_deletions) { true }

it { subject.should == false }
end

context(<<-DESC) do
skip is true and
remote_branch_deletion? is true and
ignore_branch_deletions true' do
DESC
let(:skip) { true }
let(:remote_branch_deletion?) { true }
let(:ignore_branch_deletions) { true }

it { subject.should == true }
end
end
end
end
35 changes: 35 additions & 0 deletions spec/overcommit/hook_context/pre_push_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,41 @@
it { should == remote_url }
end

describe '#remote_branch_deletion?' do
subject { context.remote_branch_deletion? }

before do
input.stub(:read).and_return("#{local_ref} #{local_sha1} #{remote_ref} #{remote_sha1}\n")
end

context 'when pushing new branch to remote ref' do
let(:local_ref) { 'refs/heads/test' }
let(:local_sha1) { '' }
let(:remote_ref) { 'refs/heads/test' }
let(:remote_sha1) { '0' * 40 }

it { should == false }
end

context 'when pushing update to remote ref' do
let(:local_ref) { 'refs/heads/test' }
let(:local_sha1) { '' }
let(:remote_ref) { 'refs/heads/test' }
let(:remote_sha1) { random_hash }

it { should == false }
end

context 'when deleting remote ref' do
let(:local_ref) { '(deleted)' }
let(:local_sha1) { '' }
let(:remote_ref) { 'refs/heads/test' }
let(:remote_sha1) { random_hash }

it { should == true }
end
end

describe '#pushed_refs' do
subject(:pushed_refs) { context.pushed_refs }

Expand Down