| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require_relative "../vendored_fileutils" |
| 4 |
|
| 5 |
module Bundler |
| 6 |
class Source |
| 7 |
class Git < Path |
| 8 |
autoload :GitProxy, File.expand_path("git/git_proxy", __dir__) |
| 9 |
|
| 10 |
attr_reader :uri, :ref, :branch, :options, :glob, :submodules |
| 11 |
|
| 12 |
def initialize(options) |
| 13 |
@options = options |
| 14 |
@checksum_store = Checksum::Store.new |
| 15 |
@glob = options["glob"] || DEFAULT_GLOB |
| 16 |
|
| 17 |
@allow_cached = false |
| 18 |
@allow_remote = false |
| 19 |
|
| 20 |
# Stringify options that could be set as symbols |
| 21 |
%w[ref branch tag revision].each {|k| options[k] = options[k].to_s if options[k] } |
| 22 |
|
| 23 |
@uri = URINormalizer.normalize_suffix(options["uri"] || "", trailing_slash: false) |
| 24 |
@safe_uri = URICredentialsFilter.credential_filtered_uri(@uri) |
| 25 |
@branch = options["branch"] |
| 26 |
@ref = options["ref"] || options["branch"] || options["tag"] |
| 27 |
@submodules = options["submodules"] |
| 28 |
@name = options["name"] |
| 29 |
@version = options["version"].to_s.strip.gsub("-", ".pre.") |
| 30 |
|
| 31 |
@copied = false |
| 32 |
@local = false |
| 33 |
end |
| 34 |
|
| 35 |
def remote! |
| 36 |
return if @allow_remote |
| 37 |
|
| 38 |
@local_specs = nil |
| 39 |
@allow_remote = true |
| 40 |
end |
| 41 |
|
| 42 |
def cached! |
| 43 |
return if @allow_cached |
| 44 |
|
| 45 |
@local_specs = nil |
| 46 |
@allow_cached = true |
| 47 |
end |
| 48 |
|
| 49 |
def self.from_lock(options) |
| 50 |
new(options.merge("uri" => options.delete("remote"))) |
| 51 |
end |
| 52 |
|
| 53 |
def to_lock |
| 54 |
out = String.new("GIT\n") |
| 55 |
out << " remote: #{@uri}\n" |
| 56 |
out << " revision: #{revision}\n" |
| 57 |
%w[ref branch tag submodules].each do |opt| |
| 58 |
out << " #{opt}: #{options[opt]}\n" if options[opt] |
| 59 |
end |
| 60 |
out << " glob: #{@glob}\n" unless default_glob? |
| 61 |
out << " specs:\n" |
| 62 |
end |
| 63 |
|
| 64 |
def to_gemfile |
| 65 |
specifiers = %w[ref branch tag submodules glob].map do |opt| |
| 66 |
"#{opt}: #{options[opt]}" if options[opt] |
| 67 |
end |
| 68 |
|
| 69 |
uri_with_specifiers(specifiers) |
| 70 |
end |
| 71 |
|
| 72 |
def hash |
| 73 |
[self.class, uri, ref, branch, name, glob, submodules].hash |
| 74 |
end |
| 75 |
|
| 76 |
def eql?(other) |
| 77 |
other.is_a?(Git) && uri == other.uri && ref == other.ref && |
| 78 |
branch == other.branch && name == other.name && |
| 79 |
glob == other.glob && |
| 80 |
submodules == other.submodules |
| 81 |
end |
| 82 |
|
| 83 |
alias_method :==, :eql? |
| 84 |
|
| 85 |
def include?(other) |
| 86 |
other.is_a?(Git) && uri == other.uri && |
| 87 |
name == other.name && |
| 88 |
glob == other.glob && |
| 89 |
submodules == other.submodules |
| 90 |
end |
| 91 |
|
| 92 |
def to_s |
| 93 |
begin |
| 94 |
at = humanized_ref || current_branch |
| 95 |
|
| 96 |
rev = "at #{at}@#{shortref_for_display(revision)}" |
| 97 |
rescue GitError |
| 98 |
"" |
| 99 |
end |
| 100 |
|
| 101 |
uri_with_specifiers([rev, glob_for_display]) |
| 102 |
end |
| 103 |
|
| 104 |
def identifier |
| 105 |
uri_with_specifiers([humanized_ref, cached_revision, glob_for_display]) |
| 106 |
end |
| 107 |
|
| 108 |
def uri_with_specifiers(specifiers) |
| 109 |
specifiers.compact! |
| 110 |
|
| 111 |
suffix = |
| 112 |
if specifiers.any? |
| 113 |
" (#{specifiers.join(", ")})" |
| 114 |
else |
| 115 |
"" |
| 116 |
end |
| 117 |
|
| 118 |
"#{@safe_uri}#{suffix}" |
| 119 |
end |
| 120 |
|
| 121 |
def name |
| 122 |
File.basename(@uri, ".git") |
| 123 |
end |
| 124 |
|
| 125 |
# This is the path which is going to contain a specific |
| 126 |
# checkout of the git repository. When using local git |
| 127 |
# repos, this is set to the local repo. |
| 128 |
def install_path |
| 129 |
@install_path ||= begin |
| 130 |
git_scope = "#{base_name}-#{shortref_for_path(revision)}" |
| 131 |
|
| 132 |
Bundler.install_path.join(git_scope) |
| 133 |
end |
| 134 |
end |
| 135 |
|
| 136 |
alias_method :path, :install_path |
| 137 |
|
| 138 |
def extension_dir_name |
| 139 |
"#{base_name}-#{shortref_for_path(revision)}" |
| 140 |
end |
| 141 |
|
| 142 |
def unlock! |
| 143 |
git_proxy.revision = nil |
| 144 |
options["revision"] = nil |
| 145 |
|
| 146 |
@unlocked = true |
| 147 |
end |
| 148 |
|
| 149 |
def local_override!(path) |
| 150 |
return false if local? |
| 151 |
|
| 152 |
original_path = path |
| 153 |
path = Pathname.new(path) |
| 154 |
path = path.expand_path(Bundler.root) unless path.relative? |
| 155 |
|
| 156 |
unless branch || Bundler.settings[:disable_local_branch_check] |
| 157 |
raise GitError, "Cannot use local override for #{name} at #{path} because " \ |
| 158 |
":branch is not specified in Gemfile. Specify a branch or run " \ |
| 159 |
"`bundle config unset local.#{override_for(original_path)}` to remove the local override" |
| 160 |
end |
| 161 |
|
| 162 |
unless path.exist? |
| 163 |
raise GitError, "Cannot use local override for #{name} because #{path} " \ |
| 164 |
"does not exist. Run `bundle config unset local.#{override_for(original_path)}` to remove the local override" |
| 165 |
end |
| 166 |
|
| 167 |
@local = true |
| 168 |
set_paths!(path) |
| 169 |
|
| 170 |
# Create a new git proxy without the cached revision |
| 171 |
# so the Gemfile.lock always picks up the new revision. |
| 172 |
@git_proxy = GitProxy.new(path, uri, options) |
| 173 |
|
| 174 |
if current_branch != branch && !Bundler.settings[:disable_local_branch_check] |
| 175 |
raise GitError, "Local override for #{name} at #{path} is using branch " \ |
| 176 |
"#{current_branch} but Gemfile specifies #{branch}" |
| 177 |
end |
| 178 |
|
| 179 |
changed = cached_revision && cached_revision != revision |
| 180 |
|
| 181 |
if !Bundler.settings[:disable_local_revision_check] && changed && !@unlocked && !git_proxy.contains?(cached_revision) |
| 182 |
raise GitError, "The Gemfile lock is pointing to revision #{shortref_for_display(cached_revision)} " \ |
| 183 |
"but the current branch in your local override for #{name} does not contain such commit. " \ |
| 184 |
"Please make sure your branch is up to date." |
| 185 |
end |
| 186 |
|
| 187 |
changed |
| 188 |
end |
| 189 |
|
| 190 |
def specs(*) |
| 191 |
set_up_app_cache!(app_cache_path) if use_app_cache? |
| 192 |
|
| 193 |
if requires_checkout? && !@copied |
| 194 |
FileUtils.rm_rf(app_cache_path) if use_app_cache? && git_proxy.not_a_repository? |
| 195 |
|
| 196 |
fetch |
| 197 |
checkout |
| 198 |
end |
| 199 |
|
| 200 |
local_specs |
| 201 |
end |
| 202 |
|
| 203 |
def install(spec, options = {}) |
| 204 |
return if Bundler.settings[:no_install] |
| 205 |
force = options[:force] |
| 206 |
|
| 207 |
print_using_message "Using #{version_message(spec, options[:previous_spec])} from #{self}" |
| 208 |
|
| 209 |
if (requires_checkout? && !@copied) || force |
| 210 |
checkout |
| 211 |
end |
| 212 |
|
| 213 |
generate_bin_options = { disable_extensions: !spec.missing_extensions?, build_args: options[:build_args] } |
| 214 |
generate_bin(spec, generate_bin_options) |
| 215 |
|
| 216 |
requires_checkout? ? spec.post_install_message : nil |
| 217 |
end |
| 218 |
|
| 219 |
def cache(spec, custom_path = nil) |
| 220 |
app_cache_path = app_cache_path(custom_path) |
| 221 |
return unless Bundler.feature_flag.cache_all? |
| 222 |
return if install_path == app_cache_path |
| 223 |
return if cache_path == app_cache_path |
| 224 |
cached! |
| 225 |
FileUtils.rm_rf(app_cache_path) |
| 226 |
git_proxy.checkout if requires_checkout? |
| 227 |
FileUtils.cp_r("#{cache_path}/.", app_cache_path) |
| 228 |
FileUtils.touch(app_cache_path.join(".bundlecache")) |
| 229 |
FileUtils.rm_rf(Dir.glob(app_cache_path.join("hooks/*.sample"))) |
| 230 |
end |
| 231 |
|
| 232 |
def load_spec_files |
| 233 |
super |
| 234 |
rescue PathError => e |
| 235 |
Bundler.ui.trace e |
| 236 |
raise GitError, "#{self} is not yet checked out. Run `bundle install` first." |
| 237 |
end |
| 238 |
|
| 239 |
# This is the path which is going to contain a cache |
| 240 |
# of the git repository. When using the same git repository |
| 241 |
# across different projects, this cache will be shared. |
| 242 |
# When using local git repos, this is set to the local repo. |
| 243 |
def cache_path |
| 244 |
@cache_path ||= if Bundler.feature_flag.global_gem_cache? |
| 245 |
Bundler.user_cache |
| 246 |
else |
| 247 |
Bundler.bundle_path.join("cache", "bundler") |
| 248 |
end.join("git", git_scope) |
| 249 |
end |
| 250 |
|
| 251 |
def app_cache_dirname |
| 252 |
"#{base_name}-#{shortref_for_path(cached_revision || revision)}" |
| 253 |
end |
| 254 |
|
| 255 |
def revision |
| 256 |
git_proxy.revision |
| 257 |
end |
| 258 |
|
| 259 |
def current_branch |
| 260 |
git_proxy.current_branch |
| 261 |
end |
| 262 |
|
| 263 |
def allow_git_ops? |
| 264 |
@allow_remote || @allow_cached |
| 265 |
end |
| 266 |
|
| 267 |
def local? |
| 268 |
@local |
| 269 |
end |
| 270 |
|
| 271 |
private |
| 272 |
|
| 273 |
def checkout |
| 274 |
Bundler.ui.debug " * Checking out revision: #{ref}" |
| 275 |
git_proxy.copy_to(install_path, submodules) |
| 276 |
serialize_gemspecs_in(install_path) |
| 277 |
@copied = true |
| 278 |
end |
| 279 |
|
| 280 |
def humanized_ref |
| 281 |
if local? |
| 282 |
path |
| 283 |
elsif user_ref = options["ref"] |
| 284 |
if /\A[a-z0-9]{4,}\z/i.match?(ref) |
| 285 |
shortref_for_display(user_ref) |
| 286 |
else |
| 287 |
user_ref |
| 288 |
end |
| 289 |
elsif ref |
| 290 |
ref |
| 291 |
end |
| 292 |
end |
| 293 |
|
| 294 |
def serialize_gemspecs_in(destination) |
| 295 |
destination = destination.expand_path(Bundler.root) if destination.relative? |
| 296 |
Dir["#{destination}/#{@glob}"].each do |spec_path| |
| 297 |
# Evaluate gemspecs and cache the result. Gemspecs |
| 298 |
# in git might require git or other dependencies. |
| 299 |
# The gemspecs we cache should already be evaluated. |
| 300 |
spec = Bundler.load_gemspec(spec_path) |
| 301 |
next unless spec |
| 302 |
spec.installed_by_version = Gem::VERSION |
| 303 |
Bundler.rubygems.validate(spec) |
| 304 |
File.open(spec_path, "wb") {|file| file.write(spec.to_ruby) } |
| 305 |
end |
| 306 |
end |
| 307 |
|
| 308 |
def set_paths!(path) |
| 309 |
set_cache_path!(path) |
| 310 |
set_install_path!(path) |
| 311 |
end |
| 312 |
|
| 313 |
def set_cache_path!(path) |
| 314 |
@git_proxy = nil |
| 315 |
@cache_path = path |
| 316 |
end |
| 317 |
|
| 318 |
def set_install_path!(path) |
| 319 |
@local_specs = nil |
| 320 |
@install_path = path |
| 321 |
end |
| 322 |
|
| 323 |
def set_up_app_cache!(path) |
| 324 |
FileUtils.mkdir_p(path.join("refs")) |
| 325 |
set_cache_path!(path) |
| 326 |
end |
| 327 |
|
| 328 |
def has_app_cache? |
| 329 |
cached_revision && super |
| 330 |
end |
| 331 |
|
| 332 |
def use_app_cache? |
| 333 |
has_app_cache? && !local? |
| 334 |
end |
| 335 |
|
| 336 |
def requires_checkout? |
| 337 |
allow_git_ops? && !local? && !cached_revision_checked_out? |
| 338 |
end |
| 339 |
|
| 340 |
def cached_revision_checked_out? |
| 341 |
cached_revision && cached_revision == revision && install_path.exist? |
| 342 |
end |
| 343 |
|
| 344 |
def base_name |
| 345 |
File.basename(uri.sub(%r{^(\w+://)?([^/:]+:)?(//\w*/)?(\w*/)*}, ""), ".git") |
| 346 |
end |
| 347 |
|
| 348 |
def shortref_for_display(ref) |
| 349 |
ref[0..6] |
| 350 |
end |
| 351 |
|
| 352 |
def shortref_for_path(ref) |
| 353 |
ref[0..11] |
| 354 |
end |
| 355 |
|
| 356 |
def glob_for_display |
| 357 |
default_glob? ? nil : "glob: #{@glob}" |
| 358 |
end |
| 359 |
|
| 360 |
def default_glob? |
| 361 |
@glob == DEFAULT_GLOB |
| 362 |
end |
| 363 |
|
| 364 |
def uri_hash |
| 365 |
if %r{^\w+://(\w+@)?}.match?(uri) |
| 366 |
# Downcase the domain component of the URI |
| 367 |
# and strip off a trailing slash, if one is present |
| 368 |
input = Gem::URI.parse(uri).normalize.to_s.sub(%r{/$}, "") |
| 369 |
else |
| 370 |
# If there is no URI scheme, assume it is an ssh/git URI |
| 371 |
input = uri |
| 372 |
end |
| 373 |
# We use SHA1 here for historical reason and to preserve backward compatibility. |
| 374 |
# But a transition to a simpler mangling algorithm would be welcome. |
| 375 |
Bundler::Digest.sha1(input) |
| 376 |
end |
| 377 |
|
| 378 |
def cached_revision |
| 379 |
options["revision"] |
| 380 |
end |
| 381 |
|
| 382 |
def cached? |
| 383 |
cache_path.exist? |
| 384 |
end |
| 385 |
|
| 386 |
def git_proxy |
| 387 |
@git_proxy ||= GitProxy.new(cache_path, uri, options, cached_revision, self) |
| 388 |
end |
| 389 |
|
| 390 |
def fetch |
| 391 |
git_proxy.checkout |
| 392 |
rescue GitError => e |
| 393 |
raise unless Bundler.feature_flag.allow_offline_install? |
| 394 |
Bundler.ui.warn "Using cached git data because of network errors:\n#{e}" |
| 395 |
end |
| 396 |
|
| 397 |
# no-op, since we validate when re-serializing the gemspec |
| 398 |
def validate_spec(_spec); end |
| 399 |
|
| 400 |
def load_gemspec(file) |
| 401 |
dirname = Pathname.new(file).dirname |
| 402 |
SharedHelpers.chdir(dirname.to_s) do |
| 403 |
stub = Gem::StubSpecification.gemspec_stub(file, install_path.parent, install_path.parent) |
| 404 |
stub.full_gem_path = dirname.expand_path(root).to_s |
| 405 |
StubSpecification.from_stub(stub) |
| 406 |
end |
| 407 |
end |
| 408 |
|
| 409 |
def git_scope |
| 410 |
"#{base_name}-#{uri_hash}" |
| 411 |
end |
| 412 |
|
| 413 |
def extension_cache_slug(_) |
| 414 |
extension_dir_name |
| 415 |
end |
| 416 |
|
| 417 |
def override_for(path) |
| 418 |
Bundler.settings.local_overrides.key(path) |
| 419 |
end |
| 420 |
end |
| 421 |
end |
| 422 |
end |