Skip to content

modify cache key calc to use entire contents of .git/refs instead of timestamp #969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 98 additions & 3 deletions src/GitVersionCore/GitVersionCacheKeyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
{
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
{
Expand All @@ -23,10 +25,103 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard style in C# is that all method names are in UpperCamelCase.

{
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);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these empty lines (line 93 as well).

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);
Copy link
Contributor

@DanielRose DanielRose Jul 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify these two lines to result.Add(file);

Copy link
Contributor Author

@tofutim tofutim Jul 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file = C:\Users\tim\AppData\Local\Temp\2\TestRepositories\cc7f7d6e-5b1f-4f63-a760-960409c862f9.git\refs\heads\master
fi.Name = master

Are you sure you want to repeat the path for every file? I was trying to reduce the amount of text to hash. Currently it goes from

2016-07-25_0826

to

2016-07-25_0827

after Initalize(). I left the path in the first item of the results, but even that is not necessary because the hash computation should not care about the physical location of the repository. It should start at "refs". Maybe I'll make that change. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way is fine. Since there normally are few files in the refs directory, it doesn't really matter.

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