Class: FormulaVersions

Inherits:
Object show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_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) ⇒ 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.

Parameters:

  • formula (Formula)
  • simulated_target (Boolean) (defaults to: false)


321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'formula_versions.rb', line 321

def initialize(formula, simulated_target: false)
  @simulated_target = simulated_target
  @name = T.let(formula.name, String)
  @path = T.let(formula.tap_path, Pathname)
  @repository = T.let(formula.tap!.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.

Returns:

  • (Exception, nil)


337
338
339
# File 'formula_versions.rb', line 337

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.

Returns:



230
231
232
233
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
# File 'formula_versions.rb', line 230

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
        options = super()
        options.singleton_class.class_eval { public :include? }
        options
      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, **options|
        next super(date:, because:, **options) if date

        raise FormulaSpecificationError, "Undated `deprecate!` cannot name a replacement" if options.present?

        instance_variable_set(:@deprecation_reason, because)
        instance_variable_set(:@deprecated, true)
      end
      define_method(:disable!) do |date: nil, because: nil, **options|
        next super(date:, because:, **options) if date

        raise FormulaSpecificationError, "Undated `disable!` cannot name a replacement" if options.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) { |**_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.

Parameters:

  • block (T.proc.void)

Returns:

  • (T.proc.void)


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, &_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.

Parameters:

  • revision (String)
  • formula_relative_path (String) (defaults to: relative_path)
  • _block (T.proc.params(arg0: Formula).returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


363
364
365
366
367
368
369
370
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
# File 'formula_versions.rb', line 363

def formula_at_revision(revision, formula_relative_path = relative_path, &_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,
        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.

Parameters:

Returns:

  • (Boolean)


411
412
413
414
415
416
# File 'formula_versions.rb', line 411

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.

Parameters:

  • branch (String)
  • all_history (Boolean) (defaults to: false)
  • _block (T.proc.params(revision: String, path: String).void)


344
345
346
347
348
349
350
351
352
353
# File 'formula_versions.rb', line 344

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