Skip to content

Commit 051baa1

Browse files
committed
vcsrepo: add mode option
Splits set_ownership_and_permissions into seperate functions per Metrics/CyclomaticComplexity & Metrics/PerceivedComplexity rubocop checks. Signed-off-by: Robin H. Johnson <[email protected]> Reference: #598
1 parent 6462c93 commit 051baa1

File tree

13 files changed

+76
-20
lines changed

13 files changed

+76
-20
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55

66
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org).
77

8+
## UNRELEASED
9+
10+
### Added
11+
12+
- support for directory modes for repos [\#599](https://github.com/puppetlabs/puppetlabs-vcsrepo/issues/599) ([robbat2](https://github.com/robbat2))
13+
814
## [v6.1.0](https://github.com/puppetlabs/puppetlabs-vcsrepo/tree/v6.1.0) - 2023-06-13
915

1016
[Full Changelog](https://github.com/puppetlabs/puppetlabs-vcsrepo/compare/v6.0.1...v6.1.0)

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -797,37 +797,37 @@ For information on the classes and types, see the [REFERENCE.md](https://github.
797797

798798
Features: `bare_repositories`, `depth`, `multiple_remotes`, `reference_tracking`, `ssh_identity`, `submodules`, `user`
799799

800-
Parameters: `depth`, `ensure`, `excludes`, `force`, `group`, `identity`, `owner`, `path`, `provider`, `remote`, `revision`, `source`, `user`
800+
Parameters: `depth`, `ensure`, `excludes`, `force`, `group`, `identity`, `owner`, `path`, `provider`, `remote`, `revision`, `source`, `user`, `mode`
801801

802802
##### `bzr` - Supports the Bazaar VCS.
803803

804804
Features: `reference_tracking`
805805

806-
Parameters: `ensure`, `excludes`, `force`, `group`, `owner`, `path`, `provider`, `revision`, `source`
806+
Parameters: `ensure`, `excludes`, `force`, `group`, `owner`, `path`, `provider`, `revision`, `source`, `mode`
807807

808808
##### `cvs` - Supports the CVS VCS.
809809

810810
Features: `cvs_rsh`, `gzip_compression`, `modules`, `reference_tracking`, `user`
811811

812-
Parameters: `compression`, `cvs_rsh`, `ensure`, `excludes`, `force`, `group`, `module`, `owner`, `path`, `provider`
812+
Parameters: `compression`, `cvs_rsh`, `ensure`, `excludes`, `force`, `group`, `module`, `owner`, `path`, `provider`, `mode`
813813

814814
##### `hg` - Supports the Mercurial VCS.
815815

816816
Features: `reference_tracking`, `ssh_identity`, `user`
817817

818-
Parameters: `ensure`, `excludes`, `force`, `group`, `identity`, `owner`, `path`, `provider`, `revision`, `source`, `user`
818+
Parameters: `ensure`, `excludes`, `force`, `group`, `identity`, `owner`, `path`, `provider`, `revision`, `source`, `user`, `mode`
819819

820820
##### `p4` - Supports the Perforce VCS.
821821

822822
Features: `p4config`, `reference_tracking`
823823

824-
Parameters: `ensure`, `excludes`, `force`, `group`, `owner`, `p4config`, `path`, `provider`, `revision`, `source`
824+
Parameters: `ensure`, `excludes`, `force`, `group`, `owner`, `p4config`, `path`, `provider`, `revision`, `source`, `mode`
825825

826826
##### `svn` - Supports the Subversion VCS.
827827

828828
Features: `basic_auth`, `configuration`, `conflict`, `depth`, `filesystem_types`, `reference_tracking`
829829

830-
Parameters: `basic_auth_password`, `basic_auth_username`, `configuration`, `conflict`, `ensure`, `excludes`, `force`, `fstype`, `group`, `includes`, `owner`, `path`, `provider`, `revision`, `source`, `trust_server_cert`
830+
Parameters: `basic_auth_password`, `basic_auth_username`, `configuration`, `conflict`, `ensure`, `excludes`, `force`, `fstype`, `group`, `includes`, `owner`, `path`, `provider`, `revision`, `source`, `trust_server_cert`, `mode`
831831

832832
<a id="features"></a>
833833
#### Features

REFERENCE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ The following parameters are available in the `vcsrepo` type.
154154
* [`trust_server_cert`](#trust_server_cert)
155155
* [`umask`](#umask)
156156
* [`user`](#user)
157+
* [`mode`](#mode)
157158

158159
##### <a name="basic_auth_password"></a>`basic_auth_password`
159160

@@ -280,3 +281,6 @@ Sets the umask to be used for all repo operations
280281

281282
The user to run for repository operations
282283

284+
##### <a name="mode"></a>`mode`
285+
286+
Sets the mode for the repository directory (non-recursive).

lib/puppet/provider/vcsrepo.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,25 @@ def check_force
1616

1717
private
1818

19+
def set_ownership_and_permissions
20+
set_ownership
21+
set_permissions
22+
end
23+
1924
def set_ownership
25+
mode = @resource.value(:mode) || nil
26+
# Change the permission on the repo itself.
27+
# The VCS should maintain permissions on files within the checkout.
28+
FileUtils.chmod(mode, @resource.value(:path)) unless mode.nil?
29+
end
30+
31+
def set_permissions
2032
owner = @resource.value(:owner) || nil
2133
group = @resource.value(:group) || nil
2234
excludes = @resource.value(:excludes) || nil
35+
# We might have no work to do, and this makes it easier for the callers.
36+
return if owner.nil? && group.nil?
37+
2338
if excludes.nil? || excludes.empty?
2439
FileUtils.chown_R(owner, group, @resource.value(:path))
2540
else
@@ -28,6 +43,7 @@ def set_ownership
2843
end
2944

3045
def files
46+
# Expensive on large repos
3147
excludes = @resource.value(:excludes)
3248
path = @resource.value(:path)
3349
Dir["#{path}/**/*"].reject { |f| excludes.any? { |p| f.start_with?("#{path}/#{p}") } }

lib/puppet/provider/vcsrepo/bzr.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,6 @@ def clone_repository(revision)
102102
end
103103

104104
def update_owner
105-
set_ownership if @resource.value(:owner) || @resource.value(:group)
105+
set_ownership_and_permissions
106106
end
107107
end

lib/puppet/provider/vcsrepo/cvs.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def create_repository(path)
135135
end
136136

137137
def update_owner
138-
set_ownership if @resource.value(:owner) || @resource.value(:group)
138+
set_ownership_and_permissions
139139
end
140140

141141
def runcvs(*args)

lib/puppet/provider/vcsrepo/git.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def create
2828
init_repository
2929
self.skip_hooks = @resource.value(:skip_hooks) unless @resource.value(:skip_hooks).nil?
3030
end
31-
update_owner_and_excludes
31+
update_owner_permission_and_excludes
3232
end
3333

3434
def destroy
@@ -88,7 +88,7 @@ def revision=(desired)
8888
end
8989
# TODO: Would this ever reach here if it is bare?
9090
update_submodules if !ensure_bare_or_mirror? && @resource.value(:submodules) == :true
91-
update_owner_and_excludes
91+
update_owner_permission_and_excludes
9292
end
9393

9494
def bare_exists?
@@ -228,7 +228,7 @@ def update_references
228228
at_path do
229229
git_remote_action('fetch', @resource.value(:remote))
230230
git_remote_action(*fetch_tags_args, @resource.value(:remote))
231-
update_owner_and_excludes
231+
update_owner_permission_and_excludes
232232
end
233233
end
234234

@@ -246,6 +246,8 @@ def convert_working_copy_to_bare
246246
FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
247247
FileUtils.rm_rf(@resource.value(:path))
248248
FileUtils.mv(tempdir, @resource.value(:path))
249+
FileUtils.chown(@resource.value(:user), @resource.value(:group), @resource.value(:path)) if @resource.value(:user) || @resource.value(:group)
250+
FileUtils.chmod(@resource.value(:mode), @resource.value(:path)) if @resource.value(:mode)
249251
at_path do
250252
exec_git('config', '--local', '--bool', 'core.bare', 'true')
251253
return unless @resource.value(:ensure) == :mirror
@@ -266,13 +268,15 @@ def convert_bare_to_working_copy
266268
notice 'Converting bare repository to working copy repository'
267269
FileUtils.mv(@resource.value(:path), tempdir)
268270
FileUtils.mkdir(@resource.value(:path))
271+
FileUtils.chown(@resource.value(:user), @resource.value(:group), @resource.value(:path)) if @resource.value(:user) || @resource.value(:group)
272+
FileUtils.chmod(@resource.value(:mode), @resource.value(:path)) if @resource.value(:mode)
269273
FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
270274
if commits?
271275
at_path do
272276
exec_git('config', '--local', '--bool', 'core.bare', 'false')
273277
reset('HEAD')
274278
git_with_identity('checkout', '--force')
275-
update_owner_and_excludes
279+
update_owner_permission_and_excludes
276280
end
277281
end
278282
set_no_mirror if mirror?
@@ -398,7 +402,8 @@ def init_repository
398402
else
399403
# normal init
400404
FileUtils.mkdir(@resource.value(:path))
401-
FileUtils.chown(@resource.value(:user), nil, @resource.value(:path)) if @resource.value(:user)
405+
FileUtils.chown(@resource.value(:user), @resource.value(:group), @resource.value(:path)) if @resource.value(:user) || @resource.value(:group)
406+
FileUtils.chmod(@resource.value(:mode), @resource.value(:path)) if @resource.value(:mode)
402407
args = ['init']
403408
args << '--bare' if @resource.value(:ensure) == :bare
404409
at_path do
@@ -580,8 +585,8 @@ def get_revision(rev = 'HEAD')
580585
end
581586

582587
# @!visibility private
583-
def update_owner_and_excludes
584-
set_ownership if @resource.value(:owner) || @resource.value(:group)
588+
def update_owner_permission_and_excludes
589+
set_ownership_and_permissions
585590
set_excludes if @resource.value(:excludes)
586591
end
587592

lib/puppet/provider/vcsrepo/hg.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def clone_repository(revision)
115115
end
116116

117117
def update_owner
118-
set_ownership if @resource.value(:owner) || @resource.value(:group)
118+
set_ownership_and_permissions
119119
end
120120

121121
def sensitive?

lib/puppet/provider/vcsrepo/p4.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def source=(_desired)
101101
private
102102

103103
def update_owner
104-
set_ownership if @resource.value(:owner) || @resource.value(:group)
104+
set_ownership_and_permissions
105105
end
106106

107107
# Sync the client workspace files to head or specified revision.

lib/puppet/provider/vcsrepo/svn.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def create_repository(path)
242242
end
243243

244244
def update_owner
245-
set_ownership if @resource.value(:owner) || @resource.value(:group)
245+
set_ownership_and_permissions
246246
end
247247

248248
def update_includes(paths)

lib/puppet/type/vcsrepo.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ def insync?(is)
213213
desc 'The group/gid that owns the repository files'
214214
end
215215

216+
newparam :mode do
217+
desc 'The permission for the repository directory itself (not recursive)'
218+
end
219+
216220
newparam :user do
217221
desc 'The user to run for repository operations'
218222
end

spec/acceptance/clone_repo_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,4 +636,25 @@
636636
it { is_expected.to be_mode '664' }
637637
end
638638
end
639+
640+
context 'with mode' do
641+
pp = <<-MANIFEST
642+
vcsrepo { "#{tmpdir}/testrepo_mode":
643+
ensure => present,
644+
provider => git,
645+
source => "file://#{tmpdir}/testrepo.git",
646+
mode => 'u=rwX,g=rX,o=X',
647+
}
648+
MANIFEST
649+
it 'clones a repo' do
650+
# Run it twice and test for idempotency
651+
idempotent_apply(pp)
652+
end
653+
654+
describe file("#{tmpdir}/testrepo_mode") do
655+
# NOTE: '0664' is not supported by 'be_mode'; this must be three digits
656+
# unless the first octet is non-zero.
657+
it { is_expected.to be_mode '751' }
658+
end
659+
end
639660
end

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def branch_a_list(include_branch = nil?)
313313
.with('config', '--local', '--bool', 'core.bare', 'false').and_return(true)
314314
expect(provider).to receive(:reset).with('HEAD').and_return(true)
315315
expect(provider).to receive(:git_with_identity).with('checkout', '--force').and_return(true)
316-
expect(provider).to receive(:update_owner_and_excludes).and_return(true)
316+
expect(provider).to receive(:update_owner_permission_and_excludes).and_return(true)
317317
expect(provider).to receive(:mirror?).and_return(false)
318318
provider.instance_eval { convert_bare_to_working_copy }
319319
end
@@ -330,7 +330,7 @@ def branch_a_list(include_branch = nil?)
330330
.with('config', '--local', '--bool', 'core.bare', 'false').and_return(true)
331331
expect(provider).to receive(:reset).with('HEAD').and_return(true)
332332
expect(provider).to receive(:git_with_identity).with('checkout', '--force').and_return(true)
333-
expect(provider).to receive(:update_owner_and_excludes).and_return(true)
333+
expect(provider).to receive(:update_owner_permission_and_excludes).and_return(true)
334334
expect(provider).to receive(:exec_git).with('config', '--unset', 'remote.origin.mirror')
335335
expect(provider).to receive(:mirror?).and_return(true)
336336
provider.instance_eval { convert_bare_to_working_copy }

0 commit comments

Comments
 (0)