Skip to content

Commit 157d366

Browse files
authored
Merge pull request #557 from socialpoint/feat/add-skip-hooks
Add skip_hooks property to vcsrepo
2 parents cb1eb6b + 8fa7088 commit 157d366

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

lib/puppet/provider/vcsrepo/git.rb

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
desc 'Supports Git repositories'
77

88
has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes,
9-
:user, :depth, :branch, :submodules, :safe_directory
9+
:user, :depth, :branch, :submodules, :safe_directory, :hooks_allowed
1010

1111
def create
1212
check_force
@@ -19,10 +19,16 @@ def create
1919
end
2020

2121
init_repository
22+
unless @resource.value(:skip_hooks).nil?
23+
self.skip_hooks = @resource.value(:skip_hooks)
24+
end
2225
else
2326
clone_repository(default_url, @resource.value(:path))
2427
update_remotes(@resource.value(:source))
2528
set_mirror if @resource.value(:ensure) == :mirror && @resource.value(:source).is_a?(Hash)
29+
unless @resource.value(:skip_hooks).nil?
30+
self.skip_hooks = @resource.value(:skip_hooks)
31+
end
2632

2733
if @resource.value(:revision)
2834
checkout
@@ -319,6 +325,42 @@ def set_no_mirror
319325
end
320326
end
321327

328+
def skip_hooks
329+
git_ver = git_version
330+
config_args = ['config']
331+
if Gem::Version.new(git_ver) >= Gem::Version.new('1.7.4')
332+
config_args.push('--local')
333+
end
334+
at_path do
335+
begin
336+
d = git_with_identity(*config_args, '--get', 'core.hooksPath')
337+
rescue Puppet::ExecutionFailure
338+
return :false
339+
end
340+
return :true if d.chomp == '/dev/null'
341+
:false
342+
end
343+
end
344+
345+
def skip_hooks=(desired)
346+
git_ver = git_version
347+
config_args = ['config']
348+
if Gem::Version.new(git_ver) >= Gem::Version.new('1.7.4')
349+
config_args.push('--local')
350+
end
351+
at_path do
352+
if desired == :true
353+
exec_git(*config_args, 'core.hooksPath', '/dev/null')
354+
elsif desired == :false
355+
begin
356+
exec_git(*config_args, '--unset', 'core.hooksPath')
357+
rescue Puppet::ExecutionFailure
358+
next
359+
end
360+
end
361+
end
362+
end
363+
322364
private
323365

324366
# @!visibility private

lib/puppet/type/vcsrepo.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
feature :safe_directory,
6565
'The provider supports setting a safe directory. This will only be used for newer versions of git.'
6666

67+
feature :hooks_allowed,
68+
'The provider supports managing hooks for the repository operations.'
69+
6770
ensurable do
6871
desc 'Ensure the version control repository.'
6972
attr_accessor :latest
@@ -312,6 +315,11 @@ def insync?(is)
312315
defaultto :false
313316
end
314317

318+
newproperty :skip_hooks, required_features: [:hooks_allowed] do
319+
desc 'Explicitly skip any global hooks for this repository.'
320+
newvalues(:true, :false)
321+
end
322+
315323
autorequire(:package) do
316324
['git', 'git-core', 'mercurial', 'subversion']
317325
end

spec/acceptance/clone_repo_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,4 +561,41 @@
561561
it { is_expected.to contain 'ref: refs/heads/main' }
562562
end
563563
end
564+
565+
context 'with skip hooks' do
566+
pp_template = <<-MANIFEST
567+
vcsrepo { "#{tmpdir}/testrepo_skip_hooks":
568+
ensure => present,
569+
provider => git,
570+
source => "file://#{tmpdir}/testrepo.git",
571+
skip_hooks => %s,
572+
}
573+
MANIFEST
574+
575+
context 'when true' do
576+
pp = pp_template % :true
577+
578+
it 'clones a repo' do
579+
# Run it twice and test for idempotency
580+
idempotent_apply(pp)
581+
end
582+
583+
describe file("#{tmpdir}/testrepo_skip_hooks/.git/config") do
584+
it { is_expected.to contain 'hooksPath = /dev/null' }
585+
end
586+
end
587+
588+
context 'when false' do
589+
pp = pp_template % :false
590+
591+
it 'clones a repo' do
592+
# Run it twice and test for idempotency
593+
idempotent_apply(pp)
594+
end
595+
596+
describe file("#{tmpdir}/testrepo_skip_hooks/.git/config") do
597+
it { is_expected.not_to contain 'hooksPath' }
598+
end
599+
end
600+
end
564601
end

spec/unit/puppet/provider/vcsrepo/git_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,4 +583,43 @@ def branch_a_list(include_branch = nil?)
583583
provider.update_references
584584
end
585585
end
586+
587+
describe 'skip_hooks' do
588+
before :each do
589+
expect_chdir('/tmp/test')
590+
expect(provider).to receive(:exec_git).with('--version').and_return('1.7.4')
591+
end
592+
593+
context 'when true' do
594+
before :each do
595+
resource[:skip_hooks] = true
596+
end
597+
598+
context 'when core.hooksPath not defined' do
599+
it 'defines core.hooksPath null' do
600+
expect(provider).to receive(:exec_git).with('config', '--local', 'core.hooksPath', '/dev/null')
601+
provider.skip_hooks = resource.value(:skip_hooks)
602+
end
603+
end
604+
context 'when core.hooksPath defined not null' do
605+
it 'defines core.hooksPath null' do
606+
expect(provider).to receive(:exec_git).with('config', '--local', 'core.hooksPath', '/dev/null')
607+
provider.skip_hooks = resource.value(:skip_hooks)
608+
end
609+
end
610+
end
611+
612+
context 'when false' do
613+
before :each do
614+
resource[:skip_hooks] = false
615+
end
616+
617+
context 'when core.hooksPath defined null' do
618+
it 'unsets core.hooksPath' do
619+
expect(provider).to receive(:exec_git).with('config', '--local', '--unset', 'core.hooksPath')
620+
provider.skip_hooks = resource.value(:skip_hooks)
621+
end
622+
end
623+
end
624+
end
586625
end

0 commit comments

Comments
 (0)