Skip to content

Commit 93a7e36

Browse files
committed
Download tarballs from GitHub archive URLs instead of cloning the repo
This will save space by not downloading the `.git` folder and just getting the source code for the specified tag.
1 parent 88a6a8f commit 93a7e36

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/git_manager.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
require 'fileutils'
12
require 'logging'
23
require 'running'
4+
require 'shellwords'
35

46
# Lightweight wrapper over Git, shells out everything.
57
class GitManager
@@ -13,7 +15,7 @@ def initialize(basedir)
1315
end
1416

1517
def remote_rails_url
16-
'https://github.com/rails/rails.git'
18+
"#{github_rails_url}.git"
1719
end
1820

1921
def update_main
@@ -38,8 +40,17 @@ def update_main
3840

3941
def checkout(tag)
4042
Dir.chdir(basedir) do
41-
log "checking out tag #{tag}"
42-
log_and_system "git -c advice.detachedHead=false clone -q --depth 1 --single-branch --branch #{tag} #{remote_rails_url} #{tag}"
43+
log "fetching archive for tag #{tag}"
44+
FileUtils.rm_rf(tag)
45+
FileUtils.mkdir(tag)
46+
47+
archive_url = remote_archive_url(tag)
48+
log "downloading #{archive_url}"
49+
50+
Dir.chdir(tag) do
51+
command = "curl -sSL #{Shellwords.shellescape(archive_url)} | tar -xzf - --strip-components=1"
52+
log_and_system 'sh', '-c', command
53+
end
4354
end
4455
end
4556

@@ -58,4 +69,14 @@ def sha1
5869
`git rev-parse HEAD`.chomp
5970
end
6071
end
72+
73+
private
74+
75+
def remote_archive_url(tag)
76+
"#{github_rails_url}/archive/refs/tags/#{tag}.tar.gz"
77+
end
78+
79+
def github_rails_url
80+
'https://github.com/rails/rails'
81+
end
6182
end

test/git_manager_test.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative 'test_helper'
22

33
require 'git_manager'
4+
require 'shellwords'
45

56
class GitManagerTest < Minitest::Test
67
def create_repository
@@ -27,10 +28,16 @@ def test_checkout
2728
system 'echo t2 > README'
2829
system 'git commit -aqm "test"'
2930
system 'git tag t2'
31+
32+
%w(t1 t2).each do |tag|
33+
archive_path = File.expand_path("../#{tag}.tar.gz", Dir.pwd)
34+
system 'sh', '-c', "git archive --format=tar --prefix=rails-#{tag}/ #{tag} | gzip > #{Shellwords.escape(archive_path)}"
35+
end
3036
end
3137

3238
gm = GitManager.new('basedir')
33-
gm.stub('remote_rails_url', "file://#{Dir.pwd}/basedir/main") do
39+
archive_dir = File.expand_path('basedir')
40+
gm.stub(:remote_archive_url, ->(requested_tag) { "file://#{File.expand_path("#{requested_tag}.tar.gz", archive_dir)}" }) do
3441
gm.checkout('t1')
3542
end
3643

0 commit comments

Comments
 (0)