Skip to content

Feature/task disk cache #737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 37 commits into from
Jan 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f2256b2
Enable disk caching of version info.
asbjornu Aug 25, 2015
4be1c31
Do scoped `IndentLog` instead of `WriteInfo` before and after.
asbjornu Oct 22, 2015
e20298d
Write != Read.
asbjornu Oct 23, 2015
cc2981d
Add .yml extension to the cached version file
asbjornu Oct 23, 2015
449515f
Improved logging scopes and code readability
asbjornu Oct 23, 2015
0291d9a
Clarify that we're loading version from disk cache if it can't be fou…
asbjornu Nov 10, 2015
4a43fb0
We don't need to check if directories exist before doing CreateDirect…
asbjornu Nov 10, 2015
cd4c474
R# cleanup
asbjornu Nov 10, 2015
dccc4fd
Moved TreeWalkForDotGitDir() from GitDirFinder to IFileSystem to make…
asbjornu Nov 10, 2015
6878c1f
Moved RepositoryLoader.GetRepo() to IFileSystem.GetRepository() to ma…
asbjornu Nov 10, 2015
0df3832
Moved GetLastDirectoryWrite() from DirectoryDateFinder to IFileSystem…
asbjornu Nov 10, 2015
fb8003f
IRepository is easier to mock than Repository
asbjornu Nov 10, 2015
0d1a33b
Added NSubstitute NuGet package reference to make mocking a non-tedio…
asbjornu Nov 10, 2015
e09c5d2
Log if the version cache file can't be found
asbjornu Nov 10, 2015
b6b86b2
Added tests for VersionBranchFinder.GetVersion() and its use of the v…
asbjornu Nov 10, 2015
7a67bb8
R# cleanup
asbjornu Nov 13, 2015
8271863
Use string.Equals() with explicit StringComparison
asbjornu Nov 13, 2015
3b25539
Use the AvailableVariables property instead of doing GetProperties() …
asbjornu Nov 13, 2015
8d49c7b
Don't crash if cleanup fails
asbjornu Nov 13, 2015
7284ed5
Add [ReflectionIgnore] attribute to properties in VersionVariables so…
asbjornu Nov 13, 2015
ea02513
Added `VersionVariables.FromFile()` method and corresponding `FileNam…
asbjornu Nov 13, 2015
5a4b29b
Make the `VersionAndBranchFinder.VersionCacheVersions` dictionary int…
asbjornu Nov 13, 2015
e22e66b
Rewrote the VersionAndBranchFinder tests so they do actual file syste…
asbjornu Nov 13, 2015
5076a66
Made the VersionAndBranchFinder tests a bit more DRY
asbjornu Nov 13, 2015
ec2d796
Deleted `IFileSystem.TreeWalkForDotGitDir()` and replaced its usage w…
asbjornu Nov 16, 2015
c2b0c34
Removed license
asbjornu Nov 16, 2015
5b408dd
Removed mocking of IRepository since it's not being used by any of th…
asbjornu Nov 16, 2015
1f9973e
R# cleanup
asbjornu Nov 16, 2015
be4dee3
Refactored `VersionAndBranchFinder` to a non-static file.
asbjornu Nov 16, 2015
ca3d183
Avoid `NullReferenceException` in `Logger` by null-guarding the logge…
asbjornu Nov 16, 2015
c70c576
Deleted `VersionAndBranchFinder`, moved its caching logic into `Execu…
asbjornu Nov 16, 2015
6bcfecf
Small code cleanup and fixed build server issues
JakeGinnivan Jan 17, 2016
a0af556
Fixed merge issues
JakeGinnivan Jan 17, 2016
d7a6402
Removing some formatting changes
JakeGinnivan Jan 17, 2016
64c50bc
Separating the caching logic out a bit
JakeGinnivan Jan 17, 2016
6d01254
Restructured cache code
JakeGinnivan Jan 17, 2016
9b336cd
Another small restructuring and removing busted test
JakeGinnivan Jan 17, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/GitVersionCore.Tests/ExecuteCoreTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Text;

using GitVersion;
using GitVersion.Helpers;

using NUnit.Framework;

using Shouldly;

[TestFixture]
public class ExecuteCoreTests
{
IFileSystem fileSystem;

[SetUp]
public void SetUp()
{
fileSystem = new FileSystem();
}

[Test]
public void CacheFileExistsOnDisk()
{
const string versionCacheFileContent = @"
Major: 4
Minor: 10
Patch: 3
PreReleaseTag: test.19
PreReleaseTagWithDash: -test.19
BuildMetaData:
BuildMetaDataPadded:
FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
MajorMinorPatch: 4.10.3
SemVer: 4.10.3-test.19
LegacySemVer: 4.10.3-test19
LegacySemVerPadded: 4.10.3-test0019
AssemblySemVer: 4.10.3.0
FullSemVer: 4.10.3-test.19
InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
BranchName: feature/test
Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
NuGetVersionV2: 4.10.3-test0019
NuGetVersion: 4.10.3-test0019
CommitsSinceVersionSource: 19
CommitsSinceVersionSourcePadded: 0019
CommitDate: 2015-11-10
";

var versionAndBranchFinder = new ExecuteCore(fileSystem);

var info = RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
{
fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
vv.AssemblySemVer.ShouldBe("4.10.3.0");
});

info.ShouldContain("Deserializing version variables from cache file", () => info);
}

[Test]
public void CacheFileIsMissing()
{
var info = RepositoryScope();
info.ShouldContain("yml not found", () => info);
}

string RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
{
// Make sure GitVersion doesn't trigger build server mode when we are running the tests
Environment.SetEnvironmentVariable("APPVEYOR", null);
var infoBuilder = new StringBuilder();
Action<string> infoLogger = s => { infoBuilder.AppendLine(s); };
executeCore = executeCore ?? new ExecuteCore(fileSystem);

Logger.SetLoggers(infoLogger, s => { }, s => { });

using (var fixture = new EmptyRepositoryFixture(new Config()))
{
fixture.Repository.MakeACommit();
var vv = executeCore.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);

vv.AssemblySemVer.ShouldBe("0.1.0.0");
vv.FileName.ShouldNotBeNullOrEmpty();

if (fixtureAction != null)
{
fixtureAction(fixture, vv);
}
}

return infoBuilder.ToString();
}
}
55 changes: 0 additions & 55 deletions src/GitVersionCore.Tests/GitDirFinderTests.cs

This file was deleted.

6 changes: 5 additions & 1 deletion src/GitVersionCore.Tests/GitVersionCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
<HintPath>..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.Rocks.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NSubstitute, Version=1.9.2.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
<HintPath>..\packages\NSubstitute.1.9.2.0\lib\net45\NSubstitute.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.core, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -125,7 +129,6 @@
<Compile Include="ConfigProviderTests.cs" />
<Compile Include="Fixtures\LocalRepositoryFixture.cs" />
<Compile Include="Fixtures\RemoteRepositoryFixture.cs" />
<Compile Include="GitDirFinderTests.cs" />
<Compile Include="Fixtures\BaseGitFlowRepositoryFixture.cs" />
<Compile Include="GitHelperTests.cs" />
<Compile Include="GitPreparerTests.cs" />
Expand Down Expand Up @@ -175,6 +178,7 @@
<Compile Include="TestFileSystem.cs" />
<Compile Include="TestStream.cs" />
<Compile Include="VariableProviderTests.cs" />
<Compile Include="ExecuteCoreTests.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculatorTests.cs" />
<Compile Include="VersionCalculation\NextVersionCalculatorTests.cs" />
<Compile Include="VersionCalculation\Strategies\ConfigNextVersionBaseVersionStrategyTests.cs" />
Expand Down
26 changes: 26 additions & 0 deletions src/GitVersionCore.Tests/TestFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using GitVersion.Helpers;

public class TestFileSystem : IFileSystem
Expand Down Expand Up @@ -35,9 +37,13 @@ public string ReadAllText(string path)
public void WriteAllText(string file, string fileContents)
{
if (fileSystem.ContainsKey(file))
{
fileSystem[file] = fileContents;
}
else
{
fileSystem.Add(file, fileContents);
}
}

public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
Expand All @@ -49,4 +55,24 @@ public Stream OpenWrite(string path)
{
return new TestStream(path, this);
}

public Stream OpenRead(string path)
{
if (fileSystem.ContainsKey(path))
{
var content = fileSystem[path];
return new MemoryStream(Encoding.UTF8.GetBytes(content));
}

throw new FileNotFoundException("File not found.", path);
}

public void CreateDirectory(string path)
{
}

public long GetLastDirectoryWrite(string path)
{
return 1;
}
}
1 change: 1 addition & 0 deletions src/GitVersionCore.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<package id="LibGit2Sharp" version="0.21.0.176" targetFramework="net45" />
<package id="ModuleInit.Fody" version="1.5.8.0" targetFramework="net45" developmentDependency="true" />
<package id="Mono.Cecil" version="0.9.6.1" targetFramework="net45" />
<package id="NSubstitute" version="1.9.2.0" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" />
<package id="NUnitTestAdapter" version="2.0.0" targetFramework="net45" />
<package id="Shouldly" version="2.5.0" targetFramework="net45" />
Expand Down
101 changes: 83 additions & 18 deletions src/GitVersionCore/ExecuteCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ namespace GitVersion
using System.Linq;
using GitVersion.Helpers;

public static class ExecuteCore
using LibGit2Sharp;

public class ExecuteCore
{
public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId)
readonly IFileSystem fileSystem;
readonly GitVersionCache gitVersionCache;

public ExecuteCore(IFileSystem fileSystem)
{
if (fileSystem == null) throw new ArgumentNullException("fileSystem");

this.fileSystem = fileSystem;
gitVersionCache = new GitVersionCache(fileSystem);
}

public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId)
{
// Normalise if we are running on build server
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, noFetch, workingDirectory);
var applicableBuildServers = BuildServerList.GetApplicableBuildServers();
var buildServer = applicableBuildServers.FirstOrDefault();

gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch));

var dotGitDirectory = gitPreparer.GetDotGitDirectory();
var projectRoot = gitPreparer.GetProjectRootDirectory();
Logger.WriteInfo(string.Format("Project root is: " + projectRoot));
Expand All @@ -23,28 +31,85 @@ public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string
// TODO Link to wiki article
throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'.", workingDirectory));
}
VersionVariables variables;
var versionFinder = new GitVersionFinder();
var configuration = ConfigurationProvider.Provide(projectRoot, fileSystem);

using (var repo = RepositoryLoader.GetRepo(dotGitDirectory))

using (var repo = GetRepository(dotGitDirectory))
{
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: commitId);
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
variables = VariableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
var versionVariables = gitVersionCache.LoadVersionVariablesFromDiskCache(repo, dotGitDirectory);
if (versionVariables == null)
{
versionVariables = ExecuteInternal(targetBranch, commitId, repo, gitPreparer, projectRoot);
gitVersionCache.WriteVariablesToDiskCache(repo, dotGitDirectory, versionVariables);
}

return versionVariables;
}
}

return variables;
public bool TryGetVersion(string directory, out VersionVariables versionVariables, bool noFetch, Authentication authentication)
{
try
{
versionVariables = ExecuteGitVersion(null, null, authentication, null, noFetch, directory, null);
return true;
}
catch (Exception ex)
{
Logger.WriteWarning("Could not determine assembly version: " + ex);
versionVariables = null;
return false;
}
}

private static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch)
static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch)
{
if (buildServer == null) return targetBranch;
if (buildServer == null)
{
return targetBranch;
}

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

return currentBranch;
}

VersionVariables ExecuteInternal(string targetBranch, string commitId, IRepository repo, GitPreparer gitPreparer, string projectRoot)
{
var applicableBuildServers = BuildServerList.GetApplicableBuildServers();
var buildServer = applicableBuildServers.FirstOrDefault();

gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch));

var versionFinder = new GitVersionFinder();
var configuration = ConfigurationProvider.Provide(projectRoot, fileSystem);

var gitVersionContext = new GitVersionContext(repo, configuration, commitId : commitId);
var semanticVersion = versionFinder.FindVersion(gitVersionContext);

return VariableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
}

IRepository GetRepository(string gitDirectory)
{
try
{
var repository = new Repository(gitDirectory);

var branch = repository.Head;
if (branch.Tip == null)
{
throw new WarningException("No Tip found. Has repo been initialized?");
}
return repository;
}
catch (Exception exception)
{
if (exception.Message.Contains("LibGit2Sharp.Core.NativeMethods") || exception.Message.Contains("FilePathMarshaler"))
{
throw new WarningException("Restart of the process may be required to load an updated version of LibGit2Sharp.");
}
throw;
}
}
}
}
20 changes: 0 additions & 20 deletions src/GitVersionCore/GitDirFinder.cs

This file was deleted.

Loading