1
1
namespace GitVersion
2
2
{
3
+ using GitVersion . Helpers ;
3
4
using System ;
4
5
using System . Collections . Generic ;
5
6
using System . IO ;
6
7
using System . Linq ;
7
8
using System . Security . Cryptography ;
8
9
using System . Text ;
9
- using GitVersion . Helpers ;
10
- using LibGit2Sharp ;
11
10
using YamlDotNet . Serialization ;
12
11
13
12
public class GitVersionCache
@@ -47,7 +46,7 @@ public void WriteVariablesToDiskCache(GitPreparer gitPreparer, VersionVariables
47
46
private string PrepareCacheDirectory ( GitPreparer gitPreparer )
48
47
{
49
48
var gitDir = gitPreparer . GetDotGitDirectory ( ) ;
50
-
49
+
51
50
// If the cacheDir already exists, CreateDirectory just won't do anything (it won't fail). @asbjornu
52
51
var cacheDir = GetCacheDir ( gitDir ) ;
53
52
fileSystem . CreateDirectory ( cacheDir ) ;
@@ -62,51 +61,52 @@ public VersionVariables LoadVersionVariablesFromDiskCache(GitPreparer gitPrepare
62
61
var cacheDir = PrepareCacheDirectory ( gitPreparer ) ;
63
62
64
63
var cacheFileName = GetCacheFileName ( GetKey ( gitPreparer ) , cacheDir ) ;
65
- VersionVariables vv = null ;
66
- if ( fileSystem . Exists ( cacheFileName ) )
64
+ if ( ! fileSystem . Exists ( cacheFileName ) )
65
+ {
66
+ Logger . WriteInfo ( "Cache file " + cacheFileName + " not found." ) ;
67
+ return null ;
68
+ }
69
+
70
+ using ( Logger . IndentLog ( "Deserializing version variables from cache file " + cacheFileName ) )
67
71
{
68
- using ( Logger . IndentLog ( "Deserializing version variables from cache file " + cacheFileName ) )
72
+ try
69
73
{
74
+ var loadedVariables = VersionVariables . FromFile ( cacheFileName , fileSystem ) ;
75
+ return loadedVariables ;
76
+ }
77
+ catch ( Exception ex )
78
+ {
79
+ Logger . WriteWarning ( "Unable to read cache file " + cacheFileName + ", deleting it." ) ;
80
+ Logger . WriteInfo ( ex . ToString ( ) ) ;
70
81
try
71
82
{
72
- vv = VersionVariables . FromFile ( cacheFileName , fileSystem ) ;
83
+ fileSystem . Delete ( cacheFileName ) ;
73
84
}
74
- catch ( Exception ex )
85
+ catch ( Exception deleteEx )
75
86
{
76
- Logger . WriteWarning ( "Unable to read cache file " + cacheFileName + ", deleting it." ) ;
77
- Logger . WriteInfo ( ex . ToString ( ) ) ;
78
- try
79
- {
80
- fileSystem . Delete ( cacheFileName ) ;
81
- }
82
- catch ( Exception deleteEx )
83
- {
84
- Logger . WriteWarning ( string . Format ( "Unable to delete corrupted version cache file {0}. Got {1} exception." , cacheFileName , deleteEx . GetType ( ) . FullName ) ) ;
85
- }
87
+ Logger . WriteWarning ( string . Format ( "Unable to delete corrupted version cache file {0}. Got {1} exception." , cacheFileName , deleteEx . GetType ( ) . FullName ) ) ;
86
88
}
89
+
90
+ return null ;
87
91
}
88
92
}
89
- else
90
- {
91
- Logger . WriteInfo ( "Cache file " + cacheFileName + " not found." ) ;
92
- }
93
-
94
- return vv ;
95
93
}
96
94
}
97
95
98
96
string GetKey ( GitPreparer gitPreparer )
99
97
{
100
- var gitDir = gitPreparer . GetDotGitDirectory ( ) ;
98
+ var dotGitDirectory = gitPreparer . GetDotGitDirectory ( ) ;
101
99
102
100
// Maybe using timestamp in .git/refs directory is enough?
103
- var ticks = fileSystem . GetLastDirectoryWrite ( Path . Combine ( gitDir , "refs" ) ) ;
101
+ var lastGitRefsChangedTicks = fileSystem . GetLastDirectoryWrite ( Path . Combine ( dotGitDirectory , "refs" ) ) ;
104
102
105
- var configPath = ConfigurationProvider . SelectConfigFilePath ( gitPreparer , fileSystem ) ;
106
- var configText = fileSystem . Exists ( configPath ) ? fileSystem . ReadAllText ( configPath ) : null ;
107
- var configHash = configText != null ? GetHash ( configText ) : null ;
103
+ // will return the same hash even when config file will be moved
104
+ // from workingDirectory to rootProjectDirectory. It's OK. Config essentially is the same.
105
+ var configFilePath = ConfigurationProvider . SelectConfigFilePath ( gitPreparer , fileSystem ) ;
106
+ var configFileContent = fileSystem . Exists ( configFilePath ) ? fileSystem . ReadAllText ( configFilePath ) : null ;
107
+ var configFileHash = configFileContent != null ? GetHash ( configFileContent ) : null ;
108
108
109
- return gitPreparer . WithRepository ( repo => string . Join ( ":" , gitDir , repo . Head . CanonicalName , repo . Head . Tip . Sha , ticks , configHash ) ) ;
109
+ return gitPreparer . WithRepository ( repo => string . Join ( ":" , dotGitDirectory , repo . Head . CanonicalName , repo . Head . Tip . Sha , lastGitRefsChangedTicks , configFileHash ) ) ;
110
110
}
111
111
112
112
static string GetCacheFileName ( string key , string cacheDir )
@@ -131,4 +131,4 @@ static string GetHash(string textToHash)
131
131
}
132
132
}
133
133
}
134
- }
134
+ }
0 commit comments