Skip to content

Commit 64c50bc

Browse files
committed
Separating the caching logic out a bit
1 parent d7a6402 commit 64c50bc

File tree

1 file changed

+53
-50
lines changed

1 file changed

+53
-50
lines changed

src/GitVersionCore/ExecuteCore.cs

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,11 @@ namespace GitVersion
1616
public class ExecuteCore
1717
{
1818
readonly IFileSystem fileSystem;
19-
readonly Func<string, Func<string, VersionVariables>, VersionVariables> getOrAddFromCache;
2019

21-
public ExecuteCore(IFileSystem fileSystem, Func<string, Func<string, VersionVariables>, VersionVariables> getOrAddFromCache = null)
20+
public ExecuteCore(IFileSystem fileSystem)
2221
{
23-
if (fileSystem == null)
24-
{
25-
throw new ArgumentNullException("fileSystem");
26-
}
27-
28-
this.getOrAddFromCache = getOrAddFromCache;
22+
if (fileSystem == null) throw new ArgumentNullException("fileSystem");
23+
2924
this.fileSystem = fileSystem;
3025
}
3126

@@ -38,16 +33,38 @@ public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicReposi
3833
var ticks = fileSystem.GetLastDirectoryWrite(Path.Combine(gitDir, "refs"));
3934
var key = string.Format("{0}:{1}:{2}:{3}", gitDir, repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks);
4035

41-
if (getOrAddFromCache != null)
36+
var versionVariables = LoadVersionVariablesFromDiskCache(key, gitDir);
37+
if (versionVariables == null)
4238
{
43-
return getOrAddFromCache(key, k =>
44-
{
45-
Logger.WriteInfo("Version not in memory cache. Attempting to load version from disk cache.");
46-
return LoadVersionVariablesFromDiskCache(key, gitDir, targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, workingDirectory, commitId);
47-
});
39+
versionVariables = ExecuteInternal(targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, workingDirectory, commitId);
40+
WriteVariablesToDiskCache(key, gitDir, versionVariables);
4841
}
4942

50-
return LoadVersionVariablesFromDiskCache(key, gitDir, targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, workingDirectory, commitId);
43+
return versionVariables;
44+
}
45+
}
46+
47+
void WriteVariablesToDiskCache(string key, string gitDir, VersionVariables variablesFromCache)
48+
{
49+
var cacheFileName = GetCacheFileName(key, GetCacheDir(gitDir));
50+
variablesFromCache.FileName = cacheFileName;
51+
52+
using (var stream = fileSystem.OpenWrite(cacheFileName))
53+
{
54+
using (var sw = new StreamWriter(stream))
55+
{
56+
Dictionary<string, string> dictionary;
57+
using (Logger.IndentLog("Creating dictionary"))
58+
{
59+
dictionary = variablesFromCache.ToDictionary(x => x.Key, x => x.Value);
60+
}
61+
62+
using (Logger.IndentLog("Storing version variables to cache file " + cacheFileName))
63+
{
64+
var serializer = new Serializer();
65+
serializer.Serialize(sw, dictionary);
66+
}
67+
}
5168
}
5269
}
5370

@@ -79,22 +96,15 @@ static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch
7996
return currentBranch;
8097
}
8198

82-
VersionVariables LoadVersionVariablesFromDiskCache(string key, string gitDir, string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId)
99+
VersionVariables LoadVersionVariablesFromDiskCache(string key, string gitDir)
83100
{
84101
using (Logger.IndentLog("Loading version variables from disk cache"))
85102
{
86-
string cacheKey;
87-
using (var sha1 = SHA1.Create())
88-
{
89-
// Make a shorter key by hashing, to avoid having to long cache filename.
90-
cacheKey = BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(key))).Replace("-", "");
91-
}
92-
93-
var cacheDir = Path.Combine(gitDir, "gitversion_cache");
94103
// If the cacheDir already exists, CreateDirectory just won't do anything (it won't fail). @asbjornu
95-
fileSystem.CreateDirectory(cacheDir);
96104

97-
var cacheFileName = string.Concat(Path.Combine(cacheDir, cacheKey), ".yml");
105+
var cacheDir = GetCacheDir(gitDir);
106+
fileSystem.CreateDirectory(cacheDir);
107+
var cacheFileName = GetCacheFileName(key, cacheDir);
98108
VersionVariables vv = null;
99109
if (fileSystem.Exists(cacheFileName))
100110
{
@@ -124,34 +134,27 @@ VersionVariables LoadVersionVariablesFromDiskCache(string key, string gitDir, st
124134
Logger.WriteInfo("Cache file " + cacheFileName + " not found.");
125135
}
126136

127-
if (vv == null)
128-
{
129-
vv = ExecuteInternal(targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, workingDirectory, commitId);
130-
vv.FileName = cacheFileName;
131-
132-
using (var stream = fileSystem.OpenWrite(cacheFileName))
133-
{
134-
using (var sw = new StreamWriter(stream))
135-
{
136-
Dictionary<string, string> dictionary;
137-
using (Logger.IndentLog("Creating dictionary"))
138-
{
139-
dictionary = vv.ToDictionary(x => x.Key, x => x.Value);
140-
}
141-
142-
using (Logger.IndentLog("Storing version variables to cache file " + cacheFileName))
143-
{
144-
var serializer = new Serializer();
145-
serializer.Serialize(sw, dictionary);
146-
}
147-
}
148-
}
149-
}
150-
151137
return vv;
152138
}
153139
}
154140

141+
static string GetCacheFileName(string key, string cacheDir)
142+
{
143+
string cacheKey;
144+
using (var sha1 = SHA1.Create())
145+
{
146+
// Make a shorter key by hashing, to avoid having to long cache filename.
147+
cacheKey = BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(key))).Replace("-", "");
148+
}
149+
var cacheFileName = string.Concat(Path.Combine(cacheDir, cacheKey), ".yml");
150+
return cacheFileName;
151+
}
152+
153+
static string GetCacheDir(string gitDir)
154+
{
155+
return Path.Combine(gitDir, "gitversion_cache");
156+
}
157+
155158
VersionVariables ExecuteInternal(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId)
156159
{
157160
// Normalise if we are running on build server

0 commit comments

Comments
 (0)