Skip to content

Commit 36c87a8

Browse files
author
Yannis Guedel
committed
Implemented different versioning modes
1 parent 62c532d commit 36c87a8

17 files changed

+154
-44
lines changed

GitVersionCore.Tests/ConfigProviderTests.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.IO;
23
using System.Linq;
34
using System.Reflection;
@@ -26,8 +27,6 @@ public void CanReadDocument()
2627
{
2728
const string text = @"
2829
assembly-versioning-scheme: MajorMinor
29-
develop-branch-tag: alpha
30-
release-branch-tag: rc
3130
next-version: 2.0.0
3231
tag-prefix: '[vV|version-]'
3332
mode: ContinuousDelivery
@@ -42,8 +41,6 @@ public void CanReadDocument()
4241

4342
var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
4443
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinor);
45-
config.DevelopBranchTag.ShouldBe("alpha");
46-
config.ReleaseBranchTag.ShouldBe("rc");
4744
config.NextVersion.ShouldBe("2.0.0");
4845
config.TagPrefix.ShouldBe("[vV|version-]");
4946
config.Release.VersioningMode.ShouldBe(VersioningMode.ContinuousDeployment);
@@ -70,10 +67,16 @@ public void CanInheritVersioningMode()
7067
[Test]
7168
public void CanReadOldDocument()
7269
{
73-
const string text = @"assemblyVersioningScheme: MajorMinor";
70+
const string text = @"
71+
assemblyVersioningScheme: MajorMinor
72+
develop-branch-tag: alpha
73+
release-branch-tag: rc
74+
";
7475
SetupConfigFileContent(text);
7576
var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
7677
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinor);
78+
config.Develop.Tag.ShouldBe("alpha");
79+
config.Release.Tag.ShouldBe("rc");
7780
}
7881

7982
[Test]
@@ -83,8 +86,8 @@ public void CanReadDefaultDocument()
8386
SetupConfigFileContent(text);
8487
var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
8588
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinorPatch);
86-
config.DevelopBranchTag.ShouldBe("unstable");
87-
config.ReleaseBranchTag.ShouldBe("beta");
89+
config.Develop.Tag.ShouldBe("unstable");
90+
config.Release.Tag.ShouldBe("beta");
8891
config.TagPrefix.ShouldBe("[vV]");
8992
config.NextVersion.ShouldBe(null);
9093
}
@@ -93,7 +96,7 @@ public void CanReadDefaultDocument()
9396
public void VerifyInit()
9497
{
9598
var config = typeof(Config);
96-
var aliases = config.GetProperties().Select(p => ((YamlAliasAttribute) p.GetCustomAttribute(typeof(YamlAliasAttribute))).Alias);
99+
var aliases = config.GetProperties().Where(p => p.GetCustomAttribute<ObsoleteAttribute>() == null).Select(p => ((YamlAliasAttribute) p.GetCustomAttribute(typeof(YamlAliasAttribute))).Alias);
97100
var writer = new StringWriter();
98101

99102
ConfigReader.WriteSample(writer);

GitVersionCore.Tests/IntegrationTests/GitFlow/DevelopScenarios.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,41 @@ public void CanChangeDevelopTagViaConfig()
3737
{
3838
using (var fixture = new EmptyRepositoryFixture(new Config
3939
{
40-
DevelopBranchTag = "alpha"
40+
Develop = {
41+
Tag = "alpha"
42+
}
4143
}))
4244
{
4345
fixture.Repository.MakeATaggedCommit("1.0.0");
4446
fixture.Repository.CreateBranch("develop").Checkout();
4547
fixture.AssertFullSemver("1.1.0-alpha.0+0");
4648
}
4749
}
50+
51+
[Test]
52+
public void CanHandleContinuousDelivery()
53+
{
54+
using (var fixture = new EmptyRepositoryFixture(new Config
55+
{
56+
Develop =
57+
{
58+
VersioningMode = VersioningMode.ContinuousDelivery
59+
}
60+
}))
61+
{
62+
fixture.Repository.MakeATaggedCommit("1.0.0");
63+
fixture.Repository.CreateBranch("develop").Checkout();
64+
fixture.Repository.MakeATaggedCommit("1.1.0-alpha7");
65+
fixture.AssertFullSemver("1.1.0-alpha.7+1");
66+
}
67+
}
4868

4969
[Test]
5070
public void CanClearDevelopTagViaConfig()
5171
{
5272
using (var fixture = new EmptyRepositoryFixture(new Config
5373
{
54-
DevelopBranchTag = ""
74+
Develop = {Tag= ""}
5575
}))
5676
{
5777
fixture.Repository.MakeATaggedCommit("1.0.0");

GitVersionCore.Tests/IntegrationTests/GitFlow/ReleaseBranchTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ public void CanTakeVersionFromReleaseBranchWithTagOverriden()
3535
}
3636
}
3737

38+
[Test]
39+
public void CanHandleContinuousDeployment()
40+
{
41+
var config = new Config
42+
{
43+
VersioningMode = VersioningMode.ContinuousDeployment
44+
};
45+
using (var fixture = new EmptyRepositoryFixture(config))
46+
{
47+
fixture.Repository.MakeATaggedCommit("1.0.3");
48+
fixture.Repository.CreateBranch("develop");
49+
fixture.Repository.MakeCommits(5);
50+
fixture.Repository.CreateBranch("release-2.0.0");
51+
fixture.Repository.Checkout("release-2.0.0");
52+
53+
fixture.AssertFullSemver("2.0.0-beta.5+5");
54+
}
55+
}
56+
3857
[Test]
3958
public void CanHandleReleaseBranchWithStability()
4059
{

GitVersionCore/Configuration/Config.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace GitVersion
22
{
3+
using System;
4+
35
using YamlDotNet.Serialization;
46

57
public class Config
@@ -9,22 +11,43 @@ public class Config
911
public Config()
1012
{
1113
AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch;
12-
DevelopBranchTag = "unstable";
13-
ReleaseBranchTag = "beta";
1414
TagPrefix = "[vV]";
15-
Release = new BranchConfig();
16-
Develop = new BranchConfig();
15+
Release = new BranchConfig { Tag = "beta" };
16+
Develop = new BranchConfig { Tag = "unstable" };
1717
VersioningMode = VersioningMode.ContinuousDelivery;
18+
Develop.VersioningMode = VersioningMode.ContinuousDeployment;
1819
}
1920

2021
[YamlAlias("assembly-versioning-scheme")]
2122
public AssemblyVersioningScheme AssemblyVersioningScheme { get; set; }
2223

2324
[YamlAlias("develop-branch-tag")]
24-
public string DevelopBranchTag { get; set; }
25+
[Obsolete]
26+
public string DevelopBranchTag
27+
{
28+
set
29+
{
30+
Develop.Tag = value;
31+
}
32+
get
33+
{
34+
return Develop.Tag;
35+
}
36+
}
2537

2638
[YamlAlias("release-branch-tag")]
27-
public string ReleaseBranchTag { get; set; }
39+
[Obsolete]
40+
public string ReleaseBranchTag
41+
{
42+
set
43+
{
44+
Release.Tag = value;
45+
}
46+
get
47+
{
48+
return Release.Tag;
49+
}
50+
}
2851

2952
[YamlAlias("mode")]
3053
public VersioningMode VersioningMode

GitVersionCore/Configuration/ConfigReader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ public static Config Read(TextReader reader)
2020
public static void WriteSample(TextWriter writer)
2121
{
2222
writer.WriteLine("# assembly-versioning-scheme: MajorMinorPatchMetadata | MajorMinorPatch | MajorMinor | Major");
23-
writer.WriteLine("# develop-branch-tag: alpha");
24-
writer.WriteLine("# release-branch-tag: rc");
2523
writer.WriteLine("# tag-prefix: '[vV|version-] # regex to match git tag prefix");
2624
writer.WriteLine("# next-version: 1.0.0");
25+
writer.WriteLine("# mode: ContinuousDelivery | ContinuousDeployment");
26+
writer.WriteLine("# release*:\n mode: ContinuousDelivery | ContinuousDeployment\n tag: rc");
27+
writer.WriteLine("# develop:\n mode: ContinuousDelivery | ContinuousDeployment\n tag: alpha");
2728
}
2829
}
2930
}

GitVersionCore/GitFlow/BranchFinders/DevelopBasedVersionFinderBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ protected SemanticVersion FindVersion(
99
GitVersionContext context,
1010
BranchType branchType)
1111
{
12+
context.CurrentBranchConfig = context.Configuration.Develop;
1213
var ancestor = FindCommonAncestorWithDevelop(context.Repository, context.CurrentBranch, branchType);
1314

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

GitVersionCore/GitFlow/BranchFinders/DevelopVersionFinder.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,23 @@ public SemanticVersion FindVersion(GitVersionContext context)
2121
var c = context.Repository.Commits.QueryBy(f);
2222
var numberOfCommitsSinceRelease = c.Count();
2323

24+
var shortVersion = new SemanticVersion()
25+
{
26+
Major = versionFromMaster.Major,
27+
Minor = versionFromMaster.Minor + 1,
28+
Patch = 0,
29+
};
30+
31+
var applicableTagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(context.Configuration, shortVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name, context.Configuration.TagPrefix)).ToList();
32+
var preReleaseTag = context.CurrentBranchConfig.VersioningMode.GetInstance().GetPreReleaseTag(context, applicableTagsInDescendingOrder, numberOfCommitsSinceRelease);
33+
34+
2435
var semanticVersion = new SemanticVersion
2536
{
26-
Major = versionFromMaster.Major,
27-
Minor = versionFromMaster.Minor + 1,
28-
Patch = 0,
29-
PreReleaseTag = context.Configuration.DevelopBranchTag + numberOfCommitsSinceRelease,
37+
Major = shortVersion.Major,
38+
Minor = shortVersion.Minor,
39+
Patch = shortVersion.Patch,
40+
PreReleaseTag = preReleaseTag,
3041
BuildMetaData = new SemanticVersionBuildMetaData(numberOfCommitsSinceRelease, context.CurrentBranch.Name,tip.Sha,tip.When()),
3142
};
3243

GitVersionCore/GitFlow/BranchFinders/HotfixVersionFinder.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace GitVersion
22
{
3+
using System.Linq;
4+
35
using LibGit2Sharp;
46

57
class HotfixVersionFinder
@@ -12,8 +14,8 @@ public SemanticVersion FindVersion(GitVersionContext context)
1214
EnsureVersionIsValid(shortVersion, context.CurrentBranch);
1315

1416
var nbHotfixCommits = BranchCommitDifferenceFinder.NumberOfCommitsInBranchNotKnownFromBaseBranch(context.Repository, context.CurrentBranch, BranchType.Hotfix, "master");
15-
16-
var semanticVersionPreReleaseTag = GetSemanticVersionPreReleaseTag(context, shortVersion, nbHotfixCommits);
17+
var tagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(context.Configuration, shortVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name, context.Configuration.TagPrefix)).ToList();
18+
var semanticVersionPreReleaseTag = context.CurrentBranchConfig.VersioningMode.GetInstance().GetPreReleaseTag(context, tagsInDescendingOrder, nbHotfixCommits);
1719
return new SemanticVersion
1820
{
1921
Major = shortVersion.Major,
@@ -24,17 +26,6 @@ public SemanticVersion FindVersion(GitVersionContext context)
2426
};
2527
}
2628

27-
static string GetSemanticVersionPreReleaseTag(GitVersionContext context, SemanticVersion shortVersion, int nbHotfixCommits)
28-
{
29-
var semanticVersionPreReleaseTag = context.Configuration.ReleaseBranchTag + ".1";
30-
var tagVersion = RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context, shortVersion);
31-
if (tagVersion != null)
32-
{
33-
semanticVersionPreReleaseTag = tagVersion;
34-
}
35-
return semanticVersionPreReleaseTag;
36-
}
37-
3829
static string GetSuffix(Branch branch)
3930
{
4031
return branch.Name.TrimStart("hotfix-").TrimStart("hotfix/");

GitVersionCore/GitFlow/BranchFinders/RecentTagVersionExtractor.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ namespace GitVersion
66

77
class RecentTagVersionExtractor
88
{
9-
internal static SemanticVersionPreReleaseTag RetrieveMostRecentOptionalTagVersion(GitVersionContext context, SemanticVersion matchVersion)
10-
{
11-
var tagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(context.Configuration, matchVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name, context.Configuration.TagPrefix));
12-
return RetrieveMostRecentOptionalTagVersion(context, tagsInDescendingOrder.ToList());
13-
}
14-
159
internal static SemanticVersionPreReleaseTag RetrieveMostRecentOptionalTagVersion(GitVersionContext context, List<Tag> applicableTagsInDescendingOrder)
1610
{
1711
if (applicableTagsInDescendingOrder.Any())

GitVersionCore/GitFlow/BranchFinders/ReleaseVersionFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public SemanticVersion FindVersion(GitVersionContext context)
1414

1515
var applicableTagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(context.Configuration, shortVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name, context.Configuration.TagPrefix)).ToList();
1616
var numberOfCommitsSinceLastTagOrBranchPoint = BranchCommitDifferenceFinder.NumberOfCommitsSinceLastTagOrBranchPoint(context, applicableTagsInDescendingOrder, BranchType.Release, "develop");
17-
var semanticVersionPreReleaseTag = RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context, applicableTagsInDescendingOrder) ?? context.Configuration.ReleaseBranchTag + ".1";
17+
var semanticVersionPreReleaseTag = context.Configuration.Release.VersioningMode.GetInstance().GetPreReleaseTag(context, applicableTagsInDescendingOrder, numberOfCommitsSinceLastTagOrBranchPoint);
1818

1919
return new SemanticVersion
2020
{

GitVersionCore/GitFlow/GitFlowVersionFinder.cs

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

1212
if (context.CurrentBranch.IsHotfix())
1313
{
14+
context.CurrentBranchConfig = context.Configuration.Release;
1415
return new HotfixVersionFinder().FindVersion(context);
1516
}
1617

1718
if (context.CurrentBranch.IsRelease())
1819
{
20+
context.CurrentBranchConfig = context.Configuration.Release;
1921
return new ReleaseVersionFinder().FindVersion(context);
2022
}
2123

2224
if (context.CurrentBranch.IsDevelop())
2325
{
26+
context.CurrentBranchConfig = context.Configuration.Develop;
2427
return new DevelopVersionFinder().FindVersion(context);
2528
}
2629

GitVersionCore/GitHubFlow/OtherBranchVersionFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public bool FindVersion(GitVersionContext context, out SemanticVersion semanticV
2323

2424
if (semanticVersionPreReleaseTag.Name == "release")
2525
{
26-
semanticVersionPreReleaseTag.Name = context.Configuration.ReleaseBranchTag;
26+
semanticVersionPreReleaseTag.Name = context.Configuration.Release.Tag;
2727
}
2828

2929
semanticVersion = new SemanticVersion

GitVersionCore/GitVersionContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public GitVersionContext(IRepository repository, Branch currentBranch, Config co
4040
public Branch CurrentBranch { get; private set; }
4141
public Commit CurrentCommit { get; private set; }
4242

43+
public BranchConfig CurrentBranchConfig { get; set; }
44+
4345
IEnumerable<Branch> GetBranchesContainingCommit(string commitSha)
4446
{
4547
var directBranchHasBeenFound = false;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
namespace GitVersion.VersioningModes
22
{
3+
using System.Collections.Generic;
4+
5+
using LibGit2Sharp;
6+
37
public class ContinuousDeliveryMode : VersioningModeBase
48
{
9+
public override SemanticVersionPreReleaseTag GetPreReleaseTag(GitVersionContext context, List<Tag> possibleCommits, int numberOfCommits)
10+
{
11+
return RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context, possibleCommits)
12+
?? context.CurrentBranchConfig.Tag + ".1";
13+
}
514
}
615
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
namespace GitVersion.VersioningModes
22
{
3+
using System.Collections.Generic;
4+
5+
using LibGit2Sharp;
6+
37
public class ContinuousDeploymentMode : VersioningModeBase
48
{
9+
public override SemanticVersionPreReleaseTag GetPreReleaseTag(GitVersionContext context, List<Tag> possibleTags, int numberOfCommits)
10+
{
11+
return context.CurrentBranchConfig.Tag + "." + numberOfCommits;
12+
}
513
}
6-
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
namespace GitVersion
22
{
3+
using System;
4+
5+
using GitVersion.VersioningModes;
6+
37
public enum VersioningMode
48
{
59
ContinuousDelivery,
610
ContinuousDeployment
711
}
12+
13+
public static class VersioningModeExtension
14+
{
15+
public static VersioningModeBase GetInstance(this VersioningMode _this)
16+
{
17+
switch (_this)
18+
{
19+
case VersioningMode.ContinuousDelivery:
20+
return new ContinuousDeliveryMode();
21+
case VersioningMode.ContinuousDeployment:
22+
return new ContinuousDeploymentMode();
23+
default:
24+
throw new ArgumentException("No instance exists for this versioning mode.");
25+
}
26+
}
27+
}
828
}

0 commit comments

Comments
 (0)