Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PDK-1033) Use --unshallow when fetching a ref #247

Merged
merged 2 commits into from
Jun 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions lib/puppetlabs_spec_helper/tasks/fixtures.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'yaml'
require 'open3'

module PuppetlabsSpecHelper; end
module PuppetlabsSpecHelper::Tasks; end
Expand Down Expand Up @@ -124,19 +125,25 @@ def clone_repo(scm, remote, target, _subdir = nil, ref = nil, branch = nil, flag
end

def update_repo(scm, target)
args = case scm
when 'hg'
['pull']
when 'git'
['fetch']
else
raise "Unfortunately #{scm} is not supported yet"
end
Dir.chdir(target) do
args = case scm
when 'hg'
['pull']
when 'git'
['fetch'].tap do |git_args|
git_args << '--unshallow' if shallow_git_repo?
end
else
raise "Unfortunately #{scm} is not supported yet"
end
system("#{scm} #{args.flatten.join(' ')}")
end
end

def shallow_git_repo?
File.file?(File.join('.git', 'shallow'))
end

def revision(scm, target, ref)
args = []
case scm
Expand All @@ -150,6 +157,22 @@ def revision(scm, target, ref)
system("cd #{target} && #{scm} #{args.flatten.join ' '}")
end

def valid_repo?(scm, target, remote)
return false unless File.directory?(target)
return true if scm == 'hg'

return true if git_remote_url(target) == remote

warn "Git remote for #{target} has changed, recloning repository"
FileUtils.rm_rf(target)
false
end

def git_remote_url(target)
output, status = Open3.capture2e('git', '-C', target, 'remote', 'get-url', 'origin')
status.success? ? output.strip : nil
end

def remove_subdirectory(target, subdir)
unless subdir.nil?
Dir.mktmpdir do |tmpdir|
Expand Down Expand Up @@ -259,7 +282,7 @@ def check_directory_for_symlinks(dir = '.')
logger.debug "New Thread started for #{remote}"
# start up a new thread and store it in the opts hash
opts[:thread] = Thread.new do
if File.directory?(target)
if valid_repo?(scm, target, remote)
update_repo(scm, target)
else
clone_repo(scm, remote, target, subdir, ref, branch, flags)
Expand Down