From 411111b5ff88b682d7e9cc54a47573bccc3e3edc Mon Sep 17 00:00:00 2001 From: John Iacona Date: Wed, 28 Aug 2013 14:32:05 -0400 Subject: update git provider to handle checking out into an existing (empty) dir --- lib/puppet/provider/vcsrepo/git.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 58330f6..47e84d2 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -147,7 +147,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end def check_force - if path_exists? + if path_exists? and not path_empty? if @resource.value(:force) notice "Removing %s to replace with vcsrepo." % @resource.value(:path) destroy -- cgit v1.2.3 From c9c6d96554e546f70a289522df3f24261674cc1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=E1=BA=BDl=20Can=C3=A9vet?= Date: Sun, 24 Nov 2013 10:44:37 +0100 Subject: Don't 'su' if passed user is current user --- lib/puppet/provider/vcsrepo/git.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 47e84d2..442ea61 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -314,7 +314,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) return ret end - elsif @resource.value(:user) + elsif @resource.value(:user) and @resource.value(:user) != Facter['id'].value su(@resource.value(:user), '-c', "git #{args.join(' ')}" ) else git(*args) -- cgit v1.2.3 From 4689c514a12764933a9cb1e2858343f0ba5923b3 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 2 Dec 2013 11:24:15 -0500 Subject: origin/HEAD is not valid; use origin/master for latest when branch == 'master' --- lib/puppet/provider/vcsrepo/git.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 47e84d2..6bc1c3d 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -39,9 +39,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) def latest branch = on_branch? - if branch == 'master' - return get_revision("#{@resource.value(:remote)}/HEAD") - elsif branch == '(no branch)' + if branch == '(no branch)' return get_revision('HEAD') else return get_revision("#{@resource.value(:remote)}/%s" % branch) -- cgit v1.2.3 From 8837be29ff114228f86d67c59337119120690e44 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 2 Dec 2013 11:38:56 -0500 Subject: Using rev-parse to determine branch name of HEAD for on_branch? method --- lib/puppet/provider/vcsrepo/git.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 6bc1c3d..17275c4 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -39,7 +39,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) def latest branch = on_branch? - if branch == '(no branch)' + if branch == '' return get_revision('HEAD') else return get_revision("#{@resource.value(:remote)}/%s" % branch) @@ -253,7 +253,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end def on_branch? - at_path { git_with_identity('branch', '-a') }.split(/\n/).grep(/\*/).first.to_s.gsub('*', '').strip + at_path { git_with_identity('rev-parse', '--abbrev-ref', 'HEAD') } end def tags -- cgit v1.2.3 From 69b93cefcdcd4a80d241dc71d5e6de90842faf11 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 3 Dec 2013 09:54:21 -0500 Subject: Stripping git on_branch? return value; contains trailing newline This commit also contains git provider `latest' method formatting changes; squashed by request. --- lib/puppet/provider/vcsrepo/git.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 17275c4..d488271 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -39,10 +39,10 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) def latest branch = on_branch? - if branch == '' + if !branch return get_revision('HEAD') else - return get_revision("#{@resource.value(:remote)}/%s" % branch) + return get_revision("#{@resource.value(:remote)}/#{branch}") end end @@ -253,7 +253,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end def on_branch? - at_path { git_with_identity('rev-parse', '--abbrev-ref', 'HEAD') } + at_path { git_with_identity('rev-parse', '--abbrev-ref', 'HEAD').chomp } end def tags -- cgit v1.2.3 From 0ea16bba47cf177dc17fa14f20d54d0091a09865 Mon Sep 17 00:00:00 2001 From: Felipe Reyes Date: Thu, 2 Jan 2014 18:13:40 -0300 Subject: Add the option to shallow clones with git The new parameter used to indicate that you want a shallow clone is `:depth` --- lib/puppet/provider/vcsrepo/git.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index d488271..af38a59 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -6,7 +6,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) ##TODO modify the commands below so that the su - is included optional_commands :git => 'git', :su => 'su' - has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user + has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user, :depth def create if !@resource.value(:source) @@ -131,6 +131,9 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) def clone_repository(source, path) check_force args = ['clone'] + if @resource.value(:depth) and @resource.value(:depth).to_i > 0 + args.push('--depth', @resource.value(:depth).to_s) + end if @resource.value(:ensure) == :bare args << '--bare' end -- cgit v1.2.3 From 7845ea132b0b40a83d3e9284723cbf63e2b61a82 Mon Sep 17 00:00:00 2001 From: Miguel Di Ciurcio Filho Date: Sat, 11 Jan 2014 15:25:28 -0200 Subject: git: actually use the remote parameter When using the following sample, the provider does not use the value of remote when cloning a repository: vcsrepo {'/path/to/repo': ensure => 'present', provider => 'git', remote => 'test', source => 'git@somerepo:repo.git', } $ git remote origin This commit makes sure that the new repository has a remote with the supplied value. Closes #MODULES-430 --- lib/puppet/provider/vcsrepo/git.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index af38a59..cdae459 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -105,10 +105,10 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end def update_remote_origin_url - current = git_with_identity('config', 'remote.origin.url') + current = git_with_identity('config', "remote.#{@resource.value(:remote)}.url") unless @resource.value(:source).nil? if current.nil? or current.strip != @resource.value(:source) - git_with_identity('config', 'remote.origin.url', @resource.value(:source)) + git_with_identity('config', "remote.#{@resource.value(:remote)}.url", @resource.value(:source)) end end end @@ -137,6 +137,9 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) if @resource.value(:ensure) == :bare args << '--bare' end + if @resource.value(:remote) != 'origin' + args.push('--origin', @resource.value(:remote)) + end if !File.exist?(File.join(@resource.value(:path), '.git')) args.push(source, path) Dir.chdir("/") do -- cgit v1.2.3 From 9c873e1c56f1147a8fcdd01b804f744deebb53f6 Mon Sep 17 00:00:00 2001 From: Huan Du Date: Wed, 12 Feb 2014 22:40:56 +0800 Subject: use `git rev-parse` to get tag canonical revision. --- lib/puppet/provider/vcsrepo/git.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index c96095b..b2e893b 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -52,7 +52,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) return current unless @resource.value(:revision) if tag_revision?(@resource.value(:revision)) - canonical = at_path { git_with_identity('show', @resource.value(:revision)).scan(/^commit (.*)/).to_s } + canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).chomp } else # if it's not a tag, look for it as a local ref canonical = at_path { git_with_identity('rev-parse', '--revs-only', @resource.value(:revision)).chomp } -- cgit v1.2.3 From cfcd03a7c59eaa6b81de8c767bbd91e6d278c0f2 Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Tue, 13 May 2014 11:27:02 -0700 Subject: Fix issue where force=>true was not destroying repository then recreating --- lib/puppet/provider/vcsrepo/git.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index b2e893b..fafe4bf 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -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 -- cgit v1.2.3 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/puppet/provider/vcsrepo/git.rb') 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 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(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') 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 932eb74ac487c815e51b1e99bb2f57ee82169be2 Mon Sep 17 00:00:00 2001 From: Daniele Sluijters Date: Thu, 22 May 2014 00:16:10 +0200 Subject: Un-optional the non-optional commands. --- lib/puppet/provider/vcsrepo/git.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 61044e0..1bccfb6 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -4,8 +4,9 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) desc "Supports Git repositories" ##TODO modify the commands below so that the su - is included - optional_commands :git => 'git', - :su => 'su' + commands :git => 'git' + optional_commands :su => 'su' + has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user, :depth def create -- cgit v1.2.3 From 0d9e938e92a25673c3712866e2d2108a255b67d5 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Tue, 20 May 2014 18:15:03 -0700 Subject: Update specs and fix FM-1361 - Add install.rb for pre-suite - Add catches for failures/changes to manifest application - Correct root ssh key copying - Add sleeps for killing processes releasing ports - Fix FM-1361 --- lib/puppet/provider/vcsrepo/git.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 61044e0..f6ee407 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -9,16 +9,15 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user, :depth def create + if @resource.value(:revision) and @resource.value(:ensure) == :bare + fail("Cannot set a revision (#{@resource.value(:revision)}) on a bare repository") + end if !@resource.value(:source) init_repository(@resource.value(:path)) else clone_repository(@resource.value(:source), @resource.value(:path)) if @resource.value(:revision) - if @resource.value(:ensure) == :bare - notice "Ignoring revision for bare repository" - else - checkout - end + checkout end if @resource.value(:ensure) != :bare update_submodules -- cgit v1.2.3 From 688c3cad1a277b4cb59445750fdf402a1c1774a3 Mon Sep 17 00:00:00 2001 From: "Joshua B. Bussdieker" Date: Wed, 25 Jun 2014 16:28:04 -0700 Subject: Fix issue with node changing every checkin --- lib/puppet/provider/vcsrepo/git.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 5c878ed..5c5dd07 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -52,7 +52,9 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) return current unless @resource.value(:revision) if tag_revision?(@resource.value(:revision)) - canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).chomp } + # git-rev-parse will give you the hash of the tag object itself rather than the commit it points to by default. + # Using tag^0 will return the actual commit. + canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:revision)}^0").chomp } else # if it's not a tag, look for it as a local ref canonical = at_path { git_with_identity('rev-parse', '--revs-only', @resource.value(:revision)).chomp } -- cgit v1.2.3 From 6624f40651f44e184878a9fbb862bda886d899e8 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Wed, 18 Jun 2014 13:54:51 -0700 Subject: (MODULES-660) Correct detached HEAD on latest Previously vcsrepo detached HEAD on checkout which caused further branch revisions to fail. This corrects the behavior, and works on git 1.7, 1.8, 1.9, and 2.0 --- lib/puppet/provider/vcsrepo/git.rb | 172 +++++++++++++++++++++---------------- 1 file changed, 99 insertions(+), 73 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 5c5dd07..0a142bb 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -31,69 +31,49 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) FileUtils.rm_rf(@resource.value(:path)) end + # Checks to see if the current revision is equal to the revision on the + # remote (whether on a branch, tag, or reference) + # + # @return [Boolean] Returns true if the repo is on the latest revision def latest? - at_path do - return self.revision == self.latest - end + return revision == latest_revision end + # Just gives the `should` value that we should be setting the repo to if + # latest? returns false + # + # @return [String] Returns the target sha/tag/branch def latest - branch = on_branch? - if !branch - return get_revision('HEAD') - else - return get_revision("#{@resource.value(:remote)}/#{branch}") - end + @resource.value(:revision) end + # Get the current revision of the repo (tag/branch/sha) + # + # @return [String] Returns the branch/tag if the current sha matches the + # remote; otherwise returns the current sha. def revision - update_references - current = at_path { git_with_identity('rev-parse', 'HEAD').chomp } - return current unless @resource.value(:revision) - - if tag_revision?(@resource.value(:revision)) - # git-rev-parse will give you the hash of the tag object itself rather than the commit it points to by default. - # Using tag^0 will return the actual commit. - canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:revision)}^0").chomp } - else - # if it's not a tag, look for it as a local ref - canonical = at_path { git_with_identity('rev-parse', '--revs-only', @resource.value(:revision)).chomp } - if canonical.empty? - # git rev-parse executed properly but didn't find the ref; - # look for it in the remote - remote_ref = at_path { git_with_identity('ls-remote', '--heads', '--tags', @resource.value(:remote), @resource.value(:revision)).chomp } - if remote_ref.empty? - fail("#{@resource.value(:revision)} is not a local or remote ref") - end - - # $ git ls-remote --heads --tags origin feature/cvs - # 7d4244b35e72904e30130cad6d2258f901c16f1a refs/heads/feature/cvs - canonical = remote_ref.split.first - end - end - - if current == canonical - @resource.value(:revision) - else - current - end + #HEAD is the default, but lets just be explicit here. + get_revision('HEAD') end + # Is passed the desired reference, whether a tag, rev, or branch. Should + # handle transitions from a rev/branch/tag to a rev/branch/tag. Detached + # heads should be treated like bare revisions. + # + # @param [String] desired The desired revision to which the repo should be + # set. def revision=(desired) + #just checkout tags and shas; fetch has already happened so they should be updated. checkout(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}") - if detached? - git_with_identity('checkout', "#{@resource.value(:revision)}") - git_with_identity('pull') - end - } + #branches require more work. + if local_branch_revision?(desired) + #reset instead of pull to avoid merge conflicts. assuming remote is + #updated and authoritative. + #TODO 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}") } end + #TODO Would this ever reach here if it is bare? if @resource.value(:ensure) != :bare update_submodules end @@ -132,10 +112,12 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) private + # @!visibility private def bare_git_config_exists? File.exist?(File.join(@resource.value(:path), 'config')) end + # @!visibility private def clone_repository(source, path) check_force args = ['clone'] @@ -158,6 +140,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end end + # @!visibility private def check_force if path_exists? and not path_empty? if @resource.value(:force) @@ -169,6 +152,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end end + # @!visibility private def init_repository(path) check_force if @resource.value(:ensure) == :bare && working_copy_exists? @@ -195,6 +179,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) # /.git # to: # / + # @!visibility private def convert_working_copy_to_bare notice "Converting working copy repository to bare repository" FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir) @@ -208,6 +193,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) # / # to: # /.git + # @!visibility private def convert_bare_to_working_copy notice "Converting bare repository to working copy repository" FileUtils.mv(@resource.value(:path), tempdir) @@ -220,6 +206,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end end + # @!visibility private def commits_in?(dot_git) Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e| return true unless %w(. ..).include?(File::basename(e)) @@ -227,26 +214,35 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) false end + # Will checkout a rev/branch/tag using the locally cached versions. Does not + # handle upstream branch changes + # @!visibility private def checkout(revision = @resource.value(:revision)) if !local_branch_revision? && remote_branch_revision? - at_path { git_with_identity('checkout', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") } + #non-locally existant branches (perhaps switching to a branch that has never been checked out) + at_path { git_with_identity('checkout', '--force', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") } else + #tags, locally existant branches (perhaps outdated), and shas at_path { git_with_identity('checkout', '--force', revision) } end end + # @!visibility private def reset(desired) at_path do git_with_identity('reset', '--hard', desired) end end + # @!visibility private def update_submodules at_path do git_with_identity('submodule', 'update', '--init', '--recursive') end end + # Determins if the branch exists at the upstream but has not yet been locally committed + # @!visibility private def remote_branch_revision?(revision = @resource.value(:revision)) # git < 1.6 returns '#{@resource.value(:remote)}/#{revision}' # git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}' @@ -254,64 +250,93 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) branch unless branch.empty? end + # Determins if the branch is already cached locally + # @!visibility private def local_branch_revision?(revision = @resource.value(:revision)) at_path { branches.include?(revision) } end + # @!visibility private def tag_revision?(revision = @resource.value(:revision)) at_path { tags.include?(revision) } end + # @!visibility private def branches at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip } end + # @!visibility private def on_branch? at_path { matches = git_with_identity('branch', '-a').match /\*\s+(.*)/ - matches[1] unless matches[1].match /detached/ - } - end - - def detached? - at_path { - git_with_identity('branch', '-a').match /\*\s+\(detached from.*\)/ + matches[1] unless matches[1].match /(\(detached from|\(no branch)/ } end + # @!visibility private def tags at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip } end + # @!visibility private def set_excludes at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}} end - def get_revision(rev) - if @resource.value(:force) && working_copy_exists? - create - end - if !working_copy_exists? - create - end - at_path do - update_remote_origin_url - git_with_identity('fetch', @resource.value(:remote)) - git_with_identity('fetch', '--tags', @resource.value(:remote)) + # Finds the latest revision or sha of the current branch if on a branch, or + # of HEAD otherwise. + # @note Calls create which can forcibly destroy and re-clone the repo if + # force => true + # @see get_revision + # + # @!visibility private + # @return [String] Returns the output of get_revision + def latest_revision + #TODO Why is create called here anyway? + create if @resource.value(:force) && working_copy_exists? + create if !working_copy_exists? + + if branch = on_branch? + return get_revision("#{@resource.value(:remote)}/#{branch}") + else + return get_revision end + end + + # Returns the current revision given if the revision is a tag or branch and + # matches the current sha. If the current sha does not match the sha of a tag + # or branch, then it will just return the sha (ie, is not in sync) + # + # @!visibility private + # + # @param [String] rev The revision of which to check if it is current + # @return [String] Returns the tag/branch of the current repo if it's up to + # date; otherwise returns the sha of the requested revision. + def get_revision(rev = 'HEAD') + update_references current = at_path { git_with_identity('rev-parse', rev).strip } if @resource.value(:revision) - if local_branch_revision? + if tag_revision? + # git-rev-parse will give you the hash of the tag object itself rather + # than the commit it points to by default. Using tag^0 will return the + # actual commit. + canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:revision)}^0").strip } + elsif local_branch_revision? canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip } elsif remote_branch_revision? - canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/" + @resource.value(:revision)).strip } + canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/#{@resource.value(:revision)}").strip } + else + #look for a sha (could match invalid shas) + canonical = at_path { git_with_identity('rev-parse', '--revs-only', @resource.value(:revision)).strip } end + fail("#{@resource.value(:revision)} is not a local or remote ref") if canonical.nil? or canonical.empty? current = @resource.value(:revision) if current == canonical end - update_owner_and_excludes return current end + # @!visibility private def update_owner_and_excludes if @resource.value(:owner) or @resource.value(:group) set_ownership @@ -321,6 +346,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end end + # @!visibility private def git_with_identity(*args) if @resource.value(:identity) Tempfile.open('git-helper') do |f| -- cgit v1.2.3 From 27c5d5847504d6d5e295b617b1461b4727ccf1f2 Mon Sep 17 00:00:00 2001 From: Paul Chechetin Date: Wed, 20 Aug 2014 11:47:34 +0400 Subject: Change uid by Puppet execution API --- lib/puppet/provider/vcsrepo/git.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 0a142bb..9e0306b 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -3,9 +3,7 @@ require File.join(File.dirname(__FILE__), '..', 'vcsrepo') Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) do desc "Supports Git repositories" - ##TODO modify the commands below so that the su - is included commands :git => 'git' - optional_commands :su => 'su' has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user, :depth @@ -365,7 +363,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) return ret end elsif @resource.value(:user) and @resource.value(:user) != Facter['id'].value - su(@resource.value(:user), '-c', "git #{args.join(' ')}" ) + Puppet::Util::Execution.execute("git #{args.join(' ')}", :uid => @resource.value(:user), :failonfail => true) else git(*args) end -- cgit v1.2.3 From 0e7c0b06e952d5133bd7f9f055d680cb704e213e Mon Sep 17 00:00:00 2001 From: Aaron Stone Date: Sat, 13 Sep 2014 23:27:25 -0700 Subject: Handle both Array/Enumerable and String values for excludes parameter Thanks to @revhazroot for identifying the problem that excludes with a string or single-element array fails on Ruby >= 1.9. --- lib/puppet/provider/vcsrepo/git.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 0a142bb..48bbe7d 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -281,7 +281,16 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) # @!visibility private def set_excludes - at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}} + # Excludes may be an Array or a String. + at_path do + open('.git/info/exclude', 'w') do |f| + if @resource.value(:excludes).respond_to?(:each) + @resource.value(:excludes).each { |ex| f.puts ex } + else + f.puts @resource.value(:excludes) + end + end + end end # Finds the latest revision or sha of the current branch if on a branch, or -- cgit v1.2.3 From 56f25d57dfa26de618416e9bdd4a853296ffcbc1 Mon Sep 17 00:00:00 2001 From: Morgan Haskel Date: Fri, 26 Dec 2014 15:27:20 -0800 Subject: MODULES-1596 - Repository repeatedly destroyed/created with force The `retrieve` method was calling `create` and `destroy` on every run with `force => true`. Retrieve should not be making any changes to the system, so removed that code, and updated `working_copy_exists` to make sure that the directory not only contains a `.git` directory, but also if `source` is specified it also matches `#{path}/.git/config` so that it will overwrite a git repo with a different source. Updated tests to not check for the old broken behavior. Added a regression test. --- lib/puppet/provider/vcsrepo/git.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 1c6588c..b1e556d 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -83,7 +83,11 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end def working_copy_exists? - File.directory?(File.join(@resource.value(:path), '.git')) + if @resource.value(:source) and File.exists?(File.join(@resource.value(:path), '.git', 'config')) + File.readlines(File.join(@resource.value(:path), '.git', 'config')).grep(/#{@resource.value(:source)}/).any? + else + File.directory?(File.join(@resource.value(:path), '.git')) + end end def exists? -- cgit v1.2.3 From dfe5f9cc316a43793da2cb1c4adbc66503907460 Mon Sep 17 00:00:00 2001 From: dduvnjak Date: Sun, 18 Jan 2015 10:49:03 +0100 Subject: Add submodules feature to git provider --- lib/puppet/provider/vcsrepo/git.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 1c6588c..9e1903c 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -5,7 +5,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) commands :git => 'git' - has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user, :depth + has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user, :depth, :submodules def create if @resource.value(:revision) and @resource.value(:ensure) == :bare @@ -18,7 +18,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) if @resource.value(:revision) checkout end - if @resource.value(:ensure) != :bare + if @resource.value(:ensure) != :bare && @resource.value(:submodules) == :true update_submodules end end -- cgit v1.2.3 From 5d6ef988af1ff90b4625b0426301cb6fad0268b8 Mon Sep 17 00:00:00 2001 From: Jonathan Tripathy Date: Tue, 20 Jan 2015 23:12:20 -0800 Subject: Implemented multiple remotes feature for git provider. --- lib/puppet/provider/vcsrepo/git.rb | 70 +++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 8 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 13e0793..9d3f7f3 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -14,13 +14,16 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) if !@resource.value(:source) init_repository(@resource.value(:path)) else - clone_repository(@resource.value(:source), @resource.value(:path)) + clone_repository(default_url, @resource.value(:path)) + update_remotes + if @resource.value(:revision) checkout end if @resource.value(:ensure) != :bare && @resource.value(:submodules) == :true update_submodules end + end update_owner_and_excludes end @@ -82,9 +85,25 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) bare_git_config_exists? && !working_copy_exists? end + # If :source is set to a hash (for supporting multiple remotes), + # we search for the URL for :remote. If it doesn't exist, + # we throw an error. If :source is just a string, we use that + # value for the default URL. + def default_url + if @resource.value(:source).is_a?(Hash) + if @resource.value(:source).has_key?(@resource.value(:remote)) + @resource.value(:source)[@resource.value(:remote)] + else + fail("You must specify the URL for #{@resource.value(:remote)} in the :source hash") + end + else + @resource.value(:source) + end + end + def working_copy_exists? if @resource.value(:source) and File.exists?(File.join(@resource.value(:path), '.git', 'config')) - File.readlines(File.join(@resource.value(:path), '.git', 'config')).grep(/#{@resource.value(:source)}/).any? + File.readlines(File.join(@resource.value(:path), '.git', 'config')).grep(/#{default_url}/).any? else File.directory?(File.join(@resource.value(:path), '.git')) end @@ -94,18 +113,53 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) working_copy_exists? || bare_exists? end - def update_remote_origin_url - current = git_with_identity('config', "remote.#{@resource.value(:remote)}.url") - unless @resource.value(:source).nil? - if current.nil? or current.strip != @resource.value(:source) - git_with_identity('config', "remote.#{@resource.value(:remote)}.url", @resource.value(:source)) + def update_remote_url(remote_name, remote_url) + do_update = false + current = git_with_identity('config', '-l') + + unless remote_url.nil? + # Check if remote exists at all, regardless of URL. + # If remote doesn't exist, add it + if not current.include? "remote.#{remote_name}.url" + git_with_identity('remote','add', remote_name, remote_url) + return true + + # If remote exists, but URL doesn't match, update URL + elsif not current.include? "remote.#{remote_name}.url=#{remote_url}" + git_with_identity('remote','set-url', remote_name, remote_url) + return true + else + return false end end + + end + + def update_remotes + do_update = false + + # If supplied source is a hash of remote name and remote url pairs, then + # we loop around the hash. Otherwise, we assume single url specified + # in source property + if @resource.value(:source).is_a?(Hash) + @resource.value(:source).each do |remote_name, remote_url| + at_path { do_update |= update_remote_url(remote_name, remote_url) } + end + else + at_path { do_update |= update_remote_url(@resource.value(:remote), @resource.value(:source)) } + end + + # If at least one remote was added or updated, then we must + # call the 'git remote update' command + if do_update == true + at_path { git_with_identity('remote','update') } + end + end def update_references at_path do - update_remote_origin_url + update_remotes git_with_identity('fetch', @resource.value(:remote)) git_with_identity('fetch', '--tags', @resource.value(:remote)) update_owner_and_excludes -- cgit v1.2.3 From 28f8646e4669bacf7a87ffc8694715333355cd32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?W=C5=82odzimierz=20Gajda?= Date: Wed, 22 Oct 2014 10:02:29 +0200 Subject: Use branch parameter --- lib/puppet/provider/vcsrepo/git.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 9d3f7f3..8a8c12f 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -5,7 +5,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) commands :git => 'git' - has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user, :depth, :submodules + has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user, :depth, :branch, :submodules def create if @resource.value(:revision) and @resource.value(:ensure) == :bare @@ -180,6 +180,9 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) if @resource.value(:depth) and @resource.value(:depth).to_i > 0 args.push('--depth', @resource.value(:depth).to_s) end + if @resource.value(:branch) + args.push('--branch', @resource.value(:branch).to_s) + end if @resource.value(:ensure) == :bare args << '--bare' end -- cgit v1.2.3 From cf5c8e6e9fb46115d59ec4e3b5d433e29168fe00 Mon Sep 17 00:00:00 2001 From: Ernesto Ruy Sanchez Date: Fri, 30 Jan 2015 10:46:59 -0800 Subject: Added submodules true condition before update_submodules on revision method --- lib/puppet/provider/vcsrepo/git.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 9d3f7f3..e31a76c 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -20,7 +20,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) if @resource.value(:revision) checkout end - if @resource.value(:ensure) != :bare && @resource.value(:submodules) == :true + if @resource.value(:ensure) != :bare && @resource.value(:submodules) == :true && @resource.value(:submodules) == :true update_submodules end @@ -75,7 +75,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) at_path { git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}") } end #TODO Would this ever reach here if it is bare? - if @resource.value(:ensure) != :bare + if @resource.value(:ensure) != :bare && @resource.value(:submodules) == :true update_submodules end update_owner_and_excludes -- cgit v1.2.3 From 56852e9cbf93c3f6f5db7dcdd40b3d364b548182 Mon Sep 17 00:00:00 2001 From: Ernesto Ruy Sanchez Date: Fri, 30 Jan 2015 11:01:01 -0800 Subject: Bugfix: removed duplicated condition --- lib/puppet/provider/vcsrepo/git.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index e31a76c..dde9bf0 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -20,7 +20,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) if @resource.value(:revision) checkout end - if @resource.value(:ensure) != :bare && @resource.value(:submodules) == :true && @resource.value(:submodules) == :true + if @resource.value(:ensure) != :bare && @resource.value(:submodules) == :true update_submodules end -- cgit v1.2.3 From d27759141ec5592fd5d76861efa77a55d07c90a8 Mon Sep 17 00:00:00 2001 From: Colleen Murphy Date: Wed, 25 Mar 2015 12:04:16 -0700 Subject: Fix remote hash ordering for unit tests Without this commit, the unit tests for the git provider changing multiple remotes mocks the remotes in a particular order. While in practice it doesn't matter which remote the update_remotes method updates first, the unit tests must be able to mock them in the correct order. For ruby 1.8.7, a Hash will not necessarily produce key value pairs in the same order on each run, which causes intermittent failures in the unit tests. This change sorts the :source property values before trying to update them, and updates the unit tests to expect the values in alphabetical order. --- lib/puppet/provider/vcsrepo/git.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 9d3f7f3..000032e 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -142,7 +142,8 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) # we loop around the hash. Otherwise, we assume single url specified # in source property if @resource.value(:source).is_a?(Hash) - @resource.value(:source).each do |remote_name, remote_url| + @resource.value(:source).keys.sort.each do |remote_name| + remote_url = @resource.value(:source)[remote_name] at_path { do_update |= update_remote_url(remote_name, remote_url) } end else -- cgit v1.2.3 From 7f97a76f4682a7c1d5bfbbc3cd5a6dd9523b1d96 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Wed, 13 May 2015 15:39:01 -0700 Subject: (MODULES-821) Don't use /tmp Very often /tmp is mounted noexec for security reasons related to it being writable by all users. This stopped vcsrepo's git provider from working. This pull request uses puppet's statedir as it is not writable by all users and is a good candidate for transient state like the git provider's identity script for the $GIT_SSH command. An alternative in the future that wouldn't require a temporary file is to set $GIT_SSH_COMMAND instead of $GIT_SSH, except this was added in git 2.3 and is too new to depend on. --- lib/puppet/provider/vcsrepo/git.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 8101253..44e4d04 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -418,7 +418,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) # @!visibility private def git_with_identity(*args) if @resource.value(:identity) - Tempfile.open('git-helper') do |f| + Tempfile.open('git-helper', Puppet[:statedir]) do |f| f.puts '#!/bin/sh' f.puts "exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -oConnectTimeout=120 -i #{@resource.value(:identity)} $*" f.close -- cgit v1.2.3 From ae60f5ccb7c00a0e162a06ba05d247688ffbce4b Mon Sep 17 00:00:00 2001 From: Jonathan Tripathy Date: Mon, 22 Jun 2015 15:22:16 +0100 Subject: MODULES-2131 Git provider now takes account of revision property when using depth property. --- lib/puppet/provider/vcsrepo/git.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 13e0793..4603029 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -125,6 +125,9 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) args = ['clone'] if @resource.value(:depth) and @resource.value(:depth).to_i > 0 args.push('--depth', @resource.value(:depth).to_s) + if @resource.value(:revision) + args.push('--branch', @resource.value(:revision).to_s) + end end if @resource.value(:ensure) == :bare args << '--bare' -- cgit v1.2.3 From b3cddcbd57639acbf0c4a7fa671a70f787e010da Mon Sep 17 00:00:00 2001 From: Samuel Keeley Date: Mon, 29 Jun 2015 12:01:10 -0700 Subject: fix for detached HEAD on git 2.4+ --- lib/puppet/provider/vcsrepo/git.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 8074950..bf11f3d 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -329,11 +329,13 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip } end + # git < 2.4 returns 'detached from' + # git 2.4+ returns 'HEAD detached at' # @!visibility private def on_branch? at_path { matches = git_with_identity('branch', '-a').match /\*\s+(.*)/ - matches[1] unless matches[1].match /(\(detached from|\(no branch)/ + matches[1] unless matches[1].match /(\(detached from|\(HEAD detached at|\(no branch)/ } end -- cgit v1.2.3 From 2a498ae29cd2d4eeac52fa123bb97e5e2d5b1c90 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Wed, 22 Jul 2015 11:51:37 -0700 Subject: Explitly unset SSH_AUTH_SOCK; it is preferred to the -i flag Even if the -i flag is explicitly passed via the command line, openssh ignores the identity file if an socket exists to an SSH authentication agent. In cases where puppet has been configured to use an explicit identity file, altering behavior based on the calling environment of puppet violates the principle of least surprise, and can lead to inconsistent deployments. Work around this odd corner case of ssh by explicitly unsetting SSH_AUTH_SOCK inside the ssh wrapper. --- lib/puppet/provider/vcsrepo/git.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index bf11f3d..7e921a9 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -425,6 +425,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) if @resource.value(:identity) Tempfile.open('git-helper', Puppet[:statedir]) do |f| f.puts '#!/bin/sh' + f.puts 'export SSH_AUTH_SOCKET=' f.puts "exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -oConnectTimeout=120 -i #{@resource.value(:identity)} $*" f.close -- cgit v1.2.3 From d68402d1f930d5a30f1ec9224ac3791b6d9d29b9 Mon Sep 17 00:00:00 2001 From: Morgan Haskel Date: Mon, 3 Aug 2015 16:49:01 -0700 Subject: MODULES-1800 - fix case where ensure => latest and no revision specified This would explode when revision was unspecified when you were on a branch. Use the branch you're currently on when updating. --- lib/puppet/provider/vcsrepo/git.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index bf11f3d..f13802b 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -45,7 +45,11 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) # # @return [String] Returns the target sha/tag/branch def latest - @resource.value(:revision) + if not @resource.value(:revision) and branch = on_branch? + return branch + else + return @resource.value(:revision) + end end # Get the current revision of the repo (tag/branch/sha) @@ -281,7 +285,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) # handle upstream branch changes # @!visibility private def checkout(revision = @resource.value(:revision)) - if !local_branch_revision? && remote_branch_revision? + if !local_branch_revision?(revision) && remote_branch_revision?(revision) #non-locally existant branches (perhaps switching to a branch that has never been checked out) at_path { git_with_identity('checkout', '--force', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") } else -- cgit v1.2.3 From 14c05f5d6c589bebc9f93eb117105c14ce7be6f1 Mon Sep 17 00:00:00 2001 From: Morgan Haskel Date: Thu, 13 Aug 2015 16:02:22 -0700 Subject: MODULES-2125 - Allow revision to be passed without source Will also work with empty repositories. --- lib/puppet/provider/vcsrepo/git.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index f13802b..7617b13 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -76,7 +76,11 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) #updated and authoritative. #TODO 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}") } + if @resource.value(:source) + at_path { git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}") } + else + at_path { git_with_identity('reset', '--hard', "#{desired}") } + end end #TODO Would this ever reach here if it is bare? if @resource.value(:ensure) != :bare && @resource.value(:submodules) == :true @@ -392,7 +396,17 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) # @return [String] Returns the tag/branch of the current repo if it's up to # date; otherwise returns the sha of the requested revision. def get_revision(rev = 'HEAD') - update_references + if @resource.value(:source) + update_references + else + status = at_path { git_with_identity('status')} + is_it_new = status =~ /Initial commit/ + if is_it_new + status =~ /On branch (.*)/ + branch = $1 + return branch + end + end current = at_path { git_with_identity('rev-parse', rev).strip } if @resource.value(:revision) if tag_revision? -- cgit v1.2.3 From f385df10c57a0a09fa316004e6af18acd56df710 Mon Sep 17 00:00:00 2001 From: Morgan Haskel Date: Mon, 17 Aug 2015 12:14:18 -0700 Subject: MODULES-2326 - Run Regexp.escape on the source URL The URL can have special characters, making the regex match fail. --- lib/puppet/provider/vcsrepo/git.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 7617b13..4623f8e 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -111,7 +111,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) def working_copy_exists? if @resource.value(:source) and File.exists?(File.join(@resource.value(:path), '.git', 'config')) - File.readlines(File.join(@resource.value(:path), '.git', 'config')).grep(/#{default_url}/).any? + File.readlines(File.join(@resource.value(:path), '.git', 'config')).grep(/#{Regexp.escape(default_url)}/).any? else File.directory?(File.join(@resource.value(:path), '.git')) end -- cgit v1.2.3 From ba9d0cf6c1d98ea70270faa5cf4e70997ea75595 Mon Sep 17 00:00:00 2001 From: Morgan Haskel Date: Mon, 24 Aug 2015 17:07:37 -0700 Subject: MODULES-1232 Make sure HOME is set correctly --- lib/puppet/provider/vcsrepo/git.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 4623f8e..a34b154 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -3,7 +3,9 @@ require File.join(File.dirname(__FILE__), '..', 'vcsrepo') Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) do desc "Supports Git repositories" - commands :git => 'git' + has_command(:git, 'git') do + environment({ 'HOME' => ENV['HOME'] }) + end has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user, :depth, :branch, :submodules @@ -457,7 +459,8 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) return ret end elsif @resource.value(:user) and @resource.value(:user) != Facter['id'].value - Puppet::Util::Execution.execute("git #{args.join(' ')}", :uid => @resource.value(:user), :failonfail => true) + env = Etc.getpwnam(@resource.value(:user)) + Puppet::Util::Execution.execute("git #{args.join(' ')}", :uid => @resource.value(:user), :failonfail => true, :custom_environment => {'HOME' => env['dir']}) else git(*args) end -- cgit v1.2.3 From 7fe9cb225b6458e468469597a54753f1ea621e00 Mon Sep 17 00:00:00 2001 From: godlikeachilles Date: Wed, 2 Dec 2015 15:07:15 +1100 Subject: fix branch existence determintaion functionality this will stop failing in case there is a tag which is a substring of an existing branch name. for example if there is a tag 'release' and a branch 'release/integration' current code will match 'release/integration' with pattern 'release' and decide that a branch checkout is needed. but release branch does not exist so it will fail. this simple fix resolves the issue. --- lib/puppet/provider/vcsrepo/git.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 3b20a83..0249a3c 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -319,7 +319,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) def remote_branch_revision?(revision = @resource.value(:revision)) # git < 1.6 returns '#{@resource.value(:remote)}/#{revision}' # git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}' - branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}/ } + branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}$/ } branch unless branch.empty? end -- cgit v1.2.3 From b8f25cea95317a4b2a622e2799f1aa7ba159bdca Mon Sep 17 00:00:00 2001 From: "Strech (Sergey Fedorov)" Date: Tue, 22 Dec 2015 23:02:26 +0100 Subject: Add mirror option for git cloning Example: vcsrepo { '/path/to/repo': ensure => mirror, provider => git, source => 'git://example.com/repo.git', } --- lib/puppet/provider/vcsrepo/git.rb | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 0249a3c..63986ab 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -10,10 +10,14 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user, :depth, :branch, :submodules def create - if @resource.value(:revision) and @resource.value(:ensure) == :bare + if @resource.value(:revision) and ensure_bare_or_mirror? fail("Cannot set a revision (#{@resource.value(:revision)}) on a bare repository") end if !@resource.value(:source) + if @resource.value(:ensure) == :mirror + fail("Cannot init repository with mirror option, try bare instead") + end + init_repository(@resource.value(:path)) else clone_repository(default_url, @resource.value(:path)) @@ -22,7 +26,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) if @resource.value(:revision) checkout end - if @resource.value(:ensure) != :bare && @resource.value(:submodules) == :true + if !ensure_bare_or_mirror? && @resource.value(:submodules) == :true update_submodules end @@ -85,7 +89,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end end #TODO Would this ever reach here if it is bare? - if @resource.value(:ensure) != :bare && @resource.value(:submodules) == :true + if !ensure_bare_or_mirror? && @resource.value(:submodules) == :true update_submodules end update_owner_and_excludes @@ -95,6 +99,10 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) bare_git_config_exists? && !working_copy_exists? end + def ensure_bare_or_mirror? + [:bare, :mirror].include? @resource.value(:ensure) + end + # If :source is set to a hash (for supporting multiple remotes), # we search for the URL for :remote. If it doesn't exist, # we throw an error. If :source is just a string, we use that @@ -197,9 +205,12 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) if @resource.value(:branch) args.push('--branch', @resource.value(:branch).to_s) end - if @resource.value(:ensure) == :bare - args << '--bare' + + case @resource.value(:ensure) + when :bare then args << '--bare' + when :mirror then args << '--mirror' end + if @resource.value(:remote) != 'origin' args.push('--origin', @resource.value(:remote)) end -- cgit v1.2.3