Skip to content
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

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

Merged
merged 5 commits into from
Sep 21, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 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, repository.Branches.SingleOrDefault(b => b.CanonicalName == targetBranch || b.FriendlyName == targetBranch) ?? repository.Head, configuration, onlyEvaluateTrackedBranches, commitId)
Copy link
Member

Choose a reason for hiding this comment

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

Can you please reduce the number of statements per line here? It's a bit hard to read with so much going on in one line of code. 😃

{
}

Expand Down