|
| 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 | +} |
0 commit comments