Skip to content

Fix for #1348, branch without commit take version from tag instead of branch name. #1743

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 2 commits into from
Dec 13, 2019
Merged
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using GitTools.Testing;
using LibGit2Sharp;
using NUnit.Framework;

namespace GitVersionCore.Tests.IntegrationTests
{
[TestFixture]
public class BranchWithoutCommitScenarios : TestBase
{
[Test]
public void CanTakeVersionFromReleaseBranch()
{
using var fixture = new EmptyRepositoryFixture();
fixture.Repository.MakeATaggedCommit("1.0.3");
var commit = fixture.Repository.MakeACommit();
fixture.Repository.CreateBranch("release-4.0.123");
fixture.Checkout(commit.Sha);

fixture.AssertFullSemver("4.0.123-beta.1+0", fixture.Repository, commit.Sha, false, "release-4.0.123");
}

[Test]
public void BranchVersionHavePrecedenceOverTagVersionIfVersionGreaterThanTag()
{
using var fixture = new EmptyRepositoryFixture();

fixture.Repository.MakeACommit();

fixture.Repository.CreateBranch("develop");
fixture.Checkout("develop");
fixture.MakeATaggedCommit("0.1.0-alpha.1"); // simulate merge from feature branch

fixture.Repository.CreateBranch("release/1.0");
fixture.Checkout("release/1.0");

fixture.AssertFullSemver("1.0.0-beta.1+0");
}
}
}
7 changes: 6 additions & 1 deletion src/GitVersionCore/SemanticVersioning/SemanticVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public static bool TryParse(string version, string tagPrefixRegex, out SemanticV
}

public int CompareTo(SemanticVersion value)
{
return CompareTo(value, true);
}

public int CompareTo(SemanticVersion value, bool includePrerelease)
{
if (value == null)
{
Expand Down Expand Up @@ -213,7 +218,7 @@ public int CompareTo(SemanticVersion value)
}
return -1;
}
if (PreReleaseTag != value.PreReleaseTag)
if (includePrerelease && PreReleaseTag != value.PreReleaseTag)
{
if (PreReleaseTag > value.PreReleaseTag)
{
Expand Down
16 changes: 12 additions & 4 deletions src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;

using GitVersion.VersionCalculation.BaseVersionCalculators;
using GitVersion.VersioningModes;
using GitVersion.Configuration;
Expand Down Expand Up @@ -28,7 +29,7 @@ public NextVersionCalculator(ILog log, IMetaDataCalculator metaDataCalculator, I
public SemanticVersion FindVersion(GitVersionContext context)
{
SemanticVersion taggedSemanticVersion = null;
// If current commit is tagged, don't do anything except add build metadata

if (context.IsCurrentCommitTagged)
{
// Will always be 0, don't bother with the +0 on tags
Expand Down Expand Up @@ -62,11 +63,18 @@ public SemanticVersion FindVersion(GitVersionContext context)
UpdatePreReleaseTag(context, semver, baseVersion.BranchNameOverride);
}


if (taggedSemanticVersion != null)
{
// set the commit count on the tagged ver
taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData.CommitsSinceVersionSource;
// replace calculated version with tagged version only if tagged version greater or equal to calculated version
Copy link
Member

Choose a reason for hiding this comment

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

Do you think we can use semver > taggedSemanticVersion instead? as the SemanticVersion implements IComparable

Copy link
Author

Choose a reason for hiding this comment

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

Full semver compare leads to failure in some tests, eg. DevelopScenarios.CanHandleContinuousDelivery -> 1.1.0-alpha.8+1 instead of 1.1.0-alpha.7

At first sight it seems GitVersion is calculating wrong.

if (semver.CompareTo(taggedSemanticVersion, false) > 0)
{
taggedSemanticVersion = null;
}
else
{
// set the commit count on the tagged ver
taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData.CommitsSinceVersionSource;
}
}

return taggedSemanticVersion ?? semver;
Expand Down