Skip to content

Commit ee55b82

Browse files
thomasmarshallhsbt
authored andcommitted
[rubygems/rubygems] Cache commit SHA ref revisions
If the `ref` option is a specific commit SHA, we can check to see if it's already fetched locally. If it is, then we don't need to re-fetch it from the remote. The `ref` option might not be a commit SHA, so we're using the `#commit` method which returns the full SHA if it's a commit ref, or the locked revision, or nil. This is a small improvement that will make `bundle update` slightly faster in cases for git-sourced gems pinned to a specific commit. ruby/rubygems@f434c2e66c
1 parent 6839ead commit ee55b82

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

lib/bundler/source/git/git_proxy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ def git_local(*command, dir: nil)
305305
end
306306

307307
def has_revision_cached?
308-
return unless @revision && path.exist?
309-
git("cat-file", "-e", @revision, dir: path)
308+
return unless commit && path.exist?
309+
git("cat-file", "-e", commit, dir: path)
310310
true
311311
rescue GitError
312312
false

spec/bundler/bundler/source/git/git_proxy_spec.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,21 @@
305305
context "with a commit ref" do
306306
let(:ref) { Digest::SHA1.hexdigest("ruby") }
307307

308-
it "fetches the specific revision" do
309-
allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
310-
expect(git_proxy).to receive(:capture).with(["fetch", "--force", "--quiet", "--no-tags", "--depth", "1", "--", uri, "#{ref}:refs/#{ref}-sha"], path).and_return(["", "", clone_result])
311-
subject.checkout
308+
context "when the revision exists locally" do
309+
it "uses the cached revision" do
310+
allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
311+
expect(git_proxy).to receive(:git).with("cat-file", "-e", ref, dir: path).and_return(true)
312+
subject.checkout
313+
end
314+
end
315+
316+
context "when the revision doesn't exist locally" do
317+
it "fetches the specific revision" do
318+
allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
319+
expect(git_proxy).to receive(:git).with("cat-file", "-e", ref, dir: path).and_raise(Bundler::GitError)
320+
expect(git_proxy).to receive(:capture).with(["fetch", "--force", "--quiet", "--no-tags", "--depth", "1", "--", uri, "#{ref}:refs/#{ref}-sha"], path).and_return(["", "", clone_result])
321+
subject.checkout
322+
end
312323
end
313324
end
314325

0 commit comments

Comments
 (0)