livecheck: refactor HEAD-only formula handling

The existing code for handling a `HEAD`-only formula involves two
return values that can be `nil` but this isn't apparent because the
related methods aren't typed. This adds type signatures to the
methods and updates the livecheck code to account for `nil` return
values (making it clear which methods can return `nil`).

Co-authored-by: Mike McQuaid <mike@mikemcquaid.com>
This commit is contained in:
Sam Ford 2025-02-19 14:51:36 -05:00
parent 01fcbfda53
commit a7cacfff1c
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D
4 changed files with 15 additions and 3 deletions

View File

@ -287,6 +287,7 @@ module Cask
#
# @see DSL::Version
# @api public
sig { params(arg: T.nilable(T.any(String, Symbol))).returns(T.nilable(DSL::Version)) }
def version(arg = nil)
set_unique_stanza(:version, arg.nil?) do
if !arg.is_a?(String) && arg != :latest

View File

@ -2403,6 +2403,7 @@ class Formula
# Returns the {PkgVersion} for this formula if it is installed.
# If not, return `nil`.
sig { returns(T.nilable(PkgVersion)) }
def any_installed_version
any_installed_keg&.version
end

View File

@ -388,6 +388,7 @@ class Keg
(path/"share/emacs/site-lisp"/name).children.any? { |f| ELISP_EXTENSIONS.include? f.extname }
end
sig { returns(PkgVersion) }
def version
require "pkg_version"
PkgVersion.parse(path.basename.to_s)

View File

@ -13,6 +13,8 @@ module Homebrew
# command. These methods print the requested livecheck information
# for formulae.
module Livecheck
NO_CURRENT_VERSION_MSG = "Unable to identify current version"
UNSTABLE_VERSION_KEYWORDS = T.let(%w[
alpha
beta
@ -249,13 +251,20 @@ module Homebrew
# comparison.
current = if formula
if formula.head_only?
Version.new(formula.any_installed_version.version.commit)
else
T.must(formula.stable).version
formula_commit = formula.any_installed_version&.version&.commit
Version.new(formula_commit) if formula_commit
elsif (stable = formula.stable)
stable.version
end
else
Version.new(formula_or_cask.version)
end
unless current
raise Livecheck::Error, NO_CURRENT_VERSION_MSG unless json
next if quiet
next status_hash(formula_or_cask, "error", [NO_CURRENT_VERSION_MSG], full_name: use_full_name, verbose:)
end
current_str = current.to_s
current = LivecheckVersion.create(formula_or_cask, current)