brew/Library/Homebrew/formula_versions.rb

70 lines
1.8 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: true
# frozen_string_literal: true
2014-06-19 17:57:36 -05:00
require "formula"
2020-08-17 06:09:57 +02:00
# Helper class for traversing a formula's previous versions.
#
# @api private
class FormulaVersions
include Context
IGNORED_EXCEPTIONS = [
ArgumentError, NameError, SyntaxError, TypeError,
FormulaSpecificationError, FormulaValidationError,
ErrorDuringExecution, LoadError, MethodDeprecatedError
].freeze
MAX_VERSIONS_DEPTH = 2
attr_reader :name, :path, :repository, :entry_name
def initialize(formula)
2015-04-01 21:06:03 -04:00
@name = formula.name
@path = formula.path
@repository = formula.tap.path
@entry_name = @path.relative_path_from(repository).to_s
@current_formula = formula
2017-06-03 00:23:06 +02:00
@formula_at_revision = {}
end
2015-05-29 00:16:50 -04:00
def rev_list(branch)
repository.cd do
2021-05-04 09:45:31 -04:00
rev_list_cmd = ["git", "rev-list", "--abbrev-commit", "--remove-empty"]
rev_list_cmd << "--first-parent" if repository != CoreTap.instance.path
2021-05-04 09:45:31 -04:00
Utils.popen_read(*rev_list_cmd, branch, "--", entry_name) do |io|
yield io.readline.chomp until io.eof?
end
end
end
def file_contents_at_revision(rev)
repository.cd { Utils.popen_read("git", "cat-file", "blob", "#{rev}:#{entry_name}") }
end
2015-01-04 14:29:00 -05:00
def formula_at_revision(rev)
2017-06-03 00:23:06 +02:00
Homebrew.raise_deprecation_exceptions = true
yield @formula_at_revision[rev] ||= begin
contents = file_contents_at_revision(rev)
nostdout { Formulary.from_contents(name, path, contents, ignore_errors: true) }
end
2017-06-03 00:23:06 +02:00
rescue *IGNORED_EXCEPTIONS => e
# We rescue these so that we can skip bad versions and
# continue walking the history
2020-07-06 15:29:15 -04:00
odebug "#{e} in #{name} at revision #{rev}", e.backtrace
2017-06-03 00:23:06 +02:00
rescue FormulaUnavailableError
2018-03-06 09:36:49 +00:00
nil
2017-06-03 00:23:06 +02:00
ensure
Homebrew.raise_deprecation_exceptions = false
end
2023-03-02 16:56:50 -08:00
def nostdout(&block)
if verbose?
yield
else
redirect_stdout(File::NULL, &block)
end
end
end