-
Notifications
You must be signed in to change notification settings - Fork 651
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
Changes from all commits
9a945cc
3464865
af3a3f2
754f93a
72ad9ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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) | ||
{ | ||
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); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can simplify these two lines to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 to 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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.