Skip to content

Commit bd15988

Browse files
committed
Show Difference fails with file names with spaces (close #38)
1 parent 0ac4721 commit bd15988

File tree

1 file changed

+53
-43
lines changed

1 file changed

+53
-43
lines changed

GitDiffMargin/Git/GitCommands.cs

+53-43
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using EnvDTE;
77
using LibGit2Sharp;
88
using Microsoft.VisualStudio.Text;
9-
using Process = System.Diagnostics.Process;
109

1110
namespace GitDiffMargin.Git
1211
{
@@ -112,19 +111,19 @@ public void StartExternalDiff(ITextDocument textDocument)
112111
var diffCmd = repo.Config.Get<string>("difftool." + diffGuiTool.Value + ".cmd");
113112
var cmd = diffCmd.Value.Replace("$LOCAL", tempFileName).Replace("$REMOTE", filename);
114113

115-
string exe;
116-
var args = CommandLineToArgs(cmd, out exe);
117-
118-
var process = new Process
119-
{
120-
StartInfo =
121-
{
122-
FileName = exe,
123-
Arguments = string.Join(" ", args)
124-
}
125-
};
126-
127-
process.Start();
114+
var si = new STARTUPINFO();
115+
PROCESS_INFORMATION pi;
116+
CreateProcess(
117+
null,
118+
cmd,
119+
IntPtr.Zero,
120+
IntPtr.Zero,
121+
false,
122+
0,
123+
IntPtr.Zero,
124+
null,
125+
ref si,
126+
out pi);
128127
}
129128
}
130129

@@ -148,36 +147,47 @@ public string GetGitRepository(string filePath)
148147
return directoryInfo != null ? directoryInfo.FullName : null;
149148
}
150149

151-
[DllImport("shell32.dll", SetLastError = true)]
152-
static extern IntPtr CommandLineToArgvW(
153-
154-
[MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine, out int pNumArgs);
155-
private static string[] CommandLineToArgs(string commandLine, out string executableName)
150+
[DllImport("kernel32.dll")]
151+
static extern bool CreateProcess(
152+
string lpApplicationName,
153+
string lpCommandLine,
154+
IntPtr lpProcessAttributes,
155+
IntPtr lpThreadAttributes,
156+
bool bInheritHandles,
157+
uint dwCreationFlags,
158+
IntPtr lpEnvironment,
159+
string lpCurrentDirectory,
160+
ref STARTUPINFO lpStartupInfo,
161+
out PROCESS_INFORMATION lpProcessInformation);
162+
163+
public struct PROCESS_INFORMATION
156164
{
157-
int argCount;
158-
var result = CommandLineToArgvW(commandLine, out argCount);
159-
if (result == IntPtr.Zero)
160-
{
161-
throw new System.ComponentModel.Win32Exception();
162-
}
163-
164-
try
165-
{
166-
var pStr = Marshal.ReadIntPtr(result, 0 * IntPtr.Size);
167-
executableName = Marshal.PtrToStringUni(pStr);
168-
var args = new string[argCount - 1];
169-
for (var i = 0; i < args.Length; i++)
170-
{
171-
pStr = Marshal.ReadIntPtr(result, (i + 1) * IntPtr.Size);
172-
var arg = Marshal.PtrToStringUni(pStr);
173-
args[i] = arg;
174-
}
175-
return args;
176-
}
177-
finally
178-
{
179-
Marshal.FreeHGlobal(result);
180-
}
165+
public IntPtr hProcess;
166+
public IntPtr hThread;
167+
public uint dwProcessId;
168+
public uint dwThreadId;
169+
}
170+
171+
public struct STARTUPINFO
172+
{
173+
public uint cb;
174+
public string lpReserved;
175+
public string lpDesktop;
176+
public string lpTitle;
177+
public uint dwX;
178+
public uint dwY;
179+
public uint dwXSize;
180+
public uint dwYSize;
181+
public uint dwXCountChars;
182+
public uint dwYCountChars;
183+
public uint dwFillAttribute;
184+
public uint dwFlags;
185+
public short wShowWindow;
186+
public short cbReserved2;
187+
public IntPtr lpReserved2;
188+
public IntPtr hStdInput;
189+
public IntPtr hStdOutput;
190+
public IntPtr hStdError;
181191
}
182192
}
183193
}

0 commit comments

Comments
 (0)