Skip to content

Commit 5e8ffea

Browse files
committed
feat: add git describe version placeholders
1 parent af3c6a6 commit 5e8ffea

File tree

9 files changed

+215
-151
lines changed

9 files changed

+215
-151
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Changelog
22

3-
## 6.3.0 (unreleased)
3+
## 4.3.0 (unreleased)
44
* **Features**
55
* add git describe version placeholders
6-
enable describe feature by set config `describeTagPattern` at root level or at branch/tag config level
76
* new placeholders
87
* `${describe}`
98
* `${describe.tag}`
10-
* `${describe.<TAG_PATTERN_GROUP_NAME or TAG_PATTERN_GROUP_INDEX>}`
9+
* `${describe.<TAG_PATTERN_GROUP_NAME or TAG_PATTERN_GROUP_INDEX>}` e.g. pattern `v(?<version>.*)` will create placeholder `${describe.version}`
1110
* `${describe.distance}`
1211

12+
* **BREAKING CHANGES**
13+
* no longer provide project property `git.dirty` due to performance issues on larger projects,
14+
version format placeholder `${dirty}` is still available
1315

1416
## 4.1.0
1517
* **Features**

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,14 @@ gitVersioning.apply(closureOf<GitVersioningPluginConfig> {
160160

161161
ℹ define placeholder overwrite value (placeholder is defined) like this `${name:+OVERWRITE_VALUE}`<br>
162162
e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`
163-
163+
164+
- `${version}`
165+
- `version` set in `pom.xml`
166+
- e.g. '1.0.0-SNAPSHOT'
167+
- `${version.release}`
168+
- like `${version}` without `-SNAPSHOT` postfix
169+
- e.g. '1.0.0'
170+
164171
- `${ref}`
165172
- current ref name (branch name, tag name or commit hash)
166173
- `${ref.slug}`
@@ -227,18 +234,20 @@ gitVersioning.apply(closureOf<GitVersioningPluginConfig> {
227234
versionFormat = '${1}'
228235
```
229236
230-
- `${version}`
231-
- `version` set in `pom.xml`
232-
- e.g. '1.0.0-SNAPSHOT'
233-
- `${version.release}`
234-
- like `${version}` without `-SNAPSHOT` postfix
235-
- e.g. '1.0.0'
237+
- `${describe}`
238+
- will resolve to `git describe` output
239+
- ⚠️ may lead to performance issue on very large projects
240+
- `${describe.tag}`
241+
- the matching tag of `git describe`
242+
- `${describe.distance}`
243+
- the distance count to last matching tag
236244
237245
- `${dirty}`
238-
- if repository has untracked files or uncommited changes this placeholder will resolve to `-DIRTY`, otherwise it will resolve to an empty string.
246+
- if repository has untracked files or uncommitted changes this placeholder will resolve to `-DIRTY`, otherwise it will resolve to an empty string.
247+
- ⚠️ may lead to performance issue on very large projects
239248
- `${dirty.snapshot}`
240-
- if repository has untracked files or uncommited changes this placeholder will resolve to `-SNAPSHOT`, otherwise it will resolve to an empty string.
241-
249+
- like `${dirty}`, but will resolve to `-SNAPSHOT`
250+
242251
- `${value}` - Only available within property format
243252
- value of matching property
244253
@@ -286,8 +295,6 @@ gitVersioning.apply(closureOf<GitVersioningPluginConfig> {
286295
- `git.tag.slug` like `git.tag` with all `/` replaced by `-`
287296
- `git.commit.timestamp` e.g. '1560694278'
288297
- `git.commit.timestamp.datetime` e.g. '2019-11-16T14:37:10Z'
289-
- `git.dirty` repository's dirty state indicator `true` or `false`
290-
291298
292299
## Gradle Tasks
293300

src/main/java/me/qoomon/gitversioning/commons/GitSituation.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,42 @@
99
import java.time.ZonedDateTime;
1010
import java.util.List;
1111
import java.util.Map;
12+
import java.util.function.Supplier;
1213
import java.util.regex.Pattern;
1314

1415
import static java.time.Instant.EPOCH;
1516
import static java.time.ZoneOffset.UTC;
1617
import static java.util.Collections.emptyList;
18+
import static java.util.Objects.requireNonNull;
1719
import static me.qoomon.gitversioning.commons.GitUtil.NO_COMMIT;
1820
import static org.eclipse.jgit.lib.Constants.HEAD;
1921

20-
//TODO rename to GitSituation
22+
2123
public class GitSituation {
2224

2325
private final Repository repository;
2426
private final File rootDirectory;
2527

2628
private final ObjectId head;
2729
private final String hash;
28-
private final Lazy<ZonedDateTime> timestamp = Lazy.of(this::timestamp);
29-
private Lazy<String> branch = Lazy.of(this::branch);
30+
private final Supplier<ZonedDateTime> timestamp = Lazy.by(this::timestamp);
31+
private Supplier<String> branch = Lazy.by(this::branch);
3032

31-
private final Lazy<Map<ObjectId, List<Ref>>> reverseTagRefMap = Lazy.of(this::reverseTagRefMap);
32-
private Lazy<List<String>> tags = Lazy.of(this::tags);
33+
private final Supplier<Map<ObjectId, List<Ref>>> reverseTagRefMap = Lazy.by(this::reverseTagRefMap);
34+
private Supplier<List<String>> tags = Lazy.by(this::tags);
3335

34-
private final Lazy<Boolean> clean = Lazy.of(this::clean);
36+
private final Supplier<Boolean> clean = Lazy.by(this::clean);
3537

36-
private final Pattern describeTagPattern;
37-
private final Lazy<GitDescription> description = Lazy.of(this::describe);
38+
private Pattern describeTagPattern = Pattern.compile(".*");
39+
private Supplier<GitDescription> description = Lazy.by(this::describe);
3840

39-
public GitSituation(Repository repository, Pattern describeTagPattern) throws IOException {
41+
public GitSituation(Repository repository) throws IOException {
4042
this.repository = repository;
4143
this.rootDirectory = repository.getWorkTree();
4244
this.head = repository.resolve(HEAD);
4345
this.hash = head != null ? head.getName() : NO_COMMIT;
44-
this.describeTagPattern = describeTagPattern != null ? describeTagPattern : Pattern.compile(".*");
4546
}
4647

47-
public GitSituation(Repository repository) throws IOException {
48-
this(repository, null);
49-
}
5048

5149
public File getRootDirectory() {
5250
return rootDirectory;
@@ -65,7 +63,7 @@ public String getBranch() {
6563
}
6664

6765
public void setBranch(String branch) {
68-
this.branch = Lazy.of(branch);
66+
this.branch = () -> branch;
6967
}
7068

7169
public boolean isDetached() {
@@ -77,13 +75,22 @@ public List<String> getTags() {
7775
}
7876

7977
public void setTags(List<String> tags) {
80-
this.tags = Lazy.of(tags);
78+
this.tags = () -> requireNonNull(tags);
8179
}
8280

8381
public boolean isClean() {
8482
return clean.get();
8583
}
8684

85+
public void setDescribeTagPattern(Pattern describeTagPattern) {
86+
this.describeTagPattern = requireNonNull(describeTagPattern);
87+
this.description = Lazy.by(this::describe);
88+
}
89+
90+
public Pattern getDescribeTagPattern() {
91+
return describeTagPattern;
92+
}
93+
8794
public GitDescription getDescription() {
8895
return description.get();
8996
}

src/main/java/me/qoomon/gitversioning/commons/GitUtil.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public static GitDescription describe(Repository repository, ObjectId revObjectI
6666
public static GitDescription describe(Repository repository, ObjectId revObjectId, Pattern tagPattern,
6767
Map<ObjectId, List<Ref>> reverseTagRefMap) throws IOException {
6868

69+
if(revObjectId == null) {
70+
return new GitDescription(NO_COMMIT, "root", 0);
71+
}
72+
6973
// Walk back commit ancestors looking for tagged one
7074
try (RevWalk walk = new RevWalk(repository)) {
7175
walk.setRetainBody(false);
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package me.qoomon.gitversioning.commons;
22

33

4+
import java.util.concurrent.Callable;
5+
import java.util.function.Supplier;
6+
47
import static java.util.Objects.requireNonNull;
58

6-
public final class Lazy<T> {
9+
public final class Lazy<T> implements Supplier<T> {
710

8-
private volatile Initializer<T> initializer;
9-
private T object;
11+
private volatile Callable<T> initializer;
12+
private volatile T value;
1013

11-
public Lazy(Initializer<T> initializer) {
14+
public Lazy(Callable<T> initializer) {
1215
this.initializer = requireNonNull(initializer);
1316
}
1417

@@ -17,27 +20,29 @@ public T get() {
1720
synchronized (this) {
1821
if (initializer != null) {
1922
try {
20-
object = initializer.get();
21-
initializer = null;
23+
value = initializer.call();
2224
} catch (Exception e) {
2325
throw new RuntimeException(e);
2426
}
27+
initializer = null;
2528
}
2629
}
2730
}
28-
return object;
31+
return value;
2932
}
3033

3134
public static <T> Lazy<T> of(T value) {
3235
return new Lazy<>(() -> value);
3336
}
3437

35-
public static <T> Lazy<T> of(Initializer<T> initializer) {
36-
return new Lazy<>(initializer);
38+
public static <T> Lazy<T> by(Callable<T> supplier) {
39+
return new Lazy<>(supplier);
3740
}
3841

39-
@FunctionalInterface
40-
public interface Initializer<T> {
41-
T get() throws Exception;
42+
public static <V> V get(Lazy<V> value) {
43+
if (value != null) {
44+
return value.get();
45+
}
46+
return null;
4247
}
4348
}

src/main/java/me/qoomon/gitversioning/commons/StringUtil.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
import java.util.HashSet;
55
import java.util.Map;
66
import java.util.Set;
7+
import java.util.function.Supplier;
78
import java.util.regex.Matcher;
89
import java.util.regex.Pattern;
910

1011
public final class StringUtil {
1112

12-
public static String substituteText(String text, Map<String, String> replacements) {
13+
public static String substituteText(String text, Map<String, Supplier<String>> replacements) {
1314
StringBuffer result = new StringBuffer();
1415
Pattern placeholderPattern = Pattern.compile("\\$\\{(?<key>[^}:]+)(?::(?<modifier>[-+])(?<value>[^}]*))?}");
1516
Matcher placeholderMatcher = placeholderPattern.matcher(text);
1617
while (placeholderMatcher.find()) {
1718
String placeholderKey = placeholderMatcher.group("key");
18-
String replacement = replacements.get(placeholderKey);
19+
Supplier<String> replacementSupplier = replacements.get(placeholderKey);
20+
String replacement = replacementSupplier != null ? replacementSupplier.get() : null;
1921
String placeholderModifier = placeholderMatcher.group("modifier");
2022
if(placeholderModifier != null){
2123
if (placeholderModifier.equals("-") && replacement == null) {
@@ -40,15 +42,15 @@ public static String substituteText(String text, Map<String, String> replacement
4042
* @param regex regular expression
4143
* @return a map of group-index and group-name to matching value
4244
*/
43-
public static Map<String, String> valueGroupMap(String text, String regex) {
44-
return valueGroupMap(text, Pattern.compile(regex));
45+
public static Map<String, String> patternGroupValues(String text, String regex) {
46+
return patternGroupValues(Pattern.compile(regex), text);
4547
}
4648
/**
47-
* @param text to parse
4849
* @param pattern pattern
50+
* @param text to parse
4951
* @return a map of group-index and group-name to matching value
5052
*/
51-
public static Map<String, String> valueGroupMap(String text, Pattern pattern) {
53+
public static Map<String, String> patternGroupValues(Pattern pattern, String text) {
5254
Map<String, String> result = new HashMap<>();
5355
Matcher groupMatcher = pattern.matcher(text);
5456
if (groupMatcher.find()) {
@@ -57,14 +59,14 @@ public static Map<String, String> valueGroupMap(String text, Pattern pattern) {
5759
result.put(String.valueOf(i), groupMatcher.group(i));
5860
}
5961

60-
for (String groupName : patternGroupNames(pattern)) {
62+
for (String groupName : patternGroups(pattern)) {
6163
result.put(groupName, groupMatcher.group(groupName));
6264
}
6365
}
6466
return result;
6567
}
6668

67-
private static Set<String> patternGroupNames(Pattern groupPattern) {
69+
public static Set<String> patternGroups(Pattern groupPattern) {
6870
Set<String> groupNames = new HashSet<>();
6971
Pattern groupNamePattern = Pattern.compile("\\(\\?<(?<name>[a-zA-Z][a-zA-Z0-9]*)>");
7072
Matcher groupNameMatcher = groupNamePattern.matcher(groupPattern.toString());

0 commit comments

Comments
 (0)