Skip to content

Commit bd16d24

Browse files
committed
Introduced MergeMessage class
This class parses the merge message and exposes all the useful information, like is it a pull request, what was the source and target branch etc.
1 parent cf29e64 commit bd16d24

File tree

3 files changed

+96
-22
lines changed

3 files changed

+96
-22
lines changed

src/GitVersionCore/GitVersionCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
<Compile Include="Helpers\ThreadSleep.cs" />
129129
<Compile Include="IncrementStrategyFinder.cs" />
130130
<Compile Include="LoggerWrapper.cs" />
131+
<Compile Include="MergeMessage.cs" />
131132
<Compile Include="OutputVariables\VersionVariables.cs" />
132133
<Compile Include="SemanticVersionExtensions.cs" />
133134
<Compile Include="SemanticVersionFormatValues.cs" />

src/GitVersionCore/MergeMessage.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text.RegularExpressions;
4+
5+
namespace GitVersion
6+
{
7+
class MergeMessage
8+
{
9+
static Regex parseMergeMessage = new Regex(
10+
@"^Merge (branch|tag) '(?<Branch>[^']*)'",
11+
RegexOptions.IgnoreCase | RegexOptions.Compiled);
12+
static Regex parseGitHubPullMergeMessage = new Regex(
13+
@"^Merge pull request #(?<PullRequestNumber>\d*) (from|in) (?<Source>.*)",
14+
RegexOptions.IgnoreCase | RegexOptions.Compiled);
15+
static Regex smartGitMergeMessage = new Regex(
16+
@"^Finish (?<Branch>.*)",
17+
RegexOptions.IgnoreCase | RegexOptions.Compiled);
18+
private string mergeMessage;
19+
private Config config;
20+
21+
public MergeMessage(string mergeMessage, Config config)
22+
{
23+
this.mergeMessage = mergeMessage;
24+
this.config = config;
25+
26+
var lastIndexOf = mergeMessage.LastIndexOf("into", StringComparison.OrdinalIgnoreCase);
27+
if (lastIndexOf != -1)
28+
{
29+
// If we have into in the merge message the rest should be the target branch
30+
TargetBranch = mergeMessage.Substring(lastIndexOf + 5);
31+
}
32+
33+
MergedBranch = ParseBranch();
34+
35+
// Remove remotes and branch prefixes like release/ feature/ hotfix/ etc
36+
var toMatch = Regex.Replace(MergedBranch, @"^(\w+[-/])*", "", RegexOptions.IgnoreCase);
37+
toMatch = Regex.Replace(toMatch, $"^{config.TagPrefix}", "");
38+
// We don't match if the version is likely an ip (i.e starts with http://)
39+
var versionMatch = new Regex(@"^(?<!://)\d+\.\d+(\.*\d+)*");
40+
var version = versionMatch.Match(toMatch);
41+
42+
if (version.Success)
43+
{
44+
SemanticVersion val;
45+
if (SemanticVersion.TryParse(version.Value, config.TagPrefix, out val))
46+
{
47+
Version = val;
48+
}
49+
}
50+
}
51+
52+
private string ParseBranch()
53+
{
54+
var match = parseMergeMessage.Match(mergeMessage);
55+
if (match.Success)
56+
{
57+
return match.Groups["Branch"].Value;
58+
}
59+
60+
match = smartGitMergeMessage.Match(mergeMessage);
61+
if (match.Success)
62+
{
63+
return match.Groups["Branch"].Value;
64+
}
65+
66+
match = parseGitHubPullMergeMessage.Match(mergeMessage);
67+
if (match.Success)
68+
{
69+
IsMergedPullRequest = true;
70+
int pullNumber;
71+
if (int.TryParse(match.Groups["PullRequestNumber"].Value, out pullNumber))
72+
{
73+
PullRequestNumber = pullNumber;
74+
}
75+
var from = match.Groups["Source"].Value;
76+
// We could remove/separate the remote name at this point?
77+
return from;
78+
}
79+
80+
return "";
81+
}
82+
83+
public string TargetBranch { get; }
84+
public string MergedBranch { get; }
85+
public bool IsMergedPullRequest { get; private set; }
86+
public int? PullRequestNumber { get; private set; }
87+
public SemanticVersion Version { get; }
88+
}
89+
}
Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
namespace GitVersion.VersionCalculation.BaseVersionCalculators
22
{
3-
using System;
43
using System.Collections.Generic;
54
using System.Linq;
6-
using System.Text.RegularExpressions;
75
using LibGit2Sharp;
86

97
/// <summary>
@@ -21,7 +19,7 @@ public override IEnumerable<BaseVersion> GetVersions(GitVersionContext context)
2119
.SelectMany(c =>
2220
{
2321
SemanticVersion semanticVersion;
24-
if (TryParse(c, context.Configuration, out semanticVersion))
22+
if (TryParse(c, context, out semanticVersion))
2523
{
2624
var shouldIncrement = !context.Configuration.PreventIncrementForMergedBranchVersion;
2725
return new[]
@@ -34,35 +32,21 @@ public override IEnumerable<BaseVersion> GetVersions(GitVersionContext context)
3432
return baseVersions;
3533
}
3634

37-
static bool TryParse(Commit mergeCommit, EffectiveConfiguration configuration, out SemanticVersion semanticVersion)
35+
static bool TryParse(Commit mergeCommit, GitVersionContext context, out SemanticVersion semanticVersion)
3836
{
39-
semanticVersion = Inner(mergeCommit, configuration);
37+
semanticVersion = Inner(mergeCommit, context);
4038
return semanticVersion != null;
4139
}
4240

43-
static SemanticVersion Inner(Commit mergeCommit, EffectiveConfiguration configuration)
41+
static SemanticVersion Inner(Commit mergeCommit, GitVersionContext context)
4442
{
4543
if (mergeCommit.Parents.Count() < 2)
4644
{
4745
return null;
4846
}
4947

50-
var commitMessage = mergeCommit.Message;
51-
var lastIndexOf = commitMessage.LastIndexOf("into", StringComparison.OrdinalIgnoreCase);
52-
if (lastIndexOf != -1)
53-
commitMessage = commitMessage.Substring(0, lastIndexOf);
54-
55-
//TODO: Make the version prefixes customizable
56-
var possibleVersions = Regex.Matches(commitMessage, @"^.*?(([rR]elease|[hH]otfix|[aA]lpha)-|-v|/|/v|'|Finish )(?<PossibleVersions>(?<!://)\d+\.\d+(\.*\d+)*)")
57-
.Cast<Match>()
58-
.Select(m => m.Groups["PossibleVersions"].Value);
59-
60-
return possibleVersions
61-
.Select(part =>
62-
{
63-
SemanticVersion v;
64-
return SemanticVersion.TryParse(part, configuration.GitTagPrefix, out v) ? v : null;
65-
}).FirstOrDefault(v => v != null);
48+
var mergeMessage = new MergeMessage(mergeCommit.Message, context.FullConfiguration);
49+
return mergeMessage.Version;
6650
}
6751
}
6852
}

0 commit comments

Comments
 (0)