brew/Library/Homebrew/formula_versions.rb
Mike McQuaid 2a94d382ac
audit: make audit_revision_and_version_scheme faster.
This is really, really slow at the moment for a few reasons:
- it goes through the list of revisions twice
- it checks many more revisions than it needs to

Even after these improvements it's still by far the slowest audit so
am also making it a `--git` only audit.

Additionally, to further improve default `brew audit` performance do not
run `brew style` checks when doing `brew audit` with no arguments.

`brew style` can be run quickly and efficiently on all of a tap (and is
cached) so no need to duplicate it here.
2020-06-08 15:00:09 +01:00

69 lines
1.9 KiB
Ruby

# frozen_string_literal: true
require "formula"
class FormulaVersions
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)
@name = formula.name
@path = formula.path
@repository = formula.tap.path
@entry_name = @path.relative_path_from(repository).to_s
@current_formula = formula
@formula_at_revision = {}
end
def rev_list(branch)
repository.cd do
Utils.popen_read("git", "rev-list", "--abbrev-commit", "--remove-empty", 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
def formula_at_revision(rev)
Homebrew.raise_deprecation_exceptions = true
yield @formula_at_revision[rev] ||= begin
contents = file_contents_at_revision(rev)
nostdout { Formulary.from_contents(name, path, contents) }
end
rescue *IGNORED_EXCEPTIONS => e
# We rescue these so that we can skip bad versions and
# continue walking the history
odebug "#{e} in #{name} at revision #{rev}", e.backtrace if Homebrew.args.debug?
rescue FormulaUnavailableError
nil
ensure
Homebrew.raise_deprecation_exceptions = false
end
def bottle_version_map(branch)
map = Hash.new { |h, k| h[k] = [] }
versions_seen = 0
rev_list(branch) do |rev|
formula_at_revision(rev) do |f|
bottle = f.bottle_specification
map[f.pkg_version] << bottle.rebuild unless bottle.checksums.empty?
versions_seen = (map.keys + [f.pkg_version]).uniq.length
end
return map if versions_seen > MAX_VERSIONS_DEPTH
end
map
end
end