Cache Tap#private? checks.

Use both an in-class and a `.git/config` cache for this so we can ensure
that the `Tap#private?` check is fast. Also, make sure this cache value
is set both when initially installing and updating a tap.

Thanks to @xu-cheng for most of the implementation here.
This commit is contained in:
Mike McQuaid 2016-04-12 11:00:23 +01:00
parent c1427fd295
commit 1d5458843e

View File

@ -77,6 +77,8 @@ class Tap
@command_files = nil @command_files = nil
@formula_renames = nil @formula_renames = nil
@tap_migrations = nil @tap_migrations = nil
@config = nil
remove_instance_variable(:@private) if instance_variable_defined?(:@private)
end end
# The remote path to this {Tap}. # The remote path to this {Tap}.
@ -143,14 +145,37 @@ class Tap
user == "Homebrew" user == "Homebrew"
end end
# @private
def read_and_set_private_config
case config["private"]
when "true" then true
when "false" then false
else
config["private"] = begin
if custom_remote?
true
else
GitHub.private_repo?(user, "homebrew-#{repo}")
end
rescue GitHub::HTTPNotFoundError
true
rescue GitHub::Error
false
end
end
end
# True if the remote of this {Tap} is a private repository. # True if the remote of this {Tap} is a private repository.
def private? def private?
return true if custom_remote? return @private if instance_variable_defined?(:@private)
GitHub.private_repo?(user, "homebrew-#{repo}") @private = read_and_set_private_config
rescue GitHub::HTTPNotFoundError end
true
rescue GitHub::Error def config
false @config ||= begin
raise TapUnavailableError, name unless installed?
TapConfig.new(self)
end
end end
# True if this {Tap} has been installed. # True if this {Tap} has been installed.
@ -567,3 +592,30 @@ class CoreTap < Tap
file.basename.to_s file.basename.to_s
end end
end end
class TapConfig
attr_reader :tap
def initialize(tap)
@tap = tap
end
def [](key)
return unless tap.git?
return unless Utils.git_available?
tap.path.cd do
Utils.popen_read("git", "config", "--local", "--get", "homebrew.#{key}").chuzzle
end
end
def []=(key, value)
return unless tap.git?
return unless Utils.git_available?
tap.path.cd do
safe_system "git", "config", "--local", "--replace-all", "homebrew.#{key}", value.to_s
end
value
end
end