Class: FormulaVersions
- Includes:
- Context, Utils::Output::Mixin
- Defined in:
- formula_versions.rb
Overview
This class is part of an internal API. This class may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this class if possible, as it may be removed or changed without warning.
Helper class for traversing a formula's previous versions.
Defined Under Namespace
Modules: LegacyChecksums, LegacyLinuxDeveloperTools, LegacyMacOS, LegacySoftwareSpec Classes: LegacyBottleSpecification, LegacyMacOSVersion, LegacyResource
Constant Summary collapse
- IGNORED_EXCEPTIONS =
This constant is part of an internal API. This constant may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
[ ArgumentError, NameError, SyntaxError, TypeError, LegacyDSLError, FormulaSpecificationError, FormulaValidationError, ErrorDuringExecution, LoadError, MethodDeprecatedError ].freeze
Instance Attribute Summary collapse
-
#load_error ⇒ Exception?
readonly
private
The original error from the most recent failed historical load.
Class Method Summary collapse
- .legacy_formula_class ⇒ T.class_of(Formula) private
-
.legacy_patch_block(block) ⇒ T.proc.void
private
Extend each historical patch resource before evaluating its declarations.
Instance Method Summary collapse
- #formula_at_revision(revision, formula_relative_path = relative_path, source: nil, &_block) ⇒ T.type_parameter(:U)? private
-
#initialize(formula, simulated_target: false, repository: nil) ⇒ void
constructor
private
With
simulated_target, historicalMacOSqueries answer only from Homebrew::SimulateSystem's target. -
#path_absent_at_revision?(revision, relative_path) ⇒ Boolean
private
Only a successful tree lookup proves absence; a failed Git command must not turn unreadable history into a skipped revision.
-
#rev_list(branch, all_history: false, &_block) ⇒ void
private
Full history includes earlier lifetimes of a deleted and re-added path.
Methods included from Utils::Output::Mixin
#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning
Methods included from Context
current, current=, #debug?, #deferred_environment_expansion?, #quiet?, #verbose?, #with_context
Constructor Details
#initialize(formula, simulated_target: false, repository: nil) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
With simulated_target, historical MacOS queries answer only from
Homebrew::SimulateSystem's target.
327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'formula_versions.rb', line 327 def initialize(formula, simulated_target: false, repository: nil) @simulated_target = simulated_target @name = T.let(formula.is_a?(Formula) ? formula.name : formula.basename(".rb").to_s, String) @path = T.let(formula.is_a?(Formula) ? formula.tap_path : formula, Pathname) @repository = T.let(repository || (formula.tap!.path if formula.is_a?(Formula)) || raise(ArgumentError, "A repository is required for a historical formula path"), Pathname) @relative_path = T.let(@path.relative_path_from(@repository).to_s, String) # Also look at e.g. older homebrew-core paths before sharding. if (match = @relative_path.match(%r{^(HomebrewFormula|Formula)/(?:[a-z]|lib)/(.+)})) @old_relative_path = T.let("#{match[1]}/#{match[2]}", T.nilable(String)) end @formula_at_revision = T.let({}, T::Hash[String, Formula]) @load_error = T.let(nil, T.nilable(Exception)) end |
Instance Attribute Details
#load_error ⇒ Exception? (readonly)
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
The original error from the most recent failed historical load.
344 345 346 |
# File 'formula_versions.rb', line 344 def load_error @load_error end |
Class Method Details
.legacy_formula_class ⇒ T.class_of(Formula)
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'formula_versions.rb', line 234 def self.legacy_formula_class @legacy_formula_class ||= Class.new(Formula) do extend LegacyChecksums const_set(:MacOS, LegacyMacOS) const_set(:StrictSubversionDownloadStrategy, SubversionDownloadStrategy) class << self define_method(:on_system) do |linux, macos:, &block| T.bind(self, T.class_of(Formula)) # `Ignorable` resumes after the modern `ArgumentError`s, so reject # invalid historical arguments before delegating. raise FormulaSpecificationError, "The first argument to `on_system` must be `:linux`" if linux != :linux version, condition = macos.to_s.split(/_(?=or_)/).map(&:to_sym) if condition && [:or_older, :or_newer].exclude?(condition) raise FormulaSpecificationError, "Invalid OS condition: #{condition.inspect}" end unless LegacyMacOSVersion::RELEASES.key?(version) raise FormulaSpecificationError, "Unknown historical macOS release: #{version.inspect}" end next super(linux, macos:, &block) if MacOSVersion::SYMBOLS.key?(version) if Homebrew::SimulateSystem.current_os == :linux on_linux(&block) else on_macos do target = LegacyMacOS.target_version matches = case condition when :or_older then target <= version when :or_newer then target >= version else target == version end block.call if matches end end end define_method(:build) do = super() .singleton_class.class_eval { public :include? } end # From April 2020 to June 2021 `date:` and `because:` were optional and # an undated call took effect immediately. define_method(:deprecate!) do |date: nil, because: nil, **| next super(date:, because:, **) if date raise FormulaSpecificationError, "Undated `deprecate!` cannot name a replacement" if .present? instance_variable_set(:@deprecation_reason, because) instance_variable_set(:@deprecated, true) end define_method(:disable!) do |date: nil, because: nil, **| next super(date:, because:, **) if date raise FormulaSpecificationError, "Undated `disable!` cannot name a replacement" if .present? instance_variable_set(:@disable_reason, because) instance_variable_set(:@disabled, true) end define_method(:cxxstdlib_check) { |_value| nil } define_method(:devel) { nil } define_method(:plist_options) { |**| nil } # Historical option checks must fail the load, not exit the consumer. define_method(:odie) { |error| raise FormulaSpecificationError, error.to_s } define_method(:bottle) do |*args, &block| next if args == [:unneeded] && block.nil? super(*args, &block) end define_method(:inherited) do |child| super(child) [child.stable, child.head].compact.each do |spec| spec.extend(LegacySoftwareSpec) spec.instance_variable_set(:@bottle_specification, LegacyBottleSpecification.new) end end end end end |
.legacy_patch_block(block) ⇒ T.proc.void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Extend each historical patch resource before evaluating its declarations.
61 62 63 64 65 66 67 68 |
# File 'formula_versions.rb', line 61 def self.legacy_patch_block(block) proc do T.bind(self, Resource::Patch) extend LegacyChecksums instance_eval(&block) end end |
Instance Method Details
#formula_at_revision(revision, formula_relative_path = relative_path, source: nil, &_block) ⇒ T.type_parameter(:U)?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'formula_versions.rb', line 371 def formula_at_revision(revision, formula_relative_path = relative_path, source: nil, &_block) @load_error = nil Homebrew.raise_deprecation_exceptions = true LegacyMacOS.simulated_target = @simulated_target # rev_list visits the current path first. At a sharding rename, the old # path is absent in the same commit; reuse the already-loaded new path. formula = @formula_at_revision[revision] || begin nostdout do Formulary.from_contents( name, path, (source || file_contents_at_revision(revision, formula_relative_path)) .sub(/\A(?:(?:[ \t]*#[^\n]*\n|[ \t]*\n)|(?:=begin[^\n]*(?:\n|\z).*?^=end[^\n]*(?:\n|\z)))*/m) do |header| "#{header}Formula = ScriptFileFormula = ::FormulaVersions.legacy_formula_class;" end, ignore_errors: true, ) end rescue FormulaUnavailableError => e @load_error = e.cause || e nil rescue Homebrew::UntrustedTapError, MacOSVersion::Error raise rescue StandardError, ScriptError => e @load_error = e raise if Homebrew::EnvConfig.disable_load_formula? require "utils/backtrace" # We rescue these so that we can skip bad versions and # continue walking the history odebug "#{e} in #{name} at revision #{revision}", Utils::Backtrace.clean(e) nil end return if formula.nil? @formula_at_revision[revision] = formula yield formula ensure Homebrew.raise_deprecation_exceptions = false LegacyMacOS.simulated_target = false end |
#path_absent_at_revision?(revision, relative_path) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Only a successful tree lookup proves absence; a failed Git command must not turn unreadable history into a skipped revision.
419 420 421 422 423 424 |
# File 'formula_versions.rb', line 419 def path_absent_at_revision?(revision, relative_path) repository.cd do Utils.popen_read("git", "ls-tree", "--full-tree", "--name-only", "-z", revision, "--", relative_path, safe: true).empty? end end |
#rev_list(branch, all_history: false, &_block) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
Full history includes earlier lifetimes of a deleted and re-added path. Vulns::History skips proven absent paths, which are not formula builds.
351 352 353 354 355 356 357 358 359 360 |
# File 'formula_versions.rb', line 351 def rev_list(branch, all_history: false, &_block) repository.cd do rev_list_cmd = ["git", "rev-list", "--abbrev-commit"] rev_list_cmd << "--remove-empty" unless all_history [relative_path, old_relative_path].compact.each do |entry| Utils.popen_read(*rev_list_cmd, branch, "--", entry, safe: all_history) .each_line(chomp: true) { |revision| yield revision, entry } end end end |