@@ -31,63 +31,32 @@ public static GitVersionDetails determineVersion(
31
31
final GitRepoSituation repoSituation ,
32
32
final VersionDescription commitVersionDescription ,
33
33
final List <VersionDescription > branchVersionDescriptions ,
34
- final List <VersionDescription > tagVersionDescriptions ) {
34
+ final List <VersionDescription > tagVersionDescriptions ,
35
+ final boolean preferTags ) {
35
36
36
37
requireNonNull (repoSituation );
37
38
requireNonNull (commitVersionDescription );
38
39
requireNonNull (branchVersionDescriptions );
39
40
requireNonNull (tagVersionDescriptions );
40
41
41
- // default versioning
42
- String gitRefType = "commit" ;
43
- String gitRefName = repoSituation .getHeadCommit ();
44
- VersionDescription versionDescription = commitVersionDescription ;
42
+ final VersioningInfo versioningInfo = getVersioningInfo (repoSituation , commitVersionDescription , branchVersionDescriptions , tagVersionDescriptions , preferTags );
45
43
46
- if (repoSituation .getHeadBranch () != null ) {
47
- // branch versioning
48
- for (final VersionDescription branchVersionDescription : branchVersionDescriptions ) {
49
- Optional <String > versionBranch = Optional .of (repoSituation .getHeadBranch ())
50
- .filter (branch -> branch .matches (branchVersionDescription .getPattern ()));
51
- if (versionBranch .isPresent ()) {
52
- gitRefType = "branch" ;
53
- gitRefName = versionBranch .get ();
54
- versionDescription = branchVersionDescription ;
55
- break ;
56
- }
57
- }
58
- } else if (!repoSituation .getHeadTags ().isEmpty ()) {
59
- // tag versioning
60
- for (final VersionDescription tagVersionDescription : tagVersionDescriptions ) {
61
- Optional <String > versionTag = repoSituation .getHeadTags ().stream ()
62
- .filter (tag -> tag .matches (tagVersionDescription .getPattern ()))
63
- .max (comparing (DefaultArtifactVersion ::new ));
64
- if (versionTag .isPresent ()) {
65
- gitRefType = "tag" ;
66
- gitRefName = versionTag .get ();
67
- versionDescription = tagVersionDescription ;
68
- break ;
69
- }
70
- }
71
- }
72
-
73
- final VersionDescription finalVersionDescription = versionDescription ;
74
-
75
- final Map <String , String > refData = valueGroupMap (versionDescription .getPattern (), gitRefName );
44
+ final Map <String , String > refData = valueGroupMap (versioningInfo .description .getPattern (), versioningInfo .gitRefName );
76
45
77
46
final Map <String , String > gitDataMap = new HashMap <>();
78
47
gitDataMap .put ("commit" , repoSituation .getHeadCommit ());
79
48
gitDataMap .put ("commit.short" , repoSituation .getHeadCommit ().substring (0 , 7 ));
80
49
gitDataMap .put ("commit.timestamp" , Long .toString (repoSituation .getHeadCommitTimestamp ()));
81
50
gitDataMap .put ("commit.timestamp.datetime" , formatHeadCommitTimestamp (repoSituation .getHeadCommitTimestamp ()));
82
- gitDataMap .put ("ref" , gitRefName );
83
- gitDataMap .put (gitRefType , gitRefName );
51
+ gitDataMap .put ("ref" , versioningInfo . gitRefName );
52
+ gitDataMap .put (versioningInfo . gitRefType , versioningInfo . gitRefName );
84
53
gitDataMap .putAll (refData );
85
54
86
55
final VersionTransformer versionTransformer = currentVersion -> {
87
56
final Map <String , String > dataMap = new HashMap <>(gitDataMap );
88
57
dataMap .put ("version" , currentVersion );
89
58
dataMap .put ("version.release" , currentVersion .replaceFirst ("-SNAPSHOT$" , "" ));
90
- return substituteText (finalVersionDescription .getVersionFormat (), dataMap )
59
+ return substituteText (versioningInfo . description .getVersionFormat (), dataMap )
91
60
.replace ("/" , "-" );
92
61
};
93
62
@@ -96,19 +65,67 @@ public static GitVersionDetails determineVersion(
96
65
dataMap .put ("version" , currentVersion );
97
66
dataMap .put ("version.release" , currentVersion .replaceFirst ("-SNAPSHOT$" , "" ));
98
67
return transformProperties (
99
- currentProperties , finalVersionDescription .getPropertyDescriptions (), dataMap );
68
+ currentProperties , versioningInfo . description .getPropertyDescriptions (), dataMap );
100
69
};
101
70
102
71
return new GitVersionDetails (
103
72
repoSituation .isClean (),
104
73
repoSituation .getHeadCommit (),
105
- gitRefType ,
106
- gitRefName ,
74
+ versioningInfo . gitRefType ,
75
+ versioningInfo . gitRefName ,
107
76
versionTransformer ,
108
77
propertiesTransformer
109
78
);
110
79
}
111
80
81
+ private static VersioningInfo getVersioningInfo (GitRepoSituation repoSituation , VersionDescription commitVersionDescription , List <VersionDescription > branchVersionDescriptions , List <VersionDescription > tagVersionDescriptions , boolean preferTags ) {
82
+
83
+ // tags take precedence over branches
84
+ VersioningInfo versioningInfo = null ;
85
+ if (preferTags ) {
86
+ versioningInfo = getTagVersioningInfo (repoSituation , tagVersionDescriptions );
87
+ }
88
+ if (versioningInfo == null ) {
89
+ versioningInfo = getBranchVersioningInfo (repoSituation , branchVersionDescriptions );
90
+ }
91
+ if (versioningInfo == null && !preferTags ) {
92
+ versioningInfo = getTagVersioningInfo (repoSituation , tagVersionDescriptions );
93
+ }
94
+
95
+ // default versioning: commit
96
+ if (versioningInfo == null ) {
97
+ versioningInfo = new VersioningInfo ("commit" , repoSituation .getHeadCommit (), commitVersionDescription );
98
+ }
99
+ return versioningInfo ;
100
+ }
101
+
102
+ private static VersioningInfo getTagVersioningInfo (GitRepoSituation repoSituation , List <VersionDescription > tagVersionDescriptions ) {
103
+ if (!repoSituation .getHeadTags ().isEmpty ()) {
104
+ for (final VersionDescription tagVersionDescription : tagVersionDescriptions ) {
105
+ Optional <String > versionTag = repoSituation .getHeadTags ().stream ()
106
+ .filter (tag -> tag .matches (tagVersionDescription .getPattern ()))
107
+ .max (comparing (DefaultArtifactVersion ::new ));
108
+ if (versionTag .isPresent ()) {
109
+ return new VersioningInfo ("tag" , versionTag .get (), tagVersionDescription );
110
+ }
111
+ }
112
+ }
113
+ return null ;
114
+ }
115
+
116
+ private static VersioningInfo getBranchVersioningInfo (GitRepoSituation repoSituation , List <VersionDescription > branchVersionDescriptions ) {
117
+ if (repoSituation .getHeadBranch () != null ) {
118
+ for (final VersionDescription branchVersionDescription : branchVersionDescriptions ) {
119
+ Optional <String > versionBranch = Optional .of (repoSituation .getHeadBranch ())
120
+ .filter (branch -> branch .matches (branchVersionDescription .getPattern ()));
121
+ if (versionBranch .isPresent ()) {
122
+ return new VersioningInfo ("branch" , versionBranch .get (), branchVersionDescription );
123
+ }
124
+ }
125
+ }
126
+ return null ;
127
+ }
128
+
112
129
private static Map <String , String > transformProperties (Map <String , String > currentProperties ,
113
130
List <PropertyDescription > propertyDescriptions ,
114
131
Map <String , String > dataMap ) {
@@ -148,4 +165,16 @@ private static String formatHeadCommitTimestamp(long headCommitDate) {
148
165
.withZone (ZoneOffset .UTC )
149
166
.format (Instant .ofEpochSecond (headCommitDate ));
150
167
}
168
+
169
+ private static class VersioningInfo {
170
+ private final String gitRefType ;
171
+ private final String gitRefName ;
172
+ private final VersionDescription description ;
173
+
174
+ private VersioningInfo (String gitRefType , String gitRefName , VersionDescription versionDescription ) {
175
+ this .gitRefType = gitRefType ;
176
+ this .gitRefName = gitRefName ;
177
+ this .description = versionDescription ;
178
+ }
179
+ }
151
180
}
0 commit comments