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

Avoid clone same repo for Git gems #109

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
36 changes: 33 additions & 3 deletions lib/gel/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def initialize(store)
@dependencies = Hash.new { |h, k| h[k] = [] }
@weights = Hash.new(1)
@pending = Hash.new(0)
@git_remotes = Hash.new

@download_pool = Gel::WorkPool.new(DOWNLOAD_CONCURRENCY, monitor: self, name: "gel-download", collect_errors: true)
@compile_pool = Gel::WorkPool.new(COMPILE_CONCURRENCY, monitor: self, name: "gel-compile", collect_errors: true)
Expand Down Expand Up @@ -76,15 +77,44 @@ def install_gem(catalogs, name, version)
def load_git_gem(remote, revision, name)
kaspth marked this conversation as resolved.
Show resolved Hide resolved
synchronize do
@pending[name] += 1
@download_pool.queue(name) do
work_git(remote, revision, name)

checkout_key = "#{remote}@#{revision}"

# Has another gem already been pulled from this git repo?
if @git_remotes.key?(checkout_key)
if waiting_gems = @git_remotes[checkout_key]
# The checkout is currently queued or in progress; we'll wait
# for it too
waiting_gems << name
else
# The checkout has already finished, so this gem is already
# available
git_ready(name)
end
else
@git_remotes[checkout_key] = [name]

@download_pool.queue(name) do
work_git(remote, revision, checkout_key)
end
end
end
end

def work_git(remote, revision, name)
def work_git(remote, revision, checkout_key)
@git_depot.checkout(remote, revision)

synchronize do
gem_names = @git_remotes[checkout_key]
@git_remotes[checkout_key] = nil

gem_names.each do |name|
git_ready(name)
end
end
end

def git_ready(name)
@messages << "Using #{name} (git)\n"
@pending[name] -= 1
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gel/lock_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def activate(env, base_store, install: false, output: nil)
revision = resolved_gem.body["revision"].first

dir = git_depot.git_path(remote, revision)
if installer && !Dir.exist?(dir)
if installer
installer.load_git_gem(remote, revision, name)

locks[name] = -> { Gel::DirectGem.new(dir, name, resolved_gem.version) }
Expand Down
29 changes: 29 additions & 0 deletions test/installer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "test_helper"

require "gel/installer"

class InstallerTest < Minitest::Test
def test_install_git_gems_only_checkout_once
Dir.mktmpdir do |dir|
remote = "https://github.com/rails/rails.git"
revision = "d56d2e740612717334840b0d0ea979e1ca7cf5b1"
names = ["actioncable", "actionmailbox"]
store = Gel::Store.new(dir)
installer = Gel::Installer.new(store)
fake_download_pool = Minitest::Mock.new
installer.instance_variable_set(
:@download_pool,
fake_download_pool
)
fake_download_pool.expect(:queue, true) do |argument|
argument == "actioncable"
end
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this expect got run twice, Minitest will throw an error

MockExpectationError: No more expects available for :queue: ["actionmailbox"]

simulates checkout twice in real scenario.


names.each do |name|
installer.load_git_gem(remote, revision, name)
end
end
end
end