Skip to content

Commit 98f18aa

Browse files
committed
feature: Add support for WSL git on windows
1 parent e28b75b commit 98f18aa

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/Commands/Command.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,19 @@ private ProcessStartInfo CreateGitStartInfo()
167167
start.Environment.Add("LANG", "C");
168168
start.Environment.Add("LC_ALL", "C");
169169
}
170+
else if (OperatingSystem.IsWindows())
171+
{
172+
// Use WSL git for WSL paths on Windows
173+
var wsl = new Models.WSL() { Path = WorkingDirectory };
174+
if (wsl.IsWSLPath())
175+
{
176+
start.FileName = "wsl";
177+
start.Arguments = $"git {start.Arguments}";
178+
179+
wsl.SetEnvironmentForProcess(start);
180+
selfExecFile = start.Environment["SSH_ASKPASS"];
181+
}
182+
}
170183

171184
// Force using this app as git editor.
172185
switch (Editor)

src/Models/WSL.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Text;
4+
5+
namespace SourceGit.Models
6+
{
7+
public class WSL
8+
{
9+
public string Path { get; set; } = "";
10+
11+
public bool IsWSLPath()
12+
{
13+
return OperatingSystem.IsWindows() && !string.IsNullOrEmpty(Path) &&
14+
(Path.StartsWith("//wsl.localhost/", StringComparison.OrdinalIgnoreCase) ||
15+
Path.StartsWith("//wsl$/", StringComparison.OrdinalIgnoreCase));
16+
}
17+
18+
public void SetEnvironmentForProcess(ProcessStartInfo start)
19+
{
20+
start.Environment.Add("LANG", "C");
21+
start.Environment.Add("LC_ALL", "C");
22+
23+
if (start.Environment.TryGetValue("SSH_ASKPASS", out var askPassPath) && !string.IsNullOrEmpty(askPassPath) && System.IO.Path.IsPathRooted(askPassPath))
24+
{
25+
// Convert Windows path to WSL path
26+
var driveLetter = askPassPath[0].ToString();
27+
start.Environment["SSH_ASKPASS"] = askPassPath
28+
.Replace($"{driveLetter}:\\", $"/mnt/{driveLetter.ToLowerInvariant()}/")
29+
.Replace('\\', '/');
30+
}
31+
32+
var wslEnvirionment = new[] { "SSH_ASKPASS", "SSH_ASKPASS_REQUIRE", "SOURCEGIT_LAUNCH_AS_ASKPASS", "GIT_SSH_COMMAND", "LANG", "LC_ALL" };
33+
var wslEnvBuilder = new StringBuilder();
34+
35+
foreach (string env in wslEnvirionment)
36+
{
37+
if (start.Environment.ContainsKey(env))
38+
wslEnvBuilder.Append($"{env}:");
39+
}
40+
41+
// Forward environment variables for WSL
42+
start.Environment.Add("WSLENV", wslEnvBuilder.ToString().TrimEnd(':'));
43+
}
44+
}
45+
}

src/ViewModels/Repository.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,15 @@ public void StartSearchCommits()
869869

870870
public void SetWatcherEnabled(bool enabled)
871871
{
872-
_watcher?.SetEnabled(enabled);
872+
var wsl = new Models.WSL() { Path = FullPath };
873+
if (wsl.IsWSLPath())
874+
{
875+
_watcher?.MarkBranchDirtyManually();
876+
}
877+
else
878+
{
879+
_watcher?.SetEnabled(enabled);
880+
}
873881
}
874882

875883
public void MarkBranchesDirtyManually()

0 commit comments

Comments
 (0)