Skip to content

Commit 1454148

Browse files
sergiorykovJakeGinnivan
authored andcommitted
added overrideConfig to cache key
Extracted cache key calculation to GitVersionCacheKeyFactory
1 parent 8310d79 commit 1454148

File tree

6 files changed

+128
-49
lines changed

6 files changed

+128
-49
lines changed

src/GitVersionCore/ExecuteCore.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,15 @@ public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicReposi
5151
throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'.", workingDirectory));
5252
}
5353

54-
if (overrideConfig != null)
55-
{
56-
using (Logger.IndentLog("Override config from command line"))
57-
{
58-
var overridenVersionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, buildServer, overrideConfig: overrideConfig);
59-
return overridenVersionVariables;
60-
}
61-
}
62-
63-
var versionVariables = gitVersionCache.LoadVersionVariablesFromDiskCache(gitPreparer);
54+
var cacheKey = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, overrideConfig);
55+
var versionVariables = gitVersionCache.LoadVersionVariablesFromDiskCache(gitPreparer, cacheKey);
6456
if (versionVariables == null)
6557
{
66-
versionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, buildServer);
58+
versionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, buildServer, overrideConfig);
59+
6760
try
6861
{
69-
gitVersionCache.WriteVariablesToDiskCache(repo, dotGitDirectory, versionVariables);
62+
gitVersionCache.WriteVariablesToDiskCache(gitPreparer, cacheKey, versionVariables);
7063
}
7164
catch (AggregateException e)
7265
{

src/GitVersionCore/GitVersionCache.cs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ namespace GitVersion
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Linq;
8-
using System.Security.Cryptography;
9-
using System.Text;
108
using YamlDotNet.Serialization;
119

1210
public class GitVersionCache
@@ -18,10 +16,11 @@ public GitVersionCache(IFileSystem fileSystem)
1816
this.fileSystem = fileSystem;
1917
}
2018

21-
public void WriteVariablesToDiskCache(GitPreparer gitPreparer, VersionVariables variablesFromCache)
19+
public void WriteVariablesToDiskCache(GitPreparer gitPreparer, GitVersionCacheKey cacheKey, VersionVariables variablesFromCache)
2220
{
2321
var cacheDir = PrepareCacheDirectory(gitPreparer);
24-
var cacheFileName = GetCacheFileName(GetKey(gitPreparer), cacheDir);
22+
var cacheFileName = GetCacheFileName(cacheKey, cacheDir);
23+
2524
variablesFromCache.FileName = cacheFileName;
2625

2726
Dictionary<string, string> dictionary;
@@ -66,13 +65,13 @@ private string PrepareCacheDirectory(GitPreparer gitPreparer)
6665
return cacheDir;
6766
}
6867

69-
public VersionVariables LoadVersionVariablesFromDiskCache(GitPreparer gitPreparer)
68+
public VersionVariables LoadVersionVariablesFromDiskCache(GitPreparer gitPreparer, GitVersionCacheKey key)
7069
{
7170
using (Logger.IndentLog("Loading version variables from disk cache"))
7271
{
7372
var cacheDir = PrepareCacheDirectory(gitPreparer);
7473

75-
var cacheFileName = GetCacheFileName(GetKey(gitPreparer), cacheDir);
74+
var cacheFileName = GetCacheFileName(key, cacheDir);
7675
if (!fileSystem.Exists(cacheFileName))
7776
{
7877
Logger.WriteInfo("Cache file " + cacheFileName + " not found.");
@@ -105,37 +104,9 @@ public VersionVariables LoadVersionVariablesFromDiskCache(GitPreparer gitPrepare
105104
}
106105
}
107106

108-
string GetKey(GitPreparer gitPreparer)
109-
{
110-
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
111-
112-
// Maybe using timestamp in .git/refs directory is enough?
113-
var lastGitRefsChangedTicks = fileSystem.GetLastDirectoryWrite(Path.Combine(dotGitDirectory, "refs"));
114-
115-
// will return the same hash even when config file will be moved
116-
// from workingDirectory to rootProjectDirectory. It's OK. Config essentially is the same.
117-
var configFilePath = ConfigurationProvider.SelectConfigFilePath(gitPreparer, fileSystem);
118-
var configFileContent = fileSystem.Exists(configFilePath) ? fileSystem.ReadAllText(configFilePath) : null;
119-
var configFileHash = configFileContent != null ? GetHash(configFileContent) : null;
120-
121-
return gitPreparer.WithRepository(repo => string.Join(":", dotGitDirectory, repo.Head.CanonicalName, repo.Head.Tip.Sha, lastGitRefsChangedTicks, configFileHash));
122-
}
123-
124-
static string GetCacheFileName(string key, string cacheDir)
125-
{
126-
var cacheKey = GetHash(key);
127-
return string.Concat(Path.Combine(cacheDir, cacheKey), ".yml");
128-
}
129-
130-
static string GetHash(string textToHash)
107+
static string GetCacheFileName(GitVersionCacheKey key, string cacheDir)
131108
{
132-
using (var sha1 = SHA1.Create())
133-
{
134-
var bytes = Encoding.UTF8.GetBytes(textToHash);
135-
var hashedBytes = sha1.ComputeHash(bytes);
136-
var hashedString = BitConverter.ToString(hashedBytes);
137-
return hashedString.Replace("-", "");
138-
}
109+
return Path.Combine(cacheDir, string.Concat(key.Value, ".yml"));
139110
}
140111
}
141112
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
5+
public class GitVersionCacheKey
6+
{
7+
public GitVersionCacheKey(string value)
8+
{
9+
if (string.IsNullOrEmpty(value))
10+
{
11+
throw new ArgumentNullException("value");
12+
}
13+
14+
Value = value;
15+
}
16+
17+
public string Value { get; private set; }
18+
}
19+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
namespace GitVersion
2+
{
3+
using GitVersion.Helpers;
4+
using System;
5+
using System.IO;
6+
using System.Security.Cryptography;
7+
using System.Text;
8+
9+
public class GitVersionCacheKeyFactory
10+
{
11+
public static GitVersionCacheKey Create(IFileSystem fileSystem, GitPreparer gitPreparer, Config overrideConfig)
12+
{
13+
var gitSystemHash = GetGitSystemHash(gitPreparer, fileSystem);
14+
var configFileHash = GetConfigFileHash(fileSystem, gitPreparer);
15+
var repositorySnapshotHash = GetRepositorySnapshotHash(gitPreparer);
16+
var overrideConfigHash = GetOverrideConfigHash(overrideConfig);
17+
18+
var compositeHash = GetHash(gitSystemHash, configFileHash, repositorySnapshotHash, overrideConfigHash);
19+
return new GitVersionCacheKey(compositeHash);
20+
}
21+
22+
private static string GetGitSystemHash(GitPreparer gitPreparer, IFileSystem fileSystem)
23+
{
24+
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
25+
26+
// Maybe using timestamp in .git/refs directory is enough?
27+
var lastGitRefsChangedTicks = fileSystem.GetLastDirectoryWrite(Path.Combine(dotGitDirectory, "refs"));
28+
29+
return GetHash(dotGitDirectory, lastGitRefsChangedTicks.ToString());
30+
}
31+
32+
private static string GetRepositorySnapshotHash(GitPreparer gitPreparer)
33+
{
34+
var repositorySnapshot = gitPreparer.WithRepository(repo => string.Join(":", repo.Head.CanonicalName, repo.Head.Tip.Sha));
35+
return GetHash(repositorySnapshot);
36+
}
37+
38+
private static string GetOverrideConfigHash(Config overrideConfig)
39+
{
40+
if (overrideConfig == null)
41+
{
42+
return string.Empty;
43+
}
44+
45+
// Doesn't depend on command line representation and
46+
// includes possible changes in default values of Config per se.
47+
var stringBuilder = new StringBuilder();
48+
using (var stream = new StringWriter(stringBuilder))
49+
{
50+
ConfigSerialiser.Write(overrideConfig, stream);
51+
stream.Flush();
52+
}
53+
var configContent = stringBuilder.ToString();
54+
55+
return GetHash(configContent);
56+
}
57+
58+
private static string GetConfigFileHash(IFileSystem fileSystem, GitPreparer gitPreparer)
59+
{
60+
// will return the same hash even when config file will be moved
61+
// from workingDirectory to rootProjectDirectory. It's OK. Config essentially is the same.
62+
var configFilePath = ConfigurationProvider.SelectConfigFilePath(gitPreparer, fileSystem);
63+
if (!fileSystem.Exists(configFilePath))
64+
{
65+
return string.Empty;
66+
}
67+
68+
var configFileContent = fileSystem.ReadAllText(configFilePath);
69+
return GetHash(configFileContent);
70+
}
71+
72+
static string GetHash(params string[] textsToHash)
73+
{
74+
var textToHash = string.Join(":", textsToHash);
75+
return GetHash(textToHash);
76+
}
77+
78+
static string GetHash(string textToHash)
79+
{
80+
if (string.IsNullOrEmpty(textToHash))
81+
{
82+
return string.Empty;
83+
}
84+
85+
using (var sha1 = SHA1.Create())
86+
{
87+
var bytes = Encoding.UTF8.GetBytes(textToHash);
88+
var hashedBytes = sha1.ComputeHash(bytes);
89+
var hashedString = BitConverter.ToString(hashedBytes);
90+
return hashedString.Replace("-", "");
91+
}
92+
}
93+
}
94+
}

src/GitVersionCore/GitVersionCore.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@
120120
<Compile Include="Extensions\ReadEmbeddedResourceExtensions.cs" />
121121
<Compile Include="GitPreparer.cs" />
122122
<Compile Include="GitVersionCache.cs" />
123+
<Compile Include="GitVersionCacheKey.cs" />
124+
<Compile Include="GitVersionCacheKeyFactory.cs" />
123125
<Compile Include="Helpers\FileSystem.cs" />
124126
<Compile Include="Helpers\IFileSystem.cs" />
125127
<Compile Include="Helpers\IThreadSleep.cs" />

src/GitVersionExe/SpecifiedArgumentRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void Run(Arguments arguments, IFileSystem fileSystem)
2626
var overrideConfig = arguments.HasOverrideConfig ? arguments.OverrideConfig : null;
2727

2828
var executeCore = new ExecuteCore(fileSystem);
29-
var variables = executeCore.ExecuteGitVersion(targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, targetPath, commitId, overrideConfig: overrideConfig);
29+
var variables = executeCore.ExecuteGitVersion(targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, targetPath, commitId, overrideConfig);
3030

3131
if (arguments.Output == OutputType.BuildServer)
3232
{

0 commit comments

Comments
 (0)