From 1d5458843e9dd691063443f35eee2d865e6a4c40 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Tue, 12 Apr 2016 11:00:23 +0100 Subject: [PATCH] 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. --- Library/Homebrew/tap.rb | 64 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index a2e21ca0a5..a84aded796 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -77,6 +77,8 @@ class Tap @command_files = nil @formula_renames = nil @tap_migrations = nil + @config = nil + remove_instance_variable(:@private) if instance_variable_defined?(:@private) end # The remote path to this {Tap}. @@ -143,14 +145,37 @@ class Tap user == "Homebrew" 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. def private? - return true if custom_remote? - GitHub.private_repo?(user, "homebrew-#{repo}") - rescue GitHub::HTTPNotFoundError - true - rescue GitHub::Error - false + return @private if instance_variable_defined?(:@private) + @private = read_and_set_private_config + end + + def config + @config ||= begin + raise TapUnavailableError, name unless installed? + TapConfig.new(self) + end end # True if this {Tap} has been installed. @@ -567,3 +592,30 @@ class CoreTap < Tap file.basename.to_s 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