From 14d142905822b5d7233030afcfeff8b97fb36009 Mon Sep 17 00:00:00 2001 From: Max Fierke Date: Sun, 28 Apr 2024 17:09:41 -0500 Subject: [PATCH] Handle missing git on macOS properly We need to ignore the default CLT shim for git and find the _real_ path ourselves to properly skip git fetches before git is available. Otherwise, it _seems_ present, but is actually missing and will pop up the Xcode CLT installer dialog and fail to fetch git resources --- src/mstrap/platform.cr | 2 +- src/mstrap/platform/darwin/macos.cr | 29 +++++++++++++++++++++++++++++ src/mstrap/platform/linux.cr | 4 ++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/mstrap/platform.cr b/src/mstrap/platform.cr index 33cc099..167d465 100644 --- a/src/mstrap/platform.cr +++ b/src/mstrap/platform.cr @@ -12,7 +12,7 @@ module MStrap # Indicates whether the host platform has Git installed def self.has_git? - ENV["MSTRAP_IGNORE_GIT"]? != "true" && has_command?("git") + ENV["MSTRAP_IGNORE_GIT"]? != "true" && platform.has_git? end # Indicates whether the host platform has a given command available diff --git a/src/mstrap/platform/darwin/macos.cr b/src/mstrap/platform/darwin/macos.cr index 8a09a81..2260f6a 100644 --- a/src/mstrap/platform/darwin/macos.cr +++ b/src/mstrap/platform/darwin/macos.cr @@ -3,6 +3,35 @@ module MStrap module MacOS extend DSL + XCODE_CLT_GIT_PATH = "/Library/Developer/CommandLineTools/usr/bin/git" + + @@git_path : String = "" + + # :nodoc: + def self.has_git? + git_path = @@git_path + return true if git_path != "" + + git_path = Process.find_executable("git") + + # Ignore the XCode CLT shim trickery! + if git_path && git_path == "/usr/bin/git" + # Try and look it up with xcrun + if has_command?("xcrun") + xcrun_git_path = `xcrun -find git 2>/dev/null`.chomp + git_path = Process.find_executable(xcrun_git_path) if $?.success? + end + + # Fallback to default CLT path + git_path ||= Process.find_executable(XCODE_CLT_GIT_PATH) + end + + return false if git_path.nil? + + @@git_path = git_path + true + end + def self.install_packages!(packages : Array(String)) cmd("brew", ["install"] + packages) end diff --git a/src/mstrap/platform/linux.cr b/src/mstrap/platform/linux.cr index e7e02e3..da7e5ae 100644 --- a/src/mstrap/platform/linux.cr +++ b/src/mstrap/platform/linux.cr @@ -118,6 +118,10 @@ module MStrap distro_family == DISTRO_UNKNOWN end + def has_git? + has_command?("git") + end + # :nodoc: def platform if arch_distro?