diff --git a/lib/bundler/source/git.rb b/lib/bundler/source/git.rb index 3f69ea1e65..9ce74adc2c 100644 --- a/lib/bundler/source/git.rb +++ b/lib/bundler/source/git.rb @@ -188,9 +188,11 @@ module Bundler end def specs(*) - set_cache_path!(app_cache_path) if has_app_cache? && !local? + set_cache_path!(app_cache_path) if use_app_cache? if requires_checkout? && !@copied + FileUtils.rm_rf(app_cache_path) if use_app_cache? && git_proxy.not_a_bare_repository? + fetch checkout end @@ -321,6 +323,10 @@ module Bundler cached_revision && super end + def use_app_cache? + has_app_cache? && !local? + end + def requires_checkout? allow_git_ops? && !local? && !cached_revision_checked_out? end diff --git a/lib/bundler/source/git/git_proxy.rb b/lib/bundler/source/git/git_proxy.rb index 2fc9c6535f..d6ff7fb488 100644 --- a/lib/bundler/source/git/git_proxy.rb +++ b/lib/bundler/source/git/git_proxy.rb @@ -84,6 +84,10 @@ module Bundler end end + def not_a_bare_repository? + git_local("rev-parse", "--is-bare-repository", dir: path).strip == "false" + end + def contains?(commit) allowed_with_path do result, status = git_null("branch", "--contains", commit, dir: path) diff --git a/spec/bundler/cache/git_spec.rb b/spec/bundler/cache/git_spec.rb index 4e3038f3ce..cbd755872c 100644 --- a/spec/bundler/cache/git_spec.rb +++ b/spec/bundler/cache/git_spec.rb @@ -258,6 +258,46 @@ RSpec.describe "bundle cache with git" do end end + it "can install after bundle cache generated with an older Bundler that kept checkouts in the cache" do + git = build_git("foo") + locked_revision = git.ref_for("main") + path_revision = git.ref_for("main", 11) + + git_path = lib_path("foo-1.0") + + gemfile <<-G + source "https://gem.repo1" + gem "foo", :git => '#{git_path}' + G + lockfile <<~L + GIT + remote: #{git_path}/ + revision: #{locked_revision} + specs: + foo (1.0) + + GEM + remote: https://gem.repo1/ + specs: + + PLATFORMS + #{lockfile_platforms} + + DEPENDENCIES + foo! + + BUNDLED WITH + #{Bundler::VERSION} + L + + # Simulate an old incorrect situation where vendor/cache would be the install location of git gems + FileUtils.mkdir_p bundled_app("vendor/cache") + FileUtils.cp_r git_path, bundled_app("vendor/cache/foo-1.0-#{path_revision}") + FileUtils.rm_rf bundled_app("vendor/cache/foo-1.0-#{path_revision}/.git") + + bundle :install, env: { "BUNDLE_DEPLOYMENT" => "true", "BUNDLE_CACHE_ALL" => "true" } + end + it "respects the --no-install flag" do git = build_git "foo", &:add_c_extension ref = git.ref_for("main", 11)