Skip to content

Commit 47a7c90

Browse files
committed
refactor: move data from $XDG_CONFIG_HOME/SourceGit to $XDG_DATA_HOME/SourceGit on Linux (#2088)
Old datas will be migrated automatically Signed-off-by: leo <longshuang@msn.cn>
1 parent 98bee99 commit 47a7c90

File tree

5 files changed

+71
-41
lines changed

5 files changed

+71
-41
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@
7070

7171
You can download the latest stable from [Releases](https://github.com/sourcegit-scm/sourcegit/releases/latest) or download workflow artifacts from [GitHub Actions](https://github.com/sourcegit-scm/sourcegit/actions) to try this app based on latest commits.
7272

73-
This software creates a folder `$"{System.Environment.SpecialFolder.ApplicationData}/SourceGit"`, which is platform-dependent, to store user settings, downloaded avatars and crash logs.
73+
This software creates a folder, which is platform-dependent, to store user settings, downloaded avatars and crash logs.
7474

75-
| OS | PATH |
76-
|---------|-----------------------------------------------------|
77-
| Windows | `%APPDATA%\SourceGit` |
78-
| Linux | `${HOME}/.config/SourceGit` or `${HOME}/.sourcegit` |
79-
| macOS | `${HOME}/Library/Application Support/SourceGit` |
75+
| OS | PATH |
76+
|---------|-------------------------------------------------|
77+
| Windows | `%APPDATA%\SourceGit` |
78+
| Linux | `${HOME}/.local/share/SourceGit` |
79+
| macOS | `${HOME}/Library/Application Support/SourceGit` |
8080

8181
> [!TIP]
8282
> * You can open this data storage directory from the main menu `Open Data Storage Directory`.

src/Native/Linux.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,45 @@ public void SetupWindow(Window window)
3333
}
3434
}
3535

36+
public string GetDataDir()
37+
{
38+
// AppImage supports portable mode
39+
var appImage = Environment.GetEnvironmentVariable("APPIMAGE");
40+
if (!string.IsNullOrEmpty(appImage) && File.Exists(appImage))
41+
{
42+
var portableDir = Path.Combine(Path.GetDirectoryName(appImage)!, "data");
43+
if (Directory.Exists(portableDir))
44+
return portableDir;
45+
}
46+
47+
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
48+
var dataDir = Path.Combine(home, ".local", "share", "SourceGit"); // New data dir: $XDG_DATA_HOME/SourceGit
49+
50+
// Migrate old data.
51+
if (!Directory.Exists(dataDir))
52+
{
53+
var oldDataDir = Path.Combine(home, ".config", "SourceGit"); // Old data dir: $XDG_CONFIG_HOME/SourceGit
54+
var oldFallbackDir = Path.Combine(home, ".sourcegit"); // Old fallback folder: $HOME/.sourcegit
55+
var moveDir = Directory.Exists(oldDataDir)
56+
? oldDataDir
57+
: (Directory.Exists(oldFallbackDir) ? oldFallbackDir : string.Empty);
58+
59+
if (!string.IsNullOrEmpty(moveDir))
60+
{
61+
try
62+
{
63+
Directory.Move(moveDir, dataDir);
64+
}
65+
catch
66+
{
67+
// Ignore errors
68+
}
69+
}
70+
}
71+
72+
return dataDir;
73+
}
74+
3675
public string FindGitExecutable()
3776
{
3877
return FindExecutable("git");

src/Native/MacOS.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public void SetupWindow(Window window)
4444
window.ExtendClientAreaToDecorationsHint = true;
4545
}
4646

47+
public string GetDataDir()
48+
{
49+
return Path.Combine(
50+
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
51+
"SourceGit");
52+
}
53+
4754
public string FindGitExecutable()
4855
{
4956
var gitPathVariants = new List<string>() {

src/Native/OS.cs

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public interface IBackend
1717
void SetupApp(AppBuilder builder);
1818
void SetupWindow(Window window);
1919

20+
string GetDataDir();
2021
string FindGitExecutable();
2122
string FindTerminal(Models.ShellOrTerminal shell);
2223
List<Models.ExternalTool> FindExternalTools();
@@ -132,47 +133,18 @@ static OS()
132133
}
133134
}
134135

135-
public static void SetupApp(AppBuilder builder)
136-
{
137-
_backend.SetupApp(builder);
138-
}
139-
140136
public static void SetupDataDir()
141137
{
142-
if (OperatingSystem.IsWindows())
143-
{
144-
var execFile = Process.GetCurrentProcess().MainModule!.FileName;
145-
var portableDir = Path.Combine(Path.GetDirectoryName(execFile)!, "data");
146-
if (Directory.Exists(portableDir))
147-
{
148-
DataDir = portableDir;
149-
return;
150-
}
151-
}
152-
else if (OperatingSystem.IsLinux())
153-
{
154-
var appImage = Environment.GetEnvironmentVariable("APPIMAGE");
155-
if (!string.IsNullOrEmpty(appImage) && File.Exists(appImage))
156-
{
157-
var portableDir = Path.Combine(Path.GetDirectoryName(appImage)!, "data");
158-
if (Directory.Exists(portableDir))
159-
{
160-
DataDir = portableDir;
161-
return;
162-
}
163-
}
164-
}
165-
166-
var osAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
167-
if (string.IsNullOrEmpty(osAppDataDir))
168-
DataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".sourcegit");
169-
else
170-
DataDir = Path.Combine(osAppDataDir, "SourceGit");
171-
138+
DataDir = _backend.GetDataDir();
172139
if (!Directory.Exists(DataDir))
173140
Directory.CreateDirectory(DataDir);
174141
}
175142

143+
public static void SetupApp(AppBuilder builder)
144+
{
145+
_backend.SetupApp(builder);
146+
}
147+
176148
public static void SetupExternalTools()
177149
{
178150
ExternalTools = _backend.FindExternalTools();

src/Native/Windows.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ public void SetupWindow(Window window)
114114
});
115115
}
116116

117+
public string GetDataDir()
118+
{
119+
var execFile = Process.GetCurrentProcess().MainModule!.FileName;
120+
var portableDir = Path.Combine(Path.GetDirectoryName(execFile)!, "data");
121+
if (Directory.Exists(portableDir))
122+
return portableDir;
123+
124+
return Path.Combine(
125+
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
126+
"SourceGit");
127+
}
128+
117129
public string FindGitExecutable()
118130
{
119131
var reg = Microsoft.Win32.RegistryKey.OpenBaseKey(

0 commit comments

Comments
 (0)