Skip to content

Commit 8000247

Browse files
committed
Separate project URL from GitVersion.Info
Allows getting the URL of the Git Version without needing to rely on other aspects of the Git Version succeeding.
1 parent dc328d9 commit 8000247

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/main/java/net/minecraftforge/gitver/api/GitVersion.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ default String getMCTagOffsetBranch(String mcVersion, Collection<String> allowed
267267
*/
268268
Info getInfo() throws GitVersionException;
269269

270+
/**
271+
* Gets the URL of the Git repository. This is used to generate the changelog, and is exposed here to allow access
272+
* to it for other purposes, such as setting links in a Maven POM.
273+
*
274+
* @return The URL of the Git repository, or {@code null} if there is none
275+
*/
276+
@Nullable String getUrl();
277+
270278
/**
271279
* Represents information about a git repository. This can be used to access other information when the standard
272280
* versioning methods in {@link GitVersion} do not suffice.
@@ -321,6 +329,7 @@ default String getBranch(boolean versionFriendly) {
321329
String getAbbreviatedId();
322330

323331
/** @return The path URL, or {@code null} if there is none */
332+
@Deprecated(forRemoval = true, since = "0.2")
324333
@Nullable String getUrl();
325334
}
326335

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public sealed class GitVersionImpl implements GitVersion permits GitVersionImpl.
3131
private final boolean strict;
3232
private Git git;
3333
private final Lazy<Info> info = Lazy.of(() -> this.calculateInfo(this::getSubprojectCommitCount));
34+
private final Lazy<String> url = Lazy.of(this::calculateUrl);
3435
private boolean closed = false;
3536

3637
// Filesystem
@@ -113,6 +114,11 @@ public GitVersion.Info getInfo() {
113114
return this.info.get();
114115
}
115116

117+
@Override
118+
public @Nullable String getUrl() {
119+
return this.url.get();
120+
}
121+
116122
/** @see #info */
117123
private Info calculateInfo(CommitCountProvider commitCountProvider) {
118124
try {
@@ -173,15 +179,26 @@ private Info calculateInfo(CommitCountProvider commitCountProvider) {
173179
}
174180
}
175181

182+
/** @see #url */
183+
private @Nullable String calculateUrl() {
184+
try {
185+
this.open();
186+
187+
return GitUtils.buildProjectUrl(this.git);
188+
} catch (Exception e) {
189+
return null;
190+
}
191+
}
192+
176193
/** @see GitVersion.Info */
177194
public record Info(
178-
@Override String getTag,
179-
@Override String getOffset,
180-
@Override String getHash,
181-
@Override String getBranch,
182-
@Override String getCommit,
183-
@Override String getAbbreviatedId,
184-
@Override @Nullable String getUrl
195+
String getTag,
196+
String getOffset,
197+
String getHash,
198+
String getBranch,
199+
String getCommit,
200+
String getAbbreviatedId,
201+
@Deprecated(forRemoval = true, since = "0.2") @Nullable String getUrl
185202
) implements GitVersion.Info {
186203
private static final Info EMPTY = new Info("0.0", "0", "00000000", "master", "0000000000000000000000", "00000000", null);
187204

@@ -196,7 +213,7 @@ private static final class Builder {
196213
private String branch;
197214
private String commit;
198215
private String abbreviatedId;
199-
private String url;
216+
@Deprecated(forRemoval = true, since = "0.2") private String url;
200217

201218
private Info build() {
202219
return new Info(this.tag, this.offset, this.hash, this.branch, this.commit, this.abbreviatedId, this.url);
@@ -355,6 +372,11 @@ public GitVersion.Info getInfo() {
355372
return GitVersionImpl.Info.EMPTY;
356373
}
357374

375+
@Override
376+
public @Nullable String getUrl() {
377+
return null;
378+
}
379+
358380
@Override
359381
public String getTagPrefix() {
360382
throw new GitVersionExceptionInternal("Cannot get tag prefix from an empty repository");

0 commit comments

Comments
 (0)