26
26
import java .util .Collections ;
27
27
import java .util .List ;
28
28
29
- public final class GitVersionImpl implements GitVersion {
29
+ public sealed class GitVersionImpl implements GitVersion permits GitVersionImpl . Empty {
30
30
// Git
31
31
private final boolean strict ;
32
32
private Git git ;
@@ -53,11 +53,11 @@ public GitVersionImpl(File gitDir, File root, File project, GitVersionConfig con
53
53
this .gitDir = gitDir ;
54
54
this .root = root ;
55
55
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" );
57
57
58
58
this .project = project ;
59
59
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" );
61
61
62
62
try {
63
63
config .validate (this .root );
@@ -297,4 +297,77 @@ public void close() {
297
297
this .git .close ();
298
298
this .git = null ;
299
299
}
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
+ }
300
373
}
0 commit comments