Skip to content

Commit dbc988b

Browse files
committed
added overrideConfig to cache key
Extracted cache key calculation to GitVersionCacheKeyFactory
1 parent f2db2a4 commit dbc988b

File tree

6 files changed

+127
-49
lines changed

6 files changed

+127
-49
lines changed

src/GitVersionCore/ExecuteCore.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,12 @@ public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicReposi
4848
throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'.", workingDirectory));
4949
}
5050

51-
if (overrideConfig != null)
52-
{
53-
using (Logger.IndentLog("Override config from command line"))
54-
{
55-
var overridenVersionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, buildServer, overrideConfig: overrideConfig);
56-
return overridenVersionVariables;
57-
}
58-
}
59-
60-
var versionVariables = gitVersionCache.LoadVersionVariablesFromDiskCache(gitPreparer);
51+
var cacheKey = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, overrideConfig);
52+
var versionVariables = gitVersionCache.LoadVersionVariablesFromDiskCache(gitPreparer, cacheKey);
6153
if (versionVariables == null)
6254
{
63-
versionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, buildServer);
64-
gitVersionCache.WriteVariablesToDiskCache(gitPreparer, versionVariables);
55+
versionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, buildServer, overrideConfig);
56+
gitVersionCache.WriteVariablesToDiskCache(gitPreparer, cacheKey, versionVariables);
6557
}
6658

6759
return versionVariables;

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
using (var stream = fileSystem.OpenWrite(cacheFileName))
@@ -60,13 +59,13 @@ private string PrepareCacheDirectory(GitPreparer gitPreparer)
6059
return cacheDir;
6160
}
6261

63-
public VersionVariables LoadVersionVariablesFromDiskCache(GitPreparer gitPreparer)
62+
public VersionVariables LoadVersionVariablesFromDiskCache(GitPreparer gitPreparer, GitVersionCacheKey key)
6463
{
6564
using (Logger.IndentLog("Loading version variables from disk cache"))
6665
{
6766
var cacheDir = PrepareCacheDirectory(gitPreparer);
6867

69-
var cacheFileName = GetCacheFileName(GetKey(gitPreparer), cacheDir);
68+
var cacheFileName = GetCacheFileName(key, cacheDir);
7069
if (!fileSystem.Exists(cacheFileName))
7170
{
7271
Logger.WriteInfo("Cache file " + cacheFileName + " not found.");
@@ -99,37 +98,9 @@ public VersionVariables LoadVersionVariablesFromDiskCache(GitPreparer gitPrepare
9998
}
10099
}
101100

102-
string GetKey(GitPreparer gitPreparer)
103-
{
104-
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
105-
106-
// Maybe using timestamp in .git/refs directory is enough?
107-
var lastGitRefsChangedTicks = fileSystem.GetLastDirectoryWrite(Path.Combine(dotGitDirectory, "refs"));
108-
109-
// will return the same hash even when config file will be moved
110-
// from workingDirectory to rootProjectDirectory. It's OK. Config essentially is the same.
111-
var configFilePath = ConfigurationProvider.SelectConfigFilePath(gitPreparer, fileSystem);
112-
var configFileContent = fileSystem.Exists(configFilePath) ? fileSystem.ReadAllText(configFilePath) : null;
113-
var configFileHash = configFileContent != null ? GetHash(configFileContent) : null;
114-
115-
return gitPreparer.WithRepository(repo => string.Join(":", dotGitDirectory, repo.Head.CanonicalName, repo.Head.Tip.Sha, lastGitRefsChangedTicks, configFileHash));
116-
}
117-
118-
static string GetCacheFileName(string key, string cacheDir)
119-
{
120-
var cacheKey = GetHash(key);
121-
return string.Concat(Path.Combine(cacheDir, cacheKey), ".yml");
122-
}
123-
124-
static string GetHash(string textToHash)
101+
static string GetCacheFileName(GitVersionCacheKey key, string cacheDir)
125102
{
126-
using (var sha1 = SHA1.Create())
127-
{
128-
var bytes = Encoding.UTF8.GetBytes(textToHash);
129-
var hashedBytes = sha1.ComputeHash(bytes);
130-
var hashedString = BitConverter.ToString(hashedBytes);
131-
return hashedString.Replace("-", "");
132-
}
103+
return Path.Combine(cacheDir, string.Concat(key.Value, ".yml"));
133104
}
134105
}
135106
}
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
@@ -118,6 +118,8 @@
118118
<Compile Include="Extensions\ReadEmbeddedResourceExtensions.cs" />
119119
<Compile Include="GitPreparer.cs" />
120120
<Compile Include="GitVersionCache.cs" />
121+
<Compile Include="GitVersionCacheKey.cs" />
122+
<Compile Include="GitVersionCacheKeyFactory.cs" />
121123
<Compile Include="Helpers\FileSystem.cs" />
122124
<Compile Include="Helpers\IFileSystem.cs" />
123125
<Compile Include="Helpers\ServiceMessageEscapeHelper.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)