Skip to content

Context assigns branch configuration #339

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
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
31 changes: 31 additions & 0 deletions GitVersionCore.Tests/Fixtures/BaseGitFlowRepositoryFixture.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
using System;
using System.IO;
using System.Text;
using GitVersion;
using GitVersion.Helpers;
using LibGit2Sharp;

/// <summary>
/// Creates a repo with a develop branch off master which is a single commit ahead of master
/// </summary>
public class BaseGitFlowRepositoryFixture : EmptyRepositoryFixture
{
/// <summary>
/// Creates a repo with a develop branch off master which is a single commit ahead of master
///
/// Master will be tagged with the initial version before branching develop
/// </summary>
public BaseGitFlowRepositoryFixture(string initialVersion) : base(new Config())
{
SetupRepo(r => r.MakeATaggedCommit(initialVersion));
}

/// <summary>
/// Creates a repo with a develop branch off master which is a single commit ahead of master
///
/// The initial setup actions will be performed before branching develop
/// </summary>
public BaseGitFlowRepositoryFixture(Action<IRepository> initialMasterAction) : base(new Config())
{
SetupRepo(initialMasterAction);
Expand All @@ -24,5 +39,21 @@ void SetupRepo(Action<IRepository> initialMasterAction)
initialMasterAction(Repository);

Repository.CreateBranch("develop").Checkout();
Repository.MakeACommit();
}

public void DumpGraph()
Copy link
Contributor

Choose a reason for hiding this comment

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

This was only for test purpose, not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, test purposes. But is useful if we need to debug issues in the future

{
var output = new StringBuilder();

ProcessHelper.Run(
o => output.AppendLine(o),
e => output.AppendLineFormat("ERROR: {0}", e),
null,
"git",
@"log --graph --abbrev-commit --decorate --date=relative --all",
RepositoryPath);

Console.Write(output.ToString());
}
}
19 changes: 5 additions & 14 deletions GitVersionCore.Tests/IntegrationTests/GitFlow/PatchScenarios.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using System.Threading;
using LibGit2Sharp;
using NUnit.Framework;

Expand All @@ -14,13 +13,13 @@ public void PatchLatestReleaseExample()
// create hotfix
fixture.Repository.CreateBranch("hotfix-1.2.1").Checkout();

fixture.AssertFullSemver("1.2.1-beta.1+0");
fixture.Repository.MakeACommit();
fixture.AssertFullSemver("1.2.1-beta.1+1");
fixture.Repository.MakeACommit();
fixture.AssertFullSemver("1.2.1-beta.1+2");
fixture.Repository.ApplyTag("1.2.1-beta.1");
fixture.AssertFullSemver("1.2.1-beta.1+1");
fixture.AssertFullSemver("1.2.1-beta.1+2");
fixture.Repository.MakeACommit();
fixture.AssertFullSemver("1.2.1-beta.2+2");
fixture.AssertFullSemver("1.2.1-beta.2+3");

// Merge hotfix branch to master
fixture.Repository.Checkout("master");
Expand All @@ -36,14 +35,7 @@ public void PatchLatestReleaseExample()
fixture.Repository.Checkout("develop");
fixture.AssertFullSemver("1.3.0-unstable.0+0");

// Warning: Hack-ish hack
//
// Ensure the merge commit is done at a different time than the previous one
// Otherwise, as they would have the same content and signature, the same sha would be generated.
// Thus 'develop' and 'master' would point at the same exact commit and the Assert below would fail.
Thread.Sleep(1000);
fixture.Repository.MergeNoFF("hotfix-1.2.1", Constants.SignatureNow());

fixture.AssertFullSemver("1.3.0-unstable.1+1");
}
}
Expand All @@ -58,7 +50,6 @@ public void PatchOlderReleaseExample()
r.MakeATaggedCommit("1.2.0");
}))
{

// create hotfix branch
fixture.Repository.CreateBranch("hotfix-1.1.1", (Commit) fixture.Repository.Tags.Single(t => t.Name == "1.1.0").Target).Checkout();

Expand All @@ -78,7 +69,7 @@ public void PatchOlderReleaseExample()

// Verify develop version
fixture.Repository.Checkout("develop");
fixture.AssertFullSemver("1.3.0-unstable.0+0");
fixture.AssertFullSemver("1.3.0-unstable.1+1");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ public class SupportBranchScenarios
public void SupportIsCalculatedCorrectly()
{
using (var fixture = new BaseGitFlowRepositoryFixture("1.1.0"))
{
{
// Create 2.0.0 release
fixture.Repository.CreateBranch("release-2.0.0").Checkout();
fixture.Repository.MakeCommits(2);

// Merge into develop and master
fixture.Repository.Checkout("master");
fixture.Repository.MergeNoFF("release-2.0.0");
fixture.Repository.ApplyTag("2.0.0");
fixture.Repository.Checkout("develop");
fixture.Repository.MergeNoFF("release-2.0.0");
fixture.AssertFullSemver("2.1.0-unstable.0+0");
fixture.AssertFullSemver("2.1.0-unstable.1+1");

// Now lets support 1.x release
fixture.Repository.Checkout("1.1.0");
Expand All @@ -47,6 +47,6 @@ public void SupportIsCalculatedCorrectly()
fixture.Repository.MergeNoFF("hotfix/1.2.1");
fixture.AssertFullSemver("1.2.1");
}
}
}
}
}
1 change: 1 addition & 0 deletions GitVersionCore/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public Config()
AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch;
TagPrefix = "[vV]";
Branches["release[/-]"] = new BranchConfig { Tag = "beta" };
Branches["hotfix[/-]"] = new BranchConfig { Tag = "beta" };
Branches["develop"] = new BranchConfig { Tag = "unstable" };
VersioningMode = VersioningMode.ContinuousDelivery;
Develop.VersioningMode = VersioningMode.ContinuousDeployment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ protected SemanticVersion FindVersion(
GitVersionContext context,
BranchType branchType)
{
context.CurrentBranchConfig = context.Configuration.Branches["develop"];
var ancestor = FindCommonAncestorWithDevelop(context.Repository, context.CurrentBranch, branchType);

if (!IsThereAnyCommitOnTheBranch(context.Repository, context.CurrentBranch))
Expand Down
3 changes: 0 additions & 3 deletions GitVersionCore/GitFlow/GitFlowVersionFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@ public SemanticVersion FindVersion(GitVersionContext context)

if (context.CurrentBranch.IsHotfix())
{
context.CurrentBranchConfig = context.Configuration.Branches["release[/-]"];
return new HotfixVersionFinder().FindVersion(context);
}

if (context.CurrentBranch.IsRelease())
{
context.CurrentBranchConfig = context.Configuration.Branches["release[/-]"];
return new ReleaseVersionFinder().FindVersion(context);
}

if (context.CurrentBranch.IsDevelop())
{
context.CurrentBranchConfig = context.Configuration.Branches["develop"];
return new DevelopVersionFinder().FindVersion(context);
}

Expand Down
29 changes: 26 additions & 3 deletions GitVersionCore/GitVersionContext.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
namespace GitVersion
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using LibGit2Sharp;

/// <summary>
/// Contextual information about where GitVersion is being run
/// </summary>
public class GitVersionContext
{
readonly bool IsContextForTrackedBranchesOnly;
Copy link
Contributor

Choose a reason for hiding this comment

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

No idea what this field is, but is it correct that the default changed from true to false?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is always assigned through ctor. The initial value was unused


public GitVersionContext(IRepository repository, Config configuration, bool isForTrackingBranchOnly = true)
: this(repository, repository.Head, configuration, isForTrackingBranchOnly)
{
Expand All @@ -34,16 +38,16 @@ public GitVersionContext(IRepository repository, Branch currentBranch, Config co
{
CurrentBranch = currentBranch;
}

AssignBranchConfiguration();
}

public Config Configuration { get; private set; }
public IRepository Repository { get; private set; }
public Branch CurrentBranch { get; private set; }
public Commit CurrentCommit { get; private set; }

public BranchConfig CurrentBranchConfig { get; set; }

readonly bool IsContextForTrackedBranchesOnly = true;
public BranchConfig CurrentBranchConfig { get; private set; }


IEnumerable<Branch> GetBranchesContainingCommit(string commitSha)
Expand Down Expand Up @@ -77,5 +81,24 @@ IEnumerable<Branch> GetBranchesContainingCommit(string commitSha)
yield return branch;
}
}

void AssignBranchConfiguration()
{
var matchingBranches = Configuration.Branches.Where(b => Regex.IsMatch("^" + CurrentBranch.Name, b.Key)).ToArray();

if (matchingBranches.Length == 0)
{
CurrentBranchConfig = new BranchConfig();
}
else if (matchingBranches.Length == 1)
{
CurrentBranchConfig = matchingBranches[0].Value;
}
else
{
const string format = "Multiple branch configurations match the current branch name of '{0}'. Matching configurations: '{1}'";
throw new Exception(string.Format(format, CurrentBranch.Name, string.Join(", ", matchingBranches.Select(b => b.Key))));
}
}
}
}
1 change: 1 addition & 0 deletions GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="GitFlow\BranchFinders\RecentTagVersionExtractor.cs" />
<Compile Include="Helpers\FileSystem.cs" />
<Compile Include="Helpers\IFileSystem.cs" />
<Compile Include="Helpers\ProcessHelper.cs" />
<Compile Include="LastMinorVersionFinder.cs" />
<Compile Include="SemanticVersionExtensions.cs" />
<Compile Include="VersioningModes\ContinuousDeliveryMode.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GitVersion
namespace GitVersion.Helpers
{
using System;
using System.Collections.Generic;
Expand All @@ -7,7 +7,7 @@ namespace GitVersion
using System.Runtime.InteropServices;
using System.Threading;

static class ProcessHelper
public static class ProcessHelper
{
static volatile object lockObject = new object();

Expand Down
2 changes: 1 addition & 1 deletion GitVersionExe.Tests/GitVersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using GitVersion;
using GitVersion.Helpers;
using LibGit2Sharp;

public static class GitVersionHelper
Expand Down
1 change: 0 additions & 1 deletion GitVersionExe/GitVersionExe.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
<Compile Include="ExtensionMethods.cs" />
<Compile Include="GitPreparer.cs" />
<Compile Include="HelpWriter.cs" />
<Compile Include="ProcessHelper.cs" />
<Compile Include="Program.cs" />
<Compile Include="AssemblyInfo.cs" />
<Compile Include="AssemblyInfoFileUpdate.cs" />
Expand Down