@@ -164,18 +164,16 @@ static AnnotationCache initializeAnnotationCache() {
164164 * @return {@link HistoryCache} instance
165165 */
166166 private HistoryCache initializeHistoryCache () {
167- HistoryCache historyCacheResult = null ;
168- if (env .useHistoryCache ()) {
169- historyCacheResult = new FileHistoryCache ();
167+ HistoryCache historyCacheResult = new FileHistoryCache ();
170168
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- }
169+ try {
170+ historyCacheResult .initialize ();
171+ } catch (CacheException he ) {
172+ LOGGER .log (Level .WARNING , "Failed to initialize the history cache" , he );
173+ // Failed to initialize, run without a history cache.
174+ historyCacheResult = null ;
178175 }
176+
179177 return historyCacheResult ;
180178 }
181179
@@ -188,13 +186,26 @@ public static HistoryGuru getInstance() {
188186 return INSTANCE ;
189187 }
190188
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 ;
189+ private boolean useHistoryCache (File file ) {
190+ if (historyCache == null ) {
191+ return false ;
192+ }
193+
194+ // file = new File(env.getSourceRootPath(), file.getPath());
195+
196+ return useHistoryCache (getRepository (file ));
197+ }
198+
199+ private boolean useHistoryCache (@ Nullable Repository repository ) {
200+ if (historyCache == null || repository == null ) {
201+ return false ;
202+ }
203+
204+ if (!historyCache .supportsRepository (repository )) {
205+ return false ;
206+ }
207+
208+ return repository .isHistoryCacheEnabled ();
198209 }
199210
200211 /**
@@ -418,7 +429,7 @@ boolean isRepoHistoryEligible(Repository repo, File file, boolean ui) {
418429 private History getHistoryFromCache (File file , Repository repository , boolean withFiles )
419430 throws CacheException {
420431
421- if (useHistoryCache () && historyCache . supportsRepository ( repository )) {
432+ if (useHistoryCache (repository )) {
422433 return historyCache .get (file , repository , withFiles );
423434 }
424435
@@ -428,7 +439,7 @@ private History getHistoryFromCache(File file, Repository repository, boolean wi
428439 @ Nullable
429440 private HistoryEntry getLastHistoryEntryFromCache (File file , Repository repository ) throws CacheException {
430441
431- if (useHistoryCache () && historyCache . supportsRepository ( repository )) {
442+ if (useHistoryCache (repository )) {
432443 return historyCache .getLastHistoryEntry (file );
433444 }
434445
@@ -451,6 +462,9 @@ public HistoryEntry getLastHistoryEntry(File file, boolean ui, boolean fallback)
451462 launderLog (file .toString ())));
452463 final File dir = file .isDirectory () ? file : file .getParentFile ();
453464 final Repository repository = getRepository (dir );
465+ if (repository == null ) {
466+ return null ;
467+ }
454468 final String meterName = "history.entry.latest" ;
455469
456470 try {
@@ -479,7 +493,7 @@ public HistoryEntry getLastHistoryEntry(File file, boolean ui, boolean fallback)
479493 if (!isRepoHistoryEligible (repository , file , ui )) {
480494 statistics .report (LOGGER , Level .FINEST ,
481495 String .format ("cannot retrieve the last history entry for ''%s'' in %s because of settings" ,
482- launderLog (file .toString ()), repository ), meterName );
496+ launderLog (file .toString ()), repository ), meterName );
483497 return null ;
484498 }
485499
@@ -691,7 +705,7 @@ private static boolean repositoryHasHistory(File file, Repository repo) {
691705 * @return if there is history cache entry for the file
692706 */
693707 public boolean hasHistoryCacheForFile (File file ) {
694- if (!useHistoryCache ()) {
708+ if (!useHistoryCache (file )) {
695709 LOGGER .finest (() -> String .format ("history cache is off for '%s' to check history cache presence" ,
696710 launderLog (file .toString ())));
697711 return false ;
@@ -823,15 +837,15 @@ public boolean fillLastHistoryEntries(File directory, List<DirectoryEntry> entri
823837 return true ;
824838 }
825839
826- if (!useHistoryCache ()) {
827- LOGGER .finest (() -> String .format ("history cache is disabled for '%s' to retrieve last modified times" ,
840+ Repository repository = getRepository (directory );
841+ if (repository == null ) {
842+ LOGGER .finest (() -> String .format ("cannot find repository for '%s' to retrieve last modified times" ,
828843 launderLog (directory .toString ())));
829844 return true ;
830845 }
831846
832- Repository repository = getRepository (directory );
833- if (repository == null ) {
834- LOGGER .finest (() -> String .format ("cannot find repository for '%s' to retrieve last modified times" ,
847+ if (!useHistoryCache (repository )) {
848+ LOGGER .finest (() -> String .format ("history cache is disabled for '%s' to retrieve last modified times" ,
835849 launderLog (directory .toString ())));
836850 return true ;
837851 }
@@ -1139,9 +1153,14 @@ private Map<Repository, Optional<Exception>> createHistoryCacheReal(Collection<R
11391153 * @return map of repository to optional exception
11401154 */
11411155 public Map <Repository , Optional <Exception >> createHistoryCache (Collection <String > repositories ) {
1142- if (!useHistoryCache ()) {
1156+ if (repositories .stream ().
1157+ map (e -> new File (env .getSourceRootPath (), e )).
1158+ map (this ::getRepository ).
1159+ filter (Objects ::nonNull ).
1160+ noneMatch (RepositoryInfo ::isHistoryCacheEnabled )) {
11431161 return Collections .emptyMap ();
11441162 }
1163+
11451164 return createHistoryCacheReal (getReposFromString (repositories ));
11461165 }
11471166
@@ -1151,12 +1170,12 @@ public Map<Repository, Optional<Exception>> createHistoryCache(Collection<String
11511170 * @param removeHistory whether to remove history cache entry for the path
11521171 */
11531172 public void clearHistoryCacheFile (String path , boolean removeHistory ) {
1154- if (!useHistoryCache ()) {
1173+ Repository repository = getRepository (new File (env .getSourceRootFile (), path ));
1174+ if (repository == null ) {
11551175 return ;
11561176 }
11571177
1158- Repository repository = getRepository (new File (env .getSourceRootFile (), path ));
1159- if (repository == null ) {
1178+ if (!useHistoryCache (repository )) {
11601179 return ;
11611180 }
11621181
@@ -1230,7 +1249,7 @@ public void clearAnnotationCacheFile(String path) {
12301249 * @return list of repository names
12311250 */
12321251 public List <String > removeHistoryCache (Collection <RepositoryInfo > repositories ) {
1233- if (! useHistoryCache ( )) {
1252+ if (repositories . stream (). noneMatch ( RepositoryInfo :: isHistoryCacheEnabled )) {
12341253 return List .of ();
12351254 }
12361255
@@ -1258,7 +1277,7 @@ public List<String> removeAnnotationCache(Collection<RepositoryInfo> repositorie
12581277 * @return map of repository to optional exception
12591278 */
12601279 public Map <Repository , Optional <Exception >> createHistoryCache () {
1261- if (! useHistoryCache ( )) {
1280+ if (repositories . values (). stream (). noneMatch ( RepositoryInfo :: isHistoryCacheEnabled )) {
12621281 return Collections .emptyMap ();
12631282 }
12641283
0 commit comments