diff --git a/lib/git_manager.rb b/lib/git_manager.rb index 72149e0..d084bc5 100644 --- a/lib/git_manager.rb +++ b/lib/git_manager.rb @@ -1,5 +1,7 @@ +require 'fileutils' require 'logging' require 'running' +require 'shellwords' # Lightweight wrapper over Git, shells out everything. class GitManager @@ -13,7 +15,7 @@ def initialize(basedir) end def remote_rails_url - 'https://github.com/rails/rails.git' + "#{github_rails_url}.git" end def update_main @@ -38,8 +40,17 @@ def update_main def checkout(tag) Dir.chdir(basedir) do - log "checking out tag #{tag}" - log_and_system "git -c advice.detachedHead=false clone -q --depth 1 --single-branch --branch #{tag} #{remote_rails_url} #{tag}" + log "fetching archive for tag #{tag}" + FileUtils.rm_rf(tag) + FileUtils.mkdir(tag) + + archive_url = remote_archive_url(tag) + log "downloading #{archive_url}" + + Dir.chdir(tag) do + command = "curl -sSL #{Shellwords.shellescape(archive_url)} | tar -xzf - --strip-components=1" + log_and_system 'sh', '-c', command + end end end @@ -58,4 +69,14 @@ def sha1 `git rev-parse HEAD`.chomp end end + + private + + def remote_archive_url(tag) + "#{github_rails_url}/archive/refs/tags/#{tag}.tar.gz" + end + + def github_rails_url + 'https://github.com/rails/rails' + end end diff --git a/test/git_manager_test.rb b/test/git_manager_test.rb index 1861620..6305b2a 100644 --- a/test/git_manager_test.rb +++ b/test/git_manager_test.rb @@ -1,6 +1,7 @@ require_relative 'test_helper' require 'git_manager' +require 'shellwords' class GitManagerTest < Minitest::Test def create_repository @@ -27,10 +28,16 @@ def test_checkout system 'echo t2 > README' system 'git commit -aqm "test"' system 'git tag t2' + + %w(t1 t2).each do |tag| + archive_path = File.expand_path("../#{tag}.tar.gz", Dir.pwd) + system 'sh', '-c', "git archive --format=tar --prefix=rails-#{tag}/ #{tag} | gzip > #{Shellwords.escape(archive_path)}" + end end gm = GitManager.new('basedir') - gm.stub('remote_rails_url', "file://#{Dir.pwd}/basedir/main") do + archive_dir = File.expand_path('basedir') + gm.stub(:remote_archive_url, ->(requested_tag) { "file://#{File.expand_path("#{requested_tag}.tar.gz", archive_dir)}" }) do gm.checkout('t1') end