Skip to content

Commit 5bec2a5

Browse files
Edouard-chinhsbt
authored andcommitted
Merge pull request #9406 from Shopify/ec-git-version-class
Check the git version only **once** per `bundle install` (cherry picked from commit e3685c9)
1 parent dd4cc31 commit 5bec2a5

4 files changed

Lines changed: 41 additions & 13 deletions

File tree

bundler/lib/bundler/source/git/git_proxy.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ class GitProxy
5757
attr_accessor :path, :uri, :branch, :tag, :ref, :explicit_ref
5858
attr_writer :revision
5959

60+
def self.version
61+
@version ||= full_version[/((\.?\d+)+).*/, 1]
62+
end
63+
64+
def self.full_version
65+
@full_version ||= begin
66+
raise GitNotInstalledError.new unless Bundler.git_present?
67+
68+
require "open3"
69+
out, err, status = Open3.capture3("git", "--version")
70+
71+
raise GitCommandError.new("--version", SharedHelpers.pwd, err) unless status.success?
72+
Bundler.ui.warn err unless err.empty?
73+
74+
out.sub(/git version\s*/, "").strip
75+
end
76+
end
77+
78+
def self.reset
79+
@version = nil
80+
@full_version = nil
81+
end
82+
6083
def initialize(path, uri, options = {}, revision = nil, git = nil)
6184
@path = path
6285
@uri = uri
@@ -92,11 +115,11 @@ def contains?(commit)
92115
end
93116

94117
def version
95-
@version ||= full_version.match(/((\.?\d+)+).*/)[1]
118+
self.class.version
96119
end
97120

98121
def full_version
99-
@full_version ||= git_local("--version").sub(/git version\s*/, "").strip
122+
self.class.full_version
100123
end
101124

102125
def checkout

bundler/spec/bundler/env_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ def with_clear_paths(env_var, env_value)
217217

218218
context "when the git version is OS specific" do
219219
it "includes OS specific information with the version number" do
220-
expect(git_proxy_stub).to receive(:git_local).with("--version").
221-
and_return("git version 1.2.3 (Apple Git-BS)")
220+
status = double("success?" => true)
221+
expect(Open3).to receive(:capture3).with("git", "--version").
222+
and_return(["git version 1.2.3 (Apple Git-BS)", "", status])
222223
expect(Bundler::Source::Git::GitProxy).to receive(:new).and_return(git_proxy_stub)
223224

224225
expect(described_class.report).to include("Git 1.2.3 (Apple Git-BS)")

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
describe "#version" do
102102
context "with a normal version number" do
103103
before do
104-
expect(git_proxy).to receive(:git_local).with("--version").
104+
expect(described_class).to receive(:full_version).
105105
and_return("git version 1.2.3")
106106
end
107107

@@ -116,7 +116,7 @@
116116

117117
context "with a OSX version number" do
118118
before do
119-
expect(git_proxy).to receive(:git_local).with("--version").
119+
expect(described_class).to receive(:full_version).
120120
and_return("git version 1.2.3 (Apple Git-BS)")
121121
end
122122

@@ -131,7 +131,7 @@
131131

132132
context "with a msysgit version number" do
133133
before do
134-
expect(git_proxy).to receive(:git_local).with("--version").
134+
expect(described_class).to receive(:full_version).
135135
and_return("git version 1.2.3.msysgit.0")
136136
end
137137

@@ -148,8 +148,9 @@
148148
describe "#full_version" do
149149
context "with a normal version number" do
150150
before do
151-
expect(git_proxy).to receive(:git_local).with("--version").
152-
and_return("git version 1.2.3")
151+
status = double("success?" => true)
152+
expect(Open3).to receive(:capture3).with("git", "--version").
153+
and_return(["git version 1.2.3", "", status])
153154
end
154155

155156
it "returns the git version number" do
@@ -159,8 +160,9 @@
159160

160161
context "with a OSX version number" do
161162
before do
162-
expect(git_proxy).to receive(:git_local).with("--version").
163-
and_return("git version 1.2.3 (Apple Git-BS)")
163+
status = double("success?" => true)
164+
expect(Open3).to receive(:capture3).with("git", "--version").
165+
and_return(["git version 1.2.3 (Apple Git-BS)", "", status])
164166
end
165167

166168
it "does not strip out OSX specific additions in the version string" do
@@ -170,8 +172,9 @@
170172

171173
context "with a msysgit version number" do
172174
before do
173-
expect(git_proxy).to receive(:git_local).with("--version").
174-
and_return("git version 1.2.3.msysgit.0")
175+
status = double("success?" => true)
176+
expect(Open3).to receive(:capture3).with("git", "--version").
177+
and_return(["git version 1.2.3.msysgit.0", "", status])
175178
end
176179

177180
it "does not strip out msysgit specific additions in the version string" do

bundler/spec/support/helpers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def reset!
2525
FileUtils.mkdir_p(home)
2626
FileUtils.mkdir_p(tmpdir)
2727
Bundler.reset!
28+
Bundler::Source::Git::GitProxy.reset
2829
Gem.clear_paths
2930
end
3031

0 commit comments

Comments
 (0)