From c7bbb904e8f83b08fe8d10913e37298fa7a03386 Mon Sep 17 00:00:00 2001 From: Adrian Ho Date: Tue, 1 Jun 2021 00:41:44 +0800 Subject: [PATCH] diagnostic: add check for broken taps Detect half-baked core taps that show up on a fairly regular basis (e.g. #11465). The logic is simple enough: Since an improper tap wouldn't have a complete Git config, and is always somewhere below `HOMEBREW_REPOSITORY`, any Git operation would look at the Brew repo instead. We simply need to test for any of: 1. Empty tap origin 2. Empty tap HEAD 3. Tap HEAD == Brew HEAD --- Library/Homebrew/diagnostic.rb | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index 93d6ade63b..18c5466983 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -147,6 +147,27 @@ module Homebrew end end + def broken_tap_msg(tap) + <<~EOS + #{tap.full_name} was not tapped properly. + To fix: + rm -rf "#{tap.path}" + brew tap #{tap.name} + EOS + end + + def broken_tap(tap) + return unless Utils::Git.available? + return unless HOMEBREW_REPOSITORY.git? + + return broken_tap_msg(tap) if tap.remote.blank? + + tap_head = tap.git_head + return broken_tap_msg(tap) if tap_head.blank? + + return broken_tap_msg(tap) if tap_head == HOMEBREW_REPOSITORY.git_head + end + def check_for_installed_developer_tools return if DevelopmentTools.installed? @@ -558,15 +579,16 @@ module Homebrew examine_git_origin(HOMEBREW_REPOSITORY, Homebrew::EnvConfig.brew_git_remote) end - def check_coretap_git_origin - examine_git_origin(CoreTap.instance.path, Homebrew::EnvConfig.core_git_remote) + def check_coretap_integrity + coretap = CoreTap.instance + broken_tap(coretap) || examine_git_origin(coretap.path, Homebrew::EnvConfig.core_git_remote) end - def check_casktap_git_origin + def check_casktap_integrity default_cask_tap = Tap.default_cask_tap return unless default_cask_tap.installed? - examine_git_origin(default_cask_tap.path, default_cask_tap.remote) + broken_tap(default_cask_tap) || examine_git_origin(default_cask_tap.path, default_cask_tap.remote) end sig { returns(T.nilable(String)) }