1
1
namespace GitVersion
2
2
{
3
3
using System ;
4
- using System . Collections . Generic ;
5
4
using System . Linq ;
6
5
using LibGit2Sharp ;
7
6
@@ -12,104 +11,46 @@ protected SemanticVersion FindVersion(
12
11
BranchType branchType ,
13
12
string baseBranchName )
14
13
{
15
- var nbHotfixCommits = NumberOfCommitsInBranchNotKnownFromBaseBranch ( context . Repository , context . CurrentBranch , branchType , baseBranchName ) ;
16
14
17
15
var versionString = context . CurrentBranch . GetSuffix ( branchType ) ;
18
16
if ( ! versionString . Contains ( "." ) )
19
17
return new SemanticVersion ( ) ;
20
- var version = SemanticVersion . Parse ( versionString ) ;
21
-
22
- EnsureVersionIsValid ( version , context . CurrentBranch , branchType ) ;
18
+ var shortVersion = ShortVersionParser . Parse ( versionString ) ;
23
19
20
+ EnsureVersionIsValid ( shortVersion , context . CurrentBranch , branchType ) ;
21
+ var semanticVersionPreReleaseTag = new SemanticVersionPreReleaseTag ( ) ;
24
22
if ( branchType == BranchType . Hotfix )
25
- version . PreReleaseTag = "beta.1" ;
23
+ semanticVersionPreReleaseTag = "beta.1" ;
26
24
if ( branchType == BranchType . Release )
27
- version . PreReleaseTag = "beta.1" ;
25
+ semanticVersionPreReleaseTag = "beta.1" ;
28
26
if ( branchType == BranchType . Unknown )
29
- version . PreReleaseTag = context . CurrentBranch . Name . Replace ( "-" + versionString , string . Empty ) + ".1" ;
27
+ semanticVersionPreReleaseTag = context . CurrentBranch . Name . Replace ( "-" + versionString , string . Empty ) + ".1" ;
30
28
31
- var tagVersion = RetrieveMostRecentOptionalTagVersion ( context . Repository , version , context . CurrentBranch . Commits . Take ( nbHotfixCommits + 1 ) ) ;
32
-
33
- var semanticVersion = new SemanticVersion
34
- {
35
- Major = version . Major ,
36
- Minor = version . Minor ,
37
- Patch = version . Patch ,
38
- PreReleaseTag = version . PreReleaseTag ,
39
- BuildMetaData = new SemanticVersionBuildMetaData (
40
- nbHotfixCommits , context . CurrentBranch . Name , context . CurrentCommit . Sha , context . CurrentCommit . When ( ) )
41
- } ;
42
29
30
+ var nbHotfixCommits = BranchCommitDifferenceFinder . NumberOfCommitsInBranchNotKnownFromBaseBranch ( context . Repository , context . CurrentBranch , branchType , baseBranchName ) ;
31
+
32
+ var tagVersion = RecentTagVersionExtractor . RetrieveMostRecentOptionalTagVersion ( context . Repository , shortVersion , context . CurrentBranch . Commits . Take ( nbHotfixCommits + 1 ) ) ;
43
33
if ( tagVersion != null )
44
34
{
45
- //If the tag is on the eact commit then dont bump the PreReleaseTag
46
- if ( context . CurrentCommit . Sha != tagVersion . Commit . Sha )
47
- {
48
- tagVersion . SemVer . PreReleaseTag . Number ++ ;
49
- }
50
- semanticVersion . PreReleaseTag = tagVersion . SemVer . PreReleaseTag ;
35
+ semanticVersionPreReleaseTag = tagVersion ;
51
36
}
52
-
53
- return semanticVersion ;
54
- }
55
-
56
- bool IsMostRecentCommitTagged ( GitVersionContext context , out SemanticVersion version )
57
- {
58
- var currentCommit = context . CurrentBranch . Commits . First ( ) ;
59
-
60
- var tags = context . Repository . Tags
61
- . Where ( tag => tag . PeeledTarget ( ) == currentCommit )
62
- . ToList ( ) ;
63
-
64
- foreach ( var tag in tags )
37
+ return new SemanticVersion
65
38
{
66
- if ( SemanticVersion . TryParse ( tag . Name , out version ) )
67
- {
68
- return true ;
69
- }
70
- }
39
+ Major = shortVersion . Major ,
40
+ Minor = shortVersion . Minor ,
41
+ Patch = shortVersion . Patch ,
42
+ PreReleaseTag = semanticVersionPreReleaseTag ,
43
+ BuildMetaData = new SemanticVersionBuildMetaData ( nbHotfixCommits , context . CurrentBranch . Name , context . CurrentCommit . Sha , context . CurrentCommit . When ( ) )
44
+ } ;
71
45
72
- version = null ;
73
- return false ;
74
46
}
75
47
76
- VersionTaggedCommit RetrieveMostRecentOptionalTagVersion (
77
- IRepository repository , SemanticVersion branchVersion , IEnumerable < Commit > take )
78
- {
79
- foreach ( var commit in take )
80
- {
81
- foreach ( var tag in repository . TagsByDate ( commit ) )
82
- {
83
- SemanticVersion version ;
84
- if ( ! SemanticVersion . TryParse ( tag . Name , out version ) )
85
- {
86
- continue ;
87
- }
88
-
89
- if ( branchVersion . Major != version . Major ||
90
- branchVersion . Minor != version . Minor ||
91
- branchVersion . Patch != version . Patch )
92
- {
93
- continue ;
94
- }
95
-
96
- return new VersionTaggedCommit ( commit , version ) ;
97
- }
98
- }
99
48
100
- return null ;
101
- }
102
49
103
- void EnsureVersionIsValid ( SemanticVersion version , Branch branch , BranchType branchType )
50
+ void EnsureVersionIsValid ( ShortVersion version , Branch branch , BranchType branchType )
104
51
{
105
- var msg = string . Format ( "Branch '{0}' doesn't respect the {1} branch naming convention. " ,
106
- branch . Name , branchType ) ;
107
-
108
- if ( version . PreReleaseTag . HasTag ( ) )
109
- {
110
- throw new WarningException ( msg + string . Format ( "Supported format is '{0}-Major.Minor.Patch'." , branchType . ToString ( ) . ToLowerInvariant ( ) ) ) ;
111
- }
112
-
52
+ var msg = string . Format ( "Branch '{0}' doesn't respect the {1} branch naming convention. " , branch . Name , branchType ) ;
53
+
113
54
switch ( branchType )
114
55
{
115
56
case BranchType . Hotfix :
@@ -136,37 +77,5 @@ void EnsureVersionIsValid(SemanticVersion version, Branch branch, BranchType bra
136
77
}
137
78
}
138
79
139
- int NumberOfCommitsInBranchNotKnownFromBaseBranch (
140
- IRepository repo ,
141
- Branch branch ,
142
- BranchType branchType ,
143
- string baseBranchName )
144
- {
145
- var baseTip = repo . FindBranch ( baseBranchName ) . Tip ;
146
- if ( branch . Tip == baseTip )
147
- {
148
- // The branch bears no additional commit
149
- return 0 ;
150
- }
151
-
152
- var ancestor = repo . Commits . FindMergeBase (
153
- baseTip ,
154
- branch . Tip ) ;
155
-
156
- if ( ancestor == null )
157
- {
158
- var message = string . Format ( "A {0} branch is expected to branch off of '{1}'. However, branch '{1}' and '{2}' do not share a common ancestor." , branchType , baseBranchName , branch . Name ) ;
159
- throw new WarningException ( message ) ;
160
- }
161
-
162
- var filter = new CommitFilter
163
- {
164
- Since = branch . Tip ,
165
- Until = ancestor ,
166
- SortBy = CommitSortStrategies . Topological | CommitSortStrategies . Time
167
- } ;
168
-
169
- return repo . Commits . QueryBy ( filter ) . Count ( ) ;
170
- }
171
80
}
172
81
}
0 commit comments