Skip to content

Commit e7b8907

Browse files
taufeksds
authored andcommitted
Introduce 'ignore_branch_deletions' Config for PrePush Hooks
Context = There are scenario we would like to skip pre-push Hooks when deleting remote branch. Below is an example of git command to delete remote branch. ``` git push origin :to-be-deleted-branch ``` When we set `ignore_branch_deletions` as true for a PrePush Hook, it will be skipped during remote branch deletion.
1 parent 5880c2c commit e7b8907

File tree

4 files changed

+139
-5
lines changed

4 files changed

+139
-5
lines changed

lib/overcommit/hook/pre_push/base.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,19 @@ class Base < Overcommit::Hook::Base
1010
def_delegators :@context, :remote_name, :remote_url, :pushed_refs
1111

1212
def skip?
13-
super || exclude_remote_names.include?(remote_name)
13+
super ||
14+
exclude_remote_names.include?(remote_name) ||
15+
skip_for_remote_branch_deletion?
16+
end
17+
18+
private
19+
20+
def skip_for_remote_branch_deletion?
21+
ignore_branch_deletions? && @context.remote_branch_deletion?
22+
end
23+
24+
def ignore_branch_deletions?
25+
@config['ignore_branch_deletions'] != false
1426
end
1527
end
1628
end

lib/overcommit/hook_context/pre_push.rb

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ def remote_url
1313
@args[1]
1414
end
1515

16+
def remote_branch_deletion?
17+
return @remote_branch_deletion if defined? @remote_branch_deletion
18+
19+
@remote_branch_deletion ||= input_lines.
20+
first.
21+
split(' ').
22+
first == '(deleted)'
23+
end
24+
1625
def pushed_refs
1726
input_lines.map do |line|
1827
PushedRef.new(*line.split(' '))

spec/overcommit/hook/pre_push/base_spec.rb

+82-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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

1516
before do
1617
allow(context).to receive(:remote_name).and_return(remote_name)
18+
allow(context).to receive(:remote_branch_deletion?).and_return(remote_branch_deletion?)
1719
allow(config).to receive(:for_hook).and_return(hook_config)
1820
end
1921

@@ -47,8 +49,6 @@
4749
context 'skip is false and exclude_remote_names is nil' do
4850
let(:skip) { false }
4951
let(:exclude_remote_names) { nil }
50-
51-
it { subject.should == false }
5252
end
5353

5454
context 'skip is true and matching exclude_remote_names is nil' do
@@ -61,8 +61,6 @@
6161
context 'skip is false and matching exclude_remote_names is nil' do
6262
let(:skip) { false }
6363
let(:exclude_remote_names) { ['origin'] }
64-
65-
it { subject.should == true }
6664
end
6765

6866
context 'skip is true and non-matching exclude_remote_names is nil' do
@@ -79,5 +77,85 @@
7977
it { subject.should == false }
8078
end
8179
end
80+
81+
context 'with ignore_branch_deletions specified' do
82+
let(:hook_config) do
83+
{ 'skip' => skip, 'ignore_branch_deletions' => ignore_branch_deletions }
84+
end
85+
let(:remote_branch_deletion?) { false }
86+
let(:ignore_branch_deletions) { false }
87+
88+
context(<<~DESC) do
89+
skip is true and
90+
remote_branch_deletion? is false and
91+
ignore_branch_deletions false' do
92+
DESC
93+
let(:skip) { true }
94+
let(:remote_branch_deletion?) { false }
95+
let(:ignore_branch_deletions) { nil }
96+
97+
it { subject.should == true }
98+
end
99+
100+
context(<<~DESC) do
101+
skip is false and
102+
remote_branch_deletion? is false and
103+
ignore_branch_deletions false' do
104+
DESC
105+
let(:skip) { false }
106+
let(:remote_branch_deletion?) { false }
107+
let(:ignore_branch_deletions) { false }
108+
109+
it { subject.should == false }
110+
end
111+
112+
context(<<~DESC) do
113+
skip is false and
114+
remote_branch_deletion? is true and
115+
ignore_branch_deletions false' do
116+
DESC
117+
let(:skip) { false }
118+
let(:remote_branch_deletion?) { true }
119+
let(:ignore_branch_deletions) { false }
120+
121+
it { subject.should == false }
122+
end
123+
124+
context(<<~DESC) do
125+
skip is false and
126+
remote_branch_deletion? is true and
127+
ignore_branch_deletions true' do
128+
DESC
129+
let(:skip) { false }
130+
let(:remote_branch_deletion?) { true }
131+
let(:ignore_branch_deletions) { true }
132+
133+
it { subject.should == true }
134+
end
135+
136+
context(<<~DESC) do
137+
skip is false and
138+
remote_branch_deletion? is false and
139+
ignore_branch_deletions true' do
140+
DESC
141+
let(:skip) { false }
142+
let(:remote_branch_deletion?) { false }
143+
let(:ignore_branch_deletions) { true }
144+
145+
it { subject.should == false }
146+
end
147+
148+
context(<<-DESC) do
149+
skip is true and
150+
remote_branch_deletion? is true and
151+
ignore_branch_deletions true' do
152+
DESC
153+
let(:skip) { true }
154+
let(:remote_branch_deletion?) { true }
155+
let(:ignore_branch_deletions) { true }
156+
157+
it { subject.should == true }
158+
end
159+
end
82160
end
83161
end

spec/overcommit/hook_context/pre_push_spec.rb

+35
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,41 @@
2323
it { should == remote_url }
2424
end
2525

26+
describe '#remote_branch_deletion?' do
27+
subject { context.remote_branch_deletion? }
28+
29+
before do
30+
input.stub(:read).and_return("#{local_ref} #{local_sha1} #{remote_ref} #{remote_sha1}\n")
31+
end
32+
33+
context 'when pushing new branch to remote ref' do
34+
let(:local_ref) { 'refs/heads/test' }
35+
let(:local_sha1) { '' }
36+
let(:remote_ref) { 'refs/heads/test' }
37+
let(:remote_sha1) { '0' * 40 }
38+
39+
it { should == false }
40+
end
41+
42+
context 'when pushing update to remote ref' do
43+
let(:local_ref) { 'refs/heads/test' }
44+
let(:local_sha1) { '' }
45+
let(:remote_ref) { 'refs/heads/test' }
46+
let(:remote_sha1) { random_hash }
47+
48+
it { should == false }
49+
end
50+
51+
context 'when deleting remote ref' do
52+
let(:local_ref) { '(deleted)' }
53+
let(:local_sha1) { '' }
54+
let(:remote_ref) { 'refs/heads/test' }
55+
let(:remote_sha1) { random_hash }
56+
57+
it { should == true }
58+
end
59+
end
60+
2661
describe '#pushed_refs' do
2762
subject(:pushed_refs) { context.pushed_refs }
2863

0 commit comments

Comments
 (0)