Fix issue where force=>true was not destroying repository then recreating
authorTravis Fields <tfields@commercehub.com>
Tue, 13 May 2014 18:27:02 +0000 (11:27 -0700)
committerTravis Fields <tfields@commercehub.com>
Tue, 13 May 2014 20:17:14 +0000 (13:17 -0700)
lib/puppet/provider/vcsrepo/git.rb
lib/puppet/type/vcsrepo.rb
spec/unit/puppet/provider/vcsrepo/git_spec.rb

index b2e893b..fafe4bf 100644 (file)
@@ -140,7 +140,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo)
     if @resource.value(:remote) != 'origin'
       args.push('--origin', @resource.value(:remote))
     end
-    if !File.exist?(File.join(@resource.value(:path), '.git'))
+    if !working_copy_exists?
       args.push(source, path)
       Dir.chdir("/") do
         git_with_identity(*args)
@@ -271,6 +271,9 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo)
   end
 
   def get_revision(rev)
+    if @resource.value(:force) && working_copy_exists?
+      create
+    end
     if !working_copy_exists?
       create
     end
index 1dab2b9..3dd7bc6 100644 (file)
@@ -76,7 +76,7 @@ Puppet::Type.newtype(:vcsrepo) do
     end
 
     newvalue :latest, :required_features => [:reference_tracking] do
-      if provider.exists?
+      if provider.exists? && !@resource.value(:force)
         if provider.respond_to?(:update_references)
           provider.update_references
         end
@@ -97,6 +97,12 @@ Puppet::Type.newtype(:vcsrepo) do
       prov = @resource.provider
       if prov
         if prov.working_copy_exists?
+          if @resource.value(:force)
+            notice "Deleting current repository before recloning"
+            prov.destroy
+            notice "Create repository from latest"
+            prov.create
+          end
           (@should.include?(:latest) && prov.latest?) ? :latest : :present
         elsif prov.class.feature?(:bare_repositories) and prov.bare_exists?
           :bare
index ba726fa..ad6ed08 100644 (file)
@@ -9,6 +9,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do
     :revision => '2634',
     :source   => 'git@repo',
     :path     => '/tmp/test',
+    :force    => false
   })}
 
   let(:provider) { resource.provider }
@@ -151,12 +152,26 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do
         provider.expects(:convert_working_copy_to_bare)
         provider.create
       end
+      it "should clone overtop it using force" do
+        resource[:force] = true
+        Dir.expects(:chdir).with('/').at_least_once.yields
+        Dir.expects(:chdir).with('/tmp/test').at_least_once.yields
+        provider.expects(:path_exists?).returns(true)
+        provider.expects(:path_empty?).returns(false)
+        provider.destroy
+        provider.expects(:git).with('clone',resource.value(:source), resource.value(:path))
+        provider.expects(:update_submodules)
+        provider.expects(:git).with('branch', '-a').returns(resource.value(:revision))
+        provider.expects(:git).with('checkout', '--force', resource.value(:revision))
+        provider.create
+      end
     end
 
     context "when the path is not empty and not a repository" do
       it "should raise an exception" do
-        expects_directory?(true)
+        provider.expects(:path_exists?).returns(true)
         provider.expects(:path_empty?).returns(false)
+        provider.expects(:working_copy_exists?).returns(false)
         proc { provider.create }.should raise_error(Puppet::Error)
       end
     end
@@ -390,7 +405,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do
     end
   end
 
-  describe 'convert_bare_to_working_copy' do
+    describe 'convert_bare_to_working_copy' do
     it do
       FileUtils.expects(:mv).returns(true)
       FileUtils.expects(:mkdir).returns(true)