Skip to content

Commit 5d1c303

Browse files
committed
Merge pull request #737 from JakeGinnivan/feature/task-disk-cache
Feature/task disk cache
2 parents 1f49eff + 9b336cd commit 5d1c303

30 files changed

+503
-346
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Text;
3+
4+
using GitVersion;
5+
using GitVersion.Helpers;
6+
7+
using NUnit.Framework;
8+
9+
using Shouldly;
10+
11+
[TestFixture]
12+
public class ExecuteCoreTests
13+
{
14+
IFileSystem fileSystem;
15+
16+
[SetUp]
17+
public void SetUp()
18+
{
19+
fileSystem = new FileSystem();
20+
}
21+
22+
[Test]
23+
public void CacheFileExistsOnDisk()
24+
{
25+
const string versionCacheFileContent = @"
26+
Major: 4
27+
Minor: 10
28+
Patch: 3
29+
PreReleaseTag: test.19
30+
PreReleaseTagWithDash: -test.19
31+
BuildMetaData:
32+
BuildMetaDataPadded:
33+
FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
34+
MajorMinorPatch: 4.10.3
35+
SemVer: 4.10.3-test.19
36+
LegacySemVer: 4.10.3-test19
37+
LegacySemVerPadded: 4.10.3-test0019
38+
AssemblySemVer: 4.10.3.0
39+
FullSemVer: 4.10.3-test.19
40+
InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
41+
BranchName: feature/test
42+
Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
43+
NuGetVersionV2: 4.10.3-test0019
44+
NuGetVersion: 4.10.3-test0019
45+
CommitsSinceVersionSource: 19
46+
CommitsSinceVersionSourcePadded: 0019
47+
CommitDate: 2015-11-10
48+
";
49+
50+
var versionAndBranchFinder = new ExecuteCore(fileSystem);
51+
52+
var info = RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
53+
{
54+
fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
55+
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
56+
vv.AssemblySemVer.ShouldBe("4.10.3.0");
57+
});
58+
59+
info.ShouldContain("Deserializing version variables from cache file", () => info);
60+
}
61+
62+
[Test]
63+
public void CacheFileIsMissing()
64+
{
65+
var info = RepositoryScope();
66+
info.ShouldContain("yml not found", () => info);
67+
}
68+
69+
string RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
70+
{
71+
// Make sure GitVersion doesn't trigger build server mode when we are running the tests
72+
Environment.SetEnvironmentVariable("APPVEYOR", null);
73+
var infoBuilder = new StringBuilder();
74+
Action<string> infoLogger = s => { infoBuilder.AppendLine(s); };
75+
executeCore = executeCore ?? new ExecuteCore(fileSystem);
76+
77+
Logger.SetLoggers(infoLogger, s => { }, s => { });
78+
79+
using (var fixture = new EmptyRepositoryFixture(new Config()))
80+
{
81+
fixture.Repository.MakeACommit();
82+
var vv = executeCore.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
83+
84+
vv.AssemblySemVer.ShouldBe("0.1.0.0");
85+
vv.FileName.ShouldNotBeNullOrEmpty();
86+
87+
if (fixtureAction != null)
88+
{
89+
fixtureAction(fixture, vv);
90+
}
91+
}
92+
93+
return infoBuilder.ToString();
94+
}
95+
}

src/GitVersionCore.Tests/GitDirFinderTests.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
<HintPath>..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.Rocks.dll</HintPath>
7373
<Private>True</Private>
7474
</Reference>
75+
<Reference Include="NSubstitute, Version=1.9.2.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
76+
<HintPath>..\packages\NSubstitute.1.9.2.0\lib\net45\NSubstitute.dll</HintPath>
77+
<Private>True</Private>
78+
</Reference>
7579
<Reference Include="nunit.core, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
7680
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll</HintPath>
7781
<Private>True</Private>
@@ -125,7 +129,6 @@
125129
<Compile Include="ConfigProviderTests.cs" />
126130
<Compile Include="Fixtures\LocalRepositoryFixture.cs" />
127131
<Compile Include="Fixtures\RemoteRepositoryFixture.cs" />
128-
<Compile Include="GitDirFinderTests.cs" />
129132
<Compile Include="Fixtures\BaseGitFlowRepositoryFixture.cs" />
130133
<Compile Include="GitHelperTests.cs" />
131134
<Compile Include="GitPreparerTests.cs" />
@@ -175,6 +178,7 @@
175178
<Compile Include="TestFileSystem.cs" />
176179
<Compile Include="TestStream.cs" />
177180
<Compile Include="VariableProviderTests.cs" />
181+
<Compile Include="ExecuteCoreTests.cs" />
178182
<Compile Include="VersionCalculation\BaseVersionCalculatorTests.cs" />
179183
<Compile Include="VersionCalculation\NextVersionCalculatorTests.cs" />
180184
<Compile Include="VersionCalculation\Strategies\ConfigNextVersionBaseVersionStrategyTests.cs" />

src/GitVersionCore.Tests/TestFileSystem.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Text;
5+
46
using GitVersion.Helpers;
57

68
public class TestFileSystem : IFileSystem
@@ -35,9 +37,13 @@ public string ReadAllText(string path)
3537
public void WriteAllText(string file, string fileContents)
3638
{
3739
if (fileSystem.ContainsKey(file))
40+
{
3841
fileSystem[file] = fileContents;
42+
}
3943
else
44+
{
4045
fileSystem.Add(file, fileContents);
46+
}
4147
}
4248

4349
public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
@@ -49,4 +55,24 @@ public Stream OpenWrite(string path)
4955
{
5056
return new TestStream(path, this);
5157
}
58+
59+
public Stream OpenRead(string path)
60+
{
61+
if (fileSystem.ContainsKey(path))
62+
{
63+
var content = fileSystem[path];
64+
return new MemoryStream(Encoding.UTF8.GetBytes(content));
65+
}
66+
67+
throw new FileNotFoundException("File not found.", path);
68+
}
69+
70+
public void CreateDirectory(string path)
71+
{
72+
}
73+
74+
public long GetLastDirectoryWrite(string path)
75+
{
76+
return 1;
77+
}
5278
}

src/GitVersionCore.Tests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<package id="LibGit2Sharp" version="0.21.0.176" targetFramework="net45" />
88
<package id="ModuleInit.Fody" version="1.5.8.0" targetFramework="net45" developmentDependency="true" />
99
<package id="Mono.Cecil" version="0.9.6.1" targetFramework="net45" />
10+
<package id="NSubstitute" version="1.9.2.0" targetFramework="net45" />
1011
<package id="NUnit" version="2.6.4" targetFramework="net45" />
1112
<package id="NUnitTestAdapter" version="2.0.0" targetFramework="net45" />
1213
<package id="Shouldly" version="2.5.0" targetFramework="net45" />

src/GitVersionCore/ExecuteCore.cs

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@ namespace GitVersion
44
using System.Linq;
55
using GitVersion.Helpers;
66

7-
public static class ExecuteCore
7+
using LibGit2Sharp;
8+
9+
public class ExecuteCore
810
{
9-
public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId)
11+
readonly IFileSystem fileSystem;
12+
readonly GitVersionCache gitVersionCache;
13+
14+
public ExecuteCore(IFileSystem fileSystem)
15+
{
16+
if (fileSystem == null) throw new ArgumentNullException("fileSystem");
17+
18+
this.fileSystem = fileSystem;
19+
gitVersionCache = new GitVersionCache(fileSystem);
20+
}
21+
22+
public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId)
1023
{
1124
// Normalise if we are running on build server
1225
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, noFetch, workingDirectory);
13-
var applicableBuildServers = BuildServerList.GetApplicableBuildServers();
14-
var buildServer = applicableBuildServers.FirstOrDefault();
15-
16-
gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch));
17-
1826
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
1927
var projectRoot = gitPreparer.GetProjectRootDirectory();
2028
Logger.WriteInfo(string.Format("Project root is: " + projectRoot));
@@ -23,28 +31,85 @@ public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string
2331
// TODO Link to wiki article
2432
throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'.", workingDirectory));
2533
}
26-
VersionVariables variables;
27-
var versionFinder = new GitVersionFinder();
28-
var configuration = ConfigurationProvider.Provide(projectRoot, fileSystem);
29-
30-
using (var repo = RepositoryLoader.GetRepo(dotGitDirectory))
34+
35+
using (var repo = GetRepository(dotGitDirectory))
3136
{
32-
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: commitId);
33-
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
34-
variables = VariableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
37+
var versionVariables = gitVersionCache.LoadVersionVariablesFromDiskCache(repo, dotGitDirectory);
38+
if (versionVariables == null)
39+
{
40+
versionVariables = ExecuteInternal(targetBranch, commitId, repo, gitPreparer, projectRoot);
41+
gitVersionCache.WriteVariablesToDiskCache(repo, dotGitDirectory, versionVariables);
42+
}
43+
44+
return versionVariables;
3545
}
46+
}
3647

37-
return variables;
48+
public bool TryGetVersion(string directory, out VersionVariables versionVariables, bool noFetch, Authentication authentication)
49+
{
50+
try
51+
{
52+
versionVariables = ExecuteGitVersion(null, null, authentication, null, noFetch, directory, null);
53+
return true;
54+
}
55+
catch (Exception ex)
56+
{
57+
Logger.WriteWarning("Could not determine assembly version: " + ex);
58+
versionVariables = null;
59+
return false;
60+
}
3861
}
3962

40-
private static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch)
63+
static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch)
4164
{
42-
if (buildServer == null) return targetBranch;
65+
if (buildServer == null)
66+
{
67+
return targetBranch;
68+
}
4369

4470
var currentBranch = buildServer.GetCurrentBranch() ?? targetBranch;
4571
Logger.WriteInfo("Branch from build environment: " + currentBranch);
4672

4773
return currentBranch;
4874
}
75+
76+
VersionVariables ExecuteInternal(string targetBranch, string commitId, IRepository repo, GitPreparer gitPreparer, string projectRoot)
77+
{
78+
var applicableBuildServers = BuildServerList.GetApplicableBuildServers();
79+
var buildServer = applicableBuildServers.FirstOrDefault();
80+
81+
gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch));
82+
83+
var versionFinder = new GitVersionFinder();
84+
var configuration = ConfigurationProvider.Provide(projectRoot, fileSystem);
85+
86+
var gitVersionContext = new GitVersionContext(repo, configuration, commitId : commitId);
87+
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
88+
89+
return VariableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
90+
}
91+
92+
IRepository GetRepository(string gitDirectory)
93+
{
94+
try
95+
{
96+
var repository = new Repository(gitDirectory);
97+
98+
var branch = repository.Head;
99+
if (branch.Tip == null)
100+
{
101+
throw new WarningException("No Tip found. Has repo been initialized?");
102+
}
103+
return repository;
104+
}
105+
catch (Exception exception)
106+
{
107+
if (exception.Message.Contains("LibGit2Sharp.Core.NativeMethods") || exception.Message.Contains("FilePathMarshaler"))
108+
{
109+
throw new WarningException("Restart of the process may be required to load an updated version of LibGit2Sharp.");
110+
}
111+
throw;
112+
}
113+
}
49114
}
50115
}

src/GitVersionCore/GitDirFinder.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)