Skip to content

Commit

Permalink
[rubygems/rubygems] Improve source gemfile/lockfile equivalence checks
Browse files Browse the repository at this point in the history
Since we no longer have multiple global sources, each top level dependency is
always pinned to a single source, so it makes little sense to talk about
adding or removing a source. Instead, source changes always mean to
change the source one or more dependencies are pinned to. This logic can
now be much simpler.

rubygems/rubygems@f1d33fa0df
  • Loading branch information
deivid-rodriguez authored and matzbot committed Dec 3, 2021
1 parent 248fae0 commit 4c5e862
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
30 changes: 10 additions & 20 deletions lib/bundler/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,35 +371,25 @@ def ensure_equivalent_gemfile_and_lockfile(explicit_flag = false)
added.concat new_platforms.map {|p| "* platform: #{p}" }
deleted.concat deleted_platforms.map {|p| "* platform: #{p}" }

gemfile_sources = sources.lock_sources

new_sources = gemfile_sources - @locked_sources
deleted_sources = @locked_sources - gemfile_sources

new_deps = @dependencies - locked_dependencies
deleted_deps = locked_dependencies - @dependencies

if @locked_sources != gemfile_sources
if new_sources.any?
added.concat new_sources.map {|source| "* source: #{source}" }
end

if deleted_sources.any?
deleted.concat deleted_sources.map {|source| "* source: #{source}" }
end
end

added.concat new_deps.map {|d| "* #{pretty_dep(d)}" } if new_deps.any?
deleted.concat deleted_deps.map {|d| "* #{pretty_dep(d)}" } if deleted_deps.any?

both_sources = Hash.new {|h, k| h[k] = [] }
@dependencies.each {|d| both_sources[d.name][0] = d }
locked_dependencies.each {|d| both_sources[d.name][1] = d.source }
locked_dependencies.each {|d| both_sources[d.name][1] = d }

both_sources.each do |name, (dep, lock_dep)|
next if dep.nil? || lock_dep.nil?

gemfile_source = dep.source || sources.default_source
lock_source = lock_dep.source || sources.default_source
next if lock_source.include?(gemfile_source)

both_sources.each do |name, (dep, lock_source)|
next if lock_source.nil? || lock_source.can_lock?(dep)
gemfile_source_name = dep.source || "no specified source"
lockfile_source_name = lock_source
gemfile_source_name = dep.source ? gemfile_source.identifier : "no specified source"
lockfile_source_name = lock_dep.source ? lock_source.identifier : "no specified source"
changed << "* #{name} from `#{lockfile_source_name}` to `#{gemfile_source_name}`"
end

Expand Down
10 changes: 5 additions & 5 deletions spec/bundler/install/deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,11 @@
bundle "config set --local deployment true"
bundle :install, :raise_on_error => false
expect(err).to include("deployment mode")
expect(err).to include("You have added to the Gemfile:\n* source: git://hubz.com")
expect(err).not_to include("You have changed in the Gemfile")
expect(err).not_to include("You have added to the Gemfile")
expect(err).to include("You have changed in the Gemfile:\n* rack from `no specified source` to `git://hubz.com`")
end

it "explodes if you unpin a source" do
it "explodes if you change a source" do
build_git "rack"

install_gemfile <<-G
Expand All @@ -377,12 +377,12 @@
bundle "config set --local deployment true"
bundle :install, :raise_on_error => false
expect(err).to include("deployment mode")
expect(err).to include("You have deleted from the Gemfile:\n* source: #{lib_path("rack-1.0")}")
expect(err).not_to include("You have deleted from the Gemfile")
expect(err).not_to include("You have added to the Gemfile")
expect(err).to include("You have changed in the Gemfile:\n* rack from `#{lib_path("rack-1.0")}` to `no specified source`")
end

it "explodes if you unpin a source, leaving it pinned somewhere else" do
it "explodes if you change a source" do
build_lib "foo", :path => lib_path("rack/foo")
build_git "rack", :path => lib_path("rack")

Expand Down

0 comments on commit 4c5e862

Please sign in to comment.