Skip to content

Commit

Permalink
Merge branch 'support/3.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeGinnivan committed Jul 26, 2016
2 parents da0cca8 + 9756b9d commit 72ef1cd
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/GitVersionCore.Tests/ExecuteCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ public void SetUp()
fileSystem = new FileSystem();
}

[Test]
public void CacheKeySameAfterReNormalizing()
{
var versionAndBranchFinder = new ExecuteCore(fileSystem);

RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
{
var targetUrl = "https://github.com/GitTools/GitVersion.git";
var targetBranch = "refs/head/master";
var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, fixture.RepositoryPath);
gitPreparer.Initialise(true, targetBranch);
var cacheKey1 = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, null);
gitPreparer.Initialise(true, targetBranch);
var cacheKey2 = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, null);
cacheKey2.Value.ShouldBe(cacheKey1.Value);
});
}

[Test]
public void CacheFileExistsOnDisk()
{
Expand Down
101 changes: 97 additions & 4 deletions src/GitVersionCore/GitVersionCacheKeyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
{
using GitVersion.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Linq;

public class GitVersionCacheKeyFactory
class GitVersionCacheKeyFactory
{
public static GitVersionCacheKey Create(IFileSystem fileSystem, GitPreparer gitPreparer, Config overrideConfig)
{
Expand All @@ -23,10 +25,101 @@ private static string GetGitSystemHash(GitPreparer gitPreparer, IFileSystem file
{
var dotGitDirectory = gitPreparer.GetDotGitDirectory();

// Maybe using timestamp in .git/refs directory is enough?
var lastGitRefsChangedTicks = fileSystem.GetLastDirectoryWrite(Path.Combine(dotGitDirectory, "refs"));
// traverse the directory and get a list of files, use that for GetHash
var contents = CalculateDirectoryContents(Path.Combine(dotGitDirectory, "refs"));

return GetHash(dotGitDirectory, lastGitRefsChangedTicks.ToString());
return GetHash(contents.ToArray());
}

// based on https://msdn.microsoft.com/en-us/library/bb513869.aspx
private static List<string> CalculateDirectoryContents(string root)
{
var result = new List<string>();

// Data structure to hold names of subfolders to be
// examined for files.
var dirs = new Stack<string>();

if (!Directory.Exists(root))
{
throw new ArgumentException();
}

dirs.Push(root);

while (dirs.Any())
{
string currentDir = dirs.Pop();

var di = new DirectoryInfo(currentDir);
result.Add(di.Name);

string[] subDirs;
try
{
subDirs = Directory.GetDirectories(currentDir);
}
// An UnauthorizedAccessException exception will be thrown if we do not have
// discovery permission on a folder or file. It may or may not be acceptable
// to ignore the exception and continue enumerating the remaining files and
// folders. It is also possible (but unlikely) that a DirectoryNotFound exception
// will be raised. This will happen if currentDir has been deleted by
// another application or thread after our call to Directory.Exists. The
// choice of which exceptions to catch depends entirely on the specific task
// you are intending to perform and also on how much you know with certainty
// about the systems on which this code will run.
catch (UnauthorizedAccessException e)
{
Logger.WriteError(e.Message);
continue;
}
catch (DirectoryNotFoundException e)
{
Logger.WriteError(e.Message);
continue;
}

string[] files = null;
try
{
files = Directory.GetFiles(currentDir);
}
catch (UnauthorizedAccessException e)
{
Logger.WriteError(e.Message);
continue;
}
catch (DirectoryNotFoundException e)
{
Logger.WriteError(e.Message);
continue;
}

foreach (string file in files)
{
try
{
var fi = new FileInfo(file);
result.Add(fi.Name);
result.Add(File.ReadAllText(file));
}
catch (IOException e)
{
Logger.WriteError(e.Message);
continue;
}
}

// Push the subdirectories onto the stack for traversal.
// This could also be done before handing the files.
// push in reverse order
for (int i = subDirs.Length - 1; i >= 0; i--)
{
dirs.Push(subDirs[i]);
}
}

return result;
}

private static string GetRepositorySnapshotHash(GitPreparer gitPreparer)
Expand Down

0 comments on commit 72ef1cd

Please sign in to comment.