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 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'lib') 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 -- cgit v1.2.3