From c3e1fb93b6fcd42b5156c999371e1f4f61537fb5 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 14 Mar 2025 10:54:09 +0800 Subject: [PATCH] refactor: fix maxOS `PATH` env Signed-off-by: leo --- src/Commands/Command.cs | 4 ---- src/Commands/ExecuteCustomAction.cs | 8 -------- src/Native/MacOS.cs | 15 ++++++++++++++- src/Native/OS.cs | 6 ------ 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/Commands/Command.cs b/src/Commands/Command.cs index 5197de11..0fef1235 100644 --- a/src/Commands/Command.cs +++ b/src/Commands/Command.cs @@ -198,10 +198,6 @@ namespace SourceGit.Commands start.Environment.Add("LC_ALL", "C"); } - // Fix macOS `PATH` env - if (OperatingSystem.IsMacOS() && !string.IsNullOrEmpty(Native.OS.CustomPathEnv)) - start.Environment.Add("PATH", Native.OS.CustomPathEnv); - // Force using this app as git editor. switch (Editor) { diff --git a/src/Commands/ExecuteCustomAction.cs b/src/Commands/ExecuteCustomAction.cs index 894637a5..000c8fd1 100644 --- a/src/Commands/ExecuteCustomAction.cs +++ b/src/Commands/ExecuteCustomAction.cs @@ -17,10 +17,6 @@ namespace SourceGit.Commands start.CreateNoWindow = true; start.WorkingDirectory = repo; - // Fix macOS `PATH` env - if (OperatingSystem.IsMacOS() && !string.IsNullOrEmpty(Native.OS.CustomPathEnv)) - start.Environment.Add("PATH", Native.OS.CustomPathEnv); - try { Process.Start(start); @@ -44,10 +40,6 @@ namespace SourceGit.Commands start.StandardErrorEncoding = Encoding.UTF8; start.WorkingDirectory = repo; - // Fix macOS `PATH` env - if (OperatingSystem.IsMacOS() && !string.IsNullOrEmpty(Native.OS.CustomPathEnv)) - start.Environment.Add("PATH", Native.OS.CustomPathEnv); - var proc = new Process() { StartInfo = start }; var builder = new StringBuilder(); diff --git a/src/Native/MacOS.cs b/src/Native/MacOS.cs index 633ef5eb..123b160b 100644 --- a/src/Native/MacOS.cs +++ b/src/Native/MacOS.cs @@ -18,9 +18,22 @@ namespace SourceGit.Native DisableDefaultApplicationMenuItems = true, }); + // Fix `PATH` env on macOS. + var path = Environment.GetEnvironmentVariable("PATH"); + if (string.IsNullOrEmpty(path)) + path = "/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"; + else if (!path.Contains("/opt/homebrew/", StringComparison.Ordinal)) + path = "/opt/homebrew/bin:/opt/homebrew/sbin:" + path; + var customPathFile = Path.Combine(OS.DataDir, "PATH"); if (File.Exists(customPathFile)) - OS.CustomPathEnv = File.ReadAllText(customPathFile).Trim(); + { + var env = File.ReadAllText(customPathFile).Trim(); + if (!string.IsNullOrEmpty(env)) + path = env; + } + + Environment.SetEnvironmentVariable("PATH", path); } public string FindGitExecutable() diff --git a/src/Native/OS.cs b/src/Native/OS.cs index 3a688654..f11d1e7f 100644 --- a/src/Native/OS.cs +++ b/src/Native/OS.cs @@ -31,12 +31,6 @@ namespace SourceGit.Native private set; } = string.Empty; - public static string CustomPathEnv - { - get; - set; - } = string.Empty; - public static string GitExecutable { get => _gitExecutable;