From 67b6a6d9d55b1e0a90cbd2aeb3f557cbf5e88801 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 21 Oct 2024 15:31:13 +0800 Subject: [PATCH] refactor: using custom `PATH` instead of reading it from `zsh` (#581) * run `echo $PATH > ~/Library/Application\ Support/SourceGit/PATH` to generate to custom PATH file for SourceGit --- README.md | 1 + src/Commands/Command.cs | 4 ++++ src/Native/MacOS.cs | 28 +++------------------------- src/Native/OS.cs | 1 + 4 files changed, 9 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index c2dee8c5..b0434ebe 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ For **macOS** users: * Make sure your mac trusts all software from anywhere. For more information, search `spctl --master-disable`. * Make sure [git-credential-manager](https://github.com/git-ecosystem/git-credential-manager/releases) is installed on your mac. * You may need to run `sudo xattr -cr /Applications/SourceGit.app` to make sure the software works. +* You man need to run `echo $PATH > ~/Library/Application\ Support/SourceGit/PATH` to introduce `PATH` env to SourceGit. For **Linux** users: diff --git a/src/Commands/Command.cs b/src/Commands/Command.cs index d774fa09..8d304410 100644 --- a/src/Commands/Command.cs +++ b/src/Commands/Command.cs @@ -195,6 +195,10 @@ namespace SourceGit.Commands if (OperatingSystem.IsLinux()) start.Environment.Add("LANG", "en_US.UTF-8"); + // 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/Native/MacOS.cs b/src/Native/MacOS.cs index 316e509c..9660920d 100644 --- a/src/Native/MacOS.cs +++ b/src/Native/MacOS.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Runtime.Versioning; -using System.Text; using Avalonia; @@ -19,30 +18,9 @@ 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. - } - } + var customPathFile = Path.Combine(OS.DataDir, "PATH"); + if (File.Exists(customPathFile)) + OS.CustomPathEnv = File.ReadAllText(customPathFile).Trim(); } public string FindGitExecutable() diff --git a/src/Native/OS.cs b/src/Native/OS.cs index bc9c5403..b53f81d9 100644 --- a/src/Native/OS.cs +++ b/src/Native/OS.cs @@ -26,6 +26,7 @@ namespace SourceGit.Native public static string GitExecutable { get; set; } = string.Empty; public static string ShellOrTerminal { get; set; } = string.Empty; public static List ExternalTools { get; set; } = []; + public static string CustomPathEnv { get; set; } = string.Empty; static OS() {