diff --git a/GitWorkTree/Helpers/GitHelper.cs b/GitWorkTree/Helpers/GitHelper.cs index 5ff87b0..bd5d3fe 100644 --- a/GitWorkTree/Helpers/GitHelper.cs +++ b/GitWorkTree/Helpers/GitHelper.cs @@ -102,8 +102,6 @@ private static async Task ExecuteAsync(GitCommandArgs gitCommandArgs, Acti } } - - public static async Task> GetWorkTreePathsAsync(string repositoryPath) { List workTreePaths = new List(); @@ -113,19 +111,23 @@ public static async Task> GetWorkTreePathsAsync(string repositoryPa Argument = "worktree list --porcelain", }, (line) => { - if (line.StartsWith("worktree")) + if (line.StartsWith("worktree ")) { - string worktreePath = line.Split(' ').ElementAtOrDefault(1); - string mainRepoPath = Path.GetFullPath(repositoryPath); - - if (!Path.GetFullPath(worktreePath).Equals(mainRepoPath, StringComparison.OrdinalIgnoreCase)) + // everything after "worktree " is the path (can contain spaces) + string rawPath = line.Substring("worktree ".Length).Trim(); + if (!string.IsNullOrEmpty(rawPath)) { - workTreePaths.Add(worktreePath); + string worktreePath = Path.GetFullPath(rawPath); + string mainRepoPath = Path.GetFullPath(repositoryPath); + + if (!worktreePath.Equals(mainRepoPath, StringComparison.OrdinalIgnoreCase)) + { + workTreePaths.Add(worktreePath); + } } } }); - if (isCompleted) return workTreePaths; - else return null; + return isCompleted ? workTreePaths : null; } public static async Task> GetBranchesAsync(string repositoryPath) @@ -153,7 +155,7 @@ public static async Task CreateWorkTreeAsync string force = shouldForceCreate ? "-f " : ""; return await ExecuteAsync(new GitCommandArgs() { - Argument = $"worktree add {force}{workTreePath} {branchName.ToGitCommandExecutableFormat()}", + Argument = $"worktree add {force}{SolutionHelper.NormalizePath(workTreePath)} {branchName.ToGitCommandExecutableFormat()}", WorkingDirectory = repositoryPath }, (line) => { @@ -166,7 +168,7 @@ public static async Task RemoveWorkTreeAsync(string repositoryPath, string string force = shouldForceCreate ? "-f " : ""; return await ExecuteAsync(new GitCommandArgs() { - Argument = $"worktree remove {force}{workTreePath}", + Argument = $"worktree remove {force}{SolutionHelper.NormalizePath(workTreePath)}", WorkingDirectory = repositoryPath }, (line) => { @@ -190,13 +192,13 @@ public static async Task GetGitFolderDirectoryAsync(string currentSoluti { string commandoutput = ""; var isCompleted = await ExecuteAsync(new GitCommandArgs() { WorkingDirectory = currentSolutionPath, Argument = "rev-parse --git-dir", }, - (line) => + (line) => + { + if (!string.IsNullOrWhiteSpace(line)) { - if (!string.IsNullOrWhiteSpace(line)) - { - commandoutput = line.Trim(); - } - }); + commandoutput = line.Trim(); + } + }); if (isCompleted) return commandoutput; return null; diff --git a/GitWorkTree/Helpers/SolutionHelper.cs b/GitWorkTree/Helpers/SolutionHelper.cs index 54b4494..8b74b37 100644 --- a/GitWorkTree/Helpers/SolutionHelper.cs +++ b/GitWorkTree/Helpers/SolutionHelper.cs @@ -1,6 +1,7 @@ using Microsoft.VisualStudio; using Microsoft.VisualStudio.Shell.Interop; using System.IO; +using System.Linq; using System.Threading.Tasks; namespace GitWorkTree.Helpers @@ -8,6 +9,10 @@ namespace GitWorkTree.Helpers public static class SolutionHelper { static LoggingHelper outputWindow = LoggingHelper.Instance; + public static string NormalizePath(string path) => + string.IsNullOrWhiteSpace(path) ? "\"\"" : + ((path = path.Trim().Trim('"').TrimEnd('\r', '\n')) + .Any(char.IsWhiteSpace) ? $"\"{path}\"" : path); public static string GetRepositoryPath(string solutionPath) { try @@ -76,7 +81,7 @@ private static async Task HandleOpenSolution(string newSolutionPath, bool private static bool OpenSolutionInNewInstance(string newSolutionPath, string[] solutionFiles) { outputWindow?.WriteToOutputWindowAsync($"Opening {newSolutionPath} in new VS instance", true); - System.Diagnostics.Process.Start("devenv.exe", solutionFiles[0]); + System.Diagnostics.Process.Start("devenv.exe", NormalizePath(solutionFiles[0])); return true; }