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

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

@ -71,6 +71,11 @@ namespace ICSharpCode.GitAddIn @@ -71,6 +71,11 @@ namespace ICSharpCode.GitAddIn
public static void RunGit(string workingDir, string arguments, Action<int> finished)
{
GitMessageView.AppendLine(workingDir + "> git " + arguments);
string git = FindGit();
if (git == null) {
GitMessageView.AppendLine("Could not find git.exe");
return;
}
ProcessRunner runner = new ProcessRunner();
runner.WorkingDirectory = workingDir;
runner.LogStandardOutputAndError = false;
@ -82,7 +87,34 @@ namespace ICSharpCode.GitAddIn @@ -82,7 +87,34 @@ namespace ICSharpCode.GitAddIn
if (finished != null)
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 @@ -103,7 +135,9 @@ namespace ICSharpCode.GitAddIn
runner.Kill();
};
runner.WorkingDirectory = workingDir;
runner.Start("git", arguments);
string git = FindGit();
if (git == null) ...
runner.Start(git, arguments);
runner.WaitForExit();
if (runner.ExitCode == 0) {
return runner.StandardOutput;

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

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

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

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

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

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

Loading…
Cancel
Save