Skip to content

Commit 9fd17dd

Browse files
committed
Merge pull request #339 from JakeGinnivan/ContextAssignsBranchConfiguration
Context assigns branch configuration
2 parents ff3cbea + e885ce7 commit 9fd17dd

File tree

11 files changed

+71
-29
lines changed

11 files changed

+71
-29
lines changed

GitVersionCore.Tests/Fixtures/BaseGitFlowRepositoryFixture.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
using System;
22
using System.IO;
3+
using System.Text;
34
using GitVersion;
5+
using GitVersion.Helpers;
46
using LibGit2Sharp;
57

8+
/// <summary>
9+
/// Creates a repo with a develop branch off master which is a single commit ahead of master
10+
/// </summary>
611
public class BaseGitFlowRepositoryFixture : EmptyRepositoryFixture
712
{
13+
/// <summary>
14+
/// Creates a repo with a develop branch off master which is a single commit ahead of master
15+
///
16+
/// Master will be tagged with the initial version before branching develop
17+
/// </summary>
818
public BaseGitFlowRepositoryFixture(string initialVersion) : base(new Config())
919
{
1020
SetupRepo(r => r.MakeATaggedCommit(initialVersion));
1121
}
1222

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

2641
Repository.CreateBranch("develop").Checkout();
42+
Repository.MakeACommit();
43+
}
44+
45+
public void DumpGraph()
46+
{
47+
var output = new StringBuilder();
48+
49+
ProcessHelper.Run(
50+
o => output.AppendLine(o),
51+
e => output.AppendLineFormat("ERROR: {0}", e),
52+
null,
53+
"git",
54+
@"log --graph --abbrev-commit --decorate --date=relative --all",
55+
RepositoryPath);
56+
57+
Console.Write(output.ToString());
2758
}
2859
}

GitVersionCore.Tests/IntegrationTests/GitFlow/PatchScenarios.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Linq;
2-
using System.Threading;
32
using LibGit2Sharp;
43
using NUnit.Framework;
54

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

17-
fixture.AssertFullSemver("1.2.1-beta.1+0");
18-
fixture.Repository.MakeACommit();
1916
fixture.AssertFullSemver("1.2.1-beta.1+1");
17+
fixture.Repository.MakeACommit();
18+
fixture.AssertFullSemver("1.2.1-beta.1+2");
2019
fixture.Repository.ApplyTag("1.2.1-beta.1");
21-
fixture.AssertFullSemver("1.2.1-beta.1+1");
20+
fixture.AssertFullSemver("1.2.1-beta.1+2");
2221
fixture.Repository.MakeACommit();
23-
fixture.AssertFullSemver("1.2.1-beta.2+2");
22+
fixture.AssertFullSemver("1.2.1-beta.2+3");
2423

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

39-
// Warning: Hack-ish hack
40-
//
41-
// Ensure the merge commit is done at a different time than the previous one
42-
// Otherwise, as they would have the same content and signature, the same sha would be generated.
43-
// Thus 'develop' and 'master' would point at the same exact commit and the Assert below would fail.
44-
Thread.Sleep(1000);
4538
fixture.Repository.MergeNoFF("hotfix-1.2.1", Constants.SignatureNow());
46-
4739
fixture.AssertFullSemver("1.3.0-unstable.1+1");
4840
}
4941
}
@@ -58,7 +50,6 @@ public void PatchOlderReleaseExample()
5850
r.MakeATaggedCommit("1.2.0");
5951
}))
6052
{
61-
6253
// create hotfix branch
6354
fixture.Repository.CreateBranch("hotfix-1.1.1", (Commit) fixture.Repository.Tags.Single(t => t.Name == "1.1.0").Target).Checkout();
6455

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

7970
// Verify develop version
8071
fixture.Repository.Checkout("develop");
81-
fixture.AssertFullSemver("1.3.0-unstable.0+0");
72+
fixture.AssertFullSemver("1.3.0-unstable.1+1");
8273
}
8374
}
8475
}

GitVersionCore.Tests/IntegrationTests/GitFlow/SupportBranchScenarios.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ public class SupportBranchScenarios
1010
public void SupportIsCalculatedCorrectly()
1111
{
1212
using (var fixture = new BaseGitFlowRepositoryFixture("1.1.0"))
13-
{
13+
{
1414
// Create 2.0.0 release
1515
fixture.Repository.CreateBranch("release-2.0.0").Checkout();
1616
fixture.Repository.MakeCommits(2);
17-
17+
1818
// Merge into develop and master
1919
fixture.Repository.Checkout("master");
2020
fixture.Repository.MergeNoFF("release-2.0.0");
2121
fixture.Repository.ApplyTag("2.0.0");
2222
fixture.Repository.Checkout("develop");
2323
fixture.Repository.MergeNoFF("release-2.0.0");
24-
fixture.AssertFullSemver("2.1.0-unstable.0+0");
24+
fixture.AssertFullSemver("2.1.0-unstable.1+1");
2525

2626
// Now lets support 1.x release
2727
fixture.Repository.Checkout("1.1.0");
@@ -47,6 +47,6 @@ public void SupportIsCalculatedCorrectly()
4747
fixture.Repository.MergeNoFF("hotfix/1.2.1");
4848
fixture.AssertFullSemver("1.2.1");
4949
}
50-
}
50+
}
5151
}
5252
}

GitVersionCore/Configuration/Config.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public Config()
1717
AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch;
1818
TagPrefix = "[vV]";
1919
Branches["release[/-]"] = new BranchConfig { Tag = "beta" };
20+
Branches["hotfix[/-]"] = new BranchConfig { Tag = "beta" };
2021
Branches["develop"] = new BranchConfig { Tag = "unstable" };
2122
VersioningMode = VersioningMode.ContinuousDelivery;
2223
Develop.VersioningMode = VersioningMode.ContinuousDeployment;

GitVersionCore/GitFlow/BranchFinders/DevelopBasedVersionFinderBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ protected SemanticVersion FindVersion(
99
GitVersionContext context,
1010
BranchType branchType)
1111
{
12-
context.CurrentBranchConfig = context.Configuration.Branches["develop"];
1312
var ancestor = FindCommonAncestorWithDevelop(context.Repository, context.CurrentBranch, branchType);
1413

1514
if (!IsThereAnyCommitOnTheBranch(context.Repository, context.CurrentBranch))

GitVersionCore/GitFlow/GitFlowVersionFinder.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@ public SemanticVersion FindVersion(GitVersionContext context)
1111

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

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

2422
if (context.CurrentBranch.IsDevelop())
2523
{
26-
context.CurrentBranchConfig = context.Configuration.Branches["develop"];
2724
return new DevelopVersionFinder().FindVersion(context);
2825
}
2926

GitVersionCore/GitVersionContext.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
namespace GitVersion
22
{
3+
using System;
34
using System.Collections.Generic;
45
using System.Linq;
6+
using System.Text.RegularExpressions;
57
using LibGit2Sharp;
68

79
/// <summary>
810
/// Contextual information about where GitVersion is being run
911
/// </summary>
1012
public class GitVersionContext
1113
{
14+
readonly bool IsContextForTrackedBranchesOnly;
15+
1216
public GitVersionContext(IRepository repository, Config configuration, bool isForTrackingBranchOnly = true)
1317
: this(repository, repository.Head, configuration, isForTrackingBranchOnly)
1418
{
@@ -34,16 +38,16 @@ public GitVersionContext(IRepository repository, Branch currentBranch, Config co
3438
{
3539
CurrentBranch = currentBranch;
3640
}
41+
42+
AssignBranchConfiguration();
3743
}
3844

3945
public Config Configuration { get; private set; }
4046
public IRepository Repository { get; private set; }
4147
public Branch CurrentBranch { get; private set; }
4248
public Commit CurrentCommit { get; private set; }
4349

44-
public BranchConfig CurrentBranchConfig { get; set; }
45-
46-
readonly bool IsContextForTrackedBranchesOnly = true;
50+
public BranchConfig CurrentBranchConfig { get; private set; }
4751

4852

4953
IEnumerable<Branch> GetBranchesContainingCommit(string commitSha)
@@ -77,5 +81,24 @@ IEnumerable<Branch> GetBranchesContainingCommit(string commitSha)
7781
yield return branch;
7882
}
7983
}
84+
85+
void AssignBranchConfiguration()
86+
{
87+
var matchingBranches = Configuration.Branches.Where(b => Regex.IsMatch("^" + CurrentBranch.Name, b.Key)).ToArray();
88+
89+
if (matchingBranches.Length == 0)
90+
{
91+
CurrentBranchConfig = new BranchConfig();
92+
}
93+
else if (matchingBranches.Length == 1)
94+
{
95+
CurrentBranchConfig = matchingBranches[0].Value;
96+
}
97+
else
98+
{
99+
const string format = "Multiple branch configurations match the current branch name of '{0}'. Matching configurations: '{1}'";
100+
throw new Exception(string.Format(format, CurrentBranch.Name, string.Join(", ", matchingBranches.Select(b => b.Key))));
101+
}
102+
}
80103
}
81104
}

GitVersionCore/GitVersionCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="GitFlow\BranchFinders\RecentTagVersionExtractor.cs" />
7979
<Compile Include="Helpers\FileSystem.cs" />
8080
<Compile Include="Helpers\IFileSystem.cs" />
81+
<Compile Include="Helpers\ProcessHelper.cs" />
8182
<Compile Include="LastMinorVersionFinder.cs" />
8283
<Compile Include="SemanticVersionExtensions.cs" />
8384
<Compile Include="VersioningModes\ContinuousDeliveryMode.cs" />

GitVersionExe/ProcessHelper.cs renamed to GitVersionCore/Helpers/ProcessHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace GitVersion
1+
namespace GitVersion.Helpers
22
{
33
using System;
44
using System.Collections.Generic;
@@ -7,7 +7,7 @@ namespace GitVersion
77
using System.Runtime.InteropServices;
88
using System.Threading;
99

10-
static class ProcessHelper
10+
public static class ProcessHelper
1111
{
1212
static volatile object lockObject = new object();
1313

GitVersionExe.Tests/GitVersionHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Text;
5-
using GitVersion;
5+
using GitVersion.Helpers;
66
using LibGit2Sharp;
77

88
public static class GitVersionHelper

GitVersionExe/GitVersionExe.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
<Compile Include="ExtensionMethods.cs" />
5858
<Compile Include="GitPreparer.cs" />
5959
<Compile Include="HelpWriter.cs" />
60-
<Compile Include="ProcessHelper.cs" />
6160
<Compile Include="Program.cs" />
6261
<Compile Include="AssemblyInfo.cs" />
6362
<Compile Include="AssemblyInfoFileUpdate.cs" />

0 commit comments

Comments
 (0)