Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit 230a86b

Browse files
committed
add force parameter defaulted to false to control reset behavior
1 parent 88c4727 commit 230a86b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/puppet/provider/repository/git.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
require 'fileutils'
2+
require 'puppet/util/errors'
23
require 'puppet/util/execution'
34
require 'shellwords'
45

56
Puppet::Type.type(:repository).provide :git do
67
include Puppet::Util::Execution
8+
include Puppet::Util::Errors
79

810
optional_commands :git => 'git'
911

@@ -47,7 +49,18 @@ def ensure_revision
4749
create unless cloned?
4850

4951
Dir.chdir @resource[:path] do
50-
execute [command(:git), "reset", "--hard", target_revision], command_opts
52+
status = execute [command(:git), "status", "--porcelain"], command_opts
53+
54+
if status.empty?
55+
execute [command(:git), "reset", "--hard", target_revision], command_opts
56+
else
57+
if @resource[:force]
58+
Puppet.warning("Repository[#{@resource[:name]}] tree is dirty and force is true: doing hard reset!")
59+
execute [command(:git), "reset", "--hard", target_revision], command_opts
60+
else
61+
fail("Repository[#{@resource[:name]}] tree is dirty and force is false: cannot sync resource!")
62+
end
63+
end
5164
end
5265
end
5366

lib/puppet/type/repository.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ def insync?(is)
8484
desc "Extra actions or information for a provider"
8585
end
8686

87+
newparam :force do
88+
desc "Whether or not to force reset if the working tree is dirty"
89+
90+
validate do |value|
91+
unless value.is_a?(TrueClass) || value.is_a?(FalseClass)
92+
raise Puppet::Error, \
93+
"Force must be true or false"
94+
end
95+
end
96+
97+
defaultto false
98+
end
99+
87100
validate do
88101
if self[:source].nil?
89102
# ensure => absent does not need a source

0 commit comments

Comments
 (0)