From a213d71ec8d3e75bef909cc5c3f11aaea8b4e94d Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Thu, 15 May 2014 16:47:24 -0700 Subject: Fix detached head state --- lib/puppet/provider/vcsrepo/git.rb | 21 ++++++++++++++++++--- spec/unit/puppet/provider/vcsrepo/git_spec.rb | 11 +++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index fafe4bf..b4bafbf 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -79,12 +79,18 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) def revision=(desired) checkout(desired) - if local_branch_revision?(desired) + if local_branch_revision? # reset instead of pull to avoid merge conflicts. assuming remote is # authoritative. # might be worthwhile to have an allow_local_changes param to decide # whether to reset or pull when we're ensuring latest. - at_path { git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}") } + at_path { + git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{@resource.value(:revision)}") + if detached? + git_with_identity('checkout', "#{@resource.value(:revision)}") + git_with_identity('pull') + end + } end if @resource.value(:ensure) != :bare update_submodules @@ -259,7 +265,16 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end def on_branch? - at_path { git_with_identity('rev-parse', '--abbrev-ref', 'HEAD').chomp } + at_path { + matches = git_with_identity('branch', '--list').match /\*\s+(.*)/ + matches[1] unless matches[1].match /detached/ + } + end + + def detached? + at_path { + git_with_identity('branch', '--list').match /\*\s+\(detached from.*\)/ + } end def tags diff --git a/spec/unit/puppet/provider/vcsrepo/git_spec.rb b/spec/unit/puppet/provider/vcsrepo/git_spec.rb index ad6ed08..20aa084 100644 --- a/spec/unit/puppet/provider/vcsrepo/git_spec.rb +++ b/spec/unit/puppet/provider/vcsrepo/git_spec.rb @@ -261,6 +261,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do it "should use 'git fetch' and 'git reset'" do resource[:revision] = 'feature/foo' provider.expects(:update_submodules) + provider.expects(:git).with('branch','--list').returns("* master") provider.expects(:git).with('branch', '-a').returns(resource.value(:revision)) provider.expects(:git).with('checkout', '--force', resource.value(:revision)) provider.expects(:git).with('branch', '-a').returns(resource.value(:revision)) @@ -272,6 +273,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do it "should use 'git fetch' and 'git reset'" do resource[:revision] = 'only/remote' provider.expects(:update_submodules) + provider.expects(:git).with('branch','--list').returns("* master") provider.expects(:git).with('branch', '-a').returns(resource.value(:revision)) provider.expects(:git).with('checkout', '--force', resource.value(:revision)) provider.expects(:git).with('branch', '-a').returns(resource.value(:revision)) @@ -341,7 +343,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do context "retrieving the current revision" do before do expects_chdir - provider.expects(:git).with('rev-parse', '--abbrev-ref', 'HEAD').returns("foo\n") + provider.expects(:git).with('branch','--list').returns("* foo") end it "will strip trailing newlines" do @@ -377,19 +379,20 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do end context 'on master' do it do - provider.expects(:git).with('rev-parse', '--abbrev-ref', 'HEAD').returns(fixture(:git_branch_a)) + provider.expects(:git).with('branch','--list').returns("* master") provider.latest.should == 'master' end end context 'no branch' do it do - provider.expects(:git).with('rev-parse', '--abbrev-ref', 'HEAD').returns(fixture(:git_branch_none)) + provider.expects(:git).with('branch','--list').returns("* master") + provider.latest.should == 'master' end end context 'feature/bar' do it do - provider.expects(:git).with('rev-parse', '--abbrev-ref', 'HEAD').returns(fixture(:git_branch_feature_bar)) + provider.expects(:git).with('branch','--list').returns("* master") provider.latest.should == 'master' end end -- cgit v1.2.3 From a909fe74c71e7e214087c3c95bb21e6dfd90c940 Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Thu, 15 May 2014 22:00:14 -0700 Subject: update to use -a and desired for hard reset --- lib/puppet/provider/vcsrepo/git.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index b4bafbf..61044e0 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -85,7 +85,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) # might be worthwhile to have an allow_local_changes param to decide # whether to reset or pull when we're ensuring latest. at_path { - git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{@resource.value(:revision)}") + git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}") if detached? git_with_identity('checkout', "#{@resource.value(:revision)}") git_with_identity('pull') @@ -266,14 +266,14 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) def on_branch? at_path { - matches = git_with_identity('branch', '--list').match /\*\s+(.*)/ + matches = git_with_identity('branch', '-a').match /\*\s+(.*)/ matches[1] unless matches[1].match /detached/ } end def detached? at_path { - git_with_identity('branch', '--list').match /\*\s+\(detached from.*\)/ + git_with_identity('branch', '-a').match /\*\s+\(detached from.*\)/ } end -- cgit v1.2.3 From 2458aba1401ad6336137653f87086e7656625bf7 Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Thu, 15 May 2014 22:32:47 -0700 Subject: Fix spec tests --- spec/unit/puppet/provider/vcsrepo/git_spec.rb | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/spec/unit/puppet/provider/vcsrepo/git_spec.rb b/spec/unit/puppet/provider/vcsrepo/git_spec.rb index 20aa084..8e296ef 100644 --- a/spec/unit/puppet/provider/vcsrepo/git_spec.rb +++ b/spec/unit/puppet/provider/vcsrepo/git_spec.rb @@ -261,10 +261,8 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do it "should use 'git fetch' and 'git reset'" do resource[:revision] = 'feature/foo' provider.expects(:update_submodules) - provider.expects(:git).with('branch','--list').returns("* master") - provider.expects(:git).with('branch', '-a').returns(resource.value(:revision)) + provider.expects(:git).with('branch', '-a').at_least_once.returns(resource.value(:revision)) provider.expects(:git).with('checkout', '--force', resource.value(:revision)) - provider.expects(:git).with('branch', '-a').returns(resource.value(:revision)) provider.expects(:git).with('reset', '--hard', "origin/#{resource.value(:revision)}") provider.revision = resource.value(:revision) end @@ -273,10 +271,8 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do it "should use 'git fetch' and 'git reset'" do resource[:revision] = 'only/remote' provider.expects(:update_submodules) - provider.expects(:git).with('branch','--list').returns("* master") - provider.expects(:git).with('branch', '-a').returns(resource.value(:revision)) + provider.expects(:git).with('branch', '-a').at_least_once.returns(resource.value(:revision)) provider.expects(:git).with('checkout', '--force', resource.value(:revision)) - provider.expects(:git).with('branch', '-a').returns(resource.value(:revision)) provider.expects(:git).with('reset', '--hard', "origin/#{resource.value(:revision)}") provider.revision = resource.value(:revision) end @@ -343,7 +339,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do context "retrieving the current revision" do before do expects_chdir - provider.expects(:git).with('branch','--list').returns("* foo") + provider.expects(:git).with('branch','-a').returns("* foo") end it "will strip trailing newlines" do @@ -379,20 +375,20 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do end context 'on master' do it do - provider.expects(:git).with('branch','--list').returns("* master") + provider.expects(:git).with('branch','-a').returns("* master") provider.latest.should == 'master' end end context 'no branch' do it do - provider.expects(:git).with('branch','--list').returns("* master") + provider.expects(:git).with('branch','-a').returns("* master") provider.latest.should == 'master' end end context 'feature/bar' do it do - provider.expects(:git).with('branch','--list').returns("* master") + provider.expects(:git).with('branch','-a').returns("* master") provider.latest.should == 'master' end end -- cgit v1.2.3 From 4613e93e35551271651bb7b1102d3b97a3f5b40d Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Fri, 16 May 2014 16:30:58 -0700 Subject: Added multiline parsing tests for branch -a --- .gitignore | 6 ++++++ spec/unit/puppet/provider/vcsrepo/git_spec.rb | 23 ++++++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index ddd2d45..44e004c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,9 @@ coverage .DS_Store .#* \#* +Gemfile.lock +log + +#Intellij +.idea +*.iml diff --git a/spec/unit/puppet/provider/vcsrepo/git_spec.rb b/spec/unit/puppet/provider/vcsrepo/git_spec.rb index 8e296ef..daec038 100644 --- a/spec/unit/puppet/provider/vcsrepo/git_spec.rb +++ b/spec/unit/puppet/provider/vcsrepo/git_spec.rb @@ -1,7 +1,16 @@ require 'spec_helper' describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do + def branch_a_list(include_branch) + < 'test', :ensure => :present, @@ -26,7 +35,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do Dir.expects(:chdir).with('/tmp/test').at_least_once.yields 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('branch', '-a').returns(branch_a_list(resource.value(:revision))) provider.expects(:git).with('checkout', '--force', resource.value(:revision)) provider.create end @@ -39,7 +48,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do Dir.expects(:chdir).with('/tmp/test').at_least_once.yields provider.expects(:git).with('clone', '--origin', 'not_origin', resource.value(:source), resource.value(:path)) provider.expects(:update_submodules) - provider.expects(:git).with('branch', '-a').returns(resource.value(:revision)) + provider.expects(:git).with('branch', '-a').returns(branch_a_list(resource.value(:revision))) provider.expects(:git).with('checkout', '--force', resource.value(:revision)) provider.create end @@ -53,7 +62,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do Dir.expects(:chdir).with('/tmp/test').at_least_once.yields provider.expects(:git).with('clone', '--depth', '1', resource.value(:source), resource.value(:path)) provider.expects(:update_submodules) - provider.expects(:git).with('branch', '-a').returns(resource.value(:revision)) + provider.expects(:git).with('branch', '-a').returns(branch_a_list(resource.value(:revision))) provider.expects(:git).with('checkout', '--force', resource.value(:revision)) provider.create end @@ -66,7 +75,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do Dir.expects(:chdir).with('/tmp/test').at_least_once.yields 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('branch', '-a').returns(branch_a_list(resource.value(:revision))) provider.expects(:git).with('checkout', '--force', resource.value(:revision)) provider.create end @@ -161,7 +170,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do 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('branch', '-a').returns(branch_a_list(resource.value(:revision))) provider.expects(:git).with('checkout', '--force', resource.value(:revision)) provider.create end @@ -261,7 +270,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do it "should use 'git fetch' and 'git reset'" do resource[:revision] = 'feature/foo' provider.expects(:update_submodules) - provider.expects(:git).with('branch', '-a').at_least_once.returns(resource.value(:revision)) + provider.expects(:git).with('branch', '-a').at_least_once.returns(branch_a_list(resource.value(:revision))) provider.expects(:git).with('checkout', '--force', resource.value(:revision)) provider.expects(:git).with('reset', '--hard', "origin/#{resource.value(:revision)}") provider.revision = resource.value(:revision) @@ -280,7 +289,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do context "when it's a commit or tag" do it "should use 'git fetch' and 'git reset'" do resource[:revision] = 'a-commit-or-tag' - provider.expects(:git).with('branch', '-a').returns(fixture(:git_branch_a)) + provider.expects(:git).with('branch', '-a').at_least_once.returns(fixture(:git_branch_a)) provider.expects(:git).with('checkout', '--force', resource.value(:revision)) provider.expects(:git).with('branch', '-a').returns(fixture(:git_branch_a)) provider.expects(:git).with('branch', '-a').returns(fixture(:git_branch_a)) -- cgit v1.2.3