From a232f507554da2e01d0a3f43fcc8fdf4cdde6350 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 14 Oct 2024 16:58:29 +0800 Subject: [PATCH] feature: try to read `PATH` from `zsh` --- src/Commands/Command.cs | 9 --------- src/Native/MacOS.cs | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/Commands/Command.cs b/src/Commands/Command.cs index 7435dbfa..d774fa09 100644 --- a/src/Commands/Command.cs +++ b/src/Commands/Command.cs @@ -195,15 +195,6 @@ namespace SourceGit.Commands if (OperatingSystem.IsLinux()) start.Environment.Add("LANG", "en_US.UTF-8"); - // Fix sometimes `LSEnvironment` not working on macOS - if (OperatingSystem.IsMacOS()) - { - if (start.Environment.TryGetValue("PATH", out var path)) - start.Environment.Add("PATH", $"/opt/homebrew/bin:/opt/homebrew/sbin:{path}"); - else - start.Environment.Add("PATH", "/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"); - } - // Force using this app as git editor. switch (Editor) { diff --git a/src/Native/MacOS.cs b/src/Native/MacOS.cs index 6e2293a7..ffbc21b7 100644 --- a/src/Native/MacOS.cs +++ b/src/Native/MacOS.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Runtime.Versioning; +using System.Text; using Avalonia; @@ -17,6 +18,31 @@ namespace SourceGit.Native { DisableDefaultApplicationMenuItems = true, }); + + { + var startInfo = new ProcessStartInfo(); + startInfo.FileName = "zsh"; + startInfo.Arguments = "--login -c \"echo $PATH\""; + startInfo.UseShellExecute = false; + startInfo.CreateNoWindow = true; + startInfo.RedirectStandardOutput = true; + startInfo.StandardOutputEncoding = Encoding.UTF8; + + try + { + var proc = new Process() { StartInfo = startInfo }; + proc.Start(); + var pathData = proc.StandardOutput.ReadToEnd(); + proc.WaitForExit(); + if (proc.ExitCode == 0) + Environment.SetEnvironmentVariable("PATH", pathData); + proc.Close(); + } + catch + { + // Ignore error. + } + } } public string FindGitExecutable()