fix: run git commands via WSL for Linux paths on Windows

This commit is contained in:
2026-03-05 14:32:22 -08:00
committed by Naomi Carrigan
parent 9af61a4a29
commit 8972194a29
+51 -1
View File
@@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};
use std::process::Command;
#[cfg(target_os = "windows")]
use crate::process_ext::HideWindow;
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -37,9 +38,38 @@ pub struct GitLogEntry {
pub message: String,
}
/// Builds the WSL argument list for running a git command at a Linux path.
/// Extracted for testability without requiring WSL to be available.
#[cfg(any(target_os = "windows", test))]
fn build_wsl_git_args<'a>(working_dir: &'a str, args: &[&'a str]) -> Vec<&'a str> {
let mut wsl_args = vec!["--", "git", "-C", working_dir];
wsl_args.extend_from_slice(args);
wsl_args
}
fn run_git_command(working_dir: &str, args: &[&str]) -> Result<String, String> {
#[cfg(target_os = "windows")]
let output = {
if working_dir.starts_with('/') {
// WSL/Linux path — run git through WSL so it can resolve the path correctly.
let wsl_args = build_wsl_git_args(working_dir, args);
Command::new("wsl")
.hide_window()
.args(&wsl_args)
.output()
.map_err(|e| format!("Failed to execute git via WSL: {}", e))?
} else {
Command::new("git")
.hide_window()
.args(args)
.current_dir(working_dir)
.output()
.map_err(|e| format!("Failed to execute git: {}", e))?
}
};
#[cfg(not(target_os = "windows"))]
let output = Command::new("git")
.hide_window()
.args(args)
.current_dir(working_dir)
.output()
@@ -297,6 +327,26 @@ mod tests {
use std::io::Write;
use tempfile::TempDir;
// ==================== build_wsl_git_args tests ====================
#[test]
fn test_build_wsl_git_args_structure() {
let args = build_wsl_git_args("/home/naomi/code/project", &["status", "--porcelain=v1"]);
assert_eq!(args[0], "--");
assert_eq!(args[1], "git");
assert_eq!(args[2], "-C");
assert_eq!(args[3], "/home/naomi/code/project");
assert_eq!(args[4], "status");
assert_eq!(args[5], "--porcelain=v1");
assert_eq!(args.len(), 6);
}
#[test]
fn test_build_wsl_git_args_no_extra_args() {
let args = build_wsl_git_args("/home/user/repo", &["init"]);
assert_eq!(args, vec!["--", "git", "-C", "/home/user/repo", "init"]);
}
// Helper to create a git repository in a temp directory
fn create_test_repo() -> TempDir {
let temp_dir = TempDir::new().unwrap();