Skip to content

Commit 3721c1d

Browse files
committed
Merge pull request #808 from okb/feature/config-should-invalidate-cache
Include the config hash in the cache key
2 parents 21baea1 + e1c3bf8 commit 3721c1d

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

src/GitVersionCore.Tests/ExecuteCoreTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Text;
34

45
using GitVersion;
@@ -68,6 +69,53 @@ public void CacheFileIsMissing()
6869
info.ShouldContain("yml not found", () => info);
6970
}
7071

72+
73+
[Test]
74+
public void ConfigChangeInvalidatesCache()
75+
{
76+
const string versionCacheFileContent = @"
77+
Major: 4
78+
Minor: 10
79+
Patch: 3
80+
PreReleaseTag: test.19
81+
PreReleaseTagWithDash: -test.19
82+
PreReleaseLabel: test
83+
PreReleaseNumber: 19
84+
BuildMetaData:
85+
BuildMetaDataPadded:
86+
FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
87+
MajorMinorPatch: 4.10.3
88+
SemVer: 4.10.3-test.19
89+
LegacySemVer: 4.10.3-test19
90+
LegacySemVerPadded: 4.10.3-test0019
91+
AssemblySemVer: 4.10.3.0
92+
FullSemVer: 4.10.3-test.19
93+
InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
94+
BranchName: feature/test
95+
Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
96+
NuGetVersionV2: 4.10.3-test0019
97+
NuGetVersion: 4.10.3-test0019
98+
CommitsSinceVersionSource: 19
99+
CommitsSinceVersionSourcePadded: 0019
100+
CommitDate: 2015-11-10
101+
";
102+
103+
var versionAndBranchFinder = new ExecuteCore(fileSystem);
104+
105+
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
106+
{
107+
fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
108+
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
109+
vv.AssemblySemVer.ShouldBe("4.10.3.0");
110+
111+
var configPath = Path.Combine(fixture.RepositoryPath, "GitVersionConfig.yaml");
112+
fileSystem.WriteAllText(configPath, "next-version: 5.0");
113+
114+
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
115+
vv.AssemblySemVer.ShouldBe("5.0.0.0");
116+
});
117+
}
118+
71119
string RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
72120
{
73121
// Make sure GitVersion doesn't trigger build server mode when we are running the tests

src/GitVersionCore/GitVersionCache.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,32 @@ string GetKey(IRepository repo, string gitDir)
8989
{
9090
// Maybe using timestamp in .git/refs directory is enough?
9191
var ticks = fileSystem.GetLastDirectoryWrite(Path.Combine(gitDir, "refs"));
92-
return string.Format("{0}:{1}:{2}:{3}", gitDir, repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks);
92+
var configPath = Path.Combine(repo.GetRepositoryDirectory(), "GitVersionConfig.yaml");
93+
var configText = fileSystem.Exists(configPath) ? fileSystem.ReadAllText(configPath) : null;
94+
var configHash = configText != null ? GetHash(configText) : null;
95+
return string.Join(":", gitDir, repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks, configHash);
9396
}
9497

9598
static string GetCacheFileName(string key, string cacheDir)
9699
{
97-
string cacheKey;
98-
using (var sha1 = SHA1.Create())
99-
{
100-
// Make a shorter key by hashing, to avoid having to long cache filename.
101-
cacheKey = BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(key))).Replace("-", "");
102-
}
103-
var cacheFileName = string.Concat(Path.Combine(cacheDir, cacheKey), ".yml");
104-
return cacheFileName;
100+
var cacheKey = GetHash(key);
101+
return string.Concat(Path.Combine(cacheDir, cacheKey), ".yml");
105102
}
106103

107104
static string GetCacheDir(string gitDir)
108105
{
109106
return Path.Combine(gitDir, "gitversion_cache");
110107
}
108+
109+
static string GetHash(string textToHash)
110+
{
111+
using (var sha1 = SHA1.Create())
112+
{
113+
var bytes = Encoding.UTF8.GetBytes(textToHash);
114+
var hashedBytes = sha1.ComputeHash(bytes);
115+
var hashedString = BitConverter.ToString(hashedBytes);
116+
return hashedString.Replace("-", "");
117+
}
118+
}
111119
}
112120
}

0 commit comments

Comments
 (0)