Skip to content

Fix for #232: GitHubVersion for pull requests are not working with dynamic repositories #245

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 15 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
1 change: 1 addition & 0 deletions GitVersion.sln.GhostDoc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Protected>false</Protected>
<Private>false</Private>
<Inherited>true</Inherited>
<InheritedFromReferences>true</InheritedFromReferences>
<EnableTags>false</EnableTags>
<TagList />
</IncludeScopes>
Expand Down
49 changes: 19 additions & 30 deletions GitVersionCore.Tests/GitFlow/MetaDataByCommitFixture.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
 using System;
using GitVersion;
using LibGit2Sharp;
using NUnit.Framework;
using Shouldly;
using LibGit2Sharp;
using NUnit.Framework;
using Shouldly;

[TestFixture]
public class MetaDataByCommitFixture
Expand Down Expand Up @@ -33,65 +31,56 @@ public void CanCorrectlyDetectCommitCountsAndReleaseDataWhenThatApplies()
using (var f = new CommitCountingRepoFixture())
{
ResetToP(f.Repository);
EnsureMetaDataMatch(f, "develop", "1.4.0-unstable.7+7");
EnsureMetaDataMatch(f, "1.4.0-unstable.7+7");

ResetToO(f.Repository);
EnsureMetaDataMatch(f, "develop", "1.4.0-unstable.6+6");
EnsureMetaDataMatch(f, "1.4.0-unstable.6+6");

ResetToN(f.Repository);
EnsureMetaDataMatch(f, "master", "1.3.1", r => (Commit) r.Tags["1.3.0"].Target);
EnsureMetaDataMatch(f, "1.3.1");

ResetToM(f.Repository);
EnsureMetaDataMatch(f, "develop", "1.4.0-unstable.5+5");
EnsureMetaDataMatch(f, "1.4.0-unstable.5+5");

ResetToL(f.Repository);
EnsureMetaDataMatch(f, "hotfix-1.3.1", "1.3.1-beta.1+1", r => (Commit) r.Tags["1.3.0"].Target);
EnsureMetaDataMatch(f, "1.3.1-beta.1+1");

ResetToK(f.Repository);
EnsureMetaDataMatch(f, "feature", "1.4.0-feature+2");
EnsureMetaDataMatch(f, "1.4.0-feature+2");

ResetToJ(f.Repository);
EnsureMetaDataMatch(f, "feature", "1.4.0-feature+1");
EnsureMetaDataMatch(f, "1.4.0-feature+1");

ResetToI(f.Repository);
EnsureMetaDataMatch(f, "develop", "1.4.0-unstable.2+2");
EnsureMetaDataMatch(f, "1.4.0-unstable.2+2");

ResetToH(f.Repository);
EnsureMetaDataMatch(f, "master", "1.3.0", r => (Commit) r.Tags["1.3.0"].Target);
EnsureMetaDataMatch(f, "1.3.0");

ResetToG(f.Repository);
EnsureMetaDataMatch(f, "release-1.3.0", "1.3.0-beta.1+2");
EnsureMetaDataMatch(f, "1.3.0-beta.1+2");

ResetToF(f.Repository);
EnsureMetaDataMatch(f, "master", "1.2.1", r => (Commit) r.Tags["1.2.0"].Target);
EnsureMetaDataMatch(f, "1.2.1");

ResetToE(f.Repository);
EnsureMetaDataMatch(f, "develop", "1.3.0-unstable.2+2");
EnsureMetaDataMatch(f, "1.3.0-unstable.2+2");

ResetToD(f.Repository);
EnsureMetaDataMatch(f, "release-1.3.0", "1.3.0-beta.1+1");
EnsureMetaDataMatch(f, "1.3.0-beta.1+1");

ResetToC(f.Repository);
EnsureMetaDataMatch(f, "hotfix-1.2.1", "1.2.1-beta.1+1", r => (Commit) r.Tags["1.2.0"].Target);
EnsureMetaDataMatch(f, "1.2.1-beta.1+1");

ResetToB(f.Repository);
EnsureMetaDataMatch(f, "develop", "1.3.0-unstable.1+1");
EnsureMetaDataMatch(f, "1.3.0-unstable.1+1");
}
}

static void EnsureMetaDataMatch(
CommitCountingRepoFixture fixture, string branchName,
string expectedSemVer, Func<IRepository, Commit> commitFinder = null)
static void EnsureMetaDataMatch(CommitCountingRepoFixture fixture,string expectedSemVer)
{

var referenceCommitFinder = commitFinder ?? (r => r.FindBranch(branchName).Tip);

var commit = referenceCommitFinder(fixture.Repository);

var result = fixture.ExecuteGitVersion();
result.ToString("f").ShouldBe(expectedSemVer);
result.BuildMetaData.ReleaseDate.OriginalCommitSha.ShouldBe(commit.Sha);
result.BuildMetaData.ReleaseDate.OriginalDate.ShouldBe(commit.Committer.When);
}

void DropTags(IRepository repo, params string[] names)
Expand Down
1 change: 1 addition & 0 deletions GitVersionCore.Tests/GitVersionCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="Helpers\NextVersionWriter.cs" />
<Compile Include="InformationalVersionBuilderTests.cs" />
<Compile Include="JsonVersionBuilderTests.cs" />
<Compile Include="LastVersionOnMasterFinderTests.cs" />
<Compile Include="ModuleInitializer.cs" />
<Compile Include="EmptyRepositoryFixture.cs" />
<Compile Include="Helpers\GitHelper.cs" />
Expand Down
3 changes: 2 additions & 1 deletion GitVersionCore.Tests/InformationalVersionBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using GitVersion;
using NUnit.Framework;

Expand All @@ -24,7 +25,7 @@ public void ValidateInformationalVersionBuilder(BranchType branchType, string br
Minor = minor,
Patch = patch,
PreReleaseTag = tag,
BuildMetaData = new SemanticVersionBuildMetaData(suffix, branchName, new ReleaseDate{ CommitSha = sha }),
BuildMetaData = new SemanticVersionBuildMetaData(suffix, branchName,sha,DateTimeOffset.MinValue),
};
var informationalVersion = semanticVersion.ToString("i");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"ClassicVersionWithTag":"1.2.3.5-unstable.4",
"BranchName":"feature1",
"Sha":"commitSha",
"OriginalRelease":"originalCommitSha.2014-03-01 00:00:01Z",
"NuGetVersionV2":"1.2.3-unstable0004",
"NuGetVersion":"1.2.3-unstable0004"
}
9 changes: 1 addition & 8 deletions GitVersionCore.Tests/JsonVersionBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ public void Json()
Minor = 2,
Patch = 3,
PreReleaseTag = "unstable4",
BuildMetaData = new SemanticVersionBuildMetaData(5, "feature1",
new ReleaseDate
{
OriginalCommitSha = "originalCommitSha",
OriginalDate = DateTimeOffset.Parse("2014-03-01 00:00:01Z"),
CommitSha = "commitSha",
Date = DateTimeOffset.Parse("2014-03-06 23:59:59Z")
})
BuildMetaData = new SemanticVersionBuildMetaData(5, "feature1", "commitSha",DateTimeOffset.Parse("2014-03-06 23:59:59Z"))
};
var variables = VariableProvider.GetVariablesFor(semanticVersion);
var json = JsonOutputFormatter.ToJson(variables);
Expand Down
214 changes: 214 additions & 0 deletions GitVersionCore.Tests/LastVersionOnMasterFinderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
 using System;
using GitVersion;
using LibGit2Sharp;
using NUnit.Framework;
using Shouldly;

[TestFixture]
public class LastVersionOnMasterFinderTests
{
/*
* hotfix-1.2.1 -----------C--
* / \
* master A----------------F-----H-------N
* \ / \ /
* hotfix-1.3.1 \ / ----L
* \ / \
* release-1.3.0 \ -D----G--- \
* \ / \ \
* develop -----B----E-------I-----M--O--P
* \ /
* feature -------J-K-
*
*
* - A is tagged `1.2.0`
* - F is tagged `1.2.1`
* - H is tagged `1.3.0`
* - N is tagged `1.3.1`
*/

[Test]
public void CanCorrectlyDetectCommitCountsAndReleaseDataWhenThatApplies()
{
using (var f = new CommitCountingRepoFixture())
{
ResetToP(f.Repository);
EnsureMetaDataMatch(f, "develop");

ResetToO(f.Repository);
EnsureMetaDataMatch(f, "develop");

ResetToN(f.Repository);
EnsureMetaDataMatch(f, "master", r => (Commit) r.Tags["1.3.0"].Target);

ResetToM(f.Repository);
EnsureMetaDataMatch(f, "develop");

ResetToL(f.Repository);
EnsureMetaDataMatch(f, "hotfix-1.3.1", r => (Commit) r.Tags["1.3.0"].Target);

ResetToK(f.Repository);
EnsureMetaDataMatch(f, "feature");

ResetToJ(f.Repository);
EnsureMetaDataMatch(f, "feature");

ResetToI(f.Repository);
EnsureMetaDataMatch(f, "develop");

ResetToH(f.Repository);
EnsureMetaDataMatch(f, "master", r => (Commit) r.Tags["1.3.0"].Target);

ResetToG(f.Repository);
EnsureMetaDataMatch(f, "release-1.3.0");

ResetToF(f.Repository);
EnsureMetaDataMatch(f, "master", r => (Commit) r.Tags["1.2.0"].Target);

ResetToE(f.Repository);
EnsureMetaDataMatch(f, "develop");

ResetToD(f.Repository);
EnsureMetaDataMatch(f, "release-1.3.0");

ResetToC(f.Repository);
EnsureMetaDataMatch(f, "hotfix-1.2.1", r => (Commit) r.Tags["1.2.0"].Target);

ResetToB(f.Repository);
EnsureMetaDataMatch(f, "develop");
}
}

static void EnsureMetaDataMatch(CommitCountingRepoFixture fixture, string branchName, Func<IRepository, Commit> commitFinder = null)
{
var referenceCommitFinder = commitFinder ?? (r => r.FindBranch(branchName).Tip);

var commit = referenceCommitFinder(fixture.Repository);
var releaseDate = LastVersionOnMasterFinder.Execute(fixture.Repository, commit);
releaseDate.ShouldBe(commit.When());
}

void DropTags(IRepository repo, params string[] names)
{
foreach (var name in names)
{
if (repo.Tags[name] == null)
{
continue;
}

repo.Tags.Remove(name);
}
}

void DropBranches(IRepository repo, params string[] names)
{
foreach (var name in names)
{
if (repo.Branches[name] == null)
{
continue;
}

repo.Branches.Remove(name);
}
}

void ResetBranch(IRepository repo, string name, string committish)
{
var b = repo.Branches[name];
Assert.NotNull(b);
repo.Refs.UpdateTarget(b.CanonicalName, committish);
}

void ResetToP(IRepository repo)
{
ResetBranch(repo, "develop", "4d65c519f88773854f9345eaf5dbb30cb49f6a74");
}

void ResetToO(IRepository repo)
{
ResetBranch(repo, "develop", "7655537837096d925a4f974232f78ec589d86ebd");
}

void ResetToN(IRepository repo)
{
ResetBranch(repo, "develop", "0b7a2482ab7d167cefa4ecfc106db001dc5c17ff");
repo.Refs.UpdateTarget("HEAD", "refs/heads/master");
}

void ResetToM(IRepository repo)
{
ResetBranch(repo, "develop", "0b7a2482ab7d167cefa4ecfc106db001dc5c17ff");
ResetBranch(repo, "master", "5b84136c848fd48f1f8b3fa4e1b767a1f6101279");
DropTags(repo, "1.3.1");
repo.Refs.UpdateTarget("HEAD", "refs/heads/develop");
}

void ResetToL(IRepository repo)
{
ResetBranch(repo, "develop", "243f56dcdb543688fd0a99bd3e0e72dd9a786603");
repo.Refs.UpdateTarget("HEAD", "refs/heads/hotfix-1.3.1");
}

void ResetToK(IRepository repo)
{
repo.Refs.UpdateTarget("HEAD", "refs/heads/feature");
DropBranches(repo, "hotfix-1.3.1");
}

void ResetToJ(IRepository repo)
{
ResetBranch(repo, "feature", "0491c5dac30d706f4e54c5cb26d082baad8228d1");
}

void ResetToI(IRepository repo)
{
repo.Refs.UpdateTarget("HEAD", "refs/heads/develop");
DropBranches(repo, "feature");
}

void ResetToH(IRepository repo)
{
ResetBranch(repo, "develop", "320f4b6820cf4b0853dc08ac153f04fbd4958200");
repo.Refs.UpdateTarget("HEAD", "refs/heads/master");
}

void ResetToG(IRepository repo)
{
ResetBranch(repo, "master", "576a28e321cd6dc764b52c5fface672fa076f37f");
repo.Refs.UpdateTarget("HEAD", "refs/heads/release-1.3.0");
DropTags(repo, "1.3.0");
}

void ResetToF(IRepository repo)
{
ResetBranch(repo, "release-1.3.0", "b53054c614d36edc9d1bee8c35cd2ed575a43607");
repo.Refs.UpdateTarget("HEAD", "refs/heads/master");
}

void ResetToE(IRepository repo)
{
ResetBranch(repo, "master", "8c890487ed143d5a72d151e64be1c5ddb314c908");
repo.Refs.UpdateTarget("HEAD", "refs/heads/develop");
DropTags(repo, "1.2.1");
}

void ResetToD(IRepository repo)
{
ResetBranch(repo, "develop", "fab69e28ee35dd912c0c95d5993dd84e4f2bcd92");
repo.Refs.UpdateTarget("HEAD", "refs/heads/release-1.3.0");
}

void ResetToC(IRepository repo)
{
repo.Refs.UpdateTarget("HEAD", "refs/heads/hotfix-1.2.1");
DropBranches(repo, "release-1.3.0");
}

void ResetToB(IRepository repo)
{
repo.Refs.UpdateTarget("HEAD", "refs/heads/develop");
DropBranches(repo, "hotfix-1.2.1");
}
}
8 changes: 0 additions & 8 deletions GitVersionCore.Tests/SemanticVersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@ public void VersionSorting()
SemanticVersion.Parse("1.0.0").ShouldBeGreaterThan(SemanticVersion.Parse("1.0.0-beta"));
}

[Test]
public void EmptyVersion()
{
Assert.IsTrue(SemanticVersion.Empty.IsEmpty());

var emptyVersion = new SemanticVersion();
Assert.IsTrue(emptyVersion.IsEmpty());
}
[Test]
public void ToStringJTests()
{
Expand Down
3 changes: 1 addition & 2 deletions GitVersionCore.Tests/ShortVersionParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ public class ShortVersionParserTests
[Test]
public void Major_minor_patch()
{
ShortVersion shortVersion;
ShortVersionParser.Parse("1.2.3", out shortVersion);
var shortVersion = ShortVersionParser.Parse("1.2.3");
Assert.AreEqual(1, shortVersion.Major);
Assert.AreEqual(2, shortVersion.Minor);
Assert.AreEqual(3, shortVersion.Patch);
Expand Down
Loading