Skip to content

Commit

Permalink
Merge pull request #392 from JakeGinnivan/pr/GitFlowWithDevelop
Browse files Browse the repository at this point in the history
Pr/git flow with develop
  • Loading branch information
JakeGinnivan committed Mar 16, 2015
2 parents 049624a + 25b0e94 commit e4dcf71
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ branches:
master:
tag:
increment: Patch
preventIncrementOfMergedBranchVersion: true
prevent-increment-of-merged-branch-version: true
release[/-]:
tag: beta
feature[/-]:
Expand All @@ -17,12 +17,13 @@ branches:
support[/-]:
tag:
increment: Patch
preventIncrementOfMergedBranchVersion: true
prevent-increment-of-merged-branch-version: true
develop:
mode: ContinuousDeployment
tag: unstable
increment: Minor
track-merge-target: true
(pull|pull\-requests|pr)[/-]:
tag: PullRequest
increment: Inherit
tagNumberPattern: '[/-](?<number>\d+)[-/]'
tag-number-pattern: '[/-](?<number>\d+)[-/]'
50 changes: 50 additions & 0 deletions GitVersionCore.Tests/IntegrationTests/ReleaseBranchScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,56 @@
[TestFixture]
public class ReleaseBranchScenarios
{
[Test]
public void NoMergeBacksToDevelopInCaseThereAreNoChangesInReleaseBranch()
{
using (var fixture = new EmptyRepositoryFixture(new Config()))
{
fixture.Repository.MakeACommit();
fixture.Repository.CreateBranch("develop").Checkout();
fixture.Repository.MakeCommits(3);
var releaseBranch = fixture.Repository.CreateBranch("release/1.0.0");
releaseBranch.Checkout();
fixture.Repository.Checkout("master");
fixture.Repository.MergeNoFF("release/1.0.0");
fixture.Repository.ApplyTag("1.0.0");
fixture.Repository.Checkout("develop");
fixture.Repository.MakeACommit();

fixture.Repository.Branches.Remove(releaseBranch);

fixture.AssertFullSemver("1.1.0-unstable.1");
}
}

[Test]
public void NoMergeBacksToDevelopInCaseThereAreChangesInReleaseBranch()
{
using (var fixture = new EmptyRepositoryFixture(new Config()))
{
fixture.Repository.MakeACommit();
fixture.Repository.CreateBranch("develop").Checkout();
fixture.Repository.MakeCommits(3);
var releaseBranch = fixture.Repository.CreateBranch("release/1.0.0");
releaseBranch.Checkout();
fixture.Repository.MakeACommit();

// Merge to master
fixture.Repository.Checkout("master");
fixture.Repository.MergeNoFF("release/1.0.0");
fixture.Repository.ApplyTag("1.0.0");

// Merge to develop
fixture.Repository.Checkout("develop");
fixture.Repository.MergeNoFF("release/1.0.0");

fixture.Repository.MakeACommit();
fixture.Repository.Branches.Remove(releaseBranch);

fixture.AssertFullSemver("1.1.0-unstable.1");
}
}

[Test]
public void CanTakeVersionFromReleaseBranch()
{
Expand Down
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);
}
}
}
1 change: 1 addition & 0 deletions GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Compile Include="VersionCalculation\BaseVersionCalculator.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculators\BaseVersion.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculators\ConfigNextVersionBaseVersionStrategy.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculators\TrackMergeTargetBaseVersionStrategy.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculators\HighestTagBaseVersionStrategy.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculators\MergeMessageBaseVersionStrategy.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculators\VersionInBranchBaseVersionStrategy.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,29 @@ public override BaseVersion GetVersion(GitVersionContext context)
return null;
}

protected virtual bool IsValidTag(GitVersionContext context, string branchName, Tag tag, Commit commit)
{
return tag.PeeledTarget() == commit;
}

bool GetVersion(GitVersionContext context, out VersionTaggedCommit versionTaggedCommit)
{
string currentBranchName = null;
var head = context.Repository.Head;
if (head != null)
{
currentBranchName = head.CanonicalName;
}

var olderThan = context.CurrentCommit.When();
var allTags = context.Repository.Tags
.Where(tag => ((Commit) tag.PeeledTarget()).When() <= olderThan)
.Where(tag => ((Commit)tag.PeeledTarget()).When() <= olderThan)
.ToList();
var tagsOnBranch = context.CurrentBranch
.Commits
.SelectMany(commit =>
{
return allTags.Where(t => t.PeeledTarget() == commit);
return allTags.Where(t => IsValidTag(context, currentBranchName, t, commit));
})
.Select(t =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace GitVersion.VersionCalculation.BaseVersionCalculators
{
using System;
using System.Linq;
using LibGit2Sharp;

public class TrackMergeTargetBaseVersionStrategy : HighestTagBaseVersionStrategy
{
protected override bool IsValidTag(GitVersionContext context, string branchName, Tag tag, Commit commit)
{
if (!string.IsNullOrWhiteSpace(branchName))
{
if (context.Configuration.TrackMergeTarget)
{
return IsDirectMergeFromCommit(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;
}
}
}
9 changes: 5 additions & 4 deletions GitVersionCore/VersionCalculation/NextVersionCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ public NextVersionCalculator(IBaseVersionCalculator baseVersionCalculator = null
baseVersionFinder = baseVersionCalculator ??
new BaseVersionCalculator(
new FallbackBaseVersionStrategy(),
new ConfigNextVersionBaseVersionStrategy(),
highestTagBaseVersionStrategy,
new MergeMessageBaseVersionStrategy(),
new VersionInBranchBaseVersionStrategy());
new ConfigNextVersionBaseVersionStrategy(),
highestTagBaseVersionStrategy,
new TrackMergeTargetBaseVersionStrategy(),
new MergeMessageBaseVersionStrategy(),
new VersionInBranchBaseVersionStrategy());
}

public SemanticVersion FindVersion(GitVersionContext context)
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 e4dcf71

Please sign in to comment.