diff options
| author | Mike McQuaid | 2016-04-12 11:00:23 +0100 | 
|---|---|---|
| committer | Mike McQuaid | 2016-04-12 11:00:23 +0100 | 
| commit | 1d5458843e9dd691063443f35eee2d865e6a4c40 (patch) | |
| tree | fa7be0f12da2fd78f7fd95578eee3aed63402fd9 | |
| parent | c1427fd295d63d141acadd03e41c5c73fb11212a (diff) | |
| download | brew-1d5458843e9dd691063443f35eee2d865e6a4c40.tar.bz2 | |
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.
| -rw-r--r-- | Library/Homebrew/tap.rb | 64 | 
1 files changed, 58 insertions, 6 deletions
| diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index a2e21ca0a..a84aded79 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 | 
