Skip to content

Commit 582eb2e

Browse files
Don't run any git commands when sorting and comparing 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.
1 parent 802457b commit 582eb2e

2 files changed

Lines changed: 26 additions & 13 deletions

File tree

bundler/lib/bundler/source/git.rb

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,7 @@ def eql?(other)
6969

7070
def to_s
7171
begin
72-
at = if local?
73-
path
74-
elsif user_ref = options["ref"]
75-
if /\A[a-z0-9]{4,}\z/i.match?(ref)
76-
shortref_for_display(user_ref)
77-
else
78-
user_ref
79-
end
80-
elsif ref
81-
ref
82-
else
83-
current_branch
84-
end
72+
at = humanized_ref || current_branch
8573

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

82+
def identifier
83+
uri_with_specifiers([humanized_ref, cached_revision, glob_for_display])
84+
end
85+
9486
def uri_with_specifiers(specifiers)
9587
specifiers.compact!
9688

@@ -256,6 +248,20 @@ def local?
256248

257249
private
258250

251+
def humanized_ref
252+
if local?
253+
path
254+
elsif user_ref = options["ref"]
255+
if /\A[a-z0-9]{4,}\z/i.match?(ref)
256+
shortref_for_display(user_ref)
257+
else
258+
user_ref
259+
end
260+
elsif ref
261+
ref
262+
end
263+
end
264+
259265
def serialize_gemspecs_in(destination)
260266
destination = destination.expand_path(Bundler.root) if destination.relative?
261267
Dir["#{destination}/#{@glob}"].each do |spec_path|

bundler/spec/install/gemfile/git_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
expect(Dir["#{default_bundle_path}/cache/bundler/git/foo-1.0-*"]).to have_attributes :size => 1
3131
end
3232

33+
it "does not write to cache on bundler/setup" do
34+
cache_path = default_bundle_path.join("cache")
35+
FileUtils.rm_rf(cache_path)
36+
ruby "require 'bundler/setup'"
37+
expect(cache_path).not_to exist
38+
end
39+
3340
it "caches the git repo globally and properly uses the cached repo on the next invocation" do
3441
simulate_new_machine
3542
bundle "config set global_gem_cache true"

0 commit comments

Comments
 (0)