Skip to content

Commit

Permalink
[rubygems/rubygems] Don't run any git commands when sorting and compa…
Browse files Browse the repository at this point in the history
…ring git sources

Previously, when sorting and comparing git Gemfile vs lockfile sources during
`bundler/setup` to figure out whether we need to re-resolve or not, we
would try to find the default branch if nothing more specific was
specified in the Gemfile.

If the git cache has been deleted thought, that would fail.

The error would still be swallowed (and the branch would simply not be
displayed), but trying to clone would still generate the side effect of
creating the parent folder for the clone.

That could affect non-writable systems that don't expect `bundler/setup`
to write to the filesystem at all.

To fix this, override `Bundler::Source::Git#identifier` to use
exclusively static information, so it does not even try to clone the
repo nor generate any side effects.

rubygems/rubygems@582eb2ef39
  • Loading branch information
deivid-rodriguez authored and hsbt committed Jul 13, 2023
1 parent 8cf5297 commit c1fb25f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/bundler/source/git.rb
Expand Up @@ -69,19 +69,7 @@ def eql?(other)

def to_s
begin
at = if local?
path
elsif user_ref = options["ref"]
if /\A[a-z0-9]{4,}\z/i.match?(ref)
shortref_for_display(user_ref)
else
user_ref
end
elsif ref
ref
else
current_branch
end
at = humanized_ref || current_branch

rev = "at #{at}@#{shortref_for_display(revision)}"
rescue GitError
Expand All @@ -91,6 +79,10 @@ def to_s
uri_with_specifiers([rev, glob_for_display])
end

def identifier
uri_with_specifiers([humanized_ref, cached_revision, glob_for_display])
end

def uri_with_specifiers(specifiers)
specifiers.compact!

Expand Down Expand Up @@ -256,6 +248,20 @@ def local?

private

def humanized_ref
if local?
path
elsif user_ref = options["ref"]
if /\A[a-z0-9]{4,}\z/i.match?(ref)
shortref_for_display(user_ref)
else
user_ref
end
elsif ref
ref
end
end

def serialize_gemspecs_in(destination)
destination = destination.expand_path(Bundler.root) if destination.relative?
Dir["#{destination}/#{@glob}"].each do |spec_path|
Expand Down
7 changes: 7 additions & 0 deletions spec/bundler/install/gemfile/git_spec.rb
Expand Up @@ -30,6 +30,13 @@
expect(Dir["#{default_bundle_path}/cache/bundler/git/foo-1.0-*"]).to have_attributes :size => 1
end

it "does not write to cache on bundler/setup" do
cache_path = default_bundle_path.join("cache")
FileUtils.rm_rf(cache_path)
ruby "require 'bundler/setup'"
expect(cache_path).not_to exist
end

it "caches the git repo globally and properly uses the cached repo on the next invocation" do
simulate_new_machine
bundle "config set global_gem_cache true"
Expand Down

0 comments on commit c1fb25f

Please sign in to comment.