Skip to content

Commit d366673

Browse files
committed
feat: add toogle to prefer tags for versioning instead of branches
1 parent 7ce7fdf commit d366673

File tree

6 files changed

+115
-54
lines changed

6 files changed

+115
-54
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ plugins {
1111
}
1212

1313
group 'me.qoomon'
14-
version '1.2.3'
14+
version '1.3.0'
1515

1616
sourceCompatibility = JavaVersion.VERSION_1_8
1717
targetCompatibility = JavaVersion.VERSION_1_8

src/main/java/me/qoomon/gitversioning/GitVersioning.java

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,63 +31,32 @@ public static GitVersionDetails determineVersion(
3131
final GitRepoSituation repoSituation,
3232
final VersionDescription commitVersionDescription,
3333
final List<VersionDescription> branchVersionDescriptions,
34-
final List<VersionDescription> tagVersionDescriptions) {
34+
final List<VersionDescription> tagVersionDescriptions,
35+
final boolean preferTags) {
3536

3637
requireNonNull(repoSituation);
3738
requireNonNull(commitVersionDescription);
3839
requireNonNull(branchVersionDescriptions);
3940
requireNonNull(tagVersionDescriptions);
4041

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);
4543

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);
7645

7746
final Map<String, String> gitDataMap = new HashMap<>();
7847
gitDataMap.put("commit", repoSituation.getHeadCommit());
7948
gitDataMap.put("commit.short", repoSituation.getHeadCommit().substring(0, 7));
8049
gitDataMap.put("commit.timestamp", Long.toString(repoSituation.getHeadCommitTimestamp()));
8150
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);
8453
gitDataMap.putAll(refData);
8554

8655
final VersionTransformer versionTransformer = currentVersion -> {
8756
final Map<String, String> dataMap = new HashMap<>(gitDataMap);
8857
dataMap.put("version", currentVersion);
8958
dataMap.put("version.release", currentVersion.replaceFirst("-SNAPSHOT$", ""));
90-
return substituteText(finalVersionDescription.getVersionFormat(), dataMap)
59+
return substituteText(versioningInfo.description.getVersionFormat(), dataMap)
9160
.replace("/", "-");
9261
};
9362

@@ -96,19 +65,67 @@ public static GitVersionDetails determineVersion(
9665
dataMap.put("version", currentVersion);
9766
dataMap.put("version.release", currentVersion.replaceFirst("-SNAPSHOT$", ""));
9867
return transformProperties(
99-
currentProperties, finalVersionDescription.getPropertyDescriptions(), dataMap);
68+
currentProperties, versioningInfo.description.getPropertyDescriptions(), dataMap);
10069
};
10170

10271
return new GitVersionDetails(
10372
repoSituation.isClean(),
10473
repoSituation.getHeadCommit(),
105-
gitRefType,
106-
gitRefName,
74+
versioningInfo.gitRefType,
75+
versioningInfo.gitRefName,
10776
versionTransformer,
10877
propertiesTransformer
10978
);
11079
}
11180

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+
112129
private static Map<String, String> transformProperties(Map<String, String> currentProperties,
113130
List<PropertyDescription> propertyDescriptions,
114131
Map<String, String> dataMap) {
@@ -148,4 +165,16 @@ private static String formatHeadCommitTimestamp(long headCommitDate) {
148165
.withZone(ZoneOffset.UTC)
149166
.format(Instant.ofEpochSecond(headCommitDate));
150167
}
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+
}
151180
}

src/main/java/me/qoomon/gradle/gitversioning/GitVersioningPlugin.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,44 @@
1111
import java.util.Map.Entry;
1212
import java.util.stream.Collectors;
1313

14+
import static java.lang.Boolean.parseBoolean;
1415
import static java.util.Collections.emptyList;
1516
import static java.util.Collections.singletonList;
1617
import static java.util.Optional.ofNullable;
1718
import static java.util.stream.Collectors.toList;
1819

1920
public class GitVersioningPlugin implements Plugin<Project> {
21+
private static final String OPTION_NAME_GIT_TAG = "git.tag";
22+
private static final String OPTION_NAME_GIT_BRANCH = "git.branch";
23+
private static final String OPTION_NAME_DISABLE = "versioning.disable";
24+
private static final String OPTION_PREFER_TAGS = "versioning.preferTags";
2025

2126
public void apply(@Nonnull Project rootProject) {
2227
rootProject.getAllprojects().forEach(it -> it.getTasks().create("version", VersionTask.class));
2328

2429
GitVersioningPluginExtension config = rootProject.getExtensions()
2530
.create("gitVersioning", GitVersioningPluginExtension.class, rootProject);
31+
32+
if (parseBoolean(getOption(rootProject, OPTION_NAME_DISABLE))) {
33+
rootProject.getLogger().warn("skip - versioning is disabled");
34+
return;
35+
}
36+
2637
rootProject.afterEvaluate(evaluatedProject -> {
2738

2839
GitRepoSituation repoSituation = GitUtil.situation(rootProject.getProjectDir());
29-
String providedTag = getOption(rootProject, "git.tag");
40+
String providedTag = getOption(rootProject, OPTION_NAME_GIT_TAG);
3041
if (providedTag != null) {
3142
repoSituation.setHeadBranch(null);
3243
repoSituation.setHeadTags(providedTag.isEmpty() ? emptyList() : singletonList(providedTag));
3344
}
34-
String providedBranch = getOption(rootProject, "git.branch");
45+
String providedBranch = getOption(rootProject, OPTION_NAME_GIT_BRANCH);
3546
if (providedBranch != null) {
3647
repoSituation.setHeadBranch(providedBranch.isEmpty() ? null : providedBranch);
3748
}
3849

50+
final boolean preferTags = config.preferTags || parseBoolean(getOption(rootProject, OPTION_PREFER_TAGS));
51+
3952
GitVersionDetails gitVersionDetails = GitVersioning.determineVersion(repoSituation,
4053
ofNullable(config.commit)
4154
.map(it -> new VersionDescription(null, it.versionFormat, mapPropertyDescription(it.properties)))
@@ -45,7 +58,8 @@ public void apply(@Nonnull Project rootProject) {
4558
.collect(toList()),
4659
config.tags.stream()
4760
.map(it -> new VersionDescription(it.pattern, it.versionFormat, mapPropertyDescription(it.properties)))
48-
.collect(toList()));
61+
.collect(toList()),
62+
preferTags);
4963

5064

5165
final Map<String, Object> originVersionMap = rootProject.getAllprojects().stream().collect(Collectors.toMap(Project::getPath, p -> p.getVersion()));
@@ -119,7 +133,12 @@ private PropertyValueDescription mapPropertyValueDescription(GitVersioningPlugin
119133
private String getOption(final Project project, final String name) {
120134
String value = (String) project.getProperties().get(name);
121135
if (value == null) {
122-
value = System.getenv("VERSIONING_" + name.replaceAll("\\.", "_").toUpperCase());
136+
String plainName = name.replaceFirst("^versioning\\.", "");
137+
String environmentVariableName = "VERSIONING_"
138+
+ String.join("_", plainName.split("(?=\\p{Lu})"))
139+
.replaceAll("\\.", "_")
140+
.toUpperCase();
141+
value = System.getenv(environmentVariableName);
123142
}
124143
return value;
125144
}

src/main/java/me/qoomon/gradle/gitversioning/GitVersioningPluginExtension.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class GitVersioningPluginExtension {
1414
public final List<VersionDescription> branches = new ArrayList<>();
1515
public final List<VersionDescription> tags = new ArrayList<>();
1616

17+
public boolean preferTags = false;
18+
1719
public GitVersioningPluginExtension(Project project) {
1820
}
1921

src/test/java/me/qoomon/gitversioning/GitVersioningTest.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ void determineVersion_forBranch() {
3030
GitVersionDetails gitVersionDetails = GitVersioning.determineVersion(repoSituation,
3131
new VersionDescription(),
3232
singletonList(new VersionDescription(null, "${branch}-branch")),
33-
emptyList());
33+
emptyList(),
34+
false);
3435

3536
String gitVersion = gitVersionDetails.getVersionTransformer().apply(currentVersion);
3637

@@ -69,7 +70,8 @@ void determineVersionWithProperties_forBranch() {
6970
GitVersionDetails gitVersionDetails = GitVersioning.determineVersion(repoSituation,
7071
new VersionDescription(),
7172
singletonList(branchVersionDescription),
72-
emptyList());
73+
emptyList(),
74+
false);
7375

7476
String gitVersion = gitVersionDetails.getVersionTransformer().apply(currentVersion);
7577
Map<String, String> gitProperties = gitVersionDetails.getPropertiesTransformer().apply(currentProperties, currentVersion);
@@ -102,7 +104,8 @@ void determineVersion_forBranchWithTag() {
102104
GitVersionDetails gitVersionDetails = GitVersioning.determineVersion(repoSituation,
103105
new VersionDescription(),
104106
singletonList(new VersionDescription(null, "${branch}-branch")),
105-
emptyList());
107+
emptyList(),
108+
false);
106109

107110
String gitVersion = gitVersionDetails.getVersionTransformer().apply(currentVersion);
108111

@@ -128,7 +131,8 @@ void determineVersion_detachedHead() {
128131
GitVersionDetails gitVersionDetails = GitVersioning.determineVersion(repoSituation,
129132
new VersionDescription(null, "${commit}-commit"),
130133
emptyList(),
131-
emptyList());
134+
emptyList(),
135+
false);
132136

133137
String gitVersion = gitVersionDetails.getVersionTransformer().apply(currentVersion);
134138

@@ -155,7 +159,8 @@ void determineVersion_detachedHeadWithTag() {
155159
GitVersionDetails gitVersionDetails = GitVersioning.determineVersion(repoSituation,
156160
new VersionDescription(),
157161
emptyList(),
158-
singletonList(new VersionDescription("v.*", "${tag}-tag")));
162+
singletonList(new VersionDescription("v.*", "${tag}-tag")),
163+
false);
159164

160165
String gitVersion = gitVersionDetails.getVersionTransformer().apply(currentVersion);
161166

@@ -184,7 +189,8 @@ void determineVersion_forBranchWithTimestamp() {
184189
GitVersionDetails gitVersionDetails = GitVersioning.determineVersion(repoSituation,
185190
new VersionDescription(),
186191
singletonList(new VersionDescription(null, "${commit.timestamp}-branch")),
187-
emptyList());
192+
emptyList(),
193+
false);
188194

189195
String gitVersion = gitVersionDetails.getVersionTransformer().apply(currentVersion);
190196

@@ -213,7 +219,8 @@ void determineVersion_forBranchWithDateTime() {
213219
GitVersionDetails gitVersionDetails = GitVersioning.determineVersion(repoSituation,
214220
new VersionDescription(),
215221
singletonList(new VersionDescription(null, "${commit.timestamp.datetime}-branch")),
216-
emptyList());
222+
emptyList(),
223+
false);
217224

218225
String gitVersion = gitVersionDetails.getVersionTransformer().apply(currentVersion);
219226

src/test/resources/testProjects/multiModuleProject/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ gitVersioning {
1616
versionFormat = '${version}_${user}_${feature}'
1717
}
1818

19-
19+
tag {
20+
pattern = 'version/(.*)'
21+
versionFormat = '${1}'
22+
}
23+
2024
}
2125

2226
allprojects {

0 commit comments

Comments
 (0)