Skip to content

Commit eb64a28

Browse files
author
Frode
committed
Testcase and fix for issue 1348
1 parent 8b1965c commit eb64a28

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

src/GitVersionCore.Tests/IntegrationTests/BranchWithoutCommitScenario.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using GitTools.Testing;
2+
using LibGit2Sharp;
3+
using NUnit.Framework;
4+
5+
namespace GitVersionCore.Tests.IntegrationTests
6+
{
7+
[TestFixture]
8+
public class BranchWithoutCommitScenarios : TestBase
9+
{
10+
[Test]
11+
public void CanTakeVersionFromReleaseBranch()
12+
{
13+
using var fixture = new EmptyRepositoryFixture();
14+
fixture.Repository.MakeATaggedCommit("1.0.3");
15+
var commit = fixture.Repository.MakeACommit();
16+
fixture.Repository.CreateBranch("release-4.0.123");
17+
fixture.Checkout(commit.Sha);
18+
19+
fixture.AssertFullSemver("4.0.123-beta.1+0", fixture.Repository, commit.Sha, false, "release-4.0.123");
20+
}
21+
22+
[Test]
23+
public void BranchVersionHavePrecedenceOverTagVersionIfVersionGreaterThanTag()
24+
{
25+
using var fixture = new EmptyRepositoryFixture();
26+
27+
fixture.Repository.MakeACommit();
28+
29+
fixture.Repository.CreateBranch("develop");
30+
fixture.Checkout("develop");
31+
fixture.MakeATaggedCommit("0.1.0-alpha.1"); // simulate merge from feature branch
32+
33+
fixture.Repository.CreateBranch("release/1.0");
34+
fixture.Checkout("release/1.0");
35+
36+
fixture.AssertFullSemver("1.0.0-beta.1+0");
37+
}
38+
}
39+
}

src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Text.RegularExpressions;
4+
45
using GitVersion.VersionCalculation.BaseVersionCalculators;
56
using GitVersion.VersioningModes;
67
using GitVersion.Configuration;
@@ -28,7 +29,7 @@ public NextVersionCalculator(ILog log, IMetaDataCalculator metaDataCalculator, I
2829
public SemanticVersion FindVersion(GitVersionContext context)
2930
{
3031
SemanticVersion taggedSemanticVersion = null;
31-
// If current commit is tagged, don't do anything except add build metadata
32+
3233
if (context.IsCurrentCommitTagged)
3334
{
3435
// Will always be 0, don't bother with the +0 on tags
@@ -62,11 +63,20 @@ public SemanticVersion FindVersion(GitVersionContext context)
6263
UpdatePreReleaseTag(context, semver, baseVersion.BranchNameOverride);
6364
}
6465

65-
6666
if (taggedSemanticVersion != null)
6767
{
68-
// set the commit count on the tagged ver
69-
taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData.CommitsSinceVersionSource;
68+
// replace calculated version with tagged version only if tagged version greater or equal to calculated version
69+
if (semver.Major > taggedSemanticVersion.Major ||
70+
(semver.Major == taggedSemanticVersion.Major && semver.Minor > taggedSemanticVersion.Minor) ||
71+
(semver.Major == taggedSemanticVersion.Major && semver.Minor == taggedSemanticVersion.Minor && semver.Patch > taggedSemanticVersion.Patch))
72+
{
73+
taggedSemanticVersion = null;
74+
}
75+
else
76+
{
77+
// set the commit count on the tagged ver
78+
taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData.CommitsSinceVersionSource;
79+
}
7080
}
7181

7282
return taggedSemanticVersion ?? semver;

0 commit comments

Comments
 (0)