Skip to content

Commit cdca529

Browse files
committed
Merge pull request #291 from dtabuenc/merge-release-back-to-develop
Ability to merge release back to develop
2 parents db6fad3 + 4927d98 commit cdca529

File tree

8 files changed

+118
-48
lines changed

8 files changed

+118
-48
lines changed

GitVersionCore.Tests/GitFlow/WikiScenarios.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void MinorReleaseExample()
7676

7777
// Make a commit after a tag should bump up the beta
7878
fixture.Repository.MakeACommit();
79-
fixture.AssertFullSemver("1.3.0-beta.2+2");
79+
fixture.AssertFullSemver("1.3.0-beta.2+0");
8080

8181
// Merge release branch to master
8282
fixture.Repository.Checkout("master");

GitVersionCore.Tests/GitHubFlow/ReleaseBranchTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,40 @@ public void WhenReleaseBranchIsMergedIntoMasterHighestVersionIsTakenWithIt()
5757
fixture.AssertFullSemver("2.0.0+11");
5858
}
5959
}
60+
61+
[Test]
62+
public void WhenMergingReleaseBackToDevShouldNotResetBetaVersion()
63+
{
64+
using (var fixture = new EmptyRepositoryFixture())
65+
{
66+
const string TaggedVersion = "1.0.3";
67+
fixture.Repository.MakeATaggedCommit(TaggedVersion);
68+
fixture.Repository.CreateBranch("develop");
69+
fixture.Repository.Checkout("develop");
70+
71+
fixture.Repository.MakeCommits(1);
72+
73+
fixture.Repository.CreateBranch("release-2.0.0");
74+
fixture.Repository.Checkout("release-2.0.0");
75+
fixture.Repository.MakeCommits(1);
76+
77+
fixture.AssertFullSemver("2.0.0-beta.1+1");
78+
79+
//tag it to bump to beta 2
80+
fixture.Repository.ApplyTag("2.0.0-beta1");
81+
82+
fixture.Repository.MakeCommits(1);
83+
84+
fixture.AssertFullSemver("2.0.0-beta.2+0");
85+
86+
//merge down to develop
87+
fixture.Repository.Checkout("develop");
88+
fixture.Repository.MergeNoFF("release-2.0.0", Constants.SignatureNow());
89+
90+
//but keep working on the release
91+
fixture.Repository.Checkout("release-2.0.0");
92+
93+
fixture.AssertFullSemver("2.0.0-beta.2+0");
94+
}
95+
}
6096
}

GitVersionCore/GitFlow/BranchFinders/BranchCommitDifferenceFinder.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace GitVersion
22
{
3+
using System.Collections.Generic;
34
using System.Linq;
45
using LibGit2Sharp;
56

@@ -34,5 +35,38 @@ public static int NumberOfCommitsInBranchNotKnownFromBaseBranch(IRepository repo
3435
return repo.Commits.QueryBy(filter)
3536
.Count();
3637
}
38+
39+
public static int NumberOfCommitsSinceLastTagOrBranchPoint(GitVersionContext context, List<Tag> tagsInDescendingOrder, BranchType branchType, string baseBranchName)
40+
{
41+
if (!tagsInDescendingOrder.Any())
42+
{
43+
return NumberOfCommitsInBranchNotKnownFromBaseBranch(context.Repository, context.CurrentBranch, branchType, baseBranchName);
44+
}
45+
46+
var mostRecentTag = tagsInDescendingOrder.First();
47+
var ancestor = mostRecentTag;
48+
if (mostRecentTag.Target == context.CurrentCommit)
49+
{
50+
var previousTag = tagsInDescendingOrder.Skip(1).FirstOrDefault();
51+
if (previousTag != null)
52+
{
53+
ancestor = previousTag;
54+
}
55+
else
56+
{
57+
return NumberOfCommitsInBranchNotKnownFromBaseBranch(context.Repository, context.CurrentBranch, BranchType.Release, baseBranchName);
58+
}
59+
60+
}
61+
62+
var filter = new CommitFilter
63+
{
64+
Since = context.CurrentCommit,
65+
Until = ancestor.Target,
66+
SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Time
67+
};
68+
69+
return context.Repository.Commits.QueryBy(filter).Count() - 1;
70+
}
3771
}
3872
}

GitVersionCore/GitFlow/BranchFinders/HotfixVersionFinder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace GitVersion
22
{
3-
using System.Linq;
43
using LibGit2Sharp;
54

65
class HotfixVersionFinder
@@ -28,7 +27,7 @@ public SemanticVersion FindVersion(GitVersionContext context)
2827
static string GetSemanticVersionPreReleaseTag(GitVersionContext context, ShortVersion shortVersion, int nbHotfixCommits)
2928
{
3029
var semanticVersionPreReleaseTag = "beta.1";
31-
var tagVersion = RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context.Repository, shortVersion, context.CurrentBranch.Commits.Take(nbHotfixCommits + 1));
30+
var tagVersion = RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context, shortVersion);
3231
if (tagVersion != null)
3332
{
3433
semanticVersionPreReleaseTag = tagVersion;
Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,33 @@
11
namespace GitVersion
22
{
33
using System.Collections.Generic;
4+
using System.Linq;
45
using LibGit2Sharp;
56

67
class RecentTagVersionExtractor
78
{
8-
internal static SemanticVersionPreReleaseTag RetrieveMostRecentOptionalTagVersion(IRepository repository, ShortVersion matchVersion, IEnumerable<Commit> take)
9+
internal static SemanticVersionPreReleaseTag RetrieveMostRecentOptionalTagVersion(GitVersionContext context, ShortVersion matchVersion)
910
{
10-
Commit first = null;
11-
foreach (var commit in take)
11+
var tagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(matchVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name));
12+
return RetrieveMostRecentOptionalTagVersion(context, tagsInDescendingOrder.ToList());
13+
}
14+
15+
internal static SemanticVersionPreReleaseTag RetrieveMostRecentOptionalTagVersion(GitVersionContext context, List<Tag> applicableTagsInDescendingOrder)
16+
{
17+
if (applicableTagsInDescendingOrder.Any())
1218
{
13-
if (first == null)
19+
var taggedCommit = applicableTagsInDescendingOrder.First().Target;
20+
var preReleaseVersion = applicableTagsInDescendingOrder.Select(tag => SemanticVersion.Parse(tag.Name)).FirstOrDefault();
21+
if (preReleaseVersion != null)
1422
{
15-
first = commit;
16-
}
17-
foreach (var tag in repository.TagsByDate(commit))
18-
{
19-
SemanticVersion version;
20-
if (!SemanticVersion.TryParse(tag.Name, out version))
21-
{
22-
continue;
23-
}
24-
25-
if (matchVersion.Major == version.Major &&
26-
matchVersion.Minor == version.Minor &&
27-
matchVersion.Patch == version.Patch)
23+
if (taggedCommit != context.CurrentCommit)
2824
{
29-
var preReleaseTag = version.PreReleaseTag;
30-
31-
//If the tag is on the eact commit then dont bump the PreReleaseTag
32-
if (first != commit)
33-
{
34-
preReleaseTag.Number++;
35-
}
36-
return preReleaseTag;
25+
preReleaseVersion.PreReleaseTag.Number++;
3726
}
27+
return preReleaseVersion.PreReleaseTag;
3828
}
3929
}
40-
4130
return null;
4231
}
43-
44-
4532
}
4633
}

GitVersionCore/GitFlow/BranchFinders/ReleaseVersionFinder.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,22 @@ public SemanticVersion FindVersion(GitVersionContext context)
1111
var shortVersion = ShortVersionParser.Parse(versionString);
1212

1313
EnsureVersionIsValid(shortVersion, context.CurrentBranch);
14-
var semanticVersionPreReleaseTag = "beta.1";
15-
16-
var nbHotfixCommits = BranchCommitDifferenceFinder.NumberOfCommitsInBranchNotKnownFromBaseBranch(context.Repository, context.CurrentBranch, BranchType.Release, "develop");
1714

18-
var tagVersion = RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context.Repository, shortVersion, context.CurrentBranch.Commits.Take(nbHotfixCommits + 1));
19-
if (tagVersion != null)
20-
{
21-
semanticVersionPreReleaseTag = tagVersion;
22-
}
15+
var applicableTagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(shortVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name)).ToList();
16+
var numberOfCommitsSinceLastTagOrBranchPoint = BranchCommitDifferenceFinder.NumberOfCommitsSinceLastTagOrBranchPoint(context, applicableTagsInDescendingOrder, BranchType.Release, "develop");
17+
var semanticVersionPreReleaseTag = RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context, applicableTagsInDescendingOrder) ?? "beta.1";
18+
2319
return new SemanticVersion
2420
{
2521
Major = shortVersion.Major,
2622
Minor = shortVersion.Minor,
2723
Patch = shortVersion.Patch,
2824
PreReleaseTag = semanticVersionPreReleaseTag,
29-
BuildMetaData = new SemanticVersionBuildMetaData(nbHotfixCommits, context.CurrentBranch.Name, context.CurrentCommit.Sha, context.CurrentCommit.When())
25+
BuildMetaData = new SemanticVersionBuildMetaData(numberOfCommitsSinceLastTagOrBranchPoint, context.CurrentBranch.Name, context.CurrentCommit.Sha, context.CurrentCommit.When())
3026
};
3127
}
3228

33-
void EnsureVersionIsValid(ShortVersion version, Branch branch)
29+
static void EnsureVersionIsValid(ShortVersion version, Branch branch)
3430
{
3531
if (version.Patch != 0)
3632
{

GitVersionCore/GitHubFlow/OtherBranchVersionFinder.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ public bool FindVersion(GitVersionContext context, out SemanticVersion semanticV
1515
}
1616
var shortVersion = ShortVersionParser.Parse(versionString);
1717

18-
SemanticVersionPreReleaseTag semanticVersionPreReleaseTag = context.CurrentBranch.Name.Replace("-" + versionString, string.Empty) + ".1";
1918

20-
var nbHotfixCommits = BranchCommitDifferenceFinder.NumberOfCommitsInBranchNotKnownFromBaseBranch(context.Repository, context.CurrentBranch, BranchType.Unknown, "master");
19+
var applicableTagsInDescendingOrder = context.Repository.SemVerTagsRelatedToVersion(shortVersion).OrderByDescending(tag => SemanticVersion.Parse(tag.Name)).ToList();
20+
var nbHotfixCommits = BranchCommitDifferenceFinder.NumberOfCommitsSinceLastTagOrBranchPoint(context, applicableTagsInDescendingOrder, BranchType.Unknown, "master");
21+
var semanticVersionPreReleaseTag = RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context, applicableTagsInDescendingOrder) ?? CreateDefaultPreReleaseTag(context, versionString);
2122

22-
var tagVersion = RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context.Repository, shortVersion, context.CurrentBranch.Commits.Take(nbHotfixCommits + 1));
23-
if (tagVersion != null)
24-
{
25-
semanticVersionPreReleaseTag = tagVersion;
26-
}
2723

2824
if (semanticVersionPreReleaseTag.Name == "release")
2925
{
@@ -41,6 +37,11 @@ public bool FindVersion(GitVersionContext context, out SemanticVersion semanticV
4137
return true;
4238
}
4339

40+
SemanticVersionPreReleaseTag CreateDefaultPreReleaseTag(GitVersionContext context, string versionString)
41+
{
42+
return context.CurrentBranch.Name.Replace("-" + versionString, string.Empty) + ".1";
43+
}
44+
4445
static string GetUnknownBranchSuffix(Branch branch)
4546
{
4647
var unknownBranchSuffix = branch.Name.Split('-', '/');

GitVersionCore/LibGitExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ public static IEnumerable<Tag> TagsByDate(this IRepository repository, Commit co
3939
});
4040
}
4141

42+
public static IEnumerable<Tag> SemVerTagsRelatedToVersion(this IRepository repository, ShortVersion version)
43+
{
44+
foreach (var tag in repository.Tags)
45+
{
46+
SemanticVersion tagVersion;
47+
if (SemanticVersion.TryParse(tag.Name, out tagVersion))
48+
{
49+
if (version.Major == tagVersion.Major &&
50+
version.Minor == tagVersion.Minor &&
51+
version.Patch == tagVersion.Patch)
52+
{
53+
yield return tag;
54+
}
55+
}
56+
}
57+
}
58+
4259
public static GitObject PeeledTarget(this Tag tag)
4360
{
4461
var target = tag.Target;

0 commit comments

Comments
 (0)