@@ -135,6 +135,11 @@ private HistoryGuru() {
135135 repositoryLookup = RepositoryLookup .cached ();
136136 }
137137
138+ @ VisibleForTesting
139+ HistoryCache getHistoryCache () {
140+ return historyCache ;
141+ }
142+
138143 /**
139144 * Set annotation cache to its default implementation.
140145 * @return {@link AnnotationCache} instance or {@code null} on error
@@ -164,18 +169,16 @@ static AnnotationCache initializeAnnotationCache() {
164169 * @return {@link HistoryCache} instance
165170 */
166171 private HistoryCache initializeHistoryCache () {
167- HistoryCache historyCacheResult = null ;
168- if (env .useHistoryCache ()) {
169- historyCacheResult = new FileHistoryCache ();
172+ HistoryCache historyCacheResult = new FileHistoryCache ();
170173
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 ;
178180 }
181+
179182 return historyCacheResult ;
180183 }
181184
@@ -188,13 +191,24 @@ public static HistoryGuru getInstance() {
188191 return INSTANCE ;
189192 }
190193
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 ();
198212 }
199213
200214 /**
@@ -418,7 +432,7 @@ boolean isRepoHistoryEligible(Repository repo, File file, boolean ui) {
418432 private History getHistoryFromCache (File file , Repository repository , boolean withFiles )
419433 throws CacheException {
420434
421- if (useHistoryCache () && historyCache . supportsRepository ( repository )) {
435+ if (useHistoryCache (repository )) {
422436 return historyCache .get (file , repository , withFiles );
423437 }
424438
@@ -428,7 +442,7 @@ private History getHistoryFromCache(File file, Repository repository, boolean wi
428442 @ Nullable
429443 private HistoryEntry getLastHistoryEntryFromCache (File file , Repository repository ) throws CacheException {
430444
431- if (useHistoryCache () && historyCache . supportsRepository ( repository )) {
445+ if (useHistoryCache (repository )) {
432446 return historyCache .getLastHistoryEntry (file );
433447 }
434448
@@ -451,6 +465,9 @@ public HistoryEntry getLastHistoryEntry(File file, boolean ui, boolean fallback)
451465 launderLog (file .toString ())));
452466 final File dir = file .isDirectory () ? file : file .getParentFile ();
453467 final Repository repository = getRepository (dir );
468+ if (repository == null ) {
469+ return null ;
470+ }
454471 final String meterName = "history.entry.latest" ;
455472
456473 try {
@@ -479,7 +496,7 @@ public HistoryEntry getLastHistoryEntry(File file, boolean ui, boolean fallback)
479496 if (!isRepoHistoryEligible (repository , file , ui )) {
480497 statistics .report (LOGGER , Level .FINEST ,
481498 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 );
483500 return null ;
484501 }
485502
@@ -691,7 +708,7 @@ private static boolean repositoryHasHistory(File file, Repository repo) {
691708 * @return if there is history cache entry for the file
692709 */
693710 public boolean hasHistoryCacheForFile (File file ) {
694- if (!useHistoryCache ()) {
711+ if (!useHistoryCache (file )) {
695712 LOGGER .finest (() -> String .format ("history cache is off for '%s' to check history cache presence" ,
696713 launderLog (file .toString ())));
697714 return false ;
@@ -823,15 +840,15 @@ public boolean fillLastHistoryEntries(File directory, List<DirectoryEntry> entri
823840 return true ;
824841 }
825842
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" ,
828846 launderLog (directory .toString ())));
829847 return true ;
830848 }
831849
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" ,
835852 launderLog (directory .toString ())));
836853 return true ;
837854 }
@@ -1036,9 +1053,17 @@ public void storeHistory(File file, History history) {
10361053 }
10371054
10381055 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+
10391063 if (!repository .isHistoryEnabled ()) {
10401064 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 );
10421067 return ;
10431068 }
10441069
@@ -1139,9 +1164,14 @@ private Map<Repository, Optional<Exception>> createHistoryCacheReal(Collection<R
11391164 * @return map of repository to optional exception
11401165 */
11411166 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 )) {
11431172 return Collections .emptyMap ();
11441173 }
1174+
11451175 return createHistoryCacheReal (getReposFromString (repositories ));
11461176 }
11471177
@@ -1151,12 +1181,12 @@ public Map<Repository, Optional<Exception>> createHistoryCache(Collection<String
11511181 * @param removeHistory whether to remove history cache entry for the path
11521182 */
11531183 public void clearHistoryCacheFile (String path , boolean removeHistory ) {
1154- if (!useHistoryCache ()) {
1184+ Repository repository = getRepository (new File (env .getSourceRootFile (), path ));
1185+ if (repository == null ) {
11551186 return ;
11561187 }
11571188
1158- Repository repository = getRepository (new File (env .getSourceRootFile (), path ));
1159- if (repository == null ) {
1189+ if (!useHistoryCache (repository )) {
11601190 return ;
11611191 }
11621192
@@ -1230,7 +1260,7 @@ public void clearAnnotationCacheFile(String path) {
12301260 * @return list of repository names
12311261 */
12321262 public List <String > removeHistoryCache (Collection <RepositoryInfo > repositories ) {
1233- if (! useHistoryCache ( )) {
1263+ if (repositories . stream (). noneMatch ( RepositoryInfo :: isHistoryCacheEnabled )) {
12341264 return List .of ();
12351265 }
12361266
@@ -1258,7 +1288,7 @@ public List<String> removeAnnotationCache(Collection<RepositoryInfo> repositorie
12581288 * @return map of repository to optional exception
12591289 */
12601290 public Map <Repository , Optional <Exception >> createHistoryCache () {
1261- if (! useHistoryCache ( )) {
1291+ if (repositories . values (). stream (). noneMatch ( RepositoryInfo :: isHistoryCacheEnabled )) {
12621292 return Collections .emptyMap ();
12631293 }
12641294
0 commit comments