Skip to content

Commit bb221bc

Browse files
authored
Fix handling worktree paths with spaces (#1)
* get worktree command also need to accept spaces * soution opening cmd also given normalised path
1 parent aa2bb63 commit bb221bc

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

GitWorkTree/Helpers/GitHelper.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ private static async Task<bool> ExecuteAsync(GitCommandArgs gitCommandArgs, Acti
102102
}
103103
}
104104

105-
106-
107105
public static async Task<List<string>> GetWorkTreePathsAsync(string repositoryPath)
108106
{
109107
List<string> workTreePaths = new List<string>();
@@ -113,19 +111,23 @@ public static async Task<List<string>> GetWorkTreePathsAsync(string repositoryPa
113111
Argument = "worktree list --porcelain",
114112
}, (line) =>
115113
{
116-
if (line.StartsWith("worktree"))
114+
if (line.StartsWith("worktree "))
117115
{
118-
string worktreePath = line.Split(' ').ElementAtOrDefault(1);
119-
string mainRepoPath = Path.GetFullPath(repositoryPath);
120-
121-
if (!Path.GetFullPath(worktreePath).Equals(mainRepoPath, StringComparison.OrdinalIgnoreCase))
116+
// everything after "worktree " is the path (can contain spaces)
117+
string rawPath = line.Substring("worktree ".Length).Trim();
118+
if (!string.IsNullOrEmpty(rawPath))
122119
{
123-
workTreePaths.Add(worktreePath);
120+
string worktreePath = Path.GetFullPath(rawPath);
121+
string mainRepoPath = Path.GetFullPath(repositoryPath);
122+
123+
if (!worktreePath.Equals(mainRepoPath, StringComparison.OrdinalIgnoreCase))
124+
{
125+
workTreePaths.Add(worktreePath);
126+
}
124127
}
125128
}
126129
});
127-
if (isCompleted) return workTreePaths;
128-
else return null;
130+
return isCompleted ? workTreePaths : null;
129131
}
130132

131133
public static async Task<List<string>> GetBranchesAsync(string repositoryPath)
@@ -153,7 +155,7 @@ public static async Task<bool> CreateWorkTreeAsync
153155
string force = shouldForceCreate ? "-f " : "";
154156
return await ExecuteAsync(new GitCommandArgs()
155157
{
156-
Argument = $"worktree add {force}{workTreePath} {branchName.ToGitCommandExecutableFormat()}",
158+
Argument = $"worktree add {force}{SolutionHelper.NormalizePath(workTreePath)} {branchName.ToGitCommandExecutableFormat()}",
157159
WorkingDirectory = repositoryPath
158160
}, (line) =>
159161
{
@@ -166,7 +168,7 @@ public static async Task<bool> RemoveWorkTreeAsync(string repositoryPath, string
166168
string force = shouldForceCreate ? "-f " : "";
167169
return await ExecuteAsync(new GitCommandArgs()
168170
{
169-
Argument = $"worktree remove {force}{workTreePath}",
171+
Argument = $"worktree remove {force}{SolutionHelper.NormalizePath(workTreePath)}",
170172
WorkingDirectory = repositoryPath
171173
}, (line) =>
172174
{
@@ -190,13 +192,13 @@ public static async Task<string> GetGitFolderDirectoryAsync(string currentSoluti
190192
{
191193
string commandoutput = "";
192194
var isCompleted = await ExecuteAsync(new GitCommandArgs() { WorkingDirectory = currentSolutionPath, Argument = "rev-parse --git-dir", },
193-
(line) =>
195+
(line) =>
196+
{
197+
if (!string.IsNullOrWhiteSpace(line))
194198
{
195-
if (!string.IsNullOrWhiteSpace(line))
196-
{
197-
commandoutput = line.Trim();
198-
}
199-
});
199+
commandoutput = line.Trim();
200+
}
201+
});
200202

201203
if (isCompleted) return commandoutput;
202204
return null;

GitWorkTree/Helpers/SolutionHelper.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
using Microsoft.VisualStudio;
22
using Microsoft.VisualStudio.Shell.Interop;
33
using System.IO;
4+
using System.Linq;
45
using System.Threading.Tasks;
56

67
namespace GitWorkTree.Helpers
78
{
89
public static class SolutionHelper
910
{
1011
static LoggingHelper outputWindow = LoggingHelper.Instance;
12+
public static string NormalizePath(string path) =>
13+
string.IsNullOrWhiteSpace(path) ? "\"\"" :
14+
((path = path.Trim().Trim('"').TrimEnd('\r', '\n'))
15+
.Any(char.IsWhiteSpace) ? $"\"{path}\"" : path);
1116
public static string GetRepositoryPath(string solutionPath)
1217
{
1318
try
@@ -76,7 +81,7 @@ private static async Task<bool> HandleOpenSolution(string newSolutionPath, bool
7681
private static bool OpenSolutionInNewInstance(string newSolutionPath, string[] solutionFiles)
7782
{
7883
outputWindow?.WriteToOutputWindowAsync($"Opening {newSolutionPath} in new VS instance", true);
79-
System.Diagnostics.Process.Start("devenv.exe", solutionFiles[0]);
84+
System.Diagnostics.Process.Start("devenv.exe", NormalizePath(solutionFiles[0]));
8085
return true;
8186
}
8287

0 commit comments

Comments
 (0)