2121import java .io .File ;
2222import java .io .IOException ;
2323import java .util .ArrayList ;
24+ import java .util .Arrays ;
2425import java .util .Collection ;
2526import java .util .Collections ;
27+ import java .util .HashSet ;
2628import java .util .List ;
2729import java .util .Objects ;
2830import java .util .Set ;
31+ import java .util .function .Function ;
2932import java .util .function .Predicate ;
3033
3134public final class GitVersionImpl implements GitVersionInternal {
@@ -44,10 +47,20 @@ public final class GitVersionImpl implements GitVersionInternal {
4447
4548 // Config
4649 // NOTE: These are not calculated lazily because they are used in both info gen and changelog gen
50+ private final List <File > includes ;
51+ private final List <File > excludes ;
4752 private final String tagPrefix ;
4853 private final List <String > filters ;
4954 private final List <File > subprojects ;
5055
56+ // Internal Config
57+ private final List <String > includesPaths ;
58+ private final List <String > excludesPaths ;
59+ private final List <String > subprojectPaths ;
60+
61+ private final Set <String > allIncludingPaths ;
62+ private final Set <String > allExcludingPaths ;
63+
5164 public GitVersionImpl (File gitDir , File root , File project , GitVersionConfig config , boolean strict ) {
5265 this .strict = strict ;
5366
@@ -69,9 +82,20 @@ public GitVersionImpl(File gitDir, File root, File project, GitVersionConfig con
6982 this .localPath = GitVersionInternal .super .getProjectPath ();
7083 var projectConfig = Objects .requireNonNull (config .getProject (this .localPath ));
7184
85+ this .includesPaths = makePaths (this .includes = parsePaths (Arrays .asList (projectConfig .getIncludePaths ())));
86+ this .excludesPaths = makePaths (this .excludes = parsePaths (Arrays .asList (projectConfig .getExcludePaths ())));
7287 this .tagPrefix = this .makeTagPrefix (projectConfig .getTagPrefix ());
7388 this .filters = this .makeFilters (projectConfig .getFilters ());
74- this .subprojects = this .makeSubprojects (config .getAllProjects ());
89+ this .subprojectPaths = makePaths (this .subprojects = parsePaths (config .getAllProjects (), GitVersionConfig .Project ::getPath ));
90+
91+ var allIncludingPaths = new HashSet <>(includesPaths );
92+ if (!this .localPath .isEmpty ())
93+ allIncludingPaths .add (this .localPath );
94+ this .allIncludingPaths = allIncludingPaths ;
95+
96+ var allExcludingPaths = new HashSet <>(excludesPaths );
97+ allExcludingPaths .addAll (this .subprojectPaths );
98+ this .allExcludingPaths = allExcludingPaths ;
7599 }
76100
77101
@@ -245,18 +269,16 @@ public String getProjectPath() {
245269 return this .localPath ;
246270 }
247271
248-
249- /* SUBPROJECTS */
250-
251- @ Override
252- public @ Unmodifiable Collection <File > getSubprojects () {
253- return this .subprojects ;
272+ private @ Unmodifiable List <File > parsePaths (Collection <String > paths ) {
273+ return parsePaths (paths , Function .identity ());
254274 }
255275
256- private @ Unmodifiable List <File > makeSubprojects (Collection <GitVersionConfig .Project > projects ) {
257- var ret = new ArrayList <File >(projects .size ());
258- for (var project : projects ) {
259- var file = new File (this .root , project .getPath ()).getAbsoluteFile ();
276+ private @ Unmodifiable <T > List <File > parsePaths (Collection <T > paths , Function <T , String > mapper ) {
277+ var ret = new ArrayList <File >(paths .size ());
278+ for (var o : paths ) {
279+ var p = mapper .apply (o );
280+
281+ var file = new File (this .root , p ).getAbsoluteFile ();
260282 var path = GitUtils .getRelativePath (this .project , file );
261283 if (path .startsWith ("../" ) || path .equals (".." )) continue ;
262284
@@ -265,34 +287,63 @@ public String getProjectPath() {
265287 return Collections .unmodifiableList (ret );
266288 }
267289
290+ private @ Unmodifiable List <String > makePaths (Collection <? extends File > files ) {
291+ var ret = new ArrayList <String >(files .size ());
292+ for (var file : files ) {
293+ var path = GitUtils .getRelativePath (this .getRoot (), file );
294+ if (path .isBlank ()) continue ;
295+
296+ ret .add (path );
297+ }
298+ return Collections .unmodifiableList (ret );
299+ }
300+
301+
302+ /* MANUAL PATHS */
303+
304+ @ Override
305+ public @ Unmodifiable Collection <File > getIncludes () {
306+ return this .includes ;
307+ }
308+
309+ @ Override
310+ public @ Unmodifiable Collection <File > getExcludes () {
311+ return this .excludes ;
312+ }
313+
314+ @ Override
315+ public @ Unmodifiable Collection <String > getIncludesPaths () {
316+ return this .includesPaths ;
317+ }
318+
319+ @ Override
320+ public @ Unmodifiable Collection <String > getExcludesPaths () {
321+ return this .excludesPaths ;
322+ }
323+
324+
325+ /* SUBPROJECTS */
326+
327+ @ Override
328+ public @ Unmodifiable Collection <File > getSubprojects () {
329+ return this .subprojects ;
330+ }
331+
268332 /** The default implementation of {@link CommitCountProvider}, ignoring subprojects */
269333 private int getSubprojectCommitCount (Git git , String tag ) {
270- var excludePaths = this .getSubprojectPaths ();
271- if (this .localPath .isEmpty () && excludePaths .isEmpty ()) return -1 ;
334+ if (this .localPath .isEmpty () && this .allExcludingPaths .isEmpty ()) return -1 ;
272335
273- var includePaths = !this .localPath .isEmpty () ? Collections .singleton (this .localPath ) : Set .<String >of ();
274336 try {
275- int count = GitUtils .countCommits (git , tag , this .tagPrefix , includePaths , excludePaths );
337+ int count = GitUtils .countCommits (git , tag , this .tagPrefix , this . allIncludingPaths , this . allExcludingPaths );
276338 return Math .max (count , 0 );
277339 } catch (GitAPIException | IOException e ) {
278- throw new GitVersionExceptionInternal ("Failed to count commits with the following parameters: Tag %s, Include Paths [%s], Exclude Paths [%s]" .formatted (tag , String .join (", " , includePaths ), String .join (", " , excludePaths )));
340+ throw new GitVersionExceptionInternal ("Failed to count commits with the following parameters: Tag %s, Include Paths [%s], Exclude Paths [%s]" .formatted (tag , String .join (", " , this . allIncludingPaths ), String .join (", " , this . allExcludingPaths )));
279341 }
280342 }
281343
282- private final Lazy <List <String >> subprojectPathsFromRoot = Lazy .of (() -> this .makeSubprojectPaths (true ));
283- private final Lazy <List <String >> subprojectPathsFromProject = Lazy .of (() -> this .makeSubprojectPaths (false ));
284-
285- private List <String > makeSubprojectPaths (boolean fromRoot ) {
286- return this .getSubprojects ()
287- .stream ()
288- .map (dir -> GitUtils .getRelativePath (fromRoot ? this .getRoot () : this .getProject (), dir ))
289- .filter (Predicate .not (String ::isBlank ))
290- .toList ();
291- }
292-
293344 @ Override
294- public Collection <String > getSubprojectPaths (boolean fromRoot ) {
295- return fromRoot ? this .subprojectPathsFromRoot . get () : this . subprojectPathsFromProject . get () ;
345+ public @ Unmodifiable Collection <String > getSubprojectPaths () {
346+ return this .subprojectPaths ;
296347 }
297348
298349
@@ -371,13 +422,31 @@ public File getProject() {
371422 throw new GitVersionExceptionInternal ("Cannot get project directory without a project" );
372423 }
373424
425+ @ Override public @ Unmodifiable Collection <File > getIncludes () {
426+ throw new GitVersionExceptionInternal ("Cannot get include paths from an empty repository" );
427+ }
428+
429+ @ Override public @ Unmodifiable Collection <File > getExcludes () {
430+ throw new GitVersionExceptionInternal ("Cannot get exclude paths from an empty repository" );
431+ }
432+
433+ @ Override
434+ public @ Unmodifiable Collection <String > getIncludesPaths () {
435+ throw new GitVersionExceptionInternal ("Cannot get include paths from an empty repository" );
436+ }
437+
438+ @ Override
439+ public @ Unmodifiable Collection <String > getExcludesPaths () {
440+ throw new GitVersionExceptionInternal ("Cannot get exclude paths from an empty repository" );
441+ }
442+
374443 @ Override
375444 public @ Unmodifiable Collection <File > getSubprojects () {
376445 throw new GitVersionExceptionInternal ("Cannot get subprojects from an empty repository" );
377446 }
378447
379448 @ Override
380- public Collection <String > getSubprojectPaths (boolean fromRoot ) {
449+ public Collection <String > getSubprojectPaths () {
381450 throw new GitVersionExceptionInternal ("Cannot get subprojects from an empty repository" );
382451 }
383452
0 commit comments