Skip to content

Improve ExecuteCore.ExecuteInternal when HEAD is not pointing at the desired branch #1291

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 5 commits into from
Sep 21, 2017
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
12 changes: 6 additions & 6 deletions src/GitVersionCore.Tests/GitToolsTestingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public static Config ApplyDefaults(this Config config)
return config;
}

public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Config configuration = null, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true)
public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Config configuration = null, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true, string targetBranch = null)
{
if (configuration == null)
{
configuration = new Config();
ConfigurationProvider.ApplyDefaultsTo(configuration);
}
var gitVersionContext = new GitVersionContext(repository ?? fixture.Repository, configuration, isForTrackedBranchOnly, commitId);
var gitVersionContext = new GitVersionContext(repository ?? fixture.Repository, targetBranch, configuration, isForTrackedBranchOnly, commitId);
var executeGitVersion = ExecuteGitVersion(gitVersionContext);
var variables = VariableProvider.GetVariablesFor(executeGitVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
try
Expand All @@ -37,19 +37,19 @@ public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Co
}
}

public static void AssertFullSemver(this RepositoryFixtureBase fixture, string fullSemver, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true)
public static void AssertFullSemver(this RepositoryFixtureBase fixture, string fullSemver, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true, string targetBranch = null)
{
fixture.AssertFullSemver(new Config(), fullSemver, repository, commitId, isForTrackedBranchOnly);
fixture.AssertFullSemver(new Config(), fullSemver, repository, commitId, isForTrackedBranchOnly, targetBranch);
}

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

try
{
var variables = fixture.GetVersion(configuration, repository, commitId, isForTrackedBranchOnly);
var variables = fixture.GetVersion(configuration, repository, commitId, isForTrackedBranchOnly, targetBranch);
variables.FullSemVer.ShouldBe(fullSemver);
}
catch (Exception)
Expand Down
3 changes: 2 additions & 1 deletion src/GitVersionCore.Tests/GitVersionContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public GitVersionContext Build()
{
var configuration = config ?? new Config();
ConfigurationProvider.ApplyDefaultsTo(configuration);
return new GitVersionContext(repository ?? CreateRepository(), configuration);
var repo = repository ?? CreateRepository();
return new GitVersionContext(repo, repo.Head, configuration);
}

IRepository CreateRepository()
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersionCore.Tests/GitVersionContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void CanFindParentBranchForInheritingIncrementStrategy()
Commands.Checkout(repo.Repository, featureBranch);
repo.Repository.MakeACommit();

var context = new GitVersionContext(repo.Repository, config);
var context = new GitVersionContext(repo.Repository, repo.Repository.Head, config);
context.Configuration.Increment.ShouldBe(IncrementStrategy.Major);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using GitTools.Testing;
using GitVersionCore.Tests;
using LibGit2Sharp;
using NUnit.Framework;

[TestFixture]
public class BranchWithoutCommitScenario
{
[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");
}
}
}
2 changes: 1 addition & 1 deletion src/GitVersionCore/ExecuteCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ VersionVariables ExecuteInternal(string targetBranch, string commitId, GitPrepar

return gitPreparer.WithRepository(repo =>
{
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: commitId);
var gitVersionContext = new GitVersionContext(repo, targetBranch, configuration, commitId: commitId);
var semanticVersion = versionFinder.FindVersion(gitVersionContext);

return VariableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
Expand Down
30 changes: 28 additions & 2 deletions src/GitVersionCore/GitVersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
/// </summary>
public class GitVersionContext
{
public GitVersionContext(IRepository repository, Config configuration, bool isForTrackingBranchOnly = true, string commitId = null)
: this(repository, repository.Head, configuration, isForTrackingBranchOnly, commitId)
public GitVersionContext(IRepository repository, string targetBranch, Config configuration, bool onlyEvaluateTrackedBranches = true, string commitId = null)
: this(repository, GitVersionContext.GetTargetBranch(repository, targetBranch), configuration, onlyEvaluateTrackedBranches, commitId)
{
}

Expand Down Expand Up @@ -146,5 +146,31 @@ void CalculateEffectiveConfiguration()
currentBranchConfig.TracksReleaseBranches.Value,
currentBranchConfig.IsReleaseBranch.Value);
}

private static Branch GetTargetBranch(IRepository repository, string targetBranch)
{
// By default, we assume HEAD is pointing to the desired branch
var desiredBranch = repository.Head;

// Make sure the desired branch has been specified
if (!string.IsNullOrEmpty(targetBranch))
{
// There are some edge cases where HEAD is not pointing to the desired branch.
// Therefore it's important to verify if 'currentBranch' is indeed the desired branch.
if (desiredBranch.CanonicalName != targetBranch)
{
// In the case where HEAD is not the desired branch, try to find the branch with matching name
desiredBranch = repository?.Branches?
.SingleOrDefault(b =>
b.CanonicalName == targetBranch ||
b.FriendlyName == targetBranch);

// Failsafe in case the specified branch is invalid
desiredBranch = desiredBranch ?? repository.Head;
}
}

return desiredBranch;
}
}
}