Skip to content

Commit

Permalink
GitProcess: allow GIT_TRACE to point to full path
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickstolee committed Oct 5, 2018
1 parent 8dba3b4 commit 3a5009f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
26 changes: 19 additions & 7 deletions GVFS/GVFS.Common/Git/GitProcess.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using GVFS.Common.FileSystem;
using GVFS.Common.Tracing;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -149,7 +148,7 @@ public bool IsValidRepo()
Result result = this.InvokeGitAgainstDotGitFolder("rev-parse --show-toplevel");
return !result.HasErrors;
}

public Result RevParse(string gitRef)
{
return this.InvokeGitAgainstDotGitFolder("rev-parse " + gitRef);
Expand Down Expand Up @@ -405,11 +404,24 @@ public Process GetGitProcess(string command, string workingDirectory, string dot

// Removing trace variables that might change git output and break parsing
// List of environment variables: https://git-scm.com/book/gr/v2/Git-Internals-Environment-Variables
foreach (string key in processInfo.EnvironmentVariables.Keys.Cast<string>()
.Where(x => x.StartsWith("GIT_TRACE", StringComparison.OrdinalIgnoreCase))
.ToList())
foreach (string key in processInfo.EnvironmentVariables.Keys.Cast<string>().ToList())
{
processInfo.EnvironmentVariables.Remove(key);
// If GIT_TRACE is set to a fully-rooted path, then Git sends the trace
// output to that path instead of stdout (GIT_TRACE=1) or stderr (GIT_TRACE=2).
if (key.StartsWith("GIT_TRACE", StringComparison.OrdinalIgnoreCase))
{
try
{
if (!Path.IsPathRooted(processInfo.EnvironmentVariables[key]))
{
processInfo.EnvironmentVariables.Remove(key);
}
}
catch (ArgumentException)
{
processInfo.EnvironmentVariables.Remove(key);
}
}
}

processInfo.EnvironmentVariables["GIT_TERMINAL_PROMPT"] = "0";
Expand Down Expand Up @@ -601,7 +613,7 @@ private Result InvokeGitAgainstDotGitFolder(
parseStdOutLine: parseStdOutLine,
timeoutMs: -1);
}

public class Result
{
public const int SuccessCode = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using GVFS.FunctionalTests.FileSystemRunners;
using GVFS.Tests.Should;
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;

namespace GVFS.FunctionalTests.Tests.EnlistmentPerFixture
{
[TestFixture]
public class StatusVerbTests : TestsWithEnlistmentPerFixture
{
private enum ExpectedReturnCode
{
Success = 0,
ParsingError = 1,
}

[TestCase]
public void GitTrace()
{
Dictionary<string, string> environmentVariables = new Dictionary<string, string>();

this.Enlistment.Status(trace: "1");
this.Enlistment.Status(trace: "2");

string logPath = Path.Combine(this.Enlistment.RepoRoot, "log-file.txt");
this.Enlistment.Status(trace: logPath);

FileSystemRunner fileSystem = new SystemIORunner();
fileSystem.FileExists(logPath).ShouldBeTrue();
string.IsNullOrWhiteSpace(fileSystem.ReadAllText(logPath)).ShouldBeFalse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ public string Diagnose()
return this.gvfsProcess.Diagnose();
}

public string Status()
public string Status(string trace = null)
{
return this.gvfsProcess.Status();
return this.gvfsProcess.Status(trace);
}

public bool WaitForBackgroundOperations(int maxWaitMilliseconds = DefaultMaxWaitMSForStatusCheck)
Expand Down
17 changes: 11 additions & 6 deletions GVFS/GVFS.FunctionalTests/Tools/GVFSProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public class GVFSProcess
private readonly string pathToGVFS;
private readonly string enlistmentRoot;
private readonly string localCacheRoot;

public GVFSProcess(string pathToGVFS, string enlistmentRoot, string localCacheRoot)
{
this.pathToGVFS = pathToGVFS;
this.enlistmentRoot = enlistmentRoot;
this.localCacheRoot = localCacheRoot;
}

public void Clone(string repositorySource, string branchToCheckout, bool skipPrefetch)
{
string args = string.Format(
Expand Down Expand Up @@ -50,7 +50,7 @@ public string Prefetch(string args, bool failOnError)
public void Repair()
{
this.CallGVFS(
"repair --confirm \"" + this.enlistmentRoot + "\"",
"repair --confirm \"" + this.enlistmentRoot + "\"",
failOnError: true);
}

Expand All @@ -59,9 +59,9 @@ public string Diagnose()
return this.CallGVFS("diagnose \"" + this.enlistmentRoot + "\"");
}

public string Status()
public string Status(string trace = null)
{
return this.CallGVFS("status " + this.enlistmentRoot);
return this.CallGVFS("status " + this.enlistmentRoot, trace: trace);
}

public string CacheServer(string args)
Expand Down Expand Up @@ -89,7 +89,7 @@ public string RunServiceVerb(string argument)
return this.CallGVFS("service " + argument, failOnError: true);
}

private string CallGVFS(string args, bool failOnError = false)
private string CallGVFS(string args, bool failOnError = false, string trace = null)
{
ProcessStartInfo processInfo = null;
processInfo = new ProcessStartInfo(this.pathToGVFS);
Expand All @@ -99,6 +99,11 @@ private string CallGVFS(string args, bool failOnError = false)
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;

if (trace != null)
{
processInfo.EnvironmentVariables["GIT_TRACE"] = trace;
}

using (Process process = Process.Start(processInfo))
{
string result = process.StandardOutput.ReadToEnd();
Expand Down

0 comments on commit 3a5009f

Please sign in to comment.