Skip to content

Commit

Permalink
Merge pull request #16813 from reitermarkus/tapconfig-sig
Browse files Browse the repository at this point in the history
Simplify `TapConfig`.
  • Loading branch information
reitermarkus committed Mar 7, 2024
2 parents 8a852cf + be9c0b8 commit 49910ad
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
72 changes: 36 additions & 36 deletions Library/Homebrew/tap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,29 @@ def official?
user == "Homebrew"
end

# True if the remote of this {Tap} is a private repository.
# Check whether the remote of this {Tap} is a private repository.
sig { returns(T::Boolean) }
def private?
return @private if instance_variable_defined?(:@private)
return @private if defined?(@private)

@private = read_or_set_private_config
@private = if (value = config[:private]).nil?
config[:private] = begin
if custom_remote?
true
else
# Don't store config if we don't know for sure.
return false if (value = GitHub.private_repo?(full_name)).nil?

value
end
rescue GitHub::API::HTTPNotFoundError
true
rescue GitHub::API::Error
false
end
else
value
end
end

# {TapConfig} of this {Tap}.
Expand All @@ -292,6 +310,7 @@ def config
end

# True if this {Tap} has been installed.
sig { returns(T::Boolean) }
def installed?
path.directory?
end
Expand Down Expand Up @@ -351,12 +370,12 @@ def install(quiet: false, clone_target: nil, force_auto_update: nil,
fix_remote_configuration(requested_remote: requested_remote, quiet: quiet)
end

unless force_auto_update.nil?
if force_auto_update
config["forceautoupdate"] = force_auto_update
elsif config["forceautoupdate"] == "true"
config.delete("forceautoupdate")
end
case force_auto_update
when true
config[:forceautoupdate] = true
return
when false
config.delete(:forceautoupdate)
return
end

Expand Down Expand Up @@ -403,7 +422,7 @@ def install(quiet: false, clone_target: nil, force_auto_update: nil,
raise
end

config["forceautoupdate"] = force_auto_update unless force_auto_update.nil?
config[:forceautoupdate] = force_auto_update unless force_auto_update.nil?

Commands.rebuild_commands_completion_list
link_completions_and_manpages
Expand Down Expand Up @@ -976,28 +995,6 @@ def audit_exception(list, formula_or_cask, value = nil)

private

def read_or_set_private_config
case config["private"]
when "true" then true
when "false" then false
else
config["private"] = begin
if custom_remote?
true
else
# Don't store config if we don't know for sure.
return false if (value = GitHub.private_repo?(full_name)).nil?

value
end
rescue GitHub::API::HTTPNotFoundError
true
rescue GitHub::API::Error
false
end
end
end

sig { params(file: Pathname).returns(T.any(T::Array[String], Hash)) }
def read_formula_list(file)
JSON.parse file.read
Expand Down Expand Up @@ -1375,23 +1372,26 @@ def initialize(tap)
@tap = tap
end

sig { params(key: T.any(Symbol, String)).returns(T.nilable(String)) }
sig { params(key: Symbol).returns(T.nilable(T::Boolean)) }
def [](key)
return unless tap.git?
return unless Utils::Git.available?

Homebrew::Settings.read key, repo: tap.path
case Homebrew::Settings.read(key, repo: tap.path)
when "true" then true
when "false" then false
end
end

sig { params(key: T.any(Symbol, String), value: T.any(T::Boolean, String)).void }
sig { params(key: Symbol, value: T::Boolean).void }
def []=(key, value)
return unless tap.git?
return unless Utils::Git.available?

Homebrew::Settings.write key, value.to_s, repo: tap.path
end

sig { params(key: T.any(Symbol, String)).void }
sig { params(key: Symbol).void }
def delete(key)
return unless tap.git?
return unless Utils::Git.available?
Expand Down
16 changes: 8 additions & 8 deletions Library/Homebrew/test/tap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,19 +363,19 @@ def setup_completion(link:)

it "defaults to nil" do
expect(already_tapped_tap).to be_installed
expect(already_tapped_tap.config["forceautoupdate"]).to be_nil
expect(already_tapped_tap.config[:forceautoupdate]).to be_nil
end

it "enables forced auto-updates when true" do
expect(already_tapped_tap).to be_installed
already_tapped_tap.install force_auto_update: true
expect(already_tapped_tap.config["forceautoupdate"]).to eq("true")
expect(already_tapped_tap.config[:forceautoupdate]).to be true
end

it "disables forced auto-updates when false" do
expect(already_tapped_tap).to be_installed
already_tapped_tap.install force_auto_update: false
expect(already_tapped_tap.config["forceautoupdate"]).to be_nil
expect(already_tapped_tap.config[:forceautoupdate]).to be_nil
end
end

Expand Down Expand Up @@ -488,11 +488,11 @@ def setup_completion(link:)
specify "#config" do
setup_git_repo

expect(homebrew_foo_tap.config["foo"]).to be_nil
homebrew_foo_tap.config["foo"] = "bar"
expect(homebrew_foo_tap.config["foo"]).to eq("bar")
homebrew_foo_tap.config.delete("foo")
expect(homebrew_foo_tap.config["foo"]).to be_nil
expect(homebrew_foo_tap.config[:foo]).to be_nil
homebrew_foo_tap.config[:foo] = true
expect(homebrew_foo_tap.config[:foo]).to be true
homebrew_foo_tap.config.delete(:foo)
expect(homebrew_foo_tap.config[:foo]).to be_nil
end

describe "#each" do
Expand Down

0 comments on commit 49910ad

Please sign in to comment.