|
6 | 6 | desc 'Supports Git repositories' |
7 | 7 |
|
8 | 8 | has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, |
9 | | - :user, :depth, :branch, :submodules |
| 9 | + :user, :depth, :branch, :submodules, :safe_directory |
10 | 10 |
|
11 | 11 | def create |
12 | 12 | check_force |
@@ -36,6 +36,7 @@ def create |
36 | 36 | end |
37 | 37 |
|
38 | 38 | def destroy |
| 39 | + remove_safe_directory if safe_directories.include?(@resource.value(:path)) |
39 | 40 | FileUtils.rm_rf(@resource.value(:path)) |
40 | 41 | end |
41 | 42 |
|
@@ -140,6 +141,7 @@ def working_copy_exists? |
140 | 141 | end |
141 | 142 |
|
142 | 143 | def exists? |
| 144 | + update_safe_directory |
143 | 145 | working_copy_exists? || bare_exists? |
144 | 146 | end |
145 | 147 |
|
@@ -564,6 +566,52 @@ def git_version |
564 | 566 | exec_git('--version').match(%r{[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?})[0] |
565 | 567 | end |
566 | 568 |
|
| 569 | + # @!visibility private |
| 570 | + def safe_directories |
| 571 | + args = ['config', '--global', '--get-all', 'safe.directory'] |
| 572 | + begin |
| 573 | + d = git_with_identity(*args) || '' |
| 574 | + d.split('\n') |
| 575 | + .reject { |v| v.empty? } |
| 576 | + .map { |v| v.chomp } |
| 577 | + rescue Puppet::ExecutionFailure |
| 578 | + [] |
| 579 | + end |
| 580 | + end |
| 581 | + |
| 582 | + # @!visibility private |
| 583 | + def update_safe_directory |
| 584 | + # If the owner parameter is not set, then we don't need to do anything. |
| 585 | + return unless @resource.value(:owner) |
| 586 | + |
| 587 | + if should_add_safe_directory? |
| 588 | + add_safe_directory |
| 589 | + else |
| 590 | + remove_safe_directory |
| 591 | + end |
| 592 | + end |
| 593 | + |
| 594 | + # @!visibility private |
| 595 | + def add_safe_directory |
| 596 | + notice("Adding '#{@resource.value(:path)}' to safe directory list") |
| 597 | + args = ['config', '--global', '--add', 'safe.directory', @resource.value(:path)] |
| 598 | + git_with_identity(*args) |
| 599 | + end |
| 600 | + |
| 601 | + # @!visibility private |
| 602 | + def remove_safe_directory |
| 603 | + notice("Removing '#{@resource.value(:path)}' from safe directory list") |
| 604 | + args = ['config', '--global', '--unset', 'safe.directory', @resource.value(:path)] |
| 605 | + git_with_identity(*args) |
| 606 | + end |
| 607 | + |
| 608 | + # @!visibility private |
| 609 | + def should_add_safe_directory? |
| 610 | + (@resource.value(:owner) != @resource.value(:user)) && # user and owner should be different |
| 611 | + @resource.value(:safe_directory) && # safe_directory should be true |
| 612 | + !safe_directories.include?(@resource.value(:path)) # directory should not already be in the list |
| 613 | + end |
| 614 | + |
567 | 615 | # @!visibility private |
568 | 616 | def git_with_identity(*args) |
569 | 617 | if @resource.value(:trust_server_cert) == :true |
@@ -599,10 +647,13 @@ def git_with_identity(*args) |
599 | 647 |
|
600 | 648 | # Execute git with the given args, running it as the user specified. |
601 | 649 | def exec_git(*args) |
602 | | - exec_args = { failonfail: true, combine: true } |
| 650 | + exec_args = { |
| 651 | + failonfail: true, |
| 652 | + combine: true, |
| 653 | + custom_environment: { 'HOME' => Dir.home }, |
| 654 | + } |
603 | 655 | if @resource.value(:user) && @resource.value(:user) != Facter['id'].value |
604 | | - env = Etc.getpwnam(@resource.value(:user)) |
605 | | - exec_args[:custom_environment] = { 'HOME' => env['dir'] } |
| 656 | + exec_args[:custom_environment] = { 'HOME' => Dir.home(@resource.value(:user)) } |
606 | 657 | exec_args[:uid] = @resource.value(:user) |
607 | 658 | end |
608 | 659 | Puppet::Util::Execution.execute([:git, args], **exec_args) |
|
0 commit comments