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 3, 2018
1 parent 8dba3b4 commit 88823d9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 17 deletions.
22 changes: 15 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,20 @@ 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>())
{
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))
{
string value = processInfo.EnvironmentVariables[key];

if (value.IndexOfAny(Path.GetInvalidPathChars()) >= 0
|| !Path.IsPathRooted(processInfo.EnvironmentVariables[key]))
{
processInfo.EnvironmentVariables.Remove(key);
}
}
}

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

public class Result
{
public const int SuccessCode = 0;
Expand Down
55 changes: 49 additions & 6 deletions GVFS/GVFS.FunctionalTests/Tests/GitCommands/GitCommandsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using GVFS.Tests.Should;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;

Expand All @@ -23,7 +24,7 @@ public class GitCommandsTests : GitRepoTests
private static readonly string RenameFilePathTo = Path.Combine("GVFS", "GVFS.Common", "Physical", "FileSystem", "FileProperties2.cs");
private static readonly string RenameFolderPathFrom = Path.Combine("GVFS", "GVFS.Common", "PrefetchPacks");
private static readonly string RenameFolderPathTo = Path.Combine("GVFS", "GVFS.Common", "PrefetchPacksRenamed");

public GitCommandsTests() : base(enlistmentPerTest: false)
{
}
Expand All @@ -45,6 +46,48 @@ public void StatusTest()
this.ValidateGitCommand("status");
}

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

string tracePath = Path.Combine(this.Enlistment.EnlistmentRoot, "trace-log.txt");
environmentVariables["GIT_TRACE"] = tracePath;
this.ValidateGitCommand(environmentVariables, "status");

this.FileSystem.FileExists(tracePath).ShouldBeTrue();

string lineToFind = "trace: built-in: git status";
int count = 0;

foreach (string line in this.FileSystem.ReadAllText(tracePath).Split('\n'))
{
if (line.IndexOf(lineToFind) >= 0)
{
count++;
}
}

// The GIT_TRACE output is appended to the file, so we should have
// one copy for each run: normal and virtualized.
count.ShouldEqual(2);
}

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

if (Path.GetInvalidPathChars().Length == 0)
{
return;
}

string tracePath = Path.Combine(this.Enlistment.EnlistmentRoot, $"trace-log{Path.GetInvalidPathChars()[0]}.txt");
environmentVariables["GIT_TRACE"] = tracePath;
this.ValidateGitCommand(environmentVariables, "status");
}

[TestCase]
public void StatusShortTest()
{
Expand Down Expand Up @@ -263,27 +306,27 @@ public void RenameFilesWithNameAheadOfDot()
{
this.FolderShouldExistAndHaveFile("GitCommandsTests", "RenameFileTests", "1", "#test");
this.MoveFile(
Path.Combine("GitCommandsTests", "RenameFileTests", "1", "#test"),
Path.Combine("GitCommandsTests", "RenameFileTests", "1", "#test"),
Path.Combine("GitCommandsTests", "RenameFileTests", "1", "#testRenamed"));

this.FolderShouldExistAndHaveFile("GitCommandsTests", "RenameFileTests", "2", "$test");
this.MoveFile(
Path.Combine("GitCommandsTests", "RenameFileTests", "2", "$test"),
Path.Combine("GitCommandsTests", "RenameFileTests", "2", "$test"),
Path.Combine("GitCommandsTests", "RenameFileTests", "2", "$testRenamed"));

this.FolderShouldExistAndHaveFile("GitCommandsTests", "RenameFileTests", "3", ")");
this.MoveFile(
Path.Combine("GitCommandsTests", "RenameFileTests", "3", ")"),
Path.Combine("GitCommandsTests", "RenameFileTests", "3", ")"),
Path.Combine("GitCommandsTests", "RenameFileTests", "3", ")Renamed"));

this.FolderShouldExistAndHaveFile("GitCommandsTests", "RenameFileTests", "4", "+.test");
this.MoveFile(
Path.Combine("GitCommandsTests", "RenameFileTests", "4", "+.test"),
Path.Combine("GitCommandsTests", "RenameFileTests", "4", "+.test"),
Path.Combine("GitCommandsTests", "RenameFileTests", "4", "+.testRenamed"));

this.FolderShouldExistAndHaveFile("GitCommandsTests", "RenameFileTests", "5", "-.test");
this.MoveFile(
Path.Combine("GitCommandsTests", "RenameFileTests", "5", "-.test"),
Path.Combine("GitCommandsTests", "RenameFileTests", "5", "-.test"),
Path.Combine("GitCommandsTests", "RenameFileTests", "5", "-.testRenamed"));

this.ValidateGitCommand("status");
Expand Down
13 changes: 11 additions & 2 deletions GVFS/GVFS.FunctionalTests/Tests/GitCommands/GitRepoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
using GVFS.FunctionalTests.Tools;
using GVFS.Tests.Should;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -182,6 +181,16 @@ protected void ValidateGitCommand(string command, params object[] args)
args);
}

protected void ValidateGitCommand(Dictionary<string, string> environmentVariables, string command, params object[] args)
{
GitHelpers.ValidateGitCommand(
this.Enlistment,
this.ControlGitRepo,
environmentVariables,
command,
args);
}

protected void CreateEmptyFile()
{
string filePath = Path.GetRandomFileName() + "emptyFile.txt";
Expand Down
14 changes: 12 additions & 2 deletions GVFS/GVFS.FunctionalTests/Tools/GitHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,23 @@ public static void ValidateGitCommand(
ControlGitRepo controlGitRepo,
string command,
params object[] args)
{
ValidateGitCommand(enlistment, controlGitRepo, null, command, args);
}

public static void ValidateGitCommand(
GVFSFunctionalTestEnlistment enlistment,
ControlGitRepo controlGitRepo,
Dictionary<string, string> environmentVariables,
string command,
params object[] args)
{
command = string.Format(command, args);
string controlRepoRoot = controlGitRepo.RootPath;
string gvfsRepoRoot = enlistment.RepoRoot;

ProcessResult expectedResult = GitProcess.InvokeProcess(controlRepoRoot, command);
ProcessResult actualResult = GitHelpers.InvokeGitAgainstGVFSRepo(gvfsRepoRoot, command);
ProcessResult expectedResult = GitProcess.InvokeProcess(controlRepoRoot, command, environmentVariables);
ProcessResult actualResult = GitHelpers.InvokeGitAgainstGVFSRepo(gvfsRepoRoot, command, environmentVariables);

ErrorsShouldMatch(command, expectedResult, actualResult);
actualResult.Output.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
Expand Down

0 comments on commit 88823d9

Please sign in to comment.