Browse Source

Avoid using git.cmd: call git.exe directly instead. This avoids issues with cmd echo output sometimes appearing in git output.

pull/15/head
Daniel Grunwald 15 years ago
parent
commit
856c4987cd
  1. 5
      src/AddIns/VersionControl/GitAddIn/GitAddIn.csproj
  2. 38
      src/AddIns/VersionControl/GitAddIn/Src/Git.cs
  3. 28
      src/AddIns/VersionControl/GitAddIn/Src/GitStatusCache.cs
  4. 38
      src/AddIns/VersionControl/GitAddIn/Src/GitVersionProvider.cs
  5. 4
      src/AddIns/VersionControl/GitAddIn/Src/OverlayIconManager.cs

5
src/AddIns/VersionControl/GitAddIn/GitAddIn.csproj

@ -13,7 +13,6 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors> <TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<TargetFrameworkVersion Condition=" '$(TargetFrameworkVersion)' == '' ">v4.0</TargetFrameworkVersion> <TargetFrameworkVersion Condition=" '$(TargetFrameworkVersion)' == '' ">v4.0</TargetFrameworkVersion>
<DefineConstants Condition="'$(TargetFrameworkVersion)' == 'v4.0' ">SD4</DefineConstants>
<TargetFrameworkProfile> <TargetFrameworkProfile>
</TargetFrameworkProfile> </TargetFrameworkProfile>
</PropertyGroup> </PropertyGroup>
@ -21,7 +20,7 @@
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>Full</DebugType> <DebugType>Full</DebugType>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow> <CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<DefineConstants>$(DefineConstants);DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<Optimize>False</Optimize> <Optimize>False</Optimize>
<StartAction>Program</StartAction> <StartAction>Program</StartAction>
<StartProgram>..\..\..\..\bin\SharpDevelop.exe</StartProgram> <StartProgram>..\..\..\..\bin\SharpDevelop.exe</StartProgram>
@ -30,7 +29,7 @@
<DebugSymbols>False</DebugSymbols> <DebugSymbols>False</DebugSymbols>
<DebugType>None</DebugType> <DebugType>None</DebugType>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow> <CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>$(DefineConstants);TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' "> <PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<RegisterForComInterop>False</RegisterForComInterop> <RegisterForComInterop>False</RegisterForComInterop>

38
src/AddIns/VersionControl/GitAddIn/Src/Git.cs

@ -71,6 +71,11 @@ namespace ICSharpCode.GitAddIn
public static void RunGit(string workingDir, string arguments, Action<int> finished) public static void RunGit(string workingDir, string arguments, Action<int> finished)
{ {
GitMessageView.AppendLine(workingDir + "> git " + arguments); GitMessageView.AppendLine(workingDir + "> git " + arguments);
string git = FindGit();
if (git == null) {
GitMessageView.AppendLine("Could not find git.exe");
return;
}
ProcessRunner runner = new ProcessRunner(); ProcessRunner runner = new ProcessRunner();
runner.WorkingDirectory = workingDir; runner.WorkingDirectory = workingDir;
runner.LogStandardOutputAndError = false; runner.LogStandardOutputAndError = false;
@ -82,7 +87,34 @@ namespace ICSharpCode.GitAddIn
if (finished != null) if (finished != null)
finished(runner.ExitCode); finished(runner.ExitCode);
}; };
runner.Start("cmd", "/c git " + arguments); runner.Start(git, arguments);
}
/// <summary>
/// Finds 'git.exe'
/// </summary>
public static string FindGit()
{
string[] paths = Environment.GetEnvironmentVariable("PATH").Split(';');
foreach (string path in paths) {
try {
string exe = Path.Combine(path, "git.exe");
if (File.Exists(exe))
return exe;
string cmd = Path.Combine(path, "git.cmd");
if (File.Exists(cmd)) {
exe = Path.Combine(path, "..\\bin\\git.exe");
if (File.Exists(exe))
return exe;
}
} catch (ArgumentException) {
// ignore invalid entries in PATH
}
}
string gitExe = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86, Environment.SpecialFolderOption.DoNotVerify), "bin\\git.exe");
if (File.Exists(gitExe))
return gitExe;
return null;
} }
/* /*
@ -103,7 +135,9 @@ namespace ICSharpCode.GitAddIn
runner.Kill(); runner.Kill();
}; };
runner.WorkingDirectory = workingDir; runner.WorkingDirectory = workingDir;
runner.Start("git", arguments); string git = FindGit();
if (git == null) ...
runner.Start(git, arguments);
runner.WaitForExit(); runner.WaitForExit();
if (runner.ExitCode == 0) { if (runner.ExitCode == 0) {
return runner.StandardOutput; return runner.StandardOutput;

28
src/AddIns/VersionControl/GitAddIn/Src/GitStatusCache.cs

@ -63,6 +63,10 @@ namespace ICSharpCode.GitAddIn
static void GitGetFiles(string wcRoot, GitStatusSet statusSet) static void GitGetFiles(string wcRoot, GitStatusSet statusSet)
{ {
string git = Git.FindGit();
if (git == null)
return;
ProcessRunner runner = new ProcessRunner(); ProcessRunner runner = new ProcessRunner();
runner.WorkingDirectory = wcRoot; runner.WorkingDirectory = wcRoot;
runner.LogStandardOutputAndError = false; runner.LogStandardOutputAndError = false;
@ -72,41 +76,41 @@ namespace ICSharpCode.GitAddIn
} }
}; };
string command = "git ls-files"; string command = "ls-files";
bool hasErrors = false; bool hasErrors = false;
runner.ErrorLineReceived += delegate(object sender, LineReceivedEventArgs e) { runner.ErrorLineReceived += delegate(object sender, LineReceivedEventArgs e) {
if (!hasErrors) { if (!hasErrors) {
hasErrors = true; hasErrors = true;
GitMessageView.AppendLine(runner.WorkingDirectory + "> " + command); GitMessageView.AppendLine(runner.WorkingDirectory + "> git " + command);
} }
GitMessageView.AppendLine(e.Line); GitMessageView.AppendLine(e.Line);
}; };
runner.Start("cmd", "/c " + command); runner.Start(git, command);
runner.WaitForExit(); runner.WaitForExit();
} }
static void GitGetStatus(string wcRoot, GitStatusSet statusSet) static void GitGetStatus(string wcRoot, GitStatusSet statusSet)
{ {
string command = "git status --porcelain --untracked-files=no"; string git = Git.FindGit();
if (git == null)
return;
string command = "status --porcelain --untracked-files=no";
bool hasErrors = false; bool hasErrors = false;
ProcessRunner runner = new ProcessRunner(); ProcessRunner runner = new ProcessRunner();
runner.WorkingDirectory = wcRoot; runner.WorkingDirectory = wcRoot;
runner.LogStandardOutputAndError = false; runner.LogStandardOutputAndError = false;
string commandPrompt = wcRoot + ">@"; // C:\work\SD>@git.exe %*
runner.OutputLineReceived += delegate(object sender, LineReceivedEventArgs e) { runner.OutputLineReceived += delegate(object sender, LineReceivedEventArgs e) {
if (!string.IsNullOrEmpty(e.Line)) { if (!string.IsNullOrEmpty(e.Line)) {
Match m = statusParseRegex.Match(e.Line); Match m = statusParseRegex.Match(e.Line);
if (m.Success) { if (m.Success) {
statusSet.AddEntry(m.Groups[2].Value, StatusFromText(m.Groups[1].Value)); statusSet.AddEntry(m.Groups[2].Value, StatusFromText(m.Groups[1].Value));
} else if (!e.Line.StartsWith(commandPrompt, StringComparison.Ordinal)) { } else {
// Suppress "unknown output" produced by git.cmd when git is installed
// in the PATH but the git unix tools aren't
if (!hasErrors) { if (!hasErrors) {
// in front of first output line, print the command line we invoked // in front of first output line, print the command line we invoked
hasErrors = true; hasErrors = true;
GitMessageView.AppendLine(runner.WorkingDirectory + "> " + command); GitMessageView.AppendLine(runner.WorkingDirectory + "> git " + command);
} }
GitMessageView.AppendLine("unknown output: " + e.Line); GitMessageView.AppendLine("unknown output: " + e.Line);
} }
@ -115,11 +119,11 @@ namespace ICSharpCode.GitAddIn
runner.ErrorLineReceived += delegate(object sender, LineReceivedEventArgs e) { runner.ErrorLineReceived += delegate(object sender, LineReceivedEventArgs e) {
if (!hasErrors) { if (!hasErrors) {
hasErrors = true; hasErrors = true;
GitMessageView.AppendLine(runner.WorkingDirectory + "> " + command); GitMessageView.AppendLine(runner.WorkingDirectory + "> git " + command);
} }
GitMessageView.AppendLine(e.Line); GitMessageView.AppendLine(e.Line);
}; };
runner.Start("cmd", "/c " + command); runner.Start(git, command);
runner.WaitForExit(); runner.WaitForExit();
} }

38
src/AddIns/VersionControl/GitAddIn/Src/GitVersionProvider.cs

@ -2,6 +2,7 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System; using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.Pipes; using System.IO.Pipes;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -77,26 +78,33 @@ namespace ICSharpCode.GitAddIn
if (!Git.IsInWorkingCopy(fileName)) if (!Git.IsInWorkingCopy(fileName))
return null; return null;
return OpenOutput(fileName, GetBlobHash(fileName)); string git = Git.FindGit();
if (git == null)
return null;
return OpenOutput(git, fileName, GetBlobHash(git, fileName));
} }
string GetBlobHash(string fileName) string GetBlobHash(string gitExe, string fileName)
{ {
ProcessRunner runner = new ProcessRunner(); ProcessRunner runner = new ProcessRunner();
runner.WorkingDirectory = Path.GetDirectoryName(fileName); runner.WorkingDirectory = Path.GetDirectoryName(fileName);
runner.Start("cmd", "/c git ls-tree HEAD " + Path.GetFileName(fileName)); runner.Start(gitExe, "ls-tree HEAD " + Path.GetFileName(fileName));
runner.WaitForExit();
string output = runner.StandardOutput.Trim(); string blobHash = null;
string[] parts = output.Split(new[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); runner.OutputLineReceived += delegate(object sender, LineReceivedEventArgs e) {
string[] parts = e.Line.Split(new[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 3) {
if (parts[2].Length == 40)
blobHash = parts[2];
}
};
if (parts.Length < 3) runner.WaitForExit();
return null; return blobHash;
return parts[2];
} }
Stream OpenOutput(string fileName, string blobHash) Stream OpenOutput(string gitExe, string fileName, string blobHash)
{ {
if (blobHash == null) if (blobHash == null)
return null; return null;
@ -112,8 +120,12 @@ namespace ICSharpCode.GitAddIn
PROCESS_INFORMATION procInfo; PROCESS_INFORMATION procInfo;
if (!CreateProcess(null, string.Format("cmd /c git cat-file blob {0}", blobHash), string commandLine = "\"" + gitExe + "\" cat-file blob " + blobHash;
IntPtr.Zero, IntPtr.Zero, true, 0, IntPtr.Zero, Path.GetDirectoryName(fileName), ref startupInfo, string workingDir = Path.GetDirectoryName(fileName);
Debug.WriteLine(workingDir + "> " + commandLine);
const uint CREATE_NO_WINDOW = 0x08000000;
if (!CreateProcess(null, commandLine,
IntPtr.Zero, IntPtr.Zero, true, CREATE_NO_WINDOW, IntPtr.Zero, workingDir, ref startupInfo,
out procInfo)) { out procInfo)) {
pipe.DisposeLocalCopyOfClientHandle(); pipe.DisposeLocalCopyOfClientHandle();
pipe.Close(); pipe.Close();

4
src/AddIns/VersionControl/GitAddIn/Src/OverlayIconManager.cs

@ -103,11 +103,7 @@ namespace ICSharpCode.GitAddIn
try { try {
RunStep(node); RunStep(node);
} catch (Exception ex) { } catch (Exception ex) {
#if SD4
MessageService.ShowException(ex); MessageService.ShowException(ex);
#else
MessageService.ShowError(ex);
#endif
} }
} }
} }

Loading…
Cancel
Save