Skip to content

Commit 9c0411f

Browse files
committed
Bundler::Source::Git: avoid re-fetching the repo
In `install`, `self.to_s` is called by `print_using_message "Using ... from #{self}`. This in turn calls `current_branch` which if the local git cache is missing will re-clone it from scratch. This is an issue, because a common optimization for build system that are bundler aware is to prune bundler's cache to remove large git repositories. See my feature request from 2020 for context: #7018 In our case, these git repositories account for over 50% of the bundler cache, causing the cache restoration and persist steps to be very significantly slowed down. Overall I don't think triggering a full on git clone operation from a call to `to_s` make sense.
1 parent 77ff2aa commit 9c0411f

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

lib/bundler/source/git.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ def include?(other)
9191

9292
def to_s
9393
begin
94-
at = humanized_ref || current_branch
95-
96-
rev = "at #{at}@#{shortref_for_display(revision)}"
94+
at = humanized_ref || @branch
95+
at = "#{at}@" if at
96+
rev = "at #{at}#{shortref_for_display(revision)}"
9797
rescue GitError
9898
""
9999
end

spec/install/git_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
gem "foo", :git => "#{lib_path("foo")}"
1111
G
1212

13-
expect(out).to include("Using foo 1.0 from #{lib_path("foo")} (at main@#{revision_for(lib_path("foo"))[0..6]})")
13+
expect(out).to include("Using foo 1.0 from #{lib_path("foo")} (at #{revision_for(lib_path("foo"))[0..6]})")
1414
expect(the_bundle).to include_gems "foo 1.0", source: "git@#{lib_path("foo")}"
1515
end
1616

@@ -23,7 +23,7 @@
2323
gem "foo", :git => "#{relative_path}"
2424
G
2525

26-
expect(out).to include("Using foo 1.0 from #{relative_path} (at main@#{revision_for(lib_path("foo"))[0..6]})")
26+
expect(out).to include("Using foo 1.0 from #{relative_path} (at #{revision_for(lib_path("foo"))[0..6]})")
2727
expect(the_bundle).to include_gems "foo 1.0", source: "git@#{lib_path("foo")}"
2828
end
2929

@@ -35,7 +35,7 @@
3535
gem "foo", :git => "#{lib_path("foo")}"
3636
G
3737

38-
expect(out).to include("Using foo 1.0 from #{lib_path("foo")} (at non-standard@#{revision_for(lib_path("foo"))[0..6]})")
38+
expect(out).to include("Using foo 1.0 from #{lib_path("foo")} (at #{revision_for(lib_path("foo"))[0..6]})")
3939
expect(the_bundle).to include_gems "foo 1.0", source: "git@#{lib_path("foo")}"
4040
end
4141

@@ -193,7 +193,7 @@
193193
gem "foo", :git => "#{lib_path("foo")}"
194194
G
195195

196-
expect(out).to include("Using foo 1.0 from #{lib_path("foo")} (at main@#{rev[0..6]})")
196+
expect(out).to include("Using foo 1.0 from #{lib_path("foo")} (at #{rev[0..6]})")
197197
expect(the_bundle).to include_gems "foo 1.0", source: "git@#{lib_path("foo")}"
198198

199199
old_lockfile = lockfile
@@ -202,20 +202,20 @@
202202
rev2 = revision_for(lib_path("foo"))
203203

204204
bundle :update, all: true, verbose: true
205-
expect(out).to include("Using foo 2.0 (was 1.0) from #{lib_path("foo")} (at main@#{rev2[0..6]})")
205+
expect(out).to include("Using foo 2.0 (was 1.0) from #{lib_path("foo")} (at #{rev2[0..6]})")
206206
expect(out).to include("Removing foo (#{rev[0..11]})")
207207
expect(the_bundle).to include_gems "foo 2.0", source: "git@#{lib_path("foo")}"
208208

209209
lockfile(old_lockfile)
210210

211211
bundle :install, verbose: true
212-
expect(out).to include("Using foo 1.0 from #{lib_path("foo")} (at main@#{rev[0..6]})")
212+
expect(out).to include("Using foo 1.0 from #{lib_path("foo")} (at #{rev[0..6]})")
213213
expect(the_bundle).to include_gems "foo 1.0", source: "git@#{lib_path("foo")}"
214214
end
215215

216216
context "when install directory exists" do
217217
let(:checkout_confirmation_log_message) { "Checking out revision" }
218-
let(:using_foo_confirmation_log_message) { "Using foo 1.0 from #{lib_path("foo")} (at main@#{revision_for(lib_path("foo"))[0..6]})" }
218+
let(:using_foo_confirmation_log_message) { "Using foo 1.0 from #{lib_path("foo")} (at #{revision_for(lib_path("foo"))[0..6]})" }
219219

220220
context "and no contents besides .git directory are present" do
221221
it "reinstalls gem" do

0 commit comments

Comments
 (0)