Skip to content

Commit 06974d4

Browse files
committed
Preliminary support for tag suffixes
Instead of adding some more convoluted and additional code to Git Version specifically to handle tags with suffixes, it instead now uses a new approach when handling tag prefix. tl;dr default filter `*-*` is now `[[:digit"]]**`. Even since before Git Version, it was established through the use of `Character#isDigit` that all tags should, in some way, include a valid number to convey the version. Prior to Git Version 0.4, when checking for tags without a tag prefix, the filter would simply check if the tag didn't have any dashes in it. Since the tag prefix should only ever remain a prefix, I've simplified this to instead check for a digit. JGit's glob filtering uses the same digit detection as `Character#isDigit`. Now, tag prefixes prepend the default filter with `TAG_PREFIX[-v]*`. This makes the filterer search for the defined tag prefix while optionally accounting for any extra dashes or v's (for legacy branches that use `v1.0` or something). So, the contract is now that a tag must always include a digit for it to be included in versioning.
1 parent ebfdcf4 commit 06974d4

File tree

2 files changed

+24
-42
lines changed

2 files changed

+24
-42
lines changed

src/main/java/net/minecraftforge/gitver/internal/GitVersionImpl.java

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Collection;
2727
import java.util.Collections;
2828
import java.util.List;
29+
import java.util.Set;
2930

3031
public sealed class GitVersionImpl implements GitVersion permits GitVersionImpl.Empty {
3132
// Git
@@ -130,10 +131,7 @@ private Info calculateInfo(CommitCountProvider commitCountProvider) {
130131
it.setLong(true);
131132

132133
try {
133-
if (!this.tagPrefix.isEmpty())
134-
it.setMatch(this.tagPrefix + "**");
135-
else
136-
it.setExclude("*-*");
134+
it.setMatch("%s[[:digit:]]**".formatted(!this.tagPrefix.isEmpty() ? this.tagPrefix + "[-v]*" : ""));
137135

138136
for (String filter : this.filters) {
139137
if (filter.startsWith("!"))
@@ -147,7 +145,7 @@ private Info calculateInfo(CommitCountProvider commitCountProvider) {
147145
}).call();
148146

149147
if (describedTag == null)
150-
throw new RefNotFoundException("Tag not found! Tag prefix: %s, Filters: %s".formatted(this.tagPrefix, String.join(", ", this.filters)));
148+
throw new RefNotFoundException("Tag not found! A valid tag must include a digit at the minimum! Tag prefix: %s, Filters: %s".formatted(this.tagPrefix, String.join(", ", this.filters)));
151149

152150
var desc = Util.rsplit(describedTag, "-", 2);
153151

@@ -159,22 +157,16 @@ private Info calculateInfo(CommitCountProvider commitCountProvider) {
159157
return target != null ? target.getName() : null;
160158
}); // matches Repository.getFullBranch() but returning null when on a detached HEAD
161159

162-
var ret = Info.builder();
163-
ret.tag = Util.make(() -> {
164-
var t = desc[0].substring(this.tagPrefix.length());
165-
return t
166-
.substring(t.startsWith("-v") && t.length() > 2 ? 2 : 0)
167-
.substring((t.indexOf('v') == 0 || t.indexOf('-') == 0) && t.length() > 1 && Character.isDigit(t.charAt(1)) ? 1 : 0);
168-
});
169-
170-
ret.offset = commitCountProvider.getAsString(this.git, desc[0], desc[1], this.strict);
171-
ret.hash = desc[2];
172-
if (longBranch != null) ret.branch = Repository.shortenRefName(longBranch);
173-
ret.commit = ObjectId.toString(head.getObjectId());
174-
ret.abbreviatedId = head.getObjectId().abbreviate(8).name();
175-
ret.url = GitUtils.buildProjectUrl(this.git);
176-
177-
return ret.build();
160+
var tag = desc[0].substring(Util.indexOf(desc[0], Character::isDigit, 0));
161+
162+
var offset = commitCountProvider.getAsString(this.git, desc[0], desc[1], this.strict);
163+
var hash = desc[2];
164+
var branch = longBranch != null ? Repository.shortenRefName(longBranch) : null;
165+
var commit = ObjectId.toString(head.getObjectId());
166+
var abbreviatedId = head.getObjectId().abbreviate(8).name();
167+
var url = GitUtils.buildProjectUrl(this.git);
168+
169+
return new Info(tag, offset, hash, branch, commit, abbreviatedId, url);
178170
} catch (Exception e) {
179171
if (this.strict) throw new GitVersionExceptionInternal("Failed to calculate version info", e);
180172

@@ -204,24 +196,6 @@ public record Info(
204196
@Deprecated(forRemoval = true, since = "0.2") @Nullable String getUrl
205197
) implements GitVersion.Info {
206198
private static final Info EMPTY = new Info("0.0", "0", "00000000", "master", "0000000000000000000000", "00000000", null);
207-
208-
private static Builder builder() {
209-
return new Builder();
210-
}
211-
212-
private static final class Builder {
213-
private String tag;
214-
private String offset;
215-
private String hash;
216-
private String branch;
217-
private String commit;
218-
private String abbreviatedId;
219-
@Deprecated(forRemoval = true, since = "0.2") private String url;
220-
221-
private Info build() {
222-
return new Info(this.tag, this.offset, this.hash, this.branch, this.commit, this.abbreviatedId, this.url);
223-
}
224-
}
225199
}
226200

227201

@@ -297,13 +271,12 @@ private List<File> calculateSubprojects(Collection<GitVersionConfig.Project> pro
297271
return Collections.unmodifiableList(ret);
298272
}
299273

300-
301274
/** The default implementation of {@link CommitCountProvider}, ignoring subprojects */
302275
private int getSubprojectCommitCount(Git git, String tag) {
303276
var excludePaths = this.getSubprojectPaths();
304277
if (this.localPath.isEmpty() && excludePaths.isEmpty()) return -1;
305278

306-
var includePaths = !this.localPath.isEmpty() ? Collections.singleton(this.localPath) : Collections.<String>emptySet();
279+
var includePaths = !this.localPath.isEmpty() ? Collections.singleton(this.localPath) : Set.<String>of();
307280
try {
308281
int count = GitUtils.countCommits(git, tag, this.tagPrefix, includePaths, excludePaths);
309282
return Math.max(count, 0);

src/main/java/net/minecraftforge/gitver/internal/Util.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
package net.minecraftforge.gitver.internal;
66

7-
import org.eclipse.jgit.util.StringUtils;
87
import org.jetbrains.annotations.Nullable;
98

109
import java.util.ArrayList;
@@ -86,6 +85,16 @@ static int count(Iterable<?> iterable) {
8685
return null;
8786
}
8887

88+
@FunctionalInterface
89+
interface CharPredicate { boolean test(char c); }
90+
static int indexOf(String s, CharPredicate matcher, int orElse) {
91+
for (var c : s.toCharArray()) {
92+
if (matcher.test(c)) return s.indexOf(c);
93+
}
94+
95+
return orElse;
96+
}
97+
8998
/**
9099
* Finds the first element in an iterable that matches the given predicate.
91100
*

0 commit comments

Comments
 (0)