Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
arturcic committed Mar 23, 2020
1 parent 4cc84d2 commit 71d8fe5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
24 changes: 24 additions & 0 deletions src/GitVersionCore/Configuration/ConfigExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text.RegularExpressions;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.VersionCalculation;

namespace GitVersion.Configuration
Expand Down Expand Up @@ -283,6 +284,29 @@ public static EffectiveConfiguration CalculateEffectiveConfiguration(this Config
preReleaseWeight);
}

public static string GetBranchSpecificTag(this EffectiveConfiguration configuration, ILog log, string branchFriendlyName, string branchNameOverride)
{
var tagToUse = configuration.Tag;
if (tagToUse == "useBranchName")
{
tagToUse = "{BranchName}";
}
if (tagToUse.Contains("{BranchName}"))
{
log.Info("Using branch name to calculate version tag");

var branchName = branchNameOverride ?? branchFriendlyName;
if (!string.IsNullOrWhiteSpace(configuration.BranchPrefixToTrim))
{
branchName = branchName.RegexReplace(configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
}
branchName = branchName.RegexReplace("[^a-zA-Z0-9-]", "-");

tagToUse = tagToUse.Replace("{BranchName}", branchName);
}
return tagToUse;
}

private static BranchConfig GetOrCreateBranchDefaults(this Config config, string branchKey)
{
if (!config.Branches.ContainsKey(branchKey))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using GitVersion.Configuration;

namespace GitVersion.VersionCalculation
{
public interface INextVersionCalculator
{
SemanticVersion FindVersion(GitVersionContext context);
string GetBranchSpecificTag(EffectiveConfiguration configuration, string branchFriendlyName, string branchNameOverride);
}
}
42 changes: 9 additions & 33 deletions src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using GitVersion.Common;
using GitVersion.Configuration;
using GitVersion.Exceptions;
Expand Down Expand Up @@ -47,20 +46,6 @@ public SemanticVersion FindVersion(GitVersionContext context)
return FindVersionInternal(context);
}

private static void EnsureHeadIsNotDetached(GitVersionContext context)
{
if (!context.CurrentBranch.IsDetachedHead())
{
return;
}

var message = string.Format(
"It looks like the branch being examined is a detached Head pointing to commit '{0}'. " +
"Without a proper branch name GitVersion cannot determine the build version.",
context.CurrentCommit.Id.ToString(7));
throw new WarningException(message);
}

public SemanticVersion FindVersionInternal(GitVersionContext context)
{
SemanticVersion taggedSemanticVersion = null;
Expand Down Expand Up @@ -129,7 +114,7 @@ private SemanticVersion PerformIncrement(GitVersionContext context, BaseVersion

private void UpdatePreReleaseTag(GitVersionContext context, SemanticVersion semanticVersion, string branchNameOverride)
{
var tagToUse = GetBranchSpecificTag(context.Configuration, context.CurrentBranch.FriendlyName, branchNameOverride);
var tagToUse = context.Configuration.GetBranchSpecificTag(log, context.CurrentBranch.FriendlyName, branchNameOverride);

int? number = null;

Expand All @@ -152,27 +137,18 @@ private void UpdatePreReleaseTag(GitVersionContext context, SemanticVersion sema
semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, number);
}

public string GetBranchSpecificTag(EffectiveConfiguration configuration, string branchFriendlyName, string branchNameOverride)
private static void EnsureHeadIsNotDetached(GitVersionContext context)
{
var tagToUse = configuration.Tag;
if (tagToUse == "useBranchName")
if (!context.CurrentBranch.IsDetachedHead())
{
tagToUse = "{BranchName}";
return;
}
if (tagToUse.Contains("{BranchName}"))
{
log.Info("Using branch name to calculate version tag");

var branchName = branchNameOverride ?? branchFriendlyName;
if (!string.IsNullOrWhiteSpace(configuration.BranchPrefixToTrim))
{
branchName = branchName.RegexReplace(configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
}
branchName = branchName.RegexReplace("[^a-zA-Z0-9-]", "-");

tagToUse = tagToUse.Replace("{BranchName}", branchName);
}
return tagToUse;
var message = string.Format(
"It looks like the branch being examined is a detached Head pointing to commit '{0}'. " +
"Without a proper branch name GitVersion cannot determine the build version.",
context.CurrentCommit.Id.ToString(7));
throw new WarningException(message);
}

private static bool MajorMinorPatchEqual(SemanticVersion lastTag, SemanticVersion baseVersion)
Expand Down
6 changes: 2 additions & 4 deletions src/GitVersionCore/VersionCalculation/VariableProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ namespace GitVersion.OutputVariables
{
public class VariableProvider : IVariableProvider
{
private readonly INextVersionCalculator nextVersionCalculator;
private readonly IEnvironment environment;
private readonly ILog log;

public VariableProvider(INextVersionCalculator nextVersionCalculator, IEnvironment environment, ILog log)
public VariableProvider(IEnvironment environment, ILog log)
{
this.nextVersionCalculator = nextVersionCalculator ?? throw new ArgumentNullException(nameof(nextVersionCalculator));
this.environment = environment ?? throw new ArgumentNullException(nameof(environment));
this.log = log ?? throw new ArgumentNullException(nameof(log));
}
Expand All @@ -31,7 +29,7 @@ public VersionVariables GetVariablesFor(SemanticVersion semanticVersion, Effecti
// Continuous Deployment always requires a pre-release tag unless the commit is tagged
if (!semanticVersion.PreReleaseTag.HasTag())
{
semanticVersion.PreReleaseTag.Name = nextVersionCalculator.GetBranchSpecificTag(config, semanticVersion.BuildMetaData.Branch, null);
semanticVersion.PreReleaseTag.Name = config.GetBranchSpecificTag(log, semanticVersion.BuildMetaData.Branch, null);
if (string.IsNullOrEmpty(semanticVersion.PreReleaseTag.Name))
{
semanticVersion.PreReleaseTag.Name = config.ContinuousDeploymentFallbackTag;
Expand Down

0 comments on commit 71d8fe5

Please sign in to comment.