Skip to content

Commit

Permalink
Switched to configuration option rather than hardcoding branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeGinnivan committed Mar 15, 2015
1 parent 1ee64bf commit 1d1b0c4
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 43 deletions.
6 changes: 4 additions & 2 deletions GitVersionCore.Tests/TestEffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public TestEffectiveConfiguration(
string branchPrefixToTrim = "",
bool preventIncrementForMergedBranchVersion = false,
string tagNumberPattern = null,
string continuousDeploymentFallbackTag = "ci") :
string continuousDeploymentFallbackTag = "ci",
bool trackMergeTarget = false) :
base(assemblyVersioningScheme, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch,
branchPrefixToTrim, preventIncrementForMergedBranchVersion, tagNumberPattern, continuousDeploymentFallbackTag)
branchPrefixToTrim, preventIncrementForMergedBranchVersion, tagNumberPattern, continuousDeploymentFallbackTag,
trackMergeTarget)
{
}
}
Expand Down
8 changes: 6 additions & 2 deletions GitVersionCore/Configuration/BranchConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public BranchConfig(BranchConfig branchConfiguration)
Increment = branchConfiguration.Increment;
PreventIncrementOfMergedBranchVersion = branchConfiguration.PreventIncrementOfMergedBranchVersion;
TagNumberPattern = branchConfiguration.TagNumberPattern;
TrackMergeTarget = branchConfiguration.TrackMergeTarget;
}

[YamlMember(Alias = "mode")]
Expand All @@ -29,10 +30,13 @@ public BranchConfig(BranchConfig branchConfiguration)
[YamlMember(Alias = "increment")]
public IncrementStrategy? Increment { get; set; }

[YamlMember(Alias = "preventIncrementOfMergedBranchVersion")]
[YamlMember(Alias = "prevent-increment-of-merged-branch-version")]
public bool? PreventIncrementOfMergedBranchVersion { get; set; }

[YamlMember(Alias = "tagNumberPattern")]
[YamlMember(Alias = "tag-number-pattern")]
public string TagNumberPattern { get; set; }

[YamlMember(Alias = "track-merge-target")]
public bool TrackMergeTarget { get; set; }
}
}
3 changes: 2 additions & 1 deletion GitVersionCore/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public Config()
{
Tag = "unstable",
Increment = IncrementStrategy.Minor,
VersioningMode = GitVersion.VersioningMode.ContinuousDeployment
VersioningMode = GitVersion.VersioningMode.ContinuousDeployment,
TrackMergeTarget = true
};
Branches[@"(pull|pull\-requests|pr)[/-]"] = new BranchConfig
{
Expand Down
5 changes: 4 additions & 1 deletion GitVersionCore/EffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public EffectiveConfiguration(
string branchPrefixToTrim,
bool preventIncrementForMergedBranchVersion,
string tagNumberPattern,
string continuousDeploymentFallbackTag)
string continuousDeploymentFallbackTag,
bool trackMergeTarget)
{
AssemblyVersioningScheme = assemblyVersioningScheme;
VersioningMode = versioningMode;
Expand All @@ -24,6 +25,7 @@ public EffectiveConfiguration(
PreventIncrementForMergedBranchVersion = preventIncrementForMergedBranchVersion;
TagNumberPattern = tagNumberPattern;
ContinuousDeploymentFallbackTag = continuousDeploymentFallbackTag;
TrackMergeTarget = trackMergeTarget;
}

public VersioningMode VersioningMode { get; private set; }
Expand Down Expand Up @@ -51,5 +53,6 @@ public EffectiveConfiguration(
public string TagNumberPattern { get; private set; }

public string ContinuousDeploymentFallbackTag { get; private set; }
public bool TrackMergeTarget { get; private set; }
}
}
3 changes: 2 additions & 1 deletion GitVersionCore/GitVersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ void CalculateEffectiveConfiguration()
assemblyVersioningScheme, versioningMode, gitTagPrefix,
tag, nextVersion, incrementStrategy, currentBranchConfig.Key,
preventIncrementForMergedBranchVersion,
tagNumberPattern, configuration.ContinuousDeploymentFallbackTag);
tagNumberPattern, configuration.ContinuousDeploymentFallbackTag,
currentBranchConfig.Value.TrackMergeTarget);
}
}
}
24 changes: 0 additions & 24 deletions GitVersionCore/LibGitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,6 @@ public static IEnumerable<Branch> GetBranchesContainingCommit(this Commit commit
}
}

public static bool IsDirectMergeFromCommit(this Tag tag, Commit commit)
{
var targetCommit = tag.Target as Commit;
if (targetCommit != null)
{
var parents = targetCommit.Parents;
if (parents != null)
{
foreach (var parent in parents)
{
if (parent != null)
{
if (string.Equals(parent.Id.Sha, commit.Id.Sha, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}
}
}

return false;
}

public static GitObject PeeledTarget(this Tag tag)
{
var target = tag.Target;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
namespace GitVersion.VersionCalculation.BaseVersionCalculators
{
using System;
using System.Linq;
using LibGit2Sharp;

public class GitFlowDevelopBranchBaseVersionStrategy : HighestTagBaseVersionStrategy
{
protected override bool IsValidTag(string branchName, Tag tag, Commit commit)
protected override bool IsValidTag(GitVersionContext context, string branchName, Tag tag, Commit commit)
{
if (!string.IsNullOrWhiteSpace(branchName))
{
if (branchName.ToLower().EndsWith("/develop"))
if (context.Configuration.TrackMergeTarget)
{
return tag.IsDirectMergeFromCommit(commit);
return IsDirectMergeFromCommit(tag, commit);
}
}

return base.IsValidTag(branchName, tag, commit);
return base.IsValidTag(context, branchName, tag, commit);
}

static bool IsDirectMergeFromCommit(Tag tag, Commit commit)
{
var targetCommit = tag.Target as Commit;
if (targetCommit != null)
{
var parents = targetCommit.Parents;
if (parents != null)
{
return parents
.Where(parent => parent != null)
.Any(parent => string.Equals(parent.Id.Sha, commit.Id.Sha, StringComparison.OrdinalIgnoreCase));
}
}

return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace GitVersion.VersionCalculation.BaseVersionCalculators
{
using System;
using System.Linq;
using LibGit2Sharp;

Expand All @@ -18,7 +17,7 @@ public override BaseVersion GetVersion(GitVersionContext context)
return null;
}

protected virtual bool IsValidTag(string branchName, Tag tag, Commit commit)
protected virtual bool IsValidTag(GitVersionContext context, string branchName, Tag tag, Commit commit)
{
return tag.PeeledTarget() == commit;
}
Expand All @@ -40,7 +39,7 @@ bool GetVersion(GitVersionContext context, out VersionTaggedCommit versionTagged
.Commits
.SelectMany(commit =>
{
return allTags.Where(t => IsValidTag(currentBranchName, t, commit));
return allTags.Where(t => IsValidTag(context, currentBranchName, t, commit));
})
.Select(t =>
{
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,24 @@ branches:
master:
tag:
increment: Patch
preventIncrementOfMergedBranchVersion: true
prevent-increment-of-merged-branch-version: true
(pull|pull\-requests|pr)[/-]:
tag: PullRequest
increment: Inherit
tagNumberPattern: '[/-](?<number>\d+)[-/]'
tag-number-pattern: '[/-](?<number>\d+)[-/]'
```
The options in here are:
- `tag`: The pre release tag to use for this branch. Use the value `useBranchNameAsTag` to use the branch name instead.
- `mode`: Same as above
- `tag`: The pre release tag to use for this branch. Use the value `use-branch-name-as-tag` to use the branch name instead.
For example `feature/foo` would become a pre-release tag of `foo` with this value
- `increment`: the part of the SemVer to increment when GitVersion detects it needs to be (i.e commit after a tag)
- `preventIncrementOfMergedBranchVersion`: When `release-2.0.0` is merged into master, we want master to build `2.0.0`.
- `prevent-increment-of-merged-branch-version`: When `release-2.0.0` is merged into master, we want master to build `2.0.0`.
If `release-2.0.0` is merged into develop we want it to build `2.1.0`, this option prevents incrementing after a versioned branch is merged
- `tagNumberPattern`: Pull requests require us to pull the pre-release number out of the branch name so `refs/pulls/534/merge` builds as `PullRequest.5`.
- `tag-number-pattern`: Pull requests require us to pull the pre-release number out of the branch name so `refs/pulls/534/merge` builds as `PullRequest.5`.
This is a regex with a named capture group called `number`
- `track-merge-target`: Strategy which will look for tagged merge commits directly off the current branch. For example
develop -> release/1.0.0 -> merge into master and tag 1.0.0. The tag is *not* on develop, but develop should be 1.0.0 now.

We don't envision many people needing to change most of these configuration values, but they are there if you need to.

Expand Down

0 comments on commit 1d1b0c4

Please sign in to comment.