Skip to content

Commit 3c00a30

Browse files
committed
Refactor caching system in GitVersion
Changes include updating cache file format from YML to JSON, removing unused methods, and optimizing file write/read operations. This allows to remove dependency on YamlDotNet. Various changes were also made to use more appropriate method identifiers and improve handling of cache file exceptions.
1 parent 7384185 commit 3c00a30

File tree

9 files changed

+187
-173
lines changed

9 files changed

+187
-173
lines changed

src/GitVersion.App.Tests/Helpers/ExecutionResults.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public virtual GitVersionVariables OutputVariables
1919
{
2020
get
2121
{
22-
var jsonStartIndex = Output.IndexOf("{", StringComparison.Ordinal);
23-
var jsonEndIndex = Output.IndexOf("}", StringComparison.Ordinal);
22+
var jsonStartIndex = Output.IndexOf('{');
23+
var jsonEndIndex = Output.IndexOf('}');
2424
var json = Output.Substring(jsonStartIndex, jsonEndIndex - jsonStartIndex + 1);
2525

2626
return VersionVariablesHelper.FromJson(json);

src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs

Lines changed: 91 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class GitVersionExecutorTests : TestBase
1616
{
1717
private IFileSystem fileSystem;
1818
private ILog log;
19-
private IGitVersionCache gitVersionCache;
19+
private GitVersionCache gitVersionCache;
2020
private IServiceProvider sp;
2121

2222
[Test]
@@ -91,33 +91,35 @@ public void CacheKeyForWorktree()
9191
[Test]
9292
public void CacheFileExistsOnDisk()
9393
{
94-
const string versionCacheFileContent = @"
95-
Major: 4
96-
Minor: 10
97-
Patch: 3
98-
PreReleaseTag: test.19
99-
PreReleaseTagWithDash: -test.19
100-
PreReleaseLabel: test
101-
PreReleaseLabelWithDash: -test
102-
PreReleaseNumber: 19
103-
WeightedPreReleaseNumber: 19
104-
BuildMetaData:
105-
FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
106-
MajorMinorPatch: 4.10.3
107-
SemVer: 4.10.3-test.19
108-
AssemblySemVer: 4.10.3.0
109-
AssemblySemFileVer: 4.10.3.0
110-
FullSemVer: 4.10.3-test.19
111-
InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
112-
BranchName: feature/test
113-
EscapedBranchName: feature-test
114-
Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
115-
ShortSha: dd2a29af
116-
VersionSourceSha: 4.10.2
117-
CommitsSinceVersionSource: 19
118-
CommitDate: 2015-11-10
119-
UncommittedChanges: 0
120-
";
94+
const string versionCacheFileContent = """
95+
{
96+
"Major": 4,
97+
"Minor": 10,
98+
"Patch": 3,
99+
"PreReleaseTag": "test.19",
100+
"PreReleaseTagWithDash": "-test.19",
101+
"PreReleaseLabel": "test",
102+
"PreReleaseLabelWithDash": "-test",
103+
"PreReleaseNumber": 19,
104+
"WeightedPreReleaseNumber": 19,
105+
"BuildMetaData": null,
106+
"FullBuildMetaData": "Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f",
107+
"MajorMinorPatch": "4.10.3",
108+
"SemVer": "4.10.3-test.19",
109+
"AssemblySemVer": "4.10.3.0",
110+
"AssemblySemFileVer": "4.10.3.0",
111+
"FullSemVer": "4.10.3-test.19",
112+
"InformationalVersion": "4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f",
113+
"BranchName": "feature/test",
114+
"EscapedBranchName": "feature-test",
115+
"Sha": "dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f",
116+
"ShortSha": "dd2a29af",
117+
"VersionSourceSha": "4.10.2",
118+
"CommitsSinceVersionSource": 19,
119+
"CommitDate": "2015-11-10T00:00:00.000Z",
120+
"UncommittedChanges": 0
121+
}
122+
""";
121123

122124
var stringBuilder = new StringBuilder();
123125
void Action(string s) => stringBuilder.AppendLine(s);
@@ -145,7 +147,7 @@ public void CacheFileExistsOnDisk()
145147

146148
var logsMessages = stringBuilder.ToString();
147149

148-
logsMessages.ShouldContain("Deserializing version variables from cache file", Case.Insensitive, logsMessages);
150+
logsMessages.ShouldContain("Loading version variables from disk cache file", Case.Insensitive, logsMessages);
149151
}
150152

151153
[Test]
@@ -228,40 +230,42 @@ public void CacheFileIsMissing()
228230
gitVersionCalculator.CalculateVersionVariables();
229231

230232
var logsMessages = stringBuilder.ToString();
231-
logsMessages.ShouldContain("yml not found", Case.Insensitive, logsMessages);
233+
logsMessages.ShouldContain(".json not found", Case.Insensitive, logsMessages);
232234
}
233235

234236
[TestCase(ConfigurationFileLocator.DefaultFileName)]
235237
[TestCase(ConfigurationFileLocator.DefaultAlternativeFileName)]
236238
public void ConfigChangeInvalidatesCache(string configFileName)
237239
{
238-
const string versionCacheFileContent = @"
239-
Major: 4
240-
Minor: 10
241-
Patch: 3
242-
PreReleaseTag: test.19
243-
PreReleaseTagWithDash: -test.19
244-
PreReleaseLabel: test
245-
PreReleaseLabelWithDash: -test
246-
PreReleaseNumber: 19
247-
WeightedPreReleaseNumber: 19
248-
BuildMetaData:
249-
FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
250-
MajorMinorPatch: 4.10.3
251-
SemVer: 4.10.3-test.19
252-
AssemblySemVer: 4.10.3.0
253-
AssemblySemFileVer: 4.10.3.0
254-
FullSemVer: 4.10.3-test.19
255-
InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
256-
BranchName: feature/test
257-
EscapedBranchName: feature-test
258-
Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
259-
ShortSha: dd2a29af
260-
VersionSourceSha: 4.10.2
261-
CommitsSinceVersionSource: 19
262-
CommitDate: 2015-11-10
263-
UncommittedChanges: 0
264-
";
240+
const string versionCacheFileContent = """
241+
{
242+
"Major": 4,
243+
"Minor": 10,
244+
"Patch": 3,
245+
"PreReleaseTag": "test.19",
246+
"PreReleaseTagWithDash": "-test.19",
247+
"PreReleaseLabel": "test",
248+
"PreReleaseLabelWithDash": "-test",
249+
"PreReleaseNumber": 19,
250+
"WeightedPreReleaseNumber": 19,
251+
"BuildMetaData": null,
252+
"FullBuildMetaData": "Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f",
253+
"MajorMinorPatch": "4.10.3",
254+
"SemVer": "4.10.3-test.19",
255+
"AssemblySemVer": "4.10.3.0",
256+
"AssemblySemFileVer": "4.10.3.0",
257+
"FullSemVer": "4.10.3-test.19",
258+
"InformationalVersion": "4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f",
259+
"BranchName": "feature/test",
260+
"EscapedBranchName": "feature-test",
261+
"Sha": "dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f",
262+
"ShortSha": "dd2a29af",
263+
"VersionSourceSha": "4.10.2",
264+
"CommitsSinceVersionSource": 19,
265+
"CommitDate": "2015-11-10T00:00:00.000Z",
266+
"UncommittedChanges": 0
267+
}
268+
""";
265269

266270
using var fixture = new EmptyRepositoryFixture();
267271

@@ -295,33 +299,35 @@ public void ConfigChangeInvalidatesCache(string configFileName)
295299
[Test]
296300
public void NoCacheBypassesCache()
297301
{
298-
const string versionCacheFileContent = @"
299-
Major: 4
300-
Minor: 10
301-
Patch: 3
302-
PreReleaseTag: test.19
303-
PreReleaseTagWithDash: -test.19
304-
PreReleaseLabel: test
305-
PreReleaseLabelWithDash: -test
306-
PreReleaseNumber: 19
307-
WeightedPreReleaseNumber: 19
308-
BuildMetaData:
309-
FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
310-
MajorMinorPatch: 4.10.3
311-
SemVer: 4.10.3-test.19
312-
AssemblySemVer: 4.10.3.0
313-
AssemblySemFileVer: 4.10.3.0
314-
FullSemVer: 4.10.3-test.19
315-
InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
316-
BranchName: feature/test
317-
EscapedBranchName: feature-test
318-
Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
319-
ShortSha: dd2a29af
320-
VersionSourceSha: 4.10.2
321-
CommitsSinceVersionSource: 19
322-
CommitDate: 2015-11-10
323-
UncommittedChanges: 0
324-
";
302+
const string versionCacheFileContent = """
303+
{
304+
"Major": 4,
305+
"Minor": 10,
306+
"Patch": 3,
307+
"PreReleaseTag": "test.19",
308+
"PreReleaseTagWithDash": "-test.19",
309+
"PreReleaseLabel": "test",
310+
"PreReleaseLabelWithDash": "-test",
311+
"PreReleaseNumber": 19,
312+
"WeightedPreReleaseNumber": 19,
313+
"BuildMetaData": null,
314+
"FullBuildMetaData": "Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f",
315+
"MajorMinorPatch": "4.10.3",
316+
"SemVer": "4.10.3-test.19",
317+
"AssemblySemVer": "4.10.3.0",
318+
"AssemblySemFileVer": "4.10.3.0",
319+
"FullSemVer": "4.10.3-test.19",
320+
"InformationalVersion": "4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f",
321+
"BranchName": "feature/test",
322+
"EscapedBranchName": "feature-test",
323+
"Sha": "dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f",
324+
"ShortSha": "dd2a29af",
325+
"VersionSourceSha": "4.10.2",
326+
"CommitsSinceVersionSource": 19,
327+
"CommitDate": "2015-11-10T00:00:00.000Z",
328+
"UncommittedChanges": 0
329+
}
330+
""";
325331

326332
using var fixture = new EmptyRepositoryFixture();
327333

@@ -571,7 +577,7 @@ private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVe
571577

572578
this.fileSystem = this.sp.GetRequiredService<IFileSystem>();
573579
this.log = this.sp.GetRequiredService<ILog>();
574-
this.gitVersionCache = this.sp.GetRequiredService<IGitVersionCache>();
580+
this.gitVersionCache = (GitVersionCache)this.sp.GetRequiredService<IGitVersionCache>();
575581

576582
return this.sp.GetRequiredService<IGitVersionCalculateTool>();
577583
}

src/GitVersion.Core/GitVersion.Core.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
1919
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
2020
<PackageReference Include="Microsoft.Extensions.Options" />
21-
<PackageReference Include="YamlDotNet" />
2221
</ItemGroup>
2322

2423
<ItemGroup>

src/GitVersion.Core/OutputVariables/VersionVariablesHelper.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Text.Encodings.Web;
22
using GitVersion.Extensions;
33
using GitVersion.Helpers;
4-
using YamlDotNet.Serialization;
54

65
namespace GitVersion.OutputVariables;
76

@@ -14,6 +13,22 @@ public static GitVersionVariables FromJson(string json)
1413
return FromDictionary(variablePairs);
1514
}
1615

16+
public static string ToJsonString(this GitVersionVariables gitVersionVariables)
17+
{
18+
var variablesType = typeof(VersionVariablesJsonModel);
19+
var variables = new VersionVariablesJsonModel();
20+
21+
foreach (var (key, value) in gitVersionVariables.OrderBy(x => x.Key))
22+
{
23+
var propertyInfo = variablesType.GetProperty(key);
24+
propertyInfo?.SetValue(variables, ChangeType(value, propertyInfo.PropertyType));
25+
}
26+
27+
var serializeOptions = JsonSerializerOptions();
28+
29+
return JsonSerializer.Serialize(variables, serializeOptions);
30+
}
31+
1732
public static GitVersionVariables FromFile(string filePath, IFileSystem fileSystem)
1833
{
1934
try
@@ -33,20 +48,23 @@ public static GitVersionVariables FromFile(string filePath, IFileSystem fileSyst
3348
}
3449
}
3550

36-
public static string ToJsonString(this GitVersionVariables gitVersionVariables)
51+
public static void ToFile(GitVersionVariables gitVersionVariables, string filePath, IFileSystem fileSystem)
3752
{
38-
var variablesType = typeof(VersionVariablesJsonModel);
39-
var variables = new VersionVariablesJsonModel();
40-
41-
foreach (var (key, value) in gitVersionVariables.OrderBy(x => x.Key))
53+
try
4254
{
43-
var propertyInfo = variablesType.GetProperty(key);
44-
propertyInfo?.SetValue(variables, ChangeType(value, propertyInfo.PropertyType));
55+
var retryAction = new RetryAction<IOException>();
56+
retryAction.Execute(() => ToFileInternal(gitVersionVariables, filePath, fileSystem));
4557
}
58+
catch (AggregateException ex)
59+
{
60+
var lastException = ex.InnerExceptions.LastOrDefault() ?? ex.InnerException;
61+
if (lastException != null)
62+
{
63+
throw lastException;
64+
}
4665

47-
var serializeOptions = JsonSerializerOptions();
48-
49-
return JsonSerializer.Serialize(variables, serializeOptions);
66+
throw;
67+
}
5068
}
5169

5270
private static GitVersionVariables FromDictionary(IEnumerable<KeyValuePair<string, string>>? properties)
@@ -65,11 +83,15 @@ private static GitVersionVariables FromDictionary(IEnumerable<KeyValuePair<strin
6583

6684
private static GitVersionVariables FromFileInternal(string filePath, IFileSystem fileSystem)
6785
{
68-
using var stream = fileSystem.OpenRead(filePath);
69-
using var reader = new StreamReader(stream);
70-
var dictionary = new Deserializer().Deserialize<Dictionary<string, string>>(reader);
71-
var versionVariables = FromDictionary(dictionary);
72-
return versionVariables;
86+
var json = fileSystem.ReadAllText(filePath);
87+
var variables = FromJson(json);
88+
return variables;
89+
}
90+
91+
private static void ToFileInternal(GitVersionVariables gitVersionVariables, string filePath, IFileSystem fileSystem)
92+
{
93+
var json = gitVersionVariables.ToJsonString();
94+
fileSystem.WriteAllText(filePath, json);
7395
}
7496

7597
private static JsonSerializerOptions JsonSerializerOptions()

src/GitVersion.Core/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -688,18 +688,14 @@ GitVersion.VersionCalculation.BaseVersion.ShouldIncrement.init -> void
688688
GitVersion.VersionCalculation.BaseVersion.Source.get -> string!
689689
GitVersion.VersionCalculation.BaseVersion.Source.init -> void
690690
GitVersion.VersionCalculation.Caching.GitVersionCache
691-
GitVersion.VersionCalculation.Caching.GitVersionCache.GetCacheDirectory() -> string!
692-
GitVersion.VersionCalculation.Caching.GitVersionCache.GetCacheFileName(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey) -> string!
693691
GitVersion.VersionCalculation.Caching.GitVersionCache.GitVersionCache(GitVersion.IFileSystem! fileSystem, GitVersion.Logging.ILog! log, GitVersion.IGitRepositoryInfo! repositoryInfo) -> void
694-
GitVersion.VersionCalculation.Caching.GitVersionCache.LoadVersionVariablesFromDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! key) -> GitVersion.OutputVariables.GitVersionVariables?
692+
GitVersion.VersionCalculation.Caching.GitVersionCache.LoadVersionVariablesFromDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey) -> GitVersion.OutputVariables.GitVersionVariables?
695693
GitVersion.VersionCalculation.Caching.GitVersionCache.WriteVariablesToDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey, GitVersion.OutputVariables.GitVersionVariables! variablesFromCache) -> void
696694
GitVersion.VersionCalculation.Caching.GitVersionCacheKey
697695
GitVersion.VersionCalculation.Caching.GitVersionCacheKey.GitVersionCacheKey(string! value) -> void
698696
GitVersion.VersionCalculation.Caching.GitVersionCacheKey.Value.get -> string!
699697
GitVersion.VersionCalculation.Caching.IGitVersionCache
700-
GitVersion.VersionCalculation.Caching.IGitVersionCache.GetCacheDirectory() -> string!
701-
GitVersion.VersionCalculation.Caching.IGitVersionCache.GetCacheFileName(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey) -> string!
702-
GitVersion.VersionCalculation.Caching.IGitVersionCache.LoadVersionVariablesFromDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! key) -> GitVersion.OutputVariables.GitVersionVariables?
698+
GitVersion.VersionCalculation.Caching.IGitVersionCache.LoadVersionVariablesFromDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey) -> GitVersion.OutputVariables.GitVersionVariables?
703699
GitVersion.VersionCalculation.Caching.IGitVersionCache.WriteVariablesToDiskCache(GitVersion.VersionCalculation.Caching.GitVersionCacheKey! cacheKey, GitVersion.OutputVariables.GitVersionVariables! variablesFromCache) -> void
704700
GitVersion.VersionCalculation.CommitMessageIncrementMode
705701
GitVersion.VersionCalculation.CommitMessageIncrementMode.Disabled = 1 -> GitVersion.VersionCalculation.CommitMessageIncrementMode
@@ -869,6 +865,7 @@ static GitVersion.Logging.LogExtensions.Warning(this GitVersion.Logging.ILog! lo
869865
static GitVersion.Logging.LogExtensions.Write(this GitVersion.Logging.ILog! log, GitVersion.Logging.LogLevel level, string! format, params object![]! args) -> void
870866
static GitVersion.OutputVariables.VersionVariablesHelper.FromFile(string! filePath, GitVersion.IFileSystem! fileSystem) -> GitVersion.OutputVariables.GitVersionVariables!
871867
static GitVersion.OutputVariables.VersionVariablesHelper.FromJson(string! json) -> GitVersion.OutputVariables.GitVersionVariables!
868+
static GitVersion.OutputVariables.VersionVariablesHelper.ToFile(GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables, string! filePath, GitVersion.IFileSystem! fileSystem) -> void
872869
static GitVersion.OutputVariables.VersionVariablesHelper.ToJsonString(this GitVersion.OutputVariables.GitVersionVariables! gitVersionVariables) -> string!
873870
static GitVersion.ReferenceName.FromBranchName(string! branchName) -> GitVersion.ReferenceName!
874871
static GitVersion.ReferenceName.Parse(string! canonicalName) -> GitVersion.ReferenceName!

0 commit comments

Comments
 (0)