diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 84e2363ca4..867c747f8e 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -5,6 +5,7 @@ require "utils/json" require "utils/inreplace" require "utils/popen" require "utils/fork" +require "utils/git" require "open-uri" class Tty diff --git a/Library/Homebrew/utils/git.rb b/Library/Homebrew/utils/git.rb new file mode 100644 index 0000000000..53057cd105 --- /dev/null +++ b/Library/Homebrew/utils/git.rb @@ -0,0 +1,23 @@ +module Utils + def self.git_available? + git = which("git") + # git isn't installed by older Xcodes + return false if git.nil? + # /usr/bin/git is a popup stub when Xcode/CLT aren't installed, so bail out + return false if git == "/usr/bin/git" && !OS::Mac.has_apple_developer_tools? + true + end + + def self.ensure_git_installed! + return if git_available? + + require "cmd/install" + begin + oh1 "Installing git" + Homebrew.perform_preinstall_checks + Homebrew.install_formula(Formulary.factory("git")) + rescue + raise "Git is unavailable" + end + end +end