@@ -135,6 +135,11 @@ private HistoryGuru() {
135
135
repositoryLookup = RepositoryLookup .cached ();
136
136
}
137
137
138
+ @ VisibleForTesting
139
+ HistoryCache getHistoryCache () {
140
+ return historyCache ;
141
+ }
142
+
138
143
/**
139
144
* Set annotation cache to its default implementation.
140
145
* @return {@link AnnotationCache} instance or {@code null} on error
@@ -164,18 +169,16 @@ static AnnotationCache initializeAnnotationCache() {
164
169
* @return {@link HistoryCache} instance
165
170
*/
166
171
private HistoryCache initializeHistoryCache () {
167
- HistoryCache historyCacheResult = null ;
168
- if (env .useHistoryCache ()) {
169
- historyCacheResult = new FileHistoryCache ();
172
+ HistoryCache historyCacheResult = new FileHistoryCache ();
170
173
171
- try {
172
- historyCacheResult .initialize ();
173
- } catch (CacheException he ) {
174
- LOGGER .log (Level .WARNING , "Failed to initialize the history cache" , he );
175
- // Failed to initialize, run without a history cache.
176
- historyCacheResult = null ;
177
- }
174
+ try {
175
+ historyCacheResult .initialize ();
176
+ } catch (CacheException he ) {
177
+ LOGGER .log (Level .WARNING , "Failed to initialize the history cache" , he );
178
+ // Failed to initialize, run without a history cache.
179
+ historyCacheResult = null ;
178
180
}
181
+
179
182
return historyCacheResult ;
180
183
}
181
184
@@ -188,13 +191,24 @@ public static HistoryGuru getInstance() {
188
191
return INSTANCE ;
189
192
}
190
193
191
- /**
192
- * Return whether cache should be used for the history log.
193
- *
194
- * @return {@code true} if the history cache has been enabled and initialized, {@code false} otherwise
195
- */
196
- private boolean useHistoryCache () {
197
- return historyCache != null ;
194
+ private boolean useHistoryCache (File file ) {
195
+ if (historyCache == null ) {
196
+ return false ;
197
+ }
198
+
199
+ return useHistoryCache (getRepository (file ));
200
+ }
201
+
202
+ private boolean useHistoryCache (@ Nullable Repository repository ) {
203
+ if (historyCache == null || repository == null ) {
204
+ return false ;
205
+ }
206
+
207
+ if (!historyCache .supportsRepository (repository )) {
208
+ return false ;
209
+ }
210
+
211
+ return repository .isHistoryCacheEnabled ();
198
212
}
199
213
200
214
/**
@@ -418,7 +432,7 @@ boolean isRepoHistoryEligible(Repository repo, File file, boolean ui) {
418
432
private History getHistoryFromCache (File file , Repository repository , boolean withFiles )
419
433
throws CacheException {
420
434
421
- if (useHistoryCache () && historyCache . supportsRepository ( repository )) {
435
+ if (useHistoryCache (repository )) {
422
436
return historyCache .get (file , repository , withFiles );
423
437
}
424
438
@@ -428,7 +442,7 @@ private History getHistoryFromCache(File file, Repository repository, boolean wi
428
442
@ Nullable
429
443
private HistoryEntry getLastHistoryEntryFromCache (File file , Repository repository ) throws CacheException {
430
444
431
- if (useHistoryCache () && historyCache . supportsRepository ( repository )) {
445
+ if (useHistoryCache (repository )) {
432
446
return historyCache .getLastHistoryEntry (file );
433
447
}
434
448
@@ -451,6 +465,9 @@ public HistoryEntry getLastHistoryEntry(File file, boolean ui, boolean fallback)
451
465
launderLog (file .toString ())));
452
466
final File dir = file .isDirectory () ? file : file .getParentFile ();
453
467
final Repository repository = getRepository (dir );
468
+ if (repository == null ) {
469
+ return null ;
470
+ }
454
471
final String meterName = "history.entry.latest" ;
455
472
456
473
try {
@@ -479,7 +496,7 @@ public HistoryEntry getLastHistoryEntry(File file, boolean ui, boolean fallback)
479
496
if (!isRepoHistoryEligible (repository , file , ui )) {
480
497
statistics .report (LOGGER , Level .FINEST ,
481
498
String .format ("cannot retrieve the last history entry for ''%s'' in %s because of settings" ,
482
- launderLog (file .toString ()), repository ), meterName );
499
+ launderLog (file .toString ()), repository ), meterName );
483
500
return null ;
484
501
}
485
502
@@ -691,7 +708,7 @@ private static boolean repositoryHasHistory(File file, Repository repo) {
691
708
* @return if there is history cache entry for the file
692
709
*/
693
710
public boolean hasHistoryCacheForFile (File file ) {
694
- if (!useHistoryCache ()) {
711
+ if (!useHistoryCache (file )) {
695
712
LOGGER .finest (() -> String .format ("history cache is off for '%s' to check history cache presence" ,
696
713
launderLog (file .toString ())));
697
714
return false ;
@@ -823,15 +840,15 @@ public boolean fillLastHistoryEntries(File directory, List<DirectoryEntry> entri
823
840
return true ;
824
841
}
825
842
826
- if (!useHistoryCache ()) {
827
- LOGGER .finest (() -> String .format ("history cache is disabled for '%s' to retrieve last modified times" ,
843
+ Repository repository = getRepository (directory );
844
+ if (repository == null ) {
845
+ LOGGER .finest (() -> String .format ("cannot find repository for '%s' to retrieve last modified times" ,
828
846
launderLog (directory .toString ())));
829
847
return true ;
830
848
}
831
849
832
- Repository repository = getRepository (directory );
833
- if (repository == null ) {
834
- LOGGER .finest (() -> String .format ("cannot find repository for '%s' to retrieve last modified times" ,
850
+ if (!useHistoryCache (repository )) {
851
+ LOGGER .finest (() -> String .format ("history cache is disabled for '%s' to retrieve last modified times" ,
835
852
launderLog (directory .toString ())));
836
853
return true ;
837
854
}
@@ -1036,9 +1053,17 @@ public void storeHistory(File file, History history) {
1036
1053
}
1037
1054
1038
1055
private void createHistoryCache (Repository repository , String sinceRevision ) throws CacheException , HistoryException {
1056
+ if (!repository .isHistoryCacheEnabled ()) {
1057
+ LOGGER .log (Level .INFO ,
1058
+ "Skipping history cache creation for {0} and its subdirectories: history cache disabled" ,
1059
+ repository );
1060
+ return ;
1061
+ }
1062
+
1039
1063
if (!repository .isHistoryEnabled ()) {
1040
1064
LOGGER .log (Level .INFO ,
1041
- "Skipping history cache creation for {0} and its subdirectories" , repository );
1065
+ "Skipping history cache creation for {0} and its subdirectories: history disabled" ,
1066
+ repository );
1042
1067
return ;
1043
1068
}
1044
1069
@@ -1139,9 +1164,14 @@ private Map<Repository, Optional<Exception>> createHistoryCacheReal(Collection<R
1139
1164
* @return map of repository to optional exception
1140
1165
*/
1141
1166
public Map <Repository , Optional <Exception >> createHistoryCache (Collection <String > repositories ) {
1142
- if (!useHistoryCache ()) {
1167
+ if (repositories .stream ().
1168
+ map (e -> new File (env .getSourceRootPath (), e )).
1169
+ map (this ::getRepository ).
1170
+ filter (Objects ::nonNull ).
1171
+ noneMatch (RepositoryInfo ::isHistoryCacheEnabled )) {
1143
1172
return Collections .emptyMap ();
1144
1173
}
1174
+
1145
1175
return createHistoryCacheReal (getReposFromString (repositories ));
1146
1176
}
1147
1177
@@ -1151,12 +1181,12 @@ public Map<Repository, Optional<Exception>> createHistoryCache(Collection<String
1151
1181
* @param removeHistory whether to remove history cache entry for the path
1152
1182
*/
1153
1183
public void clearHistoryCacheFile (String path , boolean removeHistory ) {
1154
- if (!useHistoryCache ()) {
1184
+ Repository repository = getRepository (new File (env .getSourceRootFile (), path ));
1185
+ if (repository == null ) {
1155
1186
return ;
1156
1187
}
1157
1188
1158
- Repository repository = getRepository (new File (env .getSourceRootFile (), path ));
1159
- if (repository == null ) {
1189
+ if (!useHistoryCache (repository )) {
1160
1190
return ;
1161
1191
}
1162
1192
@@ -1230,7 +1260,7 @@ public void clearAnnotationCacheFile(String path) {
1230
1260
* @return list of repository names
1231
1261
*/
1232
1262
public List <String > removeHistoryCache (Collection <RepositoryInfo > repositories ) {
1233
- if (! useHistoryCache ( )) {
1263
+ if (repositories . stream (). noneMatch ( RepositoryInfo :: isHistoryCacheEnabled )) {
1234
1264
return List .of ();
1235
1265
}
1236
1266
@@ -1258,7 +1288,7 @@ public List<String> removeAnnotationCache(Collection<RepositoryInfo> repositorie
1258
1288
* @return map of repository to optional exception
1259
1289
*/
1260
1290
public Map <Repository , Optional <Exception >> createHistoryCache () {
1261
- if (! useHistoryCache ( )) {
1291
+ if (repositories . values (). stream (). noneMatch ( RepositoryInfo :: isHistoryCacheEnabled )) {
1262
1292
return Collections .emptyMap ();
1263
1293
}
1264
1294
0 commit comments