Skip to content

Feature/introduce in memory git metadata #1243

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 33 additions & 0 deletions src/GitVersionCore.Tests/GitRepoInformation/QueryRequirements.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using GitTools;
using GitTools.Testing;
using GitVersion;
using GitVersion.GitRepoInformation;
using NUnit.Framework;
using Shouldly;
using System;

[TestFixture]
public class QueryRequirements
{
[Test]
public void ListsAllMergeCommits()
{
var config = new Config();
ConfigurationProvider.ApplyDefaultsTo(config);
using (var fixture = new EmptyRepositoryFixture())
{
fixture.MakeACommit();
fixture.MakeACommit();
fixture.BranchTo("feature/foo");
fixture.MakeACommit();
fixture.Checkout("master");
fixture.MergeNoFF("feature/foo");

fixture.Repository.DumpGraph();

var metadata = Libgit2RepoMetadataProvider.ReadMetadata(new GitVersionContext(fixture.Repository, config));

metadata.CurrentBranch.MergeMessages.Count.ShouldBe(1);
}
}
}
3 changes: 2 additions & 1 deletion src/GitVersionCore.Tests/GitVersionContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ IRepository CreateRepository()
mockBranch
},
Tags = new MockTagCollection(),
Head = mockBranch
Head = mockBranch,
ObjectDatabase = new MockObjectDatabase()
};

return mockRepository;
Expand Down
3 changes: 3 additions & 0 deletions src/GitVersionCore.Tests/GitVersionContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public void CanInheritVersioningMode(VersioningMode mode)
var mockBranch = new MockBranch("master") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
var mockRepository = new MockRepository
{
Head = mockBranch,
Branches = new MockBranchCollection
{
mockBranch
Expand Down Expand Up @@ -81,6 +82,7 @@ public void UsesBranchSpecificConfigOverTopLevelDefaults()
var develop = new MockBranch("develop") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
var mockRepository = new MockRepository
{
Head = develop,
Branches = new MockBranchCollection
{
new MockBranch("master") { new MockCommit { CommitterEx = Generate.SignatureNow() } },
Expand Down Expand Up @@ -109,6 +111,7 @@ public void UsesFirstBranchConfigWhenMultipleMatch()

var mockRepository = new MockRepository
{
Head = releaseLatestBranch,
Branches = new MockBranchCollection
{
releaseLatestBranch,
Expand Down
9 changes: 6 additions & 3 deletions src/GitVersionCore.Tests/GitVersionCore.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\NUnit3TestAdapter.3.8.0-alpha1\build\net35\NUnit3TestAdapter.props" Condition="Exists('..\packages\NUnit3TestAdapter.3.8.0-alpha1\build\net35\NUnit3TestAdapter.props')" />
<Import Project="..\packages\LibGit2Sharp.NativeBinaries.1.0.160\build\LibGit2Sharp.NativeBinaries.props" Condition="Exists('..\packages\LibGit2Sharp.NativeBinaries.1.0.160\build\LibGit2Sharp.NativeBinaries.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -58,9 +59,8 @@
<HintPath>..\packages\NSubstitute.1.10.0.0\lib\net45\NSubstitute.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=3.6.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.6.0\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
<Reference Include="nunit.framework, Version=3.7.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.7.1\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="Shouldly, Version=2.7.0.0, Culture=neutral, PublicKeyToken=6042cbcb05cbc941, processorArchitecture=MSIL">
<HintPath>..\packages\Shouldly.2.7.0\lib\net40\Shouldly.dll</HintPath>
Expand Down Expand Up @@ -94,6 +94,7 @@
<Compile Include="BuildServers\TeamCityTests.cs" />
<Compile Include="Configuration\IgnoreConfigTests.cs" />
<Compile Include="DynamicRepositoryTests.cs" />
<Compile Include="GitRepoInformation\QueryRequirements.cs" />
<Compile Include="GitRepoMetadataProviderTests.cs" />
<Compile Include="GitToolsTestingExtensions.cs" />
<Compile Include="DocumentationTests.cs" />
Expand All @@ -103,6 +104,7 @@
<Compile Include="IntegrationTests\FileSystemTests.cs" />
<Compile Include="IntegrationTests\MainlineDevelopmentMode.cs" />
<Compile Include="LogMessages.cs" />
<Compile Include="MockObjectDatabase.cs" />
<Compile Include="Mocks\MockThreadSleep.cs" />
<Compile Include="OperationWithExponentialBackoffTests.cs" />
<Compile Include="Init\InitScenarios.cs" />
Expand Down Expand Up @@ -190,6 +192,7 @@
</PropertyGroup>
<Error Condition="!Exists('..\packages\LibGit2Sharp.NativeBinaries.1.0.160\build\LibGit2Sharp.NativeBinaries.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\LibGit2Sharp.NativeBinaries.1.0.160\build\LibGit2Sharp.NativeBinaries.props'))" />
<Error Condition="!Exists('..\packages\Fody.2.0.8\build\dotnet\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.2.0.8\build\dotnet\Fody.targets'))" />
<Error Condition="!Exists('..\packages\NUnit3TestAdapter.3.8.0-alpha1\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit3TestAdapter.3.8.0-alpha1\build\net35\NUnit3TestAdapter.props'))" />
</Target>
<Import Project="..\packages\Fody.2.0.8\build\dotnet\Fody.targets" Condition="Exists('..\packages\Fody.2.0.8\build\dotnet\Fody.targets')" />
</Project>
24 changes: 24 additions & 0 deletions src/GitVersionCore.Tests/IntegrationTests/OtherScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using LibGit2Sharp;
using NUnit.Framework;
using System.Collections.Generic;
using System;

[TestFixture]
public class OtherScenarios
Expand Down Expand Up @@ -91,5 +92,28 @@ public void DoNotBlowUpWhenDevelopAndFeatureBranchPointAtSameCommit()
fixture.AssertFullSemver("1.1.0-alpha.1");
}
}

[Test]
public void AllowUnrelatedBranchesInRepo()
{
// This test unsures we handle when GitVersion cannot find mergebases etc
using (var fixture = new EmptyRepositoryFixture())
{
fixture.Repository.MakeACommit();
fixture.Repository.MakeACommit();

// Create a new root commit and then a branch pointing at that commit
var treeDefinition = new TreeDefinition();
var tree = fixture.Repository.ObjectDatabase.CreateTree(treeDefinition);
var commit = fixture.Repository.ObjectDatabase.CreateCommit(
new Signature("name", "mail", DateTimeOffset.Now),
new Signature("name", "mail", DateTimeOffset.Now),
"Create new empty branch",
tree, new Commit[0], false);
fixture.Repository.Branches.Add("gh-pages", commit);

fixture.AssertFullSemver("0.1.0+1");
}
}
}
}
9 changes: 9 additions & 0 deletions src/GitVersionCore.Tests/MockObjectDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using LibGit2Sharp;

public class MockObjectDatabase : ObjectDatabase
{
public override Commit FindMergeBase(Commit first, Commit second)
{
return second;
}
}
1 change: 1 addition & 0 deletions src/GitVersionCore.Tests/Mocks/MockRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public MockRepository()
{
Tags = new MockTagCollection();
Refs = new MockReferenceCollection();
ObjectDatabase = new MockObjectDatabase();
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ public void ShouldNotAllowIncrementOfVersion()
// So we shouldn't bump the version
var context = new GitVersionContextBuilder().WithRepository(new MockRepository
{
Head = new MockBranch("master") { new MockCommit
Head = new MockBranch("master")
{
MessageEx = "Merge branch 'hotfix-0.1.5'",
ParentsEx = GetParents(true)
} }
new MockCommit
{
MessageEx = "Merge branch 'hotfix-0.1.5'",
ParentsEx = GetParents(true)
}
},
Branches = new MockBranchCollection()
}).Build();
var sut = new MergeMessageBaseVersionStrategy();

Expand Down Expand Up @@ -108,7 +112,8 @@ static void AssertMergeMessage(string message, string expectedVersion, List<Comm
{
commit,
new MockCommit()
}
},
Branches = new MockBranchCollection()
})
.Build();
var sut = new MergeMessageBaseVersionStrategy();
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersionCore.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<package id="LibGit2Sharp.NativeBinaries" version="1.0.160" targetFramework="net45" />
<package id="ModuleInit.Fody" version="1.6.0" targetFramework="net45" developmentDependency="true" />
<package id="NSubstitute" version="1.10.0.0" targetFramework="net45" />
<package id="NUnit" version="3.6.0" targetFramework="net45" />
<package id="NUnit3TestAdapter" version="3.7.0" targetFramework="net45" />
<package id="NUnit" version="3.7.1" targetFramework="net45" />
<package id="NUnit3TestAdapter" version="3.8.0-alpha1" targetFramework="net45" />
<package id="Shouldly" version="2.7.0" targetFramework="net45" />
<package id="TestStack.ConventionTests" version="3.0.0" targetFramework="net45" />
<package id="YamlDotNet" version="3.8.0" targetFramework="net45" />
Expand Down
76 changes: 76 additions & 0 deletions src/GitVersionCore/GitRepoInformation/GitRepoMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Collections.Generic;

namespace GitVersion.GitRepoInformation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is going to be GitVersion's "Core", i.e. Domain Model (in DDD terms), wouldn't a more authoritative sounding namespace like GitVersion.Repositories, GitVersion.Graph be good?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like GitVersion.Graph! Will keep that in mind as a progress

{
public class GitRepoMetadata
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the repository metadata or data? Since GitVersion's job is to walk the graph, count commits, etc., I'd say that anything required to calculate the version is data, not metadata. I think this class can be called just Repository, no? Any collisions with LibGit2 should be resolved within the GitVersion.LibGit2 project and not exposed elsewhere, so it shouldn't be much of a problem, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure at the moment to be honest. I don't want to re-implement git data structures, this really is information about the commit graph with a bunch of pre-calculated stuff. Lets see where it goes then fix naming next

{
public GitRepoMetadata(
List<MTag> tags, MBranch currentBranch, MBranch master,
List<MBranch> releaseBranches)
{
Tags = tags;
MasterBranch = master;
CurrentBranch = currentBranch;
ReleaseBranches = releaseBranches;
}

// Wonder if this can be 'mainline'
public MBranch MasterBranch { get; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the immutability here. 👍

public List<MBranch> ReleaseBranches { get; }
public MBranch CurrentBranch { get; }
public List<MTag> Tags { get; private set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the private setter?

}

public class MTag
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the M prefix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than aliasing in a bunch of files which reference both this namespace and libgit2 the metadata related ones are prefixed with M. I expect this to go at some point

{
public MTag(string sha, string friendlyName, Config config, bool createdAfterHead)
{
Sha = sha;
Name = friendlyName;
CreatedAfterHead = createdAfterHead;
SemanticVersion version;
if (SemanticVersion.TryParse(friendlyName, config.TagPrefix, out version))
{
Version = version;
}
}

public string Sha { get; }
public string Name { get; }
public bool CreatedAfterHead { get; }
public SemanticVersion Version { get; }
}

public class MBranch
{
public MBranch(
string name,
string tipSha,
MParent parent,
List<MTag> tags,
List<MergeMessage> mergeMessages)
{
Name = name;
TipSha = tipSha;
Parent = parent;
MergeMessages = mergeMessages;
Tags = tags;
}

public string Name { get; }
public string TipSha { get; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the Tip can be an object having a Sha property instead?

public MParent Parent { get; }
public List<MergeMessage> MergeMessages { get; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it matters much, I've just got a habit of using IList<T> instead of List<T> in public declarations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

public List<MTag> Tags { get; }
}

public class MParent
{
public MParent(string mergeBase)
{
MergeBase = mergeBase;
}

public string MergeBase { get; }
}
}
Loading