Skip to content

Commit adb8ec5

Browse files
committed
Move the logic which determines the desired branch to a new constructor on the GitVersionContext class
This allows this new logic introduced by the PR to be shared by the ExecuteCore class and the the unit testing fixture without having to duplicate code.
1 parent c9e3c80 commit adb8ec5

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

src/GitVersionCore.Tests/GitToolsTestingExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static Config ApplyDefaults(this Config config)
1515
return config;
1616
}
1717

18-
public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Config configuration = null, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true)
18+
public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Config configuration = null, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true, string targetBranch = null)
1919
{
2020
if (configuration == null)
2121
{
@@ -37,19 +37,19 @@ public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Co
3737
}
3838
}
3939

40-
public static void AssertFullSemver(this RepositoryFixtureBase fixture, string fullSemver, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true)
40+
public static void AssertFullSemver(this RepositoryFixtureBase fixture, string fullSemver, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true, string targetBranch = null)
4141
{
42-
fixture.AssertFullSemver(new Config(), fullSemver, repository, commitId, isForTrackedBranchOnly);
42+
fixture.AssertFullSemver(new Config(), fullSemver, repository, commitId, isForTrackedBranchOnly, targetBranch);
4343
}
4444

45-
public static void AssertFullSemver(this RepositoryFixtureBase fixture, Config configuration, string fullSemver, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true)
45+
public static void AssertFullSemver(this RepositoryFixtureBase fixture, Config configuration, string fullSemver, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true, string targetBranch = null)
4646
{
4747
ConfigurationProvider.ApplyDefaultsTo(configuration);
4848
Console.WriteLine("---------");
4949

5050
try
5151
{
52-
var variables = fixture.GetVersion(configuration, repository, commitId, isForTrackedBranchOnly);
52+
var variables = fixture.GetVersion(configuration, repository, commitId, isForTrackedBranchOnly, targetBranch);
5353
variables.FullSemVer.ShouldBe(fullSemver);
5454
}
5555
catch (Exception)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using GitTools.Testing;
2+
using GitVersionCore.Tests;
3+
using LibGit2Sharp;
4+
using NUnit.Framework;
5+
6+
[TestFixture]
7+
public class BranchWithoutCommitScenario
8+
{
9+
[Test]
10+
public void CanTakeVersionFromReleaseBranch()
11+
{
12+
using (var fixture = new EmptyRepositoryFixture())
13+
{
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+
}

src/GitVersionCore/ExecuteCore.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,7 @@ VersionVariables ExecuteInternal(string targetBranch, string commitId, GitPrepar
108108

109109
return gitPreparer.WithRepository(repo =>
110110
{
111-
// By default, we assume HEAD is pointing to the desired branch
112-
var currentBranch = repo.Head;
113-
114-
// There are some edge cases where HEAD is not pointing to the desired branch.
115-
// Therefore it's important to verify if 'currentBranch' is indeed the desired branch
116-
if (currentBranch.CanonicalName != targetBranch)
117-
{
118-
// In the case where HEAD is not the desired branch, try to find the branch with matching name
119-
currentBranch = repo.Branches.SingleOrDefault(b => b.CanonicalName == targetBranch) ?? repo.Head;
120-
}
121-
122-
var gitVersionContext = new GitVersionContext(repo, currentBranch, configuration, commitId: commitId);
111+
var gitVersionContext = new GitVersionContext(repo, targetBranch, configuration, commitId: commitId);
123112
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
124113

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

src/GitVersionCore/GitVersionContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public GitVersionContext(IRepository repository, Config configuration, bool isFo
1414
{
1515
}
1616

17+
public GitVersionContext(IRepository repository, string targetBranch, Config configuration, bool onlyEvaluateTrackedBranches = true, string commitId = null)
18+
: this(repository, repository.Branches.SingleOrDefault(b => b.CanonicalName == targetBranch || b.FriendlyName == targetBranch) ?? repository.Head, configuration, onlyEvaluateTrackedBranches, commitId)
19+
{
20+
}
21+
1722
public GitVersionContext(IRepository repository, Branch currentBranch, Config configuration, bool onlyEvaluateTrackedBranches = true, string commitId = null)
1823
{
1924
Repository = repository;

0 commit comments

Comments
 (0)