Skip to content

Commit 018ba17

Browse files
authored
Merge pull request #1291 from Jericho/head_not_pointing_at_desired_branch
Improve ExecuteCore.ExecuteInternal when HEAD is not pointing at the desired branch
2 parents 489f9d0 + e7894ab commit 018ba17

File tree

6 files changed

+60
-11
lines changed

6 files changed

+60
-11
lines changed

src/GitVersionCore.Tests/GitToolsTestingExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ 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
{
2222
configuration = new Config();
2323
ConfigurationProvider.ApplyDefaultsTo(configuration);
2424
}
25-
var gitVersionContext = new GitVersionContext(repository ?? fixture.Repository, configuration, isForTrackedBranchOnly, commitId);
25+
var gitVersionContext = new GitVersionContext(repository ?? fixture.Repository, targetBranch, configuration, isForTrackedBranchOnly, commitId);
2626
var executeGitVersion = ExecuteGitVersion(gitVersionContext);
2727
var variables = VariableProvider.GetVariablesFor(executeGitVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
2828
try
@@ -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)

src/GitVersionCore.Tests/GitVersionContextBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public GitVersionContext Build()
6161
{
6262
var configuration = config ?? new Config();
6363
ConfigurationProvider.ApplyDefaultsTo(configuration);
64-
return new GitVersionContext(repository ?? CreateRepository(), configuration);
64+
var repo = repository ?? CreateRepository();
65+
return new GitVersionContext(repo, repo.Head, configuration);
6566
}
6667

6768
IRepository CreateRepository()

src/GitVersionCore.Tests/GitVersionContextTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public void CanFindParentBranchForInheritingIncrementStrategy()
144144
Commands.Checkout(repo.Repository, featureBranch);
145145
repo.Repository.MakeACommit();
146146

147-
var context = new GitVersionContext(repo.Repository, config);
147+
var context = new GitVersionContext(repo.Repository, repo.Repository.Head, config);
148148
context.Configuration.Increment.ShouldBe(IncrementStrategy.Major);
149149
}
150150
}
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ VersionVariables ExecuteInternal(string targetBranch, string commitId, GitPrepar
108108

109109
return gitPreparer.WithRepository(repo =>
110110
{
111-
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: commitId);
111+
var gitVersionContext = new GitVersionContext(repo, targetBranch, configuration, commitId: commitId);
112112
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
113113

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

src/GitVersionCore/GitVersionContext.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
/// </summary>
1010
public class GitVersionContext
1111
{
12-
public GitVersionContext(IRepository repository, Config configuration, bool isForTrackingBranchOnly = true, string commitId = null)
13-
: this(repository, repository.Head, configuration, isForTrackingBranchOnly, commitId)
12+
public GitVersionContext(IRepository repository, string targetBranch, Config configuration, bool onlyEvaluateTrackedBranches = true, string commitId = null)
13+
: this(repository, GitVersionContext.GetTargetBranch(repository, targetBranch), configuration, onlyEvaluateTrackedBranches, commitId)
1414
{
1515
}
1616

@@ -146,5 +146,31 @@ void CalculateEffectiveConfiguration()
146146
currentBranchConfig.TracksReleaseBranches.Value,
147147
currentBranchConfig.IsReleaseBranch.Value);
148148
}
149+
150+
private static Branch GetTargetBranch(IRepository repository, string targetBranch)
151+
{
152+
// By default, we assume HEAD is pointing to the desired branch
153+
var desiredBranch = repository.Head;
154+
155+
// Make sure the desired branch has been specified
156+
if (!string.IsNullOrEmpty(targetBranch))
157+
{
158+
// There are some edge cases where HEAD is not pointing to the desired branch.
159+
// Therefore it's important to verify if 'currentBranch' is indeed the desired branch.
160+
if (desiredBranch.CanonicalName != targetBranch)
161+
{
162+
// In the case where HEAD is not the desired branch, try to find the branch with matching name
163+
desiredBranch = repository?.Branches?
164+
.SingleOrDefault(b =>
165+
b.CanonicalName == targetBranch ||
166+
b.FriendlyName == targetBranch);
167+
168+
// Failsafe in case the specified branch is invalid
169+
desiredBranch = desiredBranch ?? repository.Head;
170+
}
171+
}
172+
173+
return desiredBranch;
174+
}
149175
}
150176
}

0 commit comments

Comments
 (0)