formula: refactor outdated_kegs logic.

- Use the `Formula.cache` and `Cacheable` rather than rolling our own.
- Use keyword arguments instead of `options = {}`
- Improve readability in general
This commit is contained in:
Mike McQuaid 2019-11-06 11:49:31 +00:00
parent ed27f237c2
commit ee1cbfc8d3
No known key found for this signature in database
GPG Key ID: 48A898132FD8EE70
2 changed files with 48 additions and 58 deletions

View File

@ -495,22 +495,18 @@ class Formula
prefix(head_version) if head_version
end
def head_version_outdated?(version, options = {})
def head_version_outdated?(version, fetch_head: false)
tab = Tab.for_keg(prefix(version))
return true if tab.version_scheme < version_scheme
return true if stable && tab.stable_version && tab.stable_version < stable.version
return true if devel && tab.devel_version && tab.devel_version < devel.version
return false unless fetch_head
return false unless head&.downloader.is_a?(VCSDownloadStrategy)
if options[:fetch_head]
return false unless head&.downloader.is_a?(VCSDownloadStrategy)
downloader = head.downloader
downloader.shutup! unless ARGV.verbose?
downloader.commit_outdated?(version.version.commit)
else
false
end
downloader = head.downloader
downloader.shutup! unless ARGV.verbose?
downloader.commit_outdated?(version.version.commit)
end
# The latest prefix for this formula. Checks for {#head}, then {#devel}
@ -1173,43 +1169,41 @@ class Formula
end
# @private
def outdated_kegs(options = {})
@outdated_kegs ||= Hash.new do |cache, key|
raise Migrator::MigrationNeededError, self if migration_needed?
def outdated_kegs(fetch_head: false)
raise Migrator::MigrationNeededError, self if migration_needed?
cache[key] = _outdated_kegs(key)
end
@outdated_kegs[options]
end
cache_key = "#{name}-#{fetch_head}"
Formula.cache[:outdated_kegs] ||= {}
Formula.cache[:outdated_kegs][cache_key] ||= begin
all_kegs = []
current_version = false
def _outdated_kegs(options = {})
all_kegs = []
installed_kegs.each do |keg|
all_kegs << keg
version = keg.version
next if version.head?
installed_kegs.each do |keg|
all_kegs << keg
version = keg.version
next if version.head?
tab = Tab.for_keg(keg)
next if version_scheme > tab.version_scheme
next if version_scheme == tab.version_scheme && pkg_version > version
tab = Tab.for_keg(keg)
next if version_scheme > tab.version_scheme
next if version_scheme == tab.version_scheme && pkg_version > version
# don't consider this keg current if there's a newer formula available
next if follow_installed_alias? && new_formula_available?
# don't consider this keg current if there's a newer formula available
next if follow_installed_alias? && new_formula_available?
# this keg is the current version of the formula, so it's not outdated
current_version = true
break
end
return [] # this keg is the current version of the formula, so it's not outdated
end
# Even if this formula hasn't been installed, there may be installations
# of other formulae which used to be targets of the alias currently
# targetting this formula. These should be counted as outdated versions.
all_kegs.concat old_installed_formulae.flat_map(&:installed_kegs)
head_version = latest_head_version
if head_version && !head_version_outdated?(head_version, options)
[]
else
all_kegs.sort_by(&:version)
if current_version
[]
elsif (head_version = latest_head_version) &&
!head_version_outdated?(head_version, fetch_head: fetch_head)
[]
else
all_kegs += old_installed_formulae.flat_map(&:installed_kegs)
all_kegs.sort_by(&:version)
end
end
end
@ -1257,8 +1251,8 @@ class Formula
end
# @private
def outdated?(options = {})
!outdated_kegs(options).empty?
def outdated?(fetch_head: false)
!outdated_kegs(fetch_head: fetch_head).empty?
rescue Migrator::MigrationNeededError
true
end

View File

@ -1091,10 +1091,6 @@ describe Formula do
tab
end
def reset_outdated_kegs
f.instance_variable_set(:@outdated_kegs, nil)
end
example "greater different tap installed" do
setup_tab_for_prefix(greater_prefix, tap: "user/repo")
expect(f.outdated_kegs).to be_empty
@ -1208,7 +1204,7 @@ describe Formula do
expect(f.outdated_kegs).to be_empty
setup_tab_for_prefix(greater_prefix, tap: "homebrew/core")
reset_outdated_kegs
described_class.clear_cache
expect(f.outdated_kegs).to be_empty
end
@ -1220,12 +1216,12 @@ describe Formula do
setup_tab_for_prefix(outdated_prefix)
setup_tab_for_prefix(extra_outdated_prefix, tap: "homebrew/core")
reset_outdated_kegs
described_class.clear_cache
expect(f.outdated_kegs).not_to be_empty
setup_tab_for_prefix(outdated_prefix, tap: "user/repo")
reset_outdated_kegs
described_class.clear_cache
expect(f.outdated_kegs).not_to be_empty
end
@ -1237,7 +1233,7 @@ describe Formula do
expect(f.outdated_kegs).to be_empty
setup_tab_for_prefix(same_prefix, tap: "user/repo")
reset_outdated_kegs
described_class.clear_cache
expect(f.outdated_kegs).to be_empty
end
@ -1249,7 +1245,7 @@ describe Formula do
tab.source["versions"] = { "stable" => f.version.to_s }
tab.write
reset_outdated_kegs
described_class.clear_cache
expect(f.outdated_kegs).to be_empty
end
@ -1288,15 +1284,15 @@ describe Formula do
tab_a.source["versions"] = { "stable" => f.version.to_s }
tab_a.write
reset_outdated_kegs
described_class.clear_cache
expect(f.outdated_kegs(fetch_head: true)).not_to be_empty
head_prefix_a.rmtree
reset_outdated_kegs
described_class.clear_cache
expect(f.outdated_kegs(fetch_head: true)).not_to be_empty
setup_tab_for_prefix(head_prefix_c, source_modified_time: 1)
reset_outdated_kegs
described_class.clear_cache
expect(f.outdated_kegs(fetch_head: true)).to be_empty
ensure
testball_repo.rmtree if testball_repo.exist?
@ -1348,13 +1344,13 @@ describe Formula do
setup_tab_for_prefix(prefix_b, versions: { "stable" => "2.14", "version_scheme" => 2 })
expect(f.outdated_kegs).not_to be_empty
reset_outdated_kegs
described_class.clear_cache
prefix_c = HOMEBREW_CELLAR/"testball/20141009"
setup_tab_for_prefix(prefix_c, versions: { "stable" => "20141009", "version_scheme" => 3 })
expect(f.outdated_kegs).not_to be_empty
reset_outdated_kegs
described_class.clear_cache
prefix_d = HOMEBREW_CELLAR/"testball/20141011"
setup_tab_for_prefix(prefix_d, versions: { "stable" => "20141009", "version_scheme" => 3 })
@ -1377,7 +1373,7 @@ describe Formula do
setup_tab_for_prefix(head_prefix, versions: { "stable" => "1.0", "version_scheme" => 1 })
expect(f.outdated_kegs).not_to be_empty
reset_outdated_kegs
described_class.clear_cache
head_prefix.rmtree
setup_tab_for_prefix(head_prefix, versions: { "stable" => "1.0", "version_scheme" => 2 })