@@ -16,16 +16,11 @@ namespace GitVersion
16
16
public class ExecuteCore
17
17
{
18
18
readonly IFileSystem fileSystem ;
19
- readonly Func < string , Func < string , VersionVariables > , VersionVariables > getOrAddFromCache ;
20
19
21
- public ExecuteCore ( IFileSystem fileSystem , Func < string , Func < string , VersionVariables > , VersionVariables > getOrAddFromCache = null )
20
+ public ExecuteCore ( IFileSystem fileSystem )
22
21
{
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
+
29
24
this . fileSystem = fileSystem ;
30
25
}
31
26
@@ -38,16 +33,38 @@ public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicReposi
38
33
var ticks = fileSystem . GetLastDirectoryWrite ( Path . Combine ( gitDir , "refs" ) ) ;
39
34
var key = string . Format ( "{0}:{1}:{2}:{3}" , gitDir , repo . Head . CanonicalName , repo . Head . Tip . Sha , ticks ) ;
40
35
41
- if ( getOrAddFromCache != null )
36
+ var versionVariables = LoadVersionVariablesFromDiskCache ( key , gitDir ) ;
37
+ if ( versionVariables == null )
42
38
{
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 ) ;
48
41
}
49
42
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
+ }
51
68
}
52
69
}
53
70
@@ -79,22 +96,15 @@ static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch
79
96
return currentBranch ;
80
97
}
81
98
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 )
83
100
{
84
101
using ( Logger . IndentLog ( "Loading version variables from disk cache" ) )
85
102
{
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" ) ;
94
103
// If the cacheDir already exists, CreateDirectory just won't do anything (it won't fail). @asbjornu
95
- fileSystem . CreateDirectory ( cacheDir ) ;
96
104
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 ) ;
98
108
VersionVariables vv = null ;
99
109
if ( fileSystem . Exists ( cacheFileName ) )
100
110
{
@@ -124,34 +134,27 @@ VersionVariables LoadVersionVariablesFromDiskCache(string key, string gitDir, st
124
134
Logger . WriteInfo ( "Cache file " + cacheFileName + " not found." ) ;
125
135
}
126
136
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
-
151
137
return vv ;
152
138
}
153
139
}
154
140
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
+
155
158
VersionVariables ExecuteInternal ( string targetUrl , string dynamicRepositoryLocation , Authentication authentication , string targetBranch , bool noFetch , string workingDirectory , string commitId )
156
159
{
157
160
// Normalise if we are running on build server
0 commit comments