Skip to content

Commit 02036f9

Browse files
committed
* GitVersionCache.cs refactoring: improved readibility
1 parent ddaff32 commit 02036f9

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

src/GitVersionCore/GitVersionCache.cs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
namespace GitVersion
22
{
3+
using GitVersion.Helpers;
34
using System;
45
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq;
78
using System.Security.Cryptography;
89
using System.Text;
9-
using GitVersion.Helpers;
10-
using LibGit2Sharp;
1110
using YamlDotNet.Serialization;
1211

1312
public class GitVersionCache
@@ -47,7 +46,7 @@ public void WriteVariablesToDiskCache(GitPreparer gitPreparer, VersionVariables
4746
private string PrepareCacheDirectory(GitPreparer gitPreparer)
4847
{
4948
var gitDir = gitPreparer.GetDotGitDirectory();
50-
49+
5150
// If the cacheDir already exists, CreateDirectory just won't do anything (it won't fail). @asbjornu
5251
var cacheDir = GetCacheDir(gitDir);
5352
fileSystem.CreateDirectory(cacheDir);
@@ -62,51 +61,52 @@ public VersionVariables LoadVersionVariablesFromDiskCache(GitPreparer gitPrepare
6261
var cacheDir = PrepareCacheDirectory(gitPreparer);
6362

6463
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))
6771
{
68-
using (Logger.IndentLog("Deserializing version variables from cache file " + cacheFileName))
72+
try
6973
{
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());
7081
try
7182
{
72-
vv = VersionVariables.FromFile(cacheFileName, fileSystem);
83+
fileSystem.Delete(cacheFileName);
7384
}
74-
catch (Exception ex)
85+
catch (Exception deleteEx)
7586
{
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));
8688
}
89+
90+
return null;
8791
}
8892
}
89-
else
90-
{
91-
Logger.WriteInfo("Cache file " + cacheFileName + " not found.");
92-
}
93-
94-
return vv;
9593
}
9694
}
9795

9896
string GetKey(GitPreparer gitPreparer)
9997
{
100-
var gitDir = gitPreparer.GetDotGitDirectory();
98+
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
10199

102100
// 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"));
104102

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;
108108

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));
110110
}
111111

112112
static string GetCacheFileName(string key, string cacheDir)
@@ -131,4 +131,4 @@ static string GetHash(string textToHash)
131131
}
132132
}
133133
}
134-
}
134+
}

0 commit comments

Comments
 (0)