1
1
namespace GitVersion
2
2
{
3
+ using System ;
3
4
using System . Linq ;
4
- using System . Text . RegularExpressions ;
5
5
using LibGit2Sharp ;
6
6
7
- class MergeMessageParser
7
+ static class MergeMessageParser
8
8
{
9
9
public static bool TryParse ( Commit mergeCommit , out string versionPart )
10
10
{
11
- versionPart = null ;
12
11
13
12
if ( mergeCommit . Parents . Count ( ) < 2 )
14
13
{
14
+ versionPart = null ;
15
15
return false ;
16
16
}
17
17
18
- var message = mergeCommit . Message ;
18
+ var message = mergeCommit . Message . TrimToFirstLine ( ) ;
19
+
19
20
20
- string trimmed ;
21
21
if ( message . StartsWith ( "Merge branch 'hotfix-" ) )
22
22
{
23
- trimmed = message . Replace ( "Merge branch 'hotfix-" , "" ) ;
23
+ var suffix = message . Replace ( "Merge branch 'hotfix-" , "" ) ;
24
+ return suffix . TryGetPrefix ( out versionPart , "'" ) ;
24
25
}
25
- else if ( message . StartsWith ( "Merge branch 'hotfix/" ) )
26
+
27
+ if ( message . StartsWith ( "Merge branch 'hotfix/" ) )
26
28
{
27
- trimmed = message . Replace ( "Merge branch 'hotfix/" , "" ) ;
29
+ var suffix = message . Replace ( "Merge branch 'hotfix/" , "" ) ;
30
+ return suffix . TryGetPrefix ( out versionPart , "'" ) ;
28
31
}
29
- else if ( message . StartsWith ( "Merge branch 'release-" ) )
32
+
33
+ if ( message . StartsWith ( "Merge branch 'release-" ) )
30
34
{
31
- trimmed = message . Replace ( "Merge branch 'release-" , "" ) ;
35
+ var suffix = message . Replace ( "Merge branch 'release-" , "" ) ;
36
+ return suffix . TryGetPrefix ( out versionPart , "'" ) ;
32
37
}
33
- else if ( message . StartsWith ( "Merge branch 'release/" ) )
38
+
39
+ if ( message . StartsWith ( "Merge branch 'release/" ) )
34
40
{
35
- trimmed = message . Replace ( "Merge branch 'release/" , "" ) ;
41
+ var suffix = message . Replace ( "Merge branch 'release/" , "" ) ;
42
+ return suffix . TryGetPrefix ( out versionPart , "'" ) ;
36
43
}
37
- else if ( Regex . IsMatch ( message , "Merge pull request #\\ d+ from " ) )
38
- {
39
- var branch = Regex . Match ( message , "from (?<branch>.*)" ) . Groups [ "branch" ] . Value ;
40
- var lastBranchPart = branch . Split ( '/' , '-' ) . Last ( ) ;
41
-
42
- if ( ! char . IsNumber ( lastBranchPart . First ( ) ) || ! lastBranchPart . Contains ( "." ) )
43
- {
44
- return false ;
45
- }
46
44
47
- versionPart = lastBranchPart ;
48
- return true ;
49
- }
50
- else if ( Regex . IsMatch ( message , "Merge pull request #\\ d+ in " ) )
45
+ if ( message . StartsWith ( "Merge branch '" ) )
51
46
{
52
- var branch = Regex . Match ( message , "in (?<branch>.*)" ) . Groups [ "branch" ] . Value ;
53
- var lastBranchPart = branch . Split ( '/' , '-' ) . Last ( ) ;
47
+ var suffix = message . Replace ( "Merge branch '" , "" ) ;
54
48
55
- if ( ! char . IsNumber ( lastBranchPart . First ( ) ) || ! lastBranchPart . Contains ( ". " ) )
49
+ if ( suffix . Contains ( "- " ) )
56
50
{
57
- return false ;
51
+ suffix = suffix . Split ( '-' ) [ 1 ] ;
58
52
}
59
-
60
- versionPart = lastBranchPart ;
61
- return true ;
53
+ return suffix . TryGetPrefix ( out versionPart , "'" ) ;
62
54
}
63
- else if ( message . StartsWith ( "Merge branch '" ) )
64
- {
65
- trimmed = message . Replace ( "Merge branch '" , "" ) ;
66
- var branchName = trimmed . Split ( '\' ' ) . First ( ) ;
67
- var dashSeparared = branchName . Split ( '-' ) ;
68
- // Support branchname-1.2.3
69
- if ( dashSeparared . Length == 2 )
70
- {
71
- trimmed = dashSeparared [ 1 ] ;
72
- if ( ! char . IsNumber ( trimmed . First ( ) ) )
73
- return false ;
74
55
75
- versionPart = trimmed ;
76
- return true ;
77
- }
78
- if ( ! char . IsNumber ( trimmed . First ( ) ) )
56
+ if ( message . StartsWith ( "Merge pull request #" ) )
57
+ {
58
+ var split = message . Split ( new [ ] { "/" } , StringSplitOptions . RemoveEmptyEntries ) ;
59
+ if ( split . Length != 2 )
79
60
{
61
+ versionPart = null ;
80
62
return false ;
81
63
}
64
+ return split [ 1 ] . TryGetSuffix ( out versionPart , "-" ) ;
82
65
}
83
- else if ( message . StartsWith ( "Finish Release-" ) ) //Match Syntevo SmartGit client's GitFlow 'release' merge commit message formatting
66
+
67
+ if ( message . StartsWith ( "Finish Release-" ) ) //Match Syntevo SmartGit client's GitFlow 'release' merge commit message formatting
84
68
{
85
- var branch = Regex . Match ( message , "Release-(?<branch>.*)" ) . Groups [ "branch" ] . Value ;
86
- var lastBranchPart = branch . Split ( '/' , '-' ) . Last ( ) ;
87
-
88
- if ( ! char . IsNumber ( lastBranchPart . First ( ) ) || ! lastBranchPart . Contains ( "." ) )
89
- {
90
- return false ;
91
- }
92
-
93
- versionPart = lastBranchPart ;
69
+ versionPart = message . Replace ( "Finish Release-" , "" ) ;
94
70
return true ;
95
71
}
96
- else if ( message . StartsWith ( "Finish " ) ) //Match Syntevo SmartGit client's GitFlow 'hotfix' merge commit message formatting
72
+
73
+ if ( message . StartsWith ( "Finish " ) ) //Match Syntevo SmartGit client's GitFlow 'hotfix' merge commit message formatting
97
74
{
98
- var branch = Regex . Match ( message , "Finish (?<branch>.*)" ) . Groups [ "branch" ] . Value ;
99
- var lastBranchPart = branch . Split ( '/' , '-' ) . Last ( ) ;
100
-
101
- if ( ! char . IsNumber ( lastBranchPart . First ( ) ) || ! lastBranchPart . Contains ( "." ) )
102
- {
103
- return false ;
104
- }
105
-
106
- versionPart = lastBranchPart ;
75
+ versionPart = message . Replace ( "Finish " , "" ) ;
107
76
return true ;
108
77
}
109
- else
78
+
79
+ versionPart = null ;
80
+ return false ;
81
+ }
82
+
83
+ static bool TryGetPrefix ( this string target , out string result , string splitter )
84
+ {
85
+ var indexOf = target . IndexOf ( splitter ) ;
86
+ if ( indexOf == - 1 )
110
87
{
88
+ result = null ;
111
89
return false ;
112
90
}
113
- trimmed = trimmed . TrimToFirstLine ( ) ;
114
- if ( ! trimmed . EndsWith ( "'" ) )
91
+ result = target . Substring ( 0 , indexOf ) ;
92
+ return true ;
93
+ }
94
+
95
+ static bool TryGetSuffix ( this string target , out string result , string splitter )
96
+ {
97
+ var indexOf = target . IndexOf ( splitter ) ;
98
+ if ( indexOf == - 1 )
115
99
{
100
+ result = null ;
116
101
return false ;
117
102
}
118
- versionPart = trimmed . TrimEnd ( ' \' ' ) ;
103
+ result = target . Substring ( indexOf + 1 , target . Length - indexOf - 1 ) ;
119
104
return true ;
120
105
}
121
-
122
106
}
123
107
}
0 commit comments