diff --git a/GitVersionCore.Tests/ConfigProviderTests.cs b/GitVersionCore.Tests/ConfigProviderTests.cs index 97b90c49b5..8dca0b478e 100644 --- a/GitVersionCore.Tests/ConfigProviderTests.cs +++ b/GitVersionCore.Tests/ConfigProviderTests.cs @@ -51,22 +51,6 @@ public void CanReadDocument() config.Branches["develop"].VersioningMode.ShouldBe(VersioningMode.ContinuousDeployment); } - [Test] - public void CanInheritVersioningMode() - { - const string text = @" -mode: ContinuousDelivery -branches: - develop: - mode: ContinuousDeployment -"; - SetupConfigFileContent(text); - var config = ConfigurationProvider.Provide(gitDirectory, fileSystem); - config.Branches["develop"].VersioningMode.ShouldBe(VersioningMode.ContinuousDeployment); - config.Branches["develop"].Tag.ShouldBe("unstable"); - config.Branches["release[/-]"].VersioningMode.ShouldBe(VersioningMode.ContinuousDelivery); - } - [Test] public void CanReadOldDocument() { diff --git a/GitVersionCore.Tests/GitVersionContextTests.cs b/GitVersionCore.Tests/GitVersionContextTests.cs new file mode 100644 index 0000000000..4bb04108a4 --- /dev/null +++ b/GitVersionCore.Tests/GitVersionContextTests.cs @@ -0,0 +1,54 @@ +namespace GitVersionCore.Tests +{ + using GitVersion; + using NUnit.Framework; + using Shouldly; + + public class GitVersionContextTests + { + [Test] + [Theory] + public void CanInheritVersioningMode(VersioningMode mode) + { + var config = new Config + { + VersioningMode = mode + }; + + var mockBranch = new MockBranch("master") { new MockCommit { CommitterEx = SignatureBuilder.SignatureNow() } }; + var mockRepository = new MockRepository + { + Branches = new MockBranchCollection + { + mockBranch + } + }; + + var context = new GitVersionContext(mockRepository, mockBranch, config); + context.Configuration.VersioningMode.ShouldBe(mode); + } + + [Test] + public void UsesBranchSpecificConfigOverTopLevelDefaults() + { + var config = new Config + { + VersioningMode = VersioningMode.ContinuousDelivery + }; + config.Branches["develop"].VersioningMode = VersioningMode.ContinuousDeployment; + config.Branches["develop"].Tag = "alpha"; + var develop = new MockBranch("develop") { new MockCommit { CommitterEx = SignatureBuilder.SignatureNow() } }; + var mockRepository = new MockRepository + { + Branches = new MockBranchCollection + { + new MockBranch("master") { new MockCommit { CommitterEx = SignatureBuilder.SignatureNow() } }, + develop + } + }; + var context = new GitVersionContext(mockRepository, develop, config); + context.Configuration.VersioningMode.ShouldBe(VersioningMode.ContinuousDeployment); + context.Configuration.Tag.ShouldBe("alpha"); + } + } +} \ No newline at end of file diff --git a/GitVersionCore.Tests/GitVersionCore.Tests.csproj b/GitVersionCore.Tests/GitVersionCore.Tests.csproj index ffd1247d03..1ee1fb42dc 100644 --- a/GitVersionCore.Tests/GitVersionCore.Tests.csproj +++ b/GitVersionCore.Tests/GitVersionCore.Tests.csproj @@ -74,6 +74,7 @@ + @@ -91,6 +92,18 @@ + + + + + + + + + + + + @@ -99,6 +112,7 @@ + diff --git a/GitVersionCore.Tests/IntegrationTests/GitFlow/MetaDataByCommitScenarios.cs b/GitVersionCore.Tests/IntegrationTests/GitFlow/MetaDataByCommitScenarios.cs index 701bedd6a9..c1f2d59c9c 100644 --- a/GitVersionCore.Tests/IntegrationTests/GitFlow/MetaDataByCommitScenarios.cs +++ b/GitVersionCore.Tests/IntegrationTests/GitFlow/MetaDataByCommitScenarios.cs @@ -1,5 +1,6 @@ using System; using GitVersion; +using GitVersionCore.Tests; using LibGit2Sharp; using NUnit.Framework; using Shouldly; @@ -157,7 +158,7 @@ static void EnsureBranchMatch(CommitCountingRepoFixture fixture, string branchNa var referenceCommitFinder = commitFinder ?? (r => r.FindBranch(branchName).Tip); var commit = referenceCommitFinder(fixture.Repository); - var releaseDate = LastMinorVersionFinder.Execute(fixture.Repository, new Config(), commit); + var releaseDate = LastMinorVersionFinder.Execute(fixture.Repository, new TestEffectiveConfiguration(), commit); releaseDate.ShouldBe(commit.When()); } diff --git a/GitVersionCore.Tests/JsonVersionBuilderTests.cs b/GitVersionCore.Tests/JsonVersionBuilderTests.cs index cc25171a9d..986a1c34a1 100644 --- a/GitVersionCore.Tests/JsonVersionBuilderTests.cs +++ b/GitVersionCore.Tests/JsonVersionBuilderTests.cs @@ -17,7 +17,7 @@ public void Json() PreReleaseTag = "unstable4", BuildMetaData = new SemanticVersionBuildMetaData(5, "feature1", "commitSha",DateTimeOffset.Parse("2014-03-06 23:59:59Z")) }; - var variables = VariableProvider.GetVariablesFor(semanticVersion, new Config()); + var variables = VariableProvider.GetVariablesFor(semanticVersion, AssemblyVersioningScheme.MajorMinorPatch, VersioningMode.ContinuousDelivery); var json = JsonOutputFormatter.ToJson(variables); Approvals.Verify(json); } diff --git a/GitVersionTask.Tests/Mocks/MockBranch.cs b/GitVersionCore.Tests/Mocks/MockBranch.cs similarity index 92% rename from GitVersionTask.Tests/Mocks/MockBranch.cs rename to GitVersionCore.Tests/Mocks/MockBranch.cs index 7da19da4bc..b03e5d5a27 100644 --- a/GitVersionTask.Tests/Mocks/MockBranch.cs +++ b/GitVersionCore.Tests/Mocks/MockBranch.cs @@ -18,7 +18,7 @@ public MockBranch(string name, string canonicalName) public MockBranch() { - + } MockCommitLog commits = new MockCommitLog(); string name; @@ -66,7 +66,7 @@ public bool Remove(Commit item) return commits.Remove(item); } - public int Count{get{return commits.Count;}} + public int Count { get { return commits.Count; } } - public bool IsReadOnly { get{return false;} } + public bool IsReadOnly { get { return false; } } } \ No newline at end of file diff --git a/GitVersionTask.Tests/Mocks/MockBranchCollection.cs b/GitVersionCore.Tests/Mocks/MockBranchCollection.cs similarity index 82% rename from GitVersionTask.Tests/Mocks/MockBranchCollection.cs rename to GitVersionCore.Tests/Mocks/MockBranchCollection.cs index 69491569b2..a9ae6df7fd 100644 --- a/GitVersionTask.Tests/Mocks/MockBranchCollection.cs +++ b/GitVersionCore.Tests/Mocks/MockBranchCollection.cs @@ -13,7 +13,7 @@ public override IEnumerator GetEnumerator() public override Branch this[string name] { - get { return Branches.FirstOrDefault(x=>x.Name ==name); } + get { return Branches.FirstOrDefault(x => x.Name == name); } } public void Add(Branch item) @@ -40,16 +40,17 @@ public override void Remove(Branch item) { Branches.Remove(item); } - bool ICollection.Remove(Branch item) + bool ICollection.Remove(Branch item) { return Branches.Remove(item); } public int Count { - get { + get + { return Branches.Count; } } - public bool IsReadOnly { get{return false;}} + public bool IsReadOnly { get { return false; } } } \ No newline at end of file diff --git a/GitVersionTask.Tests/Mocks/MockCommit.cs b/GitVersionCore.Tests/Mocks/MockCommit.cs similarity index 76% rename from GitVersionTask.Tests/Mocks/MockCommit.cs rename to GitVersionCore.Tests/Mocks/MockCommit.cs index 7b99b27034..3f6f852eec 100644 --- a/GitVersionTask.Tests/Mocks/MockCommit.cs +++ b/GitVersionCore.Tests/Mocks/MockCommit.cs @@ -4,24 +4,24 @@ using LibGit2Sharp; [DebuggerDisplay("{DebuggerDisplay}")] -public class MockCommit:Commit +public class MockCommit : Commit { public MockCommit(ObjectId id = null) { - idEx = id ?? new ObjectId(Guid.NewGuid().ToString().Replace("-", "")+ "00000000"); + idEx = id ?? new ObjectId(Guid.NewGuid().ToString().Replace("-", "") + "00000000"); MessageEx = ""; ParentsEx = new List { null }; CommitterEx = new Signature("Joe", "Joe@bloggs.net", DateTimeOffset.Now); } public string MessageEx; - public override string Message{get { return MessageEx; }} + public override string Message { get { return MessageEx; } } public Signature CommitterEx; - public override Signature Committer{get { return CommitterEx; }} + public override Signature Committer { get { return CommitterEx; } } ObjectId idEx; - public override ObjectId Id{get { return idEx; }} + public override ObjectId Id { get { return idEx; } } public override string Sha { get { return idEx.Sha; } } @@ -39,4 +39,4 @@ string DebuggerDisplay return MessageEx; } } -} +} \ No newline at end of file diff --git a/GitVersionTask.Tests/Mocks/MockCommitLog.cs b/GitVersionCore.Tests/Mocks/MockCommitLog.cs similarity index 87% rename from GitVersionTask.Tests/Mocks/MockCommitLog.cs rename to GitVersionCore.Tests/Mocks/MockCommitLog.cs index 8b97484126..aecb492525 100644 --- a/GitVersionTask.Tests/Mocks/MockCommitLog.cs +++ b/GitVersionCore.Tests/Mocks/MockCommitLog.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using LibGit2Sharp; -public class MockCommitLog:ICommitLog,ICollection +public class MockCommitLog : ICommitLog, ICollection { public List Commits = new List(); @@ -26,7 +26,7 @@ public void Clear() { Commits.Clear(); } - + public bool Contains(Commit item) { @@ -44,6 +44,8 @@ public bool Remove(Commit item) } public int Count { get { return Commits.Count; } } - public bool IsReadOnly {get { return false; } + public bool IsReadOnly + { + get { return false; } } } \ No newline at end of file diff --git a/GitVersionTask.Tests/Mocks/MockMergeCommit.cs b/GitVersionCore.Tests/Mocks/MockMergeCommit.cs similarity index 98% rename from GitVersionTask.Tests/Mocks/MockMergeCommit.cs rename to GitVersionCore.Tests/Mocks/MockMergeCommit.cs index 558a821f49..472fbd10bf 100644 --- a/GitVersionTask.Tests/Mocks/MockMergeCommit.cs +++ b/GitVersionCore.Tests/Mocks/MockMergeCommit.cs @@ -6,4 +6,4 @@ public MockMergeCommit(ObjectId id = null) : base(id) { ParentsEx.Add(null); } -} +} \ No newline at end of file diff --git a/GitVersionTask.Tests/Mocks/MockReferenceCollection.cs b/GitVersionCore.Tests/Mocks/MockReferenceCollection.cs similarity index 93% rename from GitVersionTask.Tests/Mocks/MockReferenceCollection.cs rename to GitVersionCore.Tests/Mocks/MockReferenceCollection.cs index c839d9bee7..5c51ba26f7 100644 --- a/GitVersionTask.Tests/Mocks/MockReferenceCollection.cs +++ b/GitVersionCore.Tests/Mocks/MockReferenceCollection.cs @@ -8,9 +8,9 @@ public class MockReferenceCollection : ReferenceCollection, ICollection public override ReflogCollection Log(string canonicalName) { return new MockReflogCollection - { - Commits = Commits - }; + { + Commits = Commits + }; } public List Commits = new List(); diff --git a/GitVersionTask.Tests/Mocks/MockReflogCollection.cs b/GitVersionCore.Tests/Mocks/MockReflogCollection.cs similarity index 100% rename from GitVersionTask.Tests/Mocks/MockReflogCollection.cs rename to GitVersionCore.Tests/Mocks/MockReflogCollection.cs diff --git a/GitVersionTask.Tests/Mocks/MockRepository.cs b/GitVersionCore.Tests/Mocks/MockRepository.cs similarity index 99% rename from GitVersionTask.Tests/Mocks/MockRepository.cs rename to GitVersionCore.Tests/Mocks/MockRepository.cs index f2585b5024..252f527648 100644 --- a/GitVersionTask.Tests/Mocks/MockRepository.cs +++ b/GitVersionCore.Tests/Mocks/MockRepository.cs @@ -136,4 +136,4 @@ public StashCollection Stashes { get { throw new NotImplementedException(); } } -} +} \ No newline at end of file diff --git a/GitVersionTask.Tests/Mocks/MockTag.cs b/GitVersionCore.Tests/Mocks/MockTag.cs similarity index 93% rename from GitVersionTask.Tests/Mocks/MockTag.cs rename to GitVersionCore.Tests/Mocks/MockTag.cs index 5f53d2901e..4ea1364815 100644 --- a/GitVersionTask.Tests/Mocks/MockTag.cs +++ b/GitVersionCore.Tests/Mocks/MockTag.cs @@ -1,6 +1,6 @@ using LibGit2Sharp; -public class MockTag:Tag +public class MockTag : Tag { public string NameEx; diff --git a/GitVersionTask.Tests/Mocks/MockTagAnnotation.cs b/GitVersionCore.Tests/Mocks/MockTagAnnotation.cs similarity index 84% rename from GitVersionTask.Tests/Mocks/MockTagAnnotation.cs rename to GitVersionCore.Tests/Mocks/MockTagAnnotation.cs index 6d158b9537..8d14af5d64 100644 --- a/GitVersionTask.Tests/Mocks/MockTagAnnotation.cs +++ b/GitVersionCore.Tests/Mocks/MockTagAnnotation.cs @@ -1,6 +1,6 @@ using LibGit2Sharp; -public class MockTagAnnotation:TagAnnotation +public class MockTagAnnotation : TagAnnotation { public Signature TaggerEx; diff --git a/GitVersionTask.Tests/Mocks/MockTagCollection.cs b/GitVersionCore.Tests/Mocks/MockTagCollection.cs similarity index 93% rename from GitVersionTask.Tests/Mocks/MockTagCollection.cs rename to GitVersionCore.Tests/Mocks/MockTagCollection.cs index 6223f12485..05f4724d7f 100644 --- a/GitVersionTask.Tests/Mocks/MockTagCollection.cs +++ b/GitVersionCore.Tests/Mocks/MockTagCollection.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using LibGit2Sharp; -public class MockTagCollection : TagCollection,ICollection +public class MockTagCollection : TagCollection, ICollection { public List Tags = new List(); diff --git a/GitVersionTask.Tests/Mocks/SignatureBuilder.cs b/GitVersionCore.Tests/Mocks/SignatureBuilder.cs similarity index 100% rename from GitVersionTask.Tests/Mocks/SignatureBuilder.cs rename to GitVersionCore.Tests/Mocks/SignatureBuilder.cs diff --git a/GitVersionCore.Tests/TestEffectiveConfiguration.cs b/GitVersionCore.Tests/TestEffectiveConfiguration.cs new file mode 100644 index 0000000000..055be59556 --- /dev/null +++ b/GitVersionCore.Tests/TestEffectiveConfiguration.cs @@ -0,0 +1,17 @@ +namespace GitVersionCore.Tests +{ + using GitVersion; + + public class TestEffectiveConfiguration : EffectiveConfiguration + { + public TestEffectiveConfiguration( + AssemblyVersioningScheme assemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch, + VersioningMode versioningMode = VersioningMode.ContinuousDelivery, + string gitTagPrefix = "v", + string tag = "", + string nextVersion = null) : + base(assemblyVersioningScheme, versioningMode, gitTagPrefix, tag, nextVersion) + { + } + } +} \ No newline at end of file diff --git a/GitVersionCore.Tests/VariableProviderTests.cs b/GitVersionCore.Tests/VariableProviderTests.cs index 353bbe0950..d87b5efde5 100644 --- a/GitVersionCore.Tests/VariableProviderTests.cs +++ b/GitVersionCore.Tests/VariableProviderTests.cs @@ -22,7 +22,7 @@ public void DevelopBranchFormatsSemVerForCiFeed() semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - var vars = VariableProvider.GetVariablesFor(semVer, new Config()); + var vars = VariableProvider.GetVariablesFor(semVer, AssemblyVersioningScheme.MajorMinorPatch, VersioningMode.ContinuousDelivery); vars[VariableProvider.SemVer].ShouldBe("1.2.3.5-unstable"); } diff --git a/GitVersionCore/BuildServers/BuildServerBase.cs b/GitVersionCore/BuildServers/BuildServerBase.cs index a38d178315..f5e2039fb0 100644 --- a/GitVersionCore/BuildServers/BuildServerBase.cs +++ b/GitVersionCore/BuildServers/BuildServerBase.cs @@ -1,6 +1,7 @@ namespace GitVersion { using System; + using System.Collections.Generic; public abstract class BuildServerBase : IBuildServer { @@ -9,7 +10,7 @@ public abstract class BuildServerBase : IBuildServer public abstract string GenerateSetVersionMessage(string versionToUseForBuildNumber); public abstract string[] GenerateSetParameterMessage(string name, string value); - public virtual void WriteIntegration(SemanticVersion semanticVersion, Action writer) + public virtual void WriteIntegration(SemanticVersion semanticVersion, Action writer, Dictionary variables) { if (semanticVersion == null) { @@ -22,9 +23,10 @@ public virtual void WriteIntegration(SemanticVersion semanticVersion, Action writer); + void WriteIntegration(SemanticVersion semanticVersion, Action writer, Dictionary variables); } } diff --git a/GitVersionCore/Configuration/BranchConfig.cs b/GitVersionCore/Configuration/BranchConfig.cs index ebe25de3c6..1b7494e0f0 100644 --- a/GitVersionCore/Configuration/BranchConfig.cs +++ b/GitVersionCore/Configuration/BranchConfig.cs @@ -5,7 +5,7 @@ public class BranchConfig { [YamlAlias("mode")] - public VersioningMode VersioningMode { get; set; } + public VersioningMode? VersioningMode { get; set; } [YamlAlias("tag")] public string Tag { get; set; } diff --git a/GitVersionCore/Configuration/Config.cs b/GitVersionCore/Configuration/Config.cs index 84fb23ee74..ed487c57db 100644 --- a/GitVersionCore/Configuration/Config.cs +++ b/GitVersionCore/Configuration/Config.cs @@ -6,21 +6,19 @@ public class Config { - VersioningMode versioningMode; - Dictionary branches = new Dictionary(); public Config() { AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch; TagPrefix = "[vV]"; - VersioningMode = VersioningMode.ContinuousDelivery; + VersioningMode = GitVersion.VersioningMode.ContinuousDelivery; Branches["release[/-]"] = new BranchConfig { Tag = "beta" }; Branches["hotfix[/-]"] = new BranchConfig { Tag = "beta" }; Branches["develop"] = new BranchConfig { Tag = "unstable", - VersioningMode = VersioningMode.ContinuousDeployment + VersioningMode = GitVersion.VersioningMode.ContinuousDeployment }; } @@ -28,18 +26,7 @@ public Config() public AssemblyVersioningScheme AssemblyVersioningScheme { get; set; } [YamlAlias("mode")] - public VersioningMode VersioningMode - { - get - { - return versioningMode; - } - set - { - Branches.ToList().ForEach(b => b.Value.VersioningMode = value); - versioningMode = value; - } - } + public VersioningMode? VersioningMode { get; set; } [YamlAlias("branches")] public Dictionary Branches diff --git a/GitVersionCore/EffectiveConfiguration.cs b/GitVersionCore/EffectiveConfiguration.cs new file mode 100644 index 0000000000..9daed63748 --- /dev/null +++ b/GitVersionCore/EffectiveConfiguration.cs @@ -0,0 +1,33 @@ +namespace GitVersion +{ + /// + /// Configuration can be applied to different things, effective configuration is the result after applying the appropriate configuration + /// + public class EffectiveConfiguration + { + public EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningScheme, VersioningMode versioningMode, string gitTagPrefix, string tag, string nextVersion) + { + AssemblyVersioningScheme = assemblyVersioningScheme; + VersioningMode = versioningMode; + GitTagPrefix = gitTagPrefix; + Tag = tag; + NextVersion = nextVersion; + } + + public VersioningMode VersioningMode { get; private set; } + + public AssemblyVersioningScheme AssemblyVersioningScheme { get; private set; } + + /// + /// Git tag prefix + /// + public string GitTagPrefix { get; private set; } + + /// + /// Tag to use when calculating SemVer + /// + public string Tag { get; private set; } + + public string NextVersion { get; private set; } + } +} \ No newline at end of file diff --git a/GitVersionCore/GitFlow/BranchFinders/DevelopVersionFinder.cs b/GitVersionCore/GitFlow/BranchFinders/DevelopVersionFinder.cs index cf1a05fb21..c3e45fe974 100644 --- a/GitVersionCore/GitFlow/BranchFinders/DevelopVersionFinder.cs +++ b/GitVersionCore/GitFlow/BranchFinders/DevelopVersionFinder.cs @@ -28,8 +28,8 @@ public SemanticVersion FindVersion(GitVersionContext context) Patch = 0, }; - var applicableTagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(context.Configuration, shortVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name, context.Configuration.TagPrefix)).ToList(); - var preReleaseTag = context.CurrentBranchConfig.VersioningMode.GetInstance().GetPreReleaseTag(context, applicableTagsInDescendingOrder, numberOfCommitsSinceRelease); + var applicableTagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(context.Configuration, shortVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name, context.Configuration.GitTagPrefix)).ToList(); + var preReleaseTag = context.Configuration.VersioningMode.GetInstance().GetPreReleaseTag(context, applicableTagsInDescendingOrder, numberOfCommitsSinceRelease); var semanticVersion = new SemanticVersion diff --git a/GitVersionCore/GitFlow/BranchFinders/HotfixVersionFinder.cs b/GitVersionCore/GitFlow/BranchFinders/HotfixVersionFinder.cs index 60bf4937ac..bdc7470153 100644 --- a/GitVersionCore/GitFlow/BranchFinders/HotfixVersionFinder.cs +++ b/GitVersionCore/GitFlow/BranchFinders/HotfixVersionFinder.cs @@ -9,13 +9,13 @@ class HotfixVersionFinder public SemanticVersion FindVersion(GitVersionContext context) { var versionString = GetSuffix(context.CurrentBranch); - var shortVersion = SemanticVersion.Parse(versionString, context.Configuration.TagPrefix); + var shortVersion = SemanticVersion.Parse(versionString, context.Configuration.GitTagPrefix); EnsureVersionIsValid(shortVersion, context.CurrentBranch); var nbHotfixCommits = BranchCommitDifferenceFinder.NumberOfCommitsInBranchNotKnownFromBaseBranch(context.Repository, context.CurrentBranch, BranchType.Hotfix, "master"); - var tagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(context.Configuration, shortVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name, context.Configuration.TagPrefix)).ToList(); - var semanticVersionPreReleaseTag = context.CurrentBranchConfig.VersioningMode.GetInstance().GetPreReleaseTag(context, tagsInDescendingOrder, nbHotfixCommits); + var tagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(context.Configuration, shortVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name, context.Configuration.GitTagPrefix)).ToList(); + var semanticVersionPreReleaseTag = context.Configuration.VersioningMode.GetInstance().GetPreReleaseTag(context, tagsInDescendingOrder, nbHotfixCommits); return new SemanticVersion { Major = shortVersion.Major, diff --git a/GitVersionCore/GitFlow/BranchFinders/MasterVersionFinder.cs b/GitVersionCore/GitFlow/BranchFinders/MasterVersionFinder.cs index 067e05ea21..0ceb08c7e5 100644 --- a/GitVersionCore/GitFlow/BranchFinders/MasterVersionFinder.cs +++ b/GitVersionCore/GitFlow/BranchFinders/MasterVersionFinder.cs @@ -9,7 +9,7 @@ public SemanticVersion FindVersion(GitVersionContext context) foreach (var tag in context.Repository.TagsByDate(context.CurrentCommit)) { SemanticVersion shortVersion; - if (SemanticVersion.TryParse(tag.Name, context.Configuration.TagPrefix, out shortVersion)) + if (SemanticVersion.TryParse(tag.Name, context.Configuration.GitTagPrefix, out shortVersion)) { return BuildVersion(context.CurrentCommit, shortVersion); } diff --git a/GitVersionCore/GitFlow/BranchFinders/RecentTagVersionExtractor.cs b/GitVersionCore/GitFlow/BranchFinders/RecentTagVersionExtractor.cs index ec924c5431..e10ccdfe5a 100644 --- a/GitVersionCore/GitFlow/BranchFinders/RecentTagVersionExtractor.cs +++ b/GitVersionCore/GitFlow/BranchFinders/RecentTagVersionExtractor.cs @@ -11,7 +11,7 @@ internal static SemanticVersionPreReleaseTag RetrieveMostRecentOptionalTagVersio if (applicableTagsInDescendingOrder.Any()) { var taggedCommit = applicableTagsInDescendingOrder.First().Target; - var preReleaseVersion = applicableTagsInDescendingOrder.Select(tag => SemanticVersion.Parse(tag.Name, context.Configuration.TagPrefix)).FirstOrDefault(); + var preReleaseVersion = applicableTagsInDescendingOrder.Select(tag => SemanticVersion.Parse(tag.Name, context.Configuration.GitTagPrefix)).FirstOrDefault(); if (preReleaseVersion != null) { if (taggedCommit != context.CurrentCommit) diff --git a/GitVersionCore/GitFlow/BranchFinders/ReleaseVersionFinder.cs b/GitVersionCore/GitFlow/BranchFinders/ReleaseVersionFinder.cs index 463bc54327..38066caae1 100644 --- a/GitVersionCore/GitFlow/BranchFinders/ReleaseVersionFinder.cs +++ b/GitVersionCore/GitFlow/BranchFinders/ReleaseVersionFinder.cs @@ -8,13 +8,13 @@ class ReleaseVersionFinder public SemanticVersion FindVersion(GitVersionContext context) { var versionString = GetSuffix(context.CurrentBranch); - var shortVersion = SemanticVersion.Parse(versionString, context.Configuration.TagPrefix); + var shortVersion = SemanticVersion.Parse(versionString, context.Configuration.GitTagPrefix); EnsureVersionIsValid(shortVersion, context.CurrentBranch); - var applicableTagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(context.Configuration, shortVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name, context.Configuration.TagPrefix)).ToList(); + var applicableTagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(context.Configuration, shortVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name, context.Configuration.GitTagPrefix)).ToList(); var numberOfCommitsSinceLastTagOrBranchPoint = BranchCommitDifferenceFinder.NumberOfCommitsSinceLastTagOrBranchPoint(context, applicableTagsInDescendingOrder, BranchType.Release, "develop"); - var semanticVersionPreReleaseTag = context.Configuration.Branches["release[/-]"].VersioningMode.GetInstance().GetPreReleaseTag(context, applicableTagsInDescendingOrder, numberOfCommitsSinceLastTagOrBranchPoint); + var semanticVersionPreReleaseTag = context.Configuration.VersioningMode.GetInstance().GetPreReleaseTag(context, applicableTagsInDescendingOrder, numberOfCommitsSinceLastTagOrBranchPoint); return new SemanticVersion { diff --git a/GitVersionCore/GitFlow/BranchFinders/SupportVersionFinder.cs b/GitVersionCore/GitFlow/BranchFinders/SupportVersionFinder.cs index 5af0c8d6f8..d4e4f1623e 100644 --- a/GitVersionCore/GitFlow/BranchFinders/SupportVersionFinder.cs +++ b/GitVersionCore/GitFlow/BranchFinders/SupportVersionFinder.cs @@ -4,12 +4,12 @@ namespace GitVersion class SupportVersionFinder { - public SemanticVersion FindVersion(IRepository repository, Commit tip, Config configuration) + public SemanticVersion FindVersion(IRepository repository, Commit tip, EffectiveConfiguration configuration) { foreach (var tag in repository.TagsByDate(tip)) { SemanticVersion shortVersion; - if (SemanticVersion.TryParse(tag.Name, configuration.TagPrefix, out shortVersion)) + if (SemanticVersion.TryParse(tag.Name, configuration.GitTagPrefix, out shortVersion)) { return BuildVersion(tip, shortVersion); } diff --git a/GitVersionCore/GitFlow/BranchFinders/VersionOnMasterFinder.cs b/GitVersionCore/GitFlow/BranchFinders/VersionOnMasterFinder.cs index 7b5033bc07..554c5bdaf8 100644 --- a/GitVersionCore/GitFlow/BranchFinders/VersionOnMasterFinder.cs +++ b/GitVersionCore/GitFlow/BranchFinders/VersionOnMasterFinder.cs @@ -12,7 +12,7 @@ public VersionPoint Execute(GitVersionContext context, DateTimeOffset olderThan) foreach (var tag in context.Repository.TagsByDate(commit)) { SemanticVersion semanticVersion; - if (SemanticVersion.TryParse(tag.Name, context.Configuration.TagPrefix, out semanticVersion)) + if (SemanticVersion.TryParse(tag.Name, context.Configuration.GitTagPrefix, out semanticVersion)) { return new VersionPoint { diff --git a/GitVersionCore/GitHubFlow/LastTaggedReleaseFinder.cs b/GitVersionCore/GitHubFlow/LastTaggedReleaseFinder.cs index f913a1f670..2777693816 100644 --- a/GitVersionCore/GitHubFlow/LastTaggedReleaseFinder.cs +++ b/GitVersionCore/GitHubFlow/LastTaggedReleaseFinder.cs @@ -17,7 +17,7 @@ public bool GetVersion(out VersionTaggedCommit versionTaggedCommit) var tags = context.Repository.Tags.Select(t => { SemanticVersion version; - if (SemanticVersion.TryParse(t.Name, context.Configuration.TagPrefix, out version)) + if (SemanticVersion.TryParse(t.Name, context.Configuration.GitTagPrefix, out version)) { return new VersionTaggedCommit((Commit)t.Target, version); } diff --git a/GitVersionCore/GitHubFlow/NextSemverCalculator.cs b/GitVersionCore/GitHubFlow/NextSemverCalculator.cs index 0ef2409bea..fd5af2df44 100644 --- a/GitVersionCore/GitHubFlow/NextSemverCalculator.cs +++ b/GitVersionCore/GitHubFlow/NextSemverCalculator.cs @@ -65,7 +65,7 @@ public IEnumerable GetPossibleVersions() if (!string.IsNullOrEmpty(context.Configuration.NextVersion)) { - yield return SemanticVersion.Parse(context.Configuration.NextVersion, context.Configuration.TagPrefix); + yield return SemanticVersion.Parse(context.Configuration.NextVersion, context.Configuration.GitTagPrefix); } if (hasNextVersionTxtVersion) diff --git a/GitVersionCore/GitHubFlow/NextVersionTxtFileFinder.cs b/GitVersionCore/GitHubFlow/NextVersionTxtFileFinder.cs index 58b36ecfc3..83183de9e8 100644 --- a/GitVersionCore/GitHubFlow/NextVersionTxtFileFinder.cs +++ b/GitVersionCore/GitHubFlow/NextVersionTxtFileFinder.cs @@ -5,10 +5,10 @@ public class NextVersionTxtFileFinder { + EffectiveConfiguration configuration; string repositoryDirectory; - Config configuration; - public NextVersionTxtFileFinder(string repositoryDirectory, Config configuration) + public NextVersionTxtFileFinder(string repositoryDirectory, EffectiveConfiguration configuration) { this.repositoryDirectory = repositoryDirectory; this.configuration = configuration; @@ -30,7 +30,7 @@ public bool TryGetNextVersion(out SemanticVersion semanticVersion) return false; } - if (!SemanticVersion.TryParse(version, configuration.TagPrefix, out semanticVersion)) + if (!SemanticVersion.TryParse(version, configuration.GitTagPrefix, out semanticVersion)) { throw new ArgumentException("Make sure you have a valid semantic version in NextVersion.txt"); } diff --git a/GitVersionCore/GitHubFlow/OtherBranchVersionFinder.cs b/GitVersionCore/GitHubFlow/OtherBranchVersionFinder.cs index e7e0892aa8..c556b91155 100644 --- a/GitVersionCore/GitHubFlow/OtherBranchVersionFinder.cs +++ b/GitVersionCore/GitHubFlow/OtherBranchVersionFinder.cs @@ -17,14 +17,14 @@ public bool FindVersion(GitVersionContext context, SemanticVersion defaultNextVe return false; } - var applicableTagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(context.Configuration, versionInBranch.Item2).OrderByDescending(tag => SemanticVersion.Parse(tag.Name, context.Configuration.TagPrefix)).ToList(); + var applicableTagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(context.Configuration, versionInBranch.Item2).OrderByDescending(tag => SemanticVersion.Parse(tag.Name, context.Configuration.GitTagPrefix)).ToList(); var nbHotfixCommits = BranchCommitDifferenceFinder.NumberOfCommitsSinceLastTagOrBranchPoint(context, applicableTagsInDescendingOrder, BranchType.Unknown, "master"); var semanticVersionPreReleaseTag = RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context, applicableTagsInDescendingOrder) ?? CreateDefaultPreReleaseTag(context, versionInBranch.Item1); if (semanticVersionPreReleaseTag.Name == "release") { - semanticVersionPreReleaseTag.Name = context.Configuration.Branches["release[/-]"].Tag; + semanticVersionPreReleaseTag.Name = context.Configuration.Tag; } semanticVersion = new SemanticVersion @@ -44,7 +44,7 @@ Tuple GetVersionInBranch(GitVersionContext context) foreach (var part in branchParts) { SemanticVersion semanticVersion; - if (SemanticVersion.TryParse(part, context.Configuration.TagPrefix, out semanticVersion)) + if (SemanticVersion.TryParse(part, context.Configuration.GitTagPrefix, out semanticVersion)) { return Tuple.Create(part, semanticVersion); } diff --git a/GitVersionCore/GitVersionContext.cs b/GitVersionCore/GitVersionContext.cs index d0fbfcf3e9..3bd8a0ef7e 100644 --- a/GitVersionCore/GitVersionContext.cs +++ b/GitVersionCore/GitVersionContext.cs @@ -12,21 +12,21 @@ public class GitVersionContext { readonly bool IsContextForTrackedBranchesOnly; + readonly Config configuration; public GitVersionContext(IRepository repository, Config configuration, bool isForTrackingBranchOnly = true) : this(repository, repository.Head, configuration, isForTrackingBranchOnly) { - Configuration = configuration; } public GitVersionContext(IRepository repository, Branch currentBranch, Config configuration, bool isForTrackingBranchOnly = true) { Repository = repository; - Configuration = configuration; + this.configuration = configuration; IsContextForTrackedBranchesOnly = isForTrackingBranchOnly; if (currentBranch == null) - return; + throw new InvalidOperationException("Need a branch to operate on"); CurrentCommit = currentBranch.Tip; @@ -39,17 +39,14 @@ public GitVersionContext(IRepository repository, Branch currentBranch, Config co CurrentBranch = currentBranch; } - AssignBranchConfiguration(); + CalculateEffectiveConfiguration(); } - public Config Configuration { get; private set; } + public EffectiveConfiguration 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; private set; } - - IEnumerable GetBranchesContainingCommit(string commitSha) { var directBranchHasBeenFound = false; @@ -82,23 +79,32 @@ IEnumerable GetBranchesContainingCommit(string commitSha) } } - void AssignBranchConfiguration() + void CalculateEffectiveConfiguration() { - var matchingBranches = Configuration.Branches.Where(b => Regex.IsMatch("^" + CurrentBranch.Name, b.Key)).ToArray(); + var matchingBranches = configuration.Branches.Where(b => Regex.IsMatch("^" + CurrentBranch.Name, b.Key)).ToArray(); + + var currentBranchConfig = GetBranchConfiguration(matchingBranches); + + var versioningMode = currentBranchConfig.VersioningMode ?? configuration.VersioningMode ?? VersioningMode.ContinuousDelivery; + var tag = currentBranchConfig.Tag; + var nextVersion = configuration.NextVersion; + + Configuration = new EffectiveConfiguration(configuration.AssemblyVersioningScheme, versioningMode, configuration.TagPrefix, tag, nextVersion); + } + BranchConfig GetBranchConfiguration(KeyValuePair[] matchingBranches) + { if (matchingBranches.Length == 0) { - CurrentBranchConfig = new BranchConfig(); + return new BranchConfig(); } - else if (matchingBranches.Length == 1) + 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)))); + return matchingBranches[0].Value; } + + 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)))); } } } \ No newline at end of file diff --git a/GitVersionCore/GitVersionCore.csproj b/GitVersionCore/GitVersionCore.csproj index f93b38be90..d48023d345 100644 --- a/GitVersionCore/GitVersionCore.csproj +++ b/GitVersionCore/GitVersionCore.csproj @@ -77,6 +77,7 @@ + @@ -88,6 +89,7 @@ + diff --git a/GitVersionCore/LastMinorVersionFinder.cs b/GitVersionCore/LastMinorVersionFinder.cs index e92ca41522..41c07d8942 100644 --- a/GitVersionCore/LastMinorVersionFinder.cs +++ b/GitVersionCore/LastMinorVersionFinder.cs @@ -8,7 +8,7 @@ namespace GitVersion public class LastMinorVersionFinder { - public static DateTimeOffset Execute(IRepository repo, Config configuration, Commit commit) + public static DateTimeOffset Execute(IRepository repo, EffectiveConfiguration configuration, Commit commit) { // Release/Develop = current // Hotfix/Master/Support = walk back current branch until previous commits till a merge commit (or tag) has a patch with a 0 @@ -29,10 +29,10 @@ public static DateTimeOffset Execute(IRepository repo, Config configuration, Com } - static DateTimeOffset GetTimeStampFromTag(IRepository repository, Config configuration, Commit targetCommit) + static DateTimeOffset GetTimeStampFromTag(IRepository repository, EffectiveConfiguration configuration, Commit targetCommit) { var allMajorMinorTags = repository.Tags - .Where(x => SemanticVersion.Parse(x.Name, configuration.TagPrefix).Patch == 0) + .Where(x => SemanticVersion.Parse(x.Name, configuration.GitTagPrefix).Patch == 0) .ToDictionary(x => x.PeeledTarget(), x => x); var olderThan = targetCommit.When(); foreach (var commit in repository.Head.Commits.Where(x => x.When() <= olderThan)) @@ -45,7 +45,7 @@ static DateTimeOffset GetTimeStampFromTag(IRepository repository, Config configu return DateTimeOffset.MinValue; } - static bool IsMajorMinor(Commit commit, Dictionary allMajorMinorTags, Config configuration) + static bool IsMajorMinor(Commit commit, Dictionary allMajorMinorTags, EffectiveConfiguration configuration) { SemanticVersion version; if (MergeMessageParser.TryParse(commit, configuration, out version)) diff --git a/GitVersionCore/LibGitExtensions.cs b/GitVersionCore/LibGitExtensions.cs index 7374897b2b..bbea6117b3 100644 --- a/GitVersionCore/LibGitExtensions.cs +++ b/GitVersionCore/LibGitExtensions.cs @@ -39,12 +39,12 @@ public static IEnumerable TagsByDate(this IRepository repository, Commit co }); } - public static IEnumerable SemVerTagsRelatedToVersion(this IRepository repository, Config configuration, SemanticVersion version) + public static IEnumerable SemVerTagsRelatedToVersion(this IRepository repository, EffectiveConfiguration configuration, SemanticVersion version) { foreach (var tag in repository.Tags) { SemanticVersion tagVersion; - if (SemanticVersion.TryParse(tag.Name, configuration.TagPrefix, out tagVersion)) + if (SemanticVersion.TryParse(tag.Name, configuration.GitTagPrefix, out tagVersion)) { if (version.Major == tagVersion.Major && version.Minor == tagVersion.Minor && diff --git a/GitVersionCore/MergeMessageParser.cs b/GitVersionCore/MergeMessageParser.cs index c3ff90b209..e97b741335 100644 --- a/GitVersionCore/MergeMessageParser.cs +++ b/GitVersionCore/MergeMessageParser.cs @@ -6,12 +6,12 @@ namespace GitVersion static class MergeMessageParser { - public static bool TryParse(Commit mergeCommit, Config configuration, out SemanticVersion shortVersion) + public static bool TryParse(Commit mergeCommit, EffectiveConfiguration configuration, out SemanticVersion shortVersion) { string versionPart; if (Inner(mergeCommit, out versionPart)) { - return SemanticVersion.TryParse(versionPart, configuration.TagPrefix, out shortVersion); + return SemanticVersion.TryParse(versionPart, configuration.GitTagPrefix, out shortVersion); } shortVersion = null; return false; diff --git a/GitVersionCore/OutputFormatters/BuildOutputFormatter.cs b/GitVersionCore/OutputFormatters/BuildOutputFormatter.cs index e5994075be..0f299d4fdd 100644 --- a/GitVersionCore/OutputFormatters/BuildOutputFormatter.cs +++ b/GitVersionCore/OutputFormatters/BuildOutputFormatter.cs @@ -4,11 +4,11 @@ public static class BuildOutputFormatter { - public static IEnumerable GenerateBuildLogOutput(SemanticVersion semanticVersion, IBuildServer buildServer) + public static IEnumerable GenerateBuildLogOutput(IBuildServer buildServer, Dictionary variables) { var output = new List(); - foreach (var variable in VariableProvider.GetVariablesFor(semanticVersion, new Config())) + foreach (var variable in variables) { output.AddRange(buildServer.GenerateSetParameterMessage(variable.Key, variable.Value)); } diff --git a/GitVersionCore/OutputVariables/VariableProvider.cs b/GitVersionCore/OutputVariables/VariableProvider.cs index 1ffd3feb8f..5e7182b173 100644 --- a/GitVersionCore/OutputVariables/VariableProvider.cs +++ b/GitVersionCore/OutputVariables/VariableProvider.cs @@ -31,7 +31,8 @@ public static class VariableProvider public const string NuGetVersionV3 = "NuGetVersionV3"; public const string NuGetVersion = "NuGetVersion"; - public static Dictionary GetVariablesFor(SemanticVersion semanticVersion, Config configuration) + //TODO replace Dictionary with a proper type which can be passed around + public static Dictionary GetVariablesFor(SemanticVersion semanticVersion, AssemblyVersioningScheme assemblyVersioningScheme, VersioningMode mode) { var bmd = semanticVersion.BuildMetaData; var formatter = bmd.Branch == "develop" ? new CiFeedFormatter() : null; @@ -49,8 +50,8 @@ public static Dictionary GetVariablesFor(SemanticVersion semanti {SemVer, semanticVersion.ToString(null, formatter)}, {LegacySemVer, semanticVersion.ToString("l", formatter)}, {LegacySemVerPadded, semanticVersion.ToString("lp", formatter)}, - {AssemblySemVer, semanticVersion.GetAssemblyVersion(configuration.AssemblyVersioningScheme)}, - {AssemblyFileSemVer, semanticVersion.GetAssemblyFileVersion(configuration.AssemblyVersioningScheme)}, + {AssemblySemVer, semanticVersion.GetAssemblyVersion(assemblyVersioningScheme)}, + {AssemblyFileSemVer, semanticVersion.GetAssemblyFileVersion(assemblyVersioningScheme)}, {FullSemVer, semanticVersion.ToString("f", formatter)}, {InformationalVersion, semanticVersion.ToString("i", formatter)}, {ClassicVersion, string.Format("{0}.{1}", semanticVersion.ToString("j"), (bmd.CommitsSinceTag ?? 0))}, diff --git a/GitVersionCore/SemanticVersionExtensions.cs b/GitVersionCore/SemanticVersionExtensions.cs index 0cf71f9653..af63364186 100644 --- a/GitVersionCore/SemanticVersionExtensions.cs +++ b/GitVersionCore/SemanticVersionExtensions.cs @@ -4,7 +4,7 @@ public static class SemanticVersionExtensions { - public static void OverrideVersionManuallyIfNeeded(this SemanticVersion version, IRepository repository, Config configuration) + public static void OverrideVersionManuallyIfNeeded(this SemanticVersion version, IRepository repository, EffectiveConfiguration configuration) { var nextVersionTxtFileFinder = new NextVersionTxtFileFinder(repository.GetRepositoryDirectory(), configuration); SemanticVersion manualNextVersion ; diff --git a/GitVersionCore/VersioningModes/ContinuousDeliveryMode.cs b/GitVersionCore/VersioningModes/ContinuousDeliveryMode.cs index 8a0b2854bc..e3124b2bad 100644 --- a/GitVersionCore/VersioningModes/ContinuousDeliveryMode.cs +++ b/GitVersionCore/VersioningModes/ContinuousDeliveryMode.cs @@ -9,7 +9,7 @@ public class ContinuousDeliveryMode : VersioningModeBase public override SemanticVersionPreReleaseTag GetPreReleaseTag(GitVersionContext context, List possibleCommits, int numberOfCommits) { return RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context, possibleCommits) - ?? context.CurrentBranchConfig.Tag + ".1"; + ?? context.Configuration.Tag + ".1"; } } } diff --git a/GitVersionCore/VersioningModes/ContinuousDeploymentMode.cs b/GitVersionCore/VersioningModes/ContinuousDeploymentMode.cs index 45aafddc5a..9b7c038d7d 100644 --- a/GitVersionCore/VersioningModes/ContinuousDeploymentMode.cs +++ b/GitVersionCore/VersioningModes/ContinuousDeploymentMode.cs @@ -8,7 +8,7 @@ public class ContinuousDeploymentMode : VersioningModeBase { public override SemanticVersionPreReleaseTag GetPreReleaseTag(GitVersionContext context, List possibleTags, int numberOfCommits) { - return context.CurrentBranchConfig.Tag + "." + numberOfCommits; + return context.Configuration.Tag + "." + numberOfCommits; } } } \ No newline at end of file diff --git a/GitVersionCore/VersioningModes/VersioningMode.cs b/GitVersionCore/VersioningModes/VersioningMode.cs index 55e48fa811..d3d97c0632 100644 --- a/GitVersionCore/VersioningModes/VersioningMode.cs +++ b/GitVersionCore/VersioningModes/VersioningMode.cs @@ -1,28 +1,8 @@ namespace GitVersion { - using System; - - using GitVersion.VersioningModes; - public enum VersioningMode { ContinuousDelivery, ContinuousDeployment } - - public static class VersioningModeExtension - { - public static VersioningModeBase GetInstance(this VersioningMode _this) - { - switch (_this) - { - case VersioningMode.ContinuousDelivery: - return new ContinuousDeliveryMode(); - case VersioningMode.ContinuousDeployment: - return new ContinuousDeploymentMode(); - default: - throw new ArgumentException("No instance exists for this versioning mode."); - } - } - } } diff --git a/GitVersionCore/VersioningModes/VersioningModeExtension.cs b/GitVersionCore/VersioningModes/VersioningModeExtension.cs new file mode 100644 index 0000000000..a0d604529e --- /dev/null +++ b/GitVersionCore/VersioningModes/VersioningModeExtension.cs @@ -0,0 +1,21 @@ +namespace GitVersion +{ + using System; + using GitVersion.VersioningModes; + + public static class VersioningModeExtension + { + public static VersioningModeBase GetInstance(this VersioningMode _this) + { + switch (_this) + { + case VersioningMode.ContinuousDelivery: + return new ContinuousDeliveryMode(); + case VersioningMode.ContinuousDeployment: + return new ContinuousDeploymentMode(); + default: + throw new ArgumentException("No instance exists for this versioning mode."); + } + } + } +} \ No newline at end of file diff --git a/GitVersionExe.Tests/AssemblyInfoFileUpdateTests.cs b/GitVersionExe.Tests/AssemblyInfoFileUpdateTests.cs index 11c0a58de6..55847830ac 100644 --- a/GitVersionExe.Tests/AssemblyInfoFileUpdateTests.cs +++ b/GitVersionExe.Tests/AssemblyInfoFileUpdateTests.cs @@ -45,7 +45,7 @@ public void ShouldReplaceAssemblyVersion() { AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch }; - var variable = VariableProvider.GetVariablesFor(version, config); + var variable = VariableProvider.GetVariablesFor(version, config.AssemblyVersioningScheme, VersioningMode.ContinuousDelivery); var args = new Arguments { UpdateAssemblyInfo = true, @@ -83,7 +83,7 @@ public void ShouldReplaceAssemblyVersionWithStar() { AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch }; - var variable = VariableProvider.GetVariablesFor(version, config); + var variable = VariableProvider.GetVariablesFor(version, config.AssemblyVersioningScheme, VersioningMode.ContinuousDelivery); var args = new Arguments { UpdateAssemblyInfo = true, diff --git a/GitVersionExe/Program.cs b/GitVersionExe/Program.cs index 97aa965388..d5415db732 100644 --- a/GitVersionExe/Program.cs +++ b/GitVersionExe/Program.cs @@ -86,23 +86,24 @@ static int Run() buildServer.PerformPreProcessingSteps(gitDirectory); } SemanticVersion semanticVersion; + Dictionary variables; var versionFinder = new GitVersionFinder(); var configuration = ConfigurationProvider.Provide(gitDirectory, fileSystem); using (var repo = RepositoryLoader.GetRepo(gitDirectory)) { var gitVersionContext = new GitVersionContext(repo, configuration); semanticVersion = versionFinder.FindVersion(gitVersionContext); + variables = VariableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration.AssemblyVersioningScheme, gitVersionContext.Configuration.VersioningMode); } if (arguments.Output == OutputType.BuildServer) { foreach (var buildServer in applicableBuildServers) { - buildServer.WriteIntegration(semanticVersion, Console.WriteLine); + buildServer.WriteIntegration(semanticVersion, Console.WriteLine, variables); } } - var variables = VariableProvider.GetVariablesFor(semanticVersion, configuration); if (arguments.Output == OutputType.Json) { switch (arguments.VersionPart) diff --git a/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs b/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs index a61561c0ce..dfb02cc092 100644 --- a/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs +++ b/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs @@ -4,6 +4,7 @@ using System.Runtime.CompilerServices; using ApprovalTests; using GitVersion; +using GitVersionCore.Tests; using NUnit.Framework; using Roslyn.Compilers; using Roslyn.Compilers.CSharp; @@ -22,20 +23,20 @@ public void VerifyCreatedCode() Patch = 3, PreReleaseTag = "unstable4", BuildMetaData = new SemanticVersionBuildMetaData(5, - "feature1","commitSha",DateTimeOffset.Parse("2014-03-06 23:59:59Z")) + "feature1", "commitSha", DateTimeOffset.Parse("2014-03-06 23:59:59Z")) }; var assemblyInfoBuilder = new AssemblyInfoBuilder + { + CachedVersion = new CachedVersion { - CachedVersion = new CachedVersion - { - SemanticVersion = semanticVersion, - MasterReleaseDate = DateTimeOffset.Parse("2014-03-01 00:00:01Z"), - } - }; - var assemblyInfoText = assemblyInfoBuilder.GetAssemblyInfoText(new Config()); + SemanticVersion = semanticVersion, + MasterReleaseDate = DateTimeOffset.Parse("2014-03-01 00:00:01Z"), + } + }; + var assemblyInfoText = assemblyInfoBuilder.GetAssemblyInfoText(new TestEffectiveConfiguration()); Approvals.Verify(assemblyInfoText); var syntaxTree = SyntaxTree.ParseText(assemblyInfoText); - var references = new[] {new MetadataFileReference(typeof(object).Assembly.Location)}; + var references = new[] { new MetadataFileReference(typeof(object).Assembly.Location) }; var compilation = Compilation.Create("Greeter.dll", new CompilationOptions(OutputKind.NetModule), new[] { syntaxTree }, references); var emitResult = compilation.Emit(new MemoryStream()); Assert.IsTrue(emitResult.Success, string.Join(Environment.NewLine, emitResult.Diagnostics.Select(x => x.Info))); @@ -77,7 +78,7 @@ static void VerifyAssemblyVersion(AssemblyVersioningScheme avs) Minor = 3, Patch = 4, BuildMetaData = new SemanticVersionBuildMetaData(5, - "master","commitSha",DateTimeOffset.Parse("2014-03-06 23:59:59Z")), + "master", "commitSha", DateTimeOffset.Parse("2014-03-06 23:59:59Z")), }; var assemblyInfoBuilder = new AssemblyInfoBuilder { @@ -88,10 +89,10 @@ static void VerifyAssemblyVersion(AssemblyVersioningScheme avs) }, }; - var assemblyInfoText = assemblyInfoBuilder.GetAssemblyInfoText(new Config { AssemblyVersioningScheme = avs }); + var assemblyInfoText = assemblyInfoBuilder.GetAssemblyInfoText(new TestEffectiveConfiguration(assemblyVersioningScheme: avs)); Approvals.Verify(assemblyInfoText); var syntaxTree = SyntaxTree.ParseText(assemblyInfoText); - var references = new[] { new MetadataFileReference(typeof(object).Assembly.Location)}; + var references = new[] { new MetadataFileReference(typeof(object).Assembly.Location) }; var compilation = Compilation.Create("Greeter.dll", new CompilationOptions(OutputKind.NetModule), new[] { syntaxTree }, references); var emitResult = compilation.Emit(new MemoryStream()); Assert.IsTrue(emitResult.Success, string.Join(Environment.NewLine, emitResult.Diagnostics.Select(x => x.Info))); diff --git a/GitVersionTask.Tests/BuildServers/BuildServerBaseTests.cs b/GitVersionTask.Tests/BuildServers/BuildServerBaseTests.cs index 2582c199f8..b506ddfc72 100644 --- a/GitVersionTask.Tests/BuildServers/BuildServerBaseTests.cs +++ b/GitVersionTask.Tests/BuildServers/BuildServerBaseTests.cs @@ -22,7 +22,7 @@ public void BuildNumberIsFullSemVer() semanticVersion.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); semanticVersion.BuildMetaData.Sha = "commitSha"; - new BuildServer().WriteIntegration(semanticVersion, writes.Add); + new BuildServer().WriteIntegration(semanticVersion, writes.Add, new Dictionary()); writes[1].ShouldBe("1.2.3-beta.1+5"); } diff --git a/GitVersionTask.Tests/GetVersionTaskTests.cs b/GitVersionTask.Tests/GetVersionTaskTests.cs index 9c1c57280f..8fd4b39a1c 100644 --- a/GitVersionTask.Tests/GetVersionTaskTests.cs +++ b/GitVersionTask.Tests/GetVersionTaskTests.cs @@ -15,13 +15,15 @@ public void OutputsShouldMatchVariableProvider() var properties = taskType.GetProperties() .Where(p => p.GetCustomAttributes(typeof(OutputAttribute), false).Any()) .Select(p => p.Name); + var configuration = new Config(); var variables = VariableProvider.GetVariablesFor(new SemanticVersion { Major = 1, Minor = 2, Patch = 3, BuildMetaData = new SemanticVersionBuildMetaData(5, "develop", "commitSha",DateTimeOffset.Parse("2014-03-06 23:59:59Z")) - }, new Config()).Keys; + }, configuration.AssemblyVersioningScheme, + VersioningMode.ContinuousDelivery).Keys; CollectionAssert.AreEquivalent(properties, variables); } diff --git a/GitVersionTask.Tests/GitVersionTask.Tests.csproj b/GitVersionTask.Tests/GitVersionTask.Tests.csproj index 18e3bbe037..d4c1ef68e7 100644 --- a/GitVersionTask.Tests/GitVersionTask.Tests.csproj +++ b/GitVersionTask.Tests/GitVersionTask.Tests.csproj @@ -92,6 +92,7 @@ + @@ -107,24 +108,11 @@ - - - - - - - - - - - - - diff --git a/GitVersionTask.Tests/IntegrationTests.cs b/GitVersionTask.Tests/IntegrationTests.cs index 3b417cca80..3687e8c0d9 100644 --- a/GitVersionTask.Tests/IntegrationTests.cs +++ b/GitVersionTask.Tests/IntegrationTests.cs @@ -1,6 +1,7 @@ using System.Diagnostics; using System.Linq; using GitVersion; +using GitVersionCore.Tests; using LibGit2Sharp; using NUnit.Framework; @@ -17,7 +18,7 @@ public void ProcessAllTheCommits() foreach (var commit in branch.Commits) { SemanticVersion version; - if (MergeMessageParser.TryParse(commit, new Config(), out version)) + if (MergeMessageParser.TryParse(commit, new TestEffectiveConfiguration(), out version)) { Debug.WriteLine("{0}.{1}.{2}", version.Major, version.Minor, version.Patch); } diff --git a/GitVersionTask.Tests/MergeMessageParserTests.cs b/GitVersionTask.Tests/MergeMessageParserTests.cs index caf2de3210..9b5972cf2e 100644 --- a/GitVersionTask.Tests/MergeMessageParserTests.cs +++ b/GitVersionTask.Tests/MergeMessageParserTests.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using GitVersion; +using GitVersionCore.Tests; using LibGit2Sharp; using NUnit.Framework; using Shouldly; @@ -49,7 +50,7 @@ static void AssertMereMessage(string message, string expectedVersion, List(() => task.InnerExecute()); - Assert.AreEqual("Unexpected assembly versioning scheme 'Boom'.", exception.Message); - } - [SetUp] public void SetUp() { diff --git a/GitVersionTask.Tests/VersionAndBranchFinderTests.cs b/GitVersionTask.Tests/VersionAndBranchFinderTests.cs index 8028558365..ea7d089cf3 100644 --- a/GitVersionTask.Tests/VersionAndBranchFinderTests.cs +++ b/GitVersionTask.Tests/VersionAndBranchFinderTests.cs @@ -1,4 +1,5 @@ -using GitVersion; +using System; +using GitVersion; using LibGit2Sharp; using NUnit.Framework; @@ -17,7 +18,8 @@ public void ShouldBeAbleGetVersionFromGitDir() AddOneCommitToHead(repo, "code"); } - CachedVersion versionAndBranch; + + Tuple versionAndBranch; VersionAndBranchFinder.TryGetVersion(ASBMTestRepoWorkingDirPath, out versionAndBranch, new Config()); Assert.IsNotNull(versionAndBranch); } diff --git a/GitVersionTask.Tests/VersionOnMasterFinderTests.cs b/GitVersionTask.Tests/VersionOnMasterFinderTests.cs index 53252f9291..1b5570d470 100644 --- a/GitVersionTask.Tests/VersionOnMasterFinderTests.cs +++ b/GitVersionTask.Tests/VersionOnMasterFinderTests.cs @@ -19,31 +19,32 @@ public void Should_find_previous_commit_that_was_at_least_a_minor_bump() const string sha = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"; + var mockBranch = new MockBranch("master") + { + new MockMergeCommit(new ObjectId(sha)) + { + MessageEx = "Merge branch 'hotfix-0.3.0'", + CommitterEx = signature + }, + new MockMergeCommit + { + MessageEx = "Merge branch 'hotfix-0.3.1'", + CommitterEx = signature, + }, + new MockMergeCommit + { + MessageEx = "Merge branch 'hotfix-0.2.0'", + CommitterEx = signature + }, + }; var repository = new MockRepository { Branches = new MockBranchCollection { - new MockBranch("master") - { - new MockMergeCommit(new ObjectId(sha)) - { - MessageEx = "Merge branch 'hotfix-0.3.0'", - CommitterEx = signature - }, - new MockMergeCommit - { - MessageEx = "Merge branch 'hotfix-0.3.1'", - CommitterEx = signature, - }, - new MockMergeCommit - { - MessageEx = "Merge branch 'hotfix-0.2.0'", - CommitterEx = signature - }, - }, + mockBranch, } }; - var version = finder.Execute(new GitVersionContext(repository, null, new Config()), 1.Seconds().Ago()); + var version = finder.Execute(new GitVersionContext(repository, mockBranch, new Config()), 1.Seconds().Ago()); ObjectApprover.VerifyWithJson(version); } } \ No newline at end of file diff --git a/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs b/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs index d1e239b06f..a2061715ef 100644 --- a/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs +++ b/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs @@ -6,10 +6,10 @@ public class AssemblyInfoBuilder { public CachedVersion CachedVersion; - public string GetAssemblyInfoText(Config configuration) + public string GetAssemblyInfoText(EffectiveConfiguration configuration) { var semanticVersion = CachedVersion.SemanticVersion; - var vars = VariableProvider.GetVariablesFor(semanticVersion, configuration); + var vars = VariableProvider.GetVariablesFor(semanticVersion, configuration.AssemblyVersioningScheme, configuration.VersioningMode); var assemblyInfo = string.Format(@" using System; using System.Reflection; diff --git a/GitVersionTask/AssemblyInfoBuilder/UpdateAssemblyInfo.cs b/GitVersionTask/AssemblyInfoBuilder/UpdateAssemblyInfo.cs index 1b567b89b8..5ae2cb3761 100644 --- a/GitVersionTask/AssemblyInfoBuilder/UpdateAssemblyInfo.cs +++ b/GitVersionTask/AssemblyInfoBuilder/UpdateAssemblyInfo.cs @@ -10,12 +10,6 @@ public class UpdateAssemblyInfo : Task { - public string AssemblyVersioningScheme { get; set; } - - public string TagPrefix { get; set; } - - public string NextVersion { get; set; } - [Required] public string SolutionDirectory { get; set; } @@ -71,7 +65,6 @@ public override bool Execute() public void InnerExecute() { - TempFileTracker.DeleteTempFiles(); InvalidFileChecker.CheckForInvalidFiles(CompileFiles, ProjectFile); @@ -81,41 +74,16 @@ public void InnerExecute() return; var configuration = ConfigurationProvider.Provide(gitDirectory, fileSystem); - if (!string.IsNullOrEmpty(AssemblyVersioningScheme)) - { - AssemblyVersioningScheme versioningScheme; - if (Enum.TryParse(AssemblyVersioningScheme, true, out versioningScheme)) - { - configuration.AssemblyVersioningScheme = versioningScheme; - } - else - { - throw new WarningException(string.Format("Unexpected assembly versioning scheme '{0}'.", AssemblyVersioningScheme)); - } - } - - // TODO This should be covered by tests - // TODO would be good to not have to duplicate this in both msbuild tasks - // Null is intentional. Empty string means the user has set the value to an empty string and wants to clear the tag - if (TagPrefix != null) - { - configuration.TagPrefix = TagPrefix; - } - - if (NextVersion != null) - { - configuration.NextVersion = NextVersion; - } - CachedVersion semanticVersion; + Tuple semanticVersion; if (!VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out semanticVersion, configuration)) { return; } - CreateTempAssemblyInfo(semanticVersion, configuration); + CreateTempAssemblyInfo(semanticVersion.Item1, semanticVersion.Item2.Configuration); } - void CreateTempAssemblyInfo(CachedVersion semanticVersion, Config configuration) + void CreateTempAssemblyInfo(CachedVersion semanticVersion, EffectiveConfiguration configuration) { var assemblyInfoBuilder = new AssemblyInfoBuilder { diff --git a/GitVersionTask/GetVersion.cs b/GitVersionTask/GetVersion.cs index 7f256dcbc4..75fa20251e 100644 --- a/GitVersionTask/GetVersion.cs +++ b/GitVersionTask/GetVersion.cs @@ -75,10 +75,6 @@ public class GetVersion : Task [Output] public string NuGetVersion { get; set; } - public string TagPrefix { get; set; } - - public string NextVersion { get; set; } - TaskLogger logger; IFileSystem fileSystem; @@ -94,26 +90,16 @@ public override bool Execute() { try { - CachedVersion versionAndBranch; + Tuple versionAndBranch; var gitDirectory = GitDirFinder.TreeWalkForGitDir(SolutionDirectory); var configuration = ConfigurationProvider.Provide(gitDirectory, fileSystem); - // TODO This should be covered by tests - // Null is intentional. Empty string means the user has set the value to an empty string and wants to clear the tag - if (TagPrefix != null) - { - configuration.TagPrefix = TagPrefix; - } - - if (NextVersion != null) - { - configuration.NextVersion = NextVersion; - } - if (VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out versionAndBranch, configuration)) { var thisType = typeof(GetVersion); - var variables = VariableProvider.GetVariablesFor(versionAndBranch.SemanticVersion, configuration); + var cachedVersion = versionAndBranch.Item1; + var effectiveConfiguration = versionAndBranch.Item2.Configuration; + var variables = VariableProvider.GetVariablesFor(cachedVersion.SemanticVersion, effectiveConfiguration.AssemblyVersioningScheme, effectiveConfiguration.VersioningMode); foreach (var variable in variables) { thisType.GetProperty(variable.Key).SetValue(this, variable.Value, null); diff --git a/GitVersionTask/VersionAndBranchFinder.cs b/GitVersionTask/VersionAndBranchFinder.cs index 180696064c..81ee90e302 100644 --- a/GitVersionTask/VersionAndBranchFinder.cs +++ b/GitVersionTask/VersionAndBranchFinder.cs @@ -1,10 +1,11 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using GitVersion; public static class VersionAndBranchFinder { static List processedDirectories = new List(); - public static bool TryGetVersion(string directory, out CachedVersion versionAndBranch, Config configuration) + public static bool TryGetVersion(string directory, out Tuple versionAndBranch, Config configuration) { var gitDirectory = GitDirFinder.TreeWalkForGitDir(directory); diff --git a/GitVersionTask/VersionCache.cs b/GitVersionTask/VersionCache.cs index 93e7b831b6..b6209363ec 100644 --- a/GitVersionTask/VersionCache.cs +++ b/GitVersionTask/VersionCache.cs @@ -1,11 +1,12 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using GitVersion; public static class VersionCache { - static Dictionary versionCacheVersions = new Dictionary(); + static Dictionary> versionCacheVersions = new Dictionary>(); - public static CachedVersion GetVersion(string gitDirectory, Config configuration) + public static Tuple GetVersion(string gitDirectory, Config configuration) { using (var repo = RepositoryLoader.GetRepo(gitDirectory)) { @@ -13,26 +14,26 @@ public static CachedVersion GetVersion(string gitDirectory, Config configuration var context = new GitVersionContext(repo, configuration); var ticks = DirectoryDateFinder.GetLastDirectoryWrite(gitDirectory); var key = string.Format("{0}:{1}:{2}", repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks); - CachedVersion cachedVersion; - if (versionCacheVersions.TryGetValue(key, out cachedVersion)) + + Tuple result; + if (versionCacheVersions.TryGetValue(key, out result)) { - if (cachedVersion.Timestamp != ticks) + if (result.Item1.Timestamp != ticks) { Logger.WriteInfo("Change detected. flushing cache."); - cachedVersion.SemanticVersion = versionFinder.FindVersion(context); - cachedVersion.MasterReleaseDate = LastMinorVersionFinder.Execute(repo, new Config(), repo.Head.Tip); + result.Item1.SemanticVersion = versionFinder.FindVersion(context); + result.Item1.MasterReleaseDate = LastMinorVersionFinder.Execute(repo, context.Configuration, repo.Head.Tip); } - return cachedVersion; + return result; } Logger.WriteInfo("Version not in cache. Calculating version."); - return versionCacheVersions[key] = new CachedVersion + return versionCacheVersions[key] = Tuple.Create(new CachedVersion { SemanticVersion = versionFinder.FindVersion(context), - MasterReleaseDate = LastMinorVersionFinder.Execute(repo, new Config(), repo.Head.Tip), + MasterReleaseDate = LastMinorVersionFinder.Execute(repo, context.Configuration, repo.Head.Tip), Timestamp = ticks - }; - + }, context); } } diff --git a/GitVersionTask/WriteVersionInfoToBuildLog.cs b/GitVersionTask/WriteVersionInfoToBuildLog.cs index 67b08e1ec4..a7f9364ec8 100644 --- a/GitVersionTask/WriteVersionInfoToBuildLog.cs +++ b/GitVersionTask/WriteVersionInfoToBuildLog.cs @@ -49,26 +49,32 @@ public override bool Execute() public void InnerExecute() { - CachedVersion semanticVersion; + Tuple result; var gitDirectory = GitDirFinder.TreeWalkForGitDir(SolutionDirectory); var configuration = ConfigurationProvider.Provide(gitDirectory, fileSystem); - if (!VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out semanticVersion, configuration)) + if (!VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out result, configuration)) { return; } var authentication = new Authentication(); - WriteIntegrationParameters(semanticVersion, BuildServerList.GetApplicableBuildServers(authentication)); + + var cachedVersion = result.Item1; + var assemblyVersioningScheme = result.Item2.Configuration.AssemblyVersioningScheme; + var versioningMode = result.Item2.Configuration.VersioningMode; + + var variablesFor = VariableProvider.GetVariablesFor(cachedVersion.SemanticVersion, assemblyVersioningScheme, versioningMode); + WriteIntegrationParameters(cachedVersion, BuildServerList.GetApplicableBuildServers(authentication), variablesFor); } - public void WriteIntegrationParameters(CachedVersion cachedVersion, IEnumerable applicableBuildServers) + public void WriteIntegrationParameters(CachedVersion cachedVersion, IEnumerable applicableBuildServers, Dictionary variables) { foreach (var buildServer in applicableBuildServers) { logger.LogInfo(string.Format("Executing GenerateSetVersionMessage for '{0}'.", buildServer.GetType().Name)); logger.LogInfo(buildServer.GenerateSetVersionMessage(cachedVersion.SemanticVersion.ToString())); logger.LogInfo(string.Format("Executing GenerateBuildLogOutput for '{0}'.", buildServer.GetType().Name)); - foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(cachedVersion.SemanticVersion, buildServer)) + foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(buildServer, variables)) { logger.LogInfo(buildParameter); }