From 38665a61cb9660a0668b8cee485c5e258de3bb47 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 12 Aug 2024 21:29:18 +0800 Subject: [PATCH] fix: `Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)` returns an empty string on some linux distro * fallback the storage folder to `~/.sourcegit` --- README.md | 10 +++++----- src/App.axaml.cs | 2 ++ src/Native/OS.cs | 17 +++++++++++++---- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0af00613..d9ce8f03 100644 --- a/README.md +++ b/README.md @@ -45,11 +45,11 @@ You can download the latest stable from [Releases](https://github.com/sourcegit- This software creates a folder `$"{System.Environment.SpecialFolder.ApplicationData}/SourceGit"`, which is platform-dependent, to store user settings, downloaded avatars and crash logs. -| OS | PATH | -|---------|-------------------------------------------------| -| Windows | `C:\Users\USER_NAME\AppData\Roaming\SourceGit` | -| Linux | `${HOME}/.config/SourceGit` | -| macOS | `${HOME}/Library/Application Support/SourceGit` | +| OS | PATH | +|---------|-----------------------------------------------------| +| Windows | `C:\Users\USER_NAME\AppData\Roaming\SourceGit` | +| Linux | `${HOME}/.config/SourceGit` or `${HOME}/.Sourcegit` | +| macOS | `${HOME}/Library/Application Support/SourceGit` | For **Windows** users: diff --git a/src/App.axaml.cs b/src/App.axaml.cs index a8f1e32e..be379e54 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -44,6 +44,8 @@ namespace SourceGit [STAThread] public static void Main(string[] args) { + Native.OS.SetupDataDir(); + AppDomain.CurrentDomain.UnhandledException += (_, e) => { LogException(e.ExceptionObject as Exception); diff --git a/src/Native/OS.cs b/src/Native/OS.cs index 8c2a3ada..ea2b14e6 100644 --- a/src/Native/OS.cs +++ b/src/Native/OS.cs @@ -21,9 +21,9 @@ namespace SourceGit.Native void OpenWithDefaultEditor(string file); } - public static readonly string DataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SourceGit"); + public static string DataDir { get; private set; } = string.Empty; public static string GitExecutable { get; set; } = string.Empty; - public static List ExternalTools { get; set; } = new List(); + public static List ExternalTools { get; set; } = []; static OS() { @@ -70,10 +70,19 @@ namespace SourceGit.Native public static void SetupApp(AppBuilder builder) { + _backend.SetupApp(builder); + } + + public static void SetupDataDir() + { + var osAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + if (string.IsNullOrEmpty(osAppDataDir)) + DataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".SourceGit"); + else + DataDir = Path.Combine(osAppDataDir, "SourceGit"); + if (!Directory.Exists(DataDir)) Directory.CreateDirectory(DataDir); - - _backend.SetupApp(builder); } public static void SetupEnternalTools()