Skip to content

Commit

Permalink
Merge pull request #16973 from Homebrew/keg-sort
Browse files Browse the repository at this point in the history
Sort kegs based on version scheme
  • Loading branch information
Bo98 authored Mar 29, 2024
2 parents c197b80 + dd38248 commit 02a0ea8
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Library/Homebrew/cli/named_args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def resolve_latest_keg(name)
end
end

stable_kegs.max_by(&:version)
Keg.sort(stable_kegs).first
end

def resolve_default_keg(name)
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def info_formula(formula, args:)
heads, versioned = kegs.partition { |k| k.version.head? }
kegs = [
*heads.sort_by { |k| -Tab.for_keg(k).time.to_i },
*versioned.sort_by(&:version),
*Keg.sort(versioned),
]
if kegs.empty?
puts "Not installed"
Expand Down
7 changes: 3 additions & 4 deletions Library/Homebrew/cmd/upgrade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,11 @@ def upgrade_outdated_formulae(formulae, args:)
end

not_outdated.each do |f|
versions = f.installed_kegs.map(&:version)
if versions.empty?
latest_keg = Keg.sort(f.installed_kegs).first
if latest_keg.nil?
ofail "#{f.full_specified_name} not installed"
else
version = versions.max
opoo "#{f.full_specified_name} #{version} already installed"
opoo "#{f.full_specified_name} #{latest_keg.version} already installed"
end
end
end
Expand Down
13 changes: 6 additions & 7 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1492,9 +1492,8 @@ def outdated_kegs(fetch_head: false)
version = keg.version
next if version.head?

tab = Tab.for_keg(keg)
next if version_scheme > tab.version_scheme && pkg_version != version
next if version_scheme == tab.version_scheme && pkg_version > version
next if version_scheme > keg.version_scheme && pkg_version != version
next if version_scheme == keg.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?
Expand All @@ -1509,7 +1508,7 @@ def outdated_kegs(fetch_head: false)
[]
else
all_kegs += old_installed_formulae.flat_map(&:installed_kegs)
all_kegs.sort_by(&:version)
Keg.sort(all_kegs)
end
end
end
Expand Down Expand Up @@ -2244,7 +2243,7 @@ def to_hash
"conflicts_with_reasons" => conflicts.map(&:reason),
"link_overwrite" => self.class.link_overwrite_paths.to_a,
"caveats" => caveats_with_placeholders,
"installed" => [],
"installed" => T.let([], T::Array[T::Hash[String, T.untyped]]),
"linked_keg" => linked_version&.to_s,
"pinned" => pinned?,
"outdated" => outdated?,
Expand All @@ -2269,7 +2268,7 @@ def to_hash

hsh.merge!(dependencies_hash)

hsh["installed"] = installed_kegs.sort_by(&:version).map do |keg|
hsh["installed"] = Keg.sort(installed_kegs).map do |keg|
tab = Tab.for_keg keg
{
"version" => keg.version.to_s,
Expand Down Expand Up @@ -2842,7 +2841,7 @@ def eligible_kegs_for_cleanup(quiet: false)
eligible_kegs = if head? && (head_prefix = latest_head_prefix)
head, stable = installed_kegs.partition { |k| k.version.head? }
# Remove newest head and stable kegs
head - [Keg.new(head_prefix)] + stable.sort_by(&:version).slice(0...-1)
head - [Keg.new(head_prefix)] + Keg.sort(stable).drop(1)
else
installed_kegs.select do |keg|
tab = Tab.for_keg(keg)
Expand Down
5 changes: 4 additions & 1 deletion Library/Homebrew/formula_pin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def pin_at(version)
end

def pin
pin_at(@formula.installed_kegs.map(&:version).max)
latest_keg = Keg.sort(@formula.installed_kegs).first
return if latest_keg.nil?

pin_at(latest_keg.version)
end

def unpin
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ def self.from_rack(
flags: T.unsafe(nil)
)
kegs = rack.directory? ? rack.subdirs.map { |d| Keg.new(d) } : []
keg = kegs.find(&:linked?) || kegs.find(&:optlinked?) || kegs.max_by(&:version)
keg = kegs.find(&:linked?) || kegs.find(&:optlinked?) || Keg.sort(kegs).first

options = {
alias_path:,
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/installed_dependents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def find_some_installed_dependents(kegs, casks: [])
f_kegs = kegs_by_source[[f.name, f.tap]]
next unless f_kegs

f_kegs.max_by(&:version)
Keg.sort(f_kegs).first
end

next if required_kegs.empty?
Expand Down
9 changes: 9 additions & 0 deletions Library/Homebrew/keg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ def self.all
Formula.racks.flat_map(&:subdirs).map { |d| new(d) }
end

sig { params(kegs: T::Array[Keg]).returns(T::Array[Keg]) }
def self.sort(kegs)
kegs.sort_by { |keg| [keg.version_scheme, keg.version] }.reverse!
end

attr_reader :path, :name, :linked_keg_record, :opt_record

protected :path
Expand Down Expand Up @@ -387,6 +392,10 @@ def version
PkgVersion.parse(path.basename.to_s)
end

def version_scheme
@version_scheme ||= tab.version_scheme
end

def to_formula
Formulary.from_keg(self)
end
Expand Down

0 comments on commit 02a0ea8

Please sign in to comment.