Skip to content

Commit d27bfdd

Browse files
committed
CR: Refactor MergeMessage to remove private property setters
1 parent f353dd8 commit d27bfdd

File tree

1 file changed

+28
-40
lines changed

1 file changed

+28
-40
lines changed

src/GitVersionCore/MergeMessage.cs

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Text.RegularExpressions;
45

56
namespace GitVersion
@@ -22,57 +23,44 @@ public MergeMessage(string mergeMessage, Config config)
2223
if (mergeMessage == null)
2324
throw new NullReferenceException();
2425

25-
foreach(var entry in config.MergeMessageFormats)
26-
{
27-
var pattern = Pattern(entry.Key, entry.Value);
28-
if (ApplyPattern(mergeMessage, config.TagPrefix, pattern))
29-
{
30-
return;
31-
}
32-
}
26+
// Concat config messages with the defaults.
27+
// Ensure configs are processed first.
28+
var allPatterns = config.MergeMessageFormats
29+
.Select(x => Pattern(x.Key, x.Value))
30+
.Concat(DefaultPatterns);
3331

34-
foreach (var pattern in DefaultPatterns)
32+
foreach (var pattern in allPatterns)
3533
{
36-
if (ApplyPattern(mergeMessage, config.TagPrefix, pattern))
34+
var match = pattern.Value.Match(mergeMessage);
35+
if (match.Success)
3736
{
38-
return;
39-
}
40-
}
41-
}
37+
MatchDefinition = pattern.Key;
38+
MergedBranch = match.Groups["SourceBranch"].Value;
4239

43-
public string MatchDefinition { get; private set; }
44-
public string TargetBranch { get; private set; }
45-
public string MergedBranch { get; private set; } = "";
46-
public bool IsMergedPullRequest => PullRequestNumber != null;
47-
public int? PullRequestNumber { get; private set; }
48-
public SemanticVersion Version { get; private set; }
40+
if (match.Groups["TargetBranch"].Success)
41+
{
42+
TargetBranch = match.Groups["TargetBranch"].Value;
43+
}
4944

50-
private bool ApplyPattern(string mergeMessage, string tagPrefix, KeyValuePair<string, Regex> pattern)
51-
{
52-
var match = pattern.Value.Match(mergeMessage);
53-
if (match.Success)
54-
{
55-
MatchDefinition = pattern.Key;
56-
MergedBranch = match.Groups["SourceBranch"].Value;
45+
if (int.TryParse(match.Groups["PullRequestNumber"].Value, out var pullNumber))
46+
{
47+
PullRequestNumber = pullNumber;
48+
}
5749

58-
if (match.Groups["TargetBranch"].Success)
59-
{
60-
TargetBranch = match.Groups["TargetBranch"].Value;
61-
}
50+
Version = ParseVersion(MergedBranch, config.TagPrefix);
6251

63-
if (int.TryParse(match.Groups["PullRequestNumber"].Value, out var pullNumber))
64-
{
65-
PullRequestNumber = pullNumber;
52+
break;
6653
}
67-
68-
Version = ParseVersion(MergedBranch, tagPrefix);
69-
70-
return true;
7154
}
72-
73-
return false;
7455
}
7556

57+
public string MatchDefinition { get; }
58+
public string TargetBranch { get; }
59+
public string MergedBranch { get; } = "";
60+
public bool IsMergedPullRequest => PullRequestNumber != null;
61+
public int? PullRequestNumber { get; }
62+
public SemanticVersion Version { get; }
63+
7664
private SemanticVersion ParseVersion(string branchName, string tagPrefix)
7765
{
7866
// Remove remotes and branch prefixes like release/ feature/ hotfix/ etc

0 commit comments

Comments
 (0)