Skip to content

Commit 00a5a3b

Browse files
committed
Recover with an empty GitVersion in non-strict mode
1 parent 6c06e4f commit 00a5a3b

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,14 @@ public GitVersion build() {
138138
if (this.config == null)
139139
this.config = GitVersionConfig.parse(new File(this.root, ".gitversion"));
140140

141-
return new GitVersionImpl(this.gitDir, this.root, this.project, this.config, this.strict);
141+
try {
142+
return new GitVersionImpl(this.gitDir, this.root, this.project, this.config, this.strict);
143+
} catch (GitVersionException e) {
144+
if (!this.strict)
145+
return GitVersionImpl.emptyFor(this.project);
146+
147+
throw e;
148+
}
142149
}
143150
}
144151

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

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.util.Collections;
2727
import java.util.List;
2828

29-
public final class GitVersionImpl implements GitVersion {
29+
public sealed class GitVersionImpl implements GitVersion permits GitVersionImpl.Empty {
3030
// Git
3131
private final boolean strict;
3232
private Git git;
@@ -53,11 +53,11 @@ public GitVersionImpl(File gitDir, File root, File project, GitVersionConfig con
5353
this.gitDir = gitDir;
5454
this.root = root;
5555
if (!this.gitDir.exists())
56-
throw new GitVersionExceptionInternal("Root directory is not a git repository!");
56+
throw new GitVersionExceptionInternal("Root directory is not a git repository");
5757

5858
this.project = project;
5959
if (this.project.compareTo(this.root) < 0)
60-
throw new IllegalArgumentException("Project directory must be (a subdirectory of) the root directory!");
60+
throw new IllegalArgumentException("Project directory must be (a subdirectory of) the root directory");
6161

6262
try {
6363
config.validate(this.root);
@@ -297,4 +297,77 @@ public void close() {
297297
this.git.close();
298298
this.git = null;
299299
}
300+
301+
302+
/* EMPTY */
303+
304+
public static GitVersion emptyFor(File project) {
305+
return new Empty(project);
306+
}
307+
308+
private GitVersionImpl() {
309+
this.strict = false;
310+
this.gitDir = null;
311+
this.root = null;
312+
this.project = null;
313+
this.localPath = "";
314+
this.tagPrefix = "";
315+
this.filters = new String[0];
316+
this.subprojects = Collections.emptyList();
317+
this.filtersView = Collections.emptyList();
318+
}
319+
320+
private static final class Empty extends GitVersionImpl {
321+
private final File project;
322+
323+
public Empty(File project) {
324+
super();
325+
this.project = project;
326+
}
327+
328+
@Override
329+
public String generateChangelog(@Nullable String start, @Nullable String url, boolean plainText) throws GitVersionException {
330+
throw new GitVersionExceptionInternal("Cannot generate a changelog without a repository");
331+
}
332+
333+
@Override
334+
public GitVersion.Info getInfo() {
335+
return GitVersionImpl.Info.EMPTY;
336+
}
337+
338+
@Override
339+
public String getTagPrefix() {
340+
throw new GitVersionExceptionInternal("Cannot get tag prefix from an empty repository");
341+
}
342+
343+
@Override
344+
public @UnmodifiableView Collection<String> getFilters() {
345+
throw new GitVersionExceptionInternal("Cannot get filters from an empty repository");
346+
}
347+
348+
@Override
349+
public File getGitDir() {
350+
throw new GitVersionExceptionInternal("Cannot get git directory from an empty repository");
351+
}
352+
353+
@Override
354+
public File getRoot() {
355+
throw new GitVersionExceptionInternal("Cannot get root directory from an empty repository");
356+
}
357+
358+
@Override
359+
public File getProject() {
360+
return this.project;
361+
}
362+
363+
@Override
364+
public String getProjectPath() {
365+
throw new GitVersionExceptionInternal("Cannot get project path from root with an empty repository");
366+
}
367+
368+
@Override
369+
public @UnmodifiableView Collection<File> getSubprojects() {
370+
throw new GitVersionExceptionInternal("Cannot get subprojects from an empty repository");
371+
}
372+
}
300373
}

0 commit comments

Comments
 (0)