Skip to content

Commit 3fca07a

Browse files
committed
avoid global isHistoryEnabled() checks in favor of per repository check
fixes #4063
1 parent c76efba commit 3fca07a

File tree

16 files changed

+104
-87
lines changed

16 files changed

+104
-87
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/AnalyzerGuru.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, 2021, Chris Fraire <cfraire@me.com>.
2323
*/
2424
package org.opengrok.indexer.analysis;
@@ -596,7 +596,7 @@ public void populateDocument(Document doc, File file, String path, AbstractAnaly
596596
doc.add(new SortedDocValuesField(QueryBuilder.FULLPATH,
597597
new BytesRef(file.getAbsolutePath())));
598598

599-
if (RuntimeEnvironment.getInstance().isHistoryEnabled()) {
599+
if (HistoryGuru.getInstance().repositorySupportsHistory(file)) {
600600
populateDocumentHistory(doc, file);
601601
}
602602
doc.add(new Field(QueryBuilder.DATE, date, string_ft_stored_nanalyzed_norms));

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Configuration.java

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2007, 2025, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, 2020, Chris Fraire <cfraire@me.com>.
2323
* Portions Copyright (c) 2020, Aleksandr Kirillov <alexkirillovsamara@gmail.com>.
2424
*/
@@ -580,7 +580,7 @@ public Configuration() {
580580
setHandleHistoryOfRenamedFiles(false);
581581
setHistoryBasedReindex(true);
582582
setHistoryCache(true);
583-
setHistoryEnabled(true);
583+
setHistoryEnabled(false);
584584
setHitsPerPage(25);
585585
setIgnoredNames(new IgnoredNames());
586586
setIncludedNames(new Filter());
@@ -1610,37 +1610,4 @@ public ConfigurationException(String message) {
16101610
super(message);
16111611
}
16121612
}
1613-
1614-
/**
1615-
* Check if configuration is populated and self-consistent.
1616-
* @throws ConfigurationException on error
1617-
*/
1618-
public void checkConfiguration() throws ConfigurationException {
1619-
1620-
if (getSourceRoot() == null) {
1621-
throw new ConfigurationException("Source root is not specified.");
1622-
}
1623-
1624-
if (getDataRoot() == null) {
1625-
throw new ConfigurationException("Data root is not specified.");
1626-
}
1627-
1628-
if (!new File(getSourceRoot()).canRead()) {
1629-
throw new ConfigurationException("Source root directory '" + getSourceRoot() + "' must be readable.");
1630-
}
1631-
1632-
if (!new File(getDataRoot()).canWrite()) {
1633-
throw new ConfigurationException("Data root directory '" + getDataRoot() + "' must be writable.");
1634-
}
1635-
1636-
if (!isHistoryEnabled() && isHistoryBasedReindex()) {
1637-
LOGGER.log(Level.INFO, "History based reindex is on, however history is off. " +
1638-
"History has to be enabled for history based reindex.");
1639-
}
1640-
1641-
if (!isHistoryCache() && isHistoryBasedReindex()) {
1642-
LOGGER.log(Level.INFO, "History based reindex is on, however history cache is off. " +
1643-
"History cache has to be enabled for history based reindex.");
1644-
}
1645-
}
16461613
}

opengrok-indexer/src/main/java/org/opengrok/indexer/history/HistoryGuru.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, 2020, Chris Fraire <cfraire@me.com>.
2323
*/
2424
package org.opengrok.indexer.history;
@@ -54,7 +54,6 @@
5454
import org.opengrok.indexer.analysis.AnalyzerFactory;
5555
import org.opengrok.indexer.analysis.AnalyzerGuru;
5656
import org.opengrok.indexer.configuration.CommandTimeoutType;
57-
import org.opengrok.indexer.configuration.Configuration;
5857
import org.opengrok.indexer.configuration.Configuration.RemoteSCM;
5958
import org.opengrok.indexer.configuration.OpenGrokThreadFactory;
6059
import org.opengrok.indexer.configuration.PathAccepter;
@@ -630,6 +629,14 @@ public boolean hasHistory(File file) {
630629
}
631630
}
632631

632+
return repositorySupportsHistory(file);
633+
}
634+
635+
/**
636+
* @param file file object
637+
* @return whether related {@link Repository} and settings allow for history retrieval
638+
*/
639+
public boolean repositorySupportsHistory(File file) {
633640
Repository repo = getRepository(file);
634641
if (repo == null) {
635642
LOGGER.finest(() -> String.format("cannot find repository for '%s' to check history presence",
@@ -642,7 +649,7 @@ public boolean hasHistory(File file) {
642649
}
643650

644651
// This should return true for Annotate view.
645-
Configuration.RemoteSCM globalRemoteSupport = env.getRemoteScmSupported();
652+
RemoteSCM globalRemoteSupport = env.getRemoteScmSupported();
646653
boolean remoteSupported = ((globalRemoteSupport == RemoteSCM.ON)
647654
|| (globalRemoteSupport == RemoteSCM.UIONLY)
648655
|| (globalRemoteSupport == RemoteSCM.DIRBASED)

opengrok-indexer/src/main/java/org/opengrok/indexer/history/LatestRevisionUtil.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.history;
2424

2525
import org.apache.lucene.document.DateTools;
2626
import org.apache.lucene.document.Document;
2727
import org.jetbrains.annotations.Nullable;
2828
import org.jetbrains.annotations.VisibleForTesting;
29-
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3029
import org.opengrok.indexer.index.IndexDatabase;
3130
import org.opengrok.indexer.logger.LoggerFactory;
3231
import org.opengrok.indexer.search.QueryBuilder;
@@ -52,11 +51,11 @@ private LatestRevisionUtil() {
5251

5352
/**
5453
* @param file file object corresponding to a file under source root
55-
* @return last revision string for {@code file} or null
54+
* @return last revision string for {@code file} or {@code null}
5655
*/
5756
@Nullable
5857
public static String getLatestRevision(File file) {
59-
if (!RuntimeEnvironment.getInstance().isHistoryEnabled()) {
58+
if (!HistoryGuru.getInstance().repositorySupportsHistory(file)) {
6059
return null;
6160
}
6261

opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,6 @@ public static int runMain(String[] argv) {
209209
exitWithHelp();
210210
}
211211

212-
checkConfiguration();
213-
214212
if (awaitProfiler) {
215213
pauseToAwaitProfiler();
216214
}
@@ -278,6 +276,8 @@ public static int runMain(String[] argv) {
278276
}
279277
}
280278

279+
checkConfiguration(cfg);
280+
281281
// Set updated configuration in RuntimeEnvironment. This is called so that the tunables set
282282
// via command line options are available.
283283
env.setConfiguration(cfg, subFilePaths, CommandTimeoutType.INDEXER);
@@ -494,11 +494,44 @@ private static void checkIndexAndExit(Set<String> subFileArgs) {
494494
System.exit(0);
495495
}
496496

497+
/**
498+
* Check if configuration is populated and self-consistent.
499+
* @throws Configuration.ConfigurationException on error
500+
*/
501+
public static void checkConfigurationValues(Configuration cfg) throws Configuration.ConfigurationException {
502+
503+
if (cfg.getSourceRoot() == null) {
504+
throw new Configuration.ConfigurationException("Source root is not specified.");
505+
}
506+
507+
if (cfg.getDataRoot() == null) {
508+
throw new Configuration.ConfigurationException("Data root is not specified.");
509+
}
510+
511+
if (!new File(cfg.getSourceRoot()).canRead()) {
512+
throw new Configuration.ConfigurationException("Source root directory '" + cfg.getSourceRoot() + "' must be readable.");
513+
}
514+
515+
if (!new File(cfg.getDataRoot()).canWrite()) {
516+
throw new Configuration.ConfigurationException("Data root directory '" + cfg.getDataRoot() + "' must be writable.");
517+
}
518+
519+
if (!cfg.isHistoryEnabled() && cfg.isHistoryBasedReindex()) {
520+
LOGGER.log(Level.INFO, "History based reindex is on, however history is off. " +
521+
"History has to be enabled for history based reindex.");
522+
}
523+
524+
if (!cfg.isHistoryCache() && cfg.isHistoryBasedReindex()) {
525+
LOGGER.log(Level.INFO, "History based reindex is on, however history cache is off. " +
526+
"History cache has to be enabled for history based reindex.");
527+
}
528+
}
529+
497530
/**
498531
* This is supposed to be run after {@link #parseOptions(String[])}.
499532
* It will exit the program if there is some serious configuration (meaning {@link #cfg}) discrepancy.
500533
*/
501-
private static void checkConfiguration() {
534+
private static void checkConfiguration(Configuration cfg) {
502535
if (bareConfig && (env.getConfigURI() == null || env.getConfigURI().isEmpty())) {
503536
die("Missing webappURI setting");
504537
}
@@ -513,7 +546,7 @@ private static void checkConfiguration() {
513546
}
514547

515548
try {
516-
cfg.checkConfiguration();
549+
checkConfigurationValues(cfg);
517550
} catch (Configuration.ConfigurationException e) {
518551
die(e.getMessage());
519552
}
@@ -1026,8 +1059,6 @@ public static String[] parseOptions(String[] argv) throws ParseException {
10261059
cfg = new Configuration();
10271060
}
10281061

1029-
cfg.setHistoryEnabled(false); // force user to turn on history capture
1030-
10311062
argv = optParser.parse(argv);
10321063

10331064
return argv;

opengrok-indexer/src/test/java/org/opengrok/indexer/history/FileAnnotationCacheTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.history;
2424

@@ -61,6 +61,8 @@ void setUp() throws Exception {
6161
repositories = new TestRepository();
6262
repositories.create(getClass().getResource("/repositories"));
6363

64+
env.setHistoryEnabled(true);
65+
6466
// This needs to be set before the call to env.setRepositories() below as it instantiates HistoryGuru.
6567
env.setAnnotationCacheEnabled(true);
6668

opengrok-indexer/src/test/java/org/opengrok/indexer/history/HistoryGuruTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class HistoryGuruTest {
9090
@BeforeAll
9191
static void setUpClass() throws Exception {
9292
env = RuntimeEnvironment.getInstance();
93+
env.setHistoryEnabled(true);
9394
env.setAnnotationCacheEnabled(true);
9495
savedNestingMaximum = env.getNestingMaximum();
9596

opengrok-indexer/src/test/java/org/opengrok/indexer/history/MercurialRepositoryTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.apache.commons.lang3.tuple.Pair;
2727
import org.junit.jupiter.api.AfterEach;
28+
import org.junit.jupiter.api.BeforeAll;
2829
import org.junit.jupiter.api.BeforeEach;
2930
import org.junit.jupiter.api.Test;
3031
import org.junit.jupiter.params.ParameterizedTest;
@@ -108,6 +109,12 @@ private void setUpTestRepository() throws IOException, URISyntaxException {
108109
repositoryRoot = new File(repository.getSourceRoot(), "mercurial");
109110
}
110111

112+
@BeforeAll
113+
static void setUpClass() throws Exception {
114+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
115+
env.setHistoryEnabled(true);
116+
}
117+
111118
@BeforeEach
112119
void setup() throws IOException, URISyntaxException {
113120
setUpTestRepository();

opengrok-indexer/src/test/java/org/opengrok/indexer/index/IndexerVsDeletedDocumentsTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.index;
2424

@@ -36,6 +36,7 @@
3636
import org.eclipse.jgit.treewalk.TreeWalk;
3737
import org.eclipse.jgit.treewalk.filter.PathFilter;
3838
import org.junit.jupiter.api.AfterEach;
39+
import org.junit.jupiter.api.BeforeAll;
3940
import org.junit.jupiter.api.BeforeEach;
4041
import org.junit.jupiter.params.ParameterizedTest;
4142
import org.junit.jupiter.params.provider.Arguments;
@@ -81,6 +82,12 @@
8182
class IndexerVsDeletedDocumentsTest {
8283
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
8384

85+
@BeforeAll
86+
static void setUpClass() {
87+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
88+
env.setHistoryEnabled(true);
89+
}
90+
8491
private static String getRandomString(int numChars) {
8592
Random random = new Random();
8693
final StringBuilder sb = new StringBuilder();

opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/ProjectsController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>.
2323
*/
2424
package org.opengrok.web.api.v1.controller;
@@ -113,7 +113,7 @@ private void addProjectWorkHorse(String projectName) {
113113
if (!env.getProjects().containsKey(projectName)) {
114114
Project project = new Project(projectName, "/" + projectName);
115115

116-
if (env.isHistoryEnabled()) {
116+
if (project.isHistoryEnabled()) {
117117
// Add repositories in this project.
118118
List<RepositoryInfo> repos = getRepositoriesInDir(projDir);
119119

@@ -130,7 +130,7 @@ private void addProjectWorkHorse(String projectName) {
130130
Project project = env.getProjects().get(projectName);
131131
Map<Project, List<RepositoryInfo>> map = env.getProjectRepositoriesMap();
132132

133-
if (env.isHistoryEnabled()) {
133+
if (project.isHistoryEnabled()) {
134134
// Refresh the list of repositories of this project.
135135
// This is the goal of this action: if an existing project
136136
// is re-added, this means its list of repositories has changed.
@@ -202,7 +202,7 @@ private void deleteProjectWorkHorse(Project project) {
202202
group.getProjects().remove(project);
203203
}
204204

205-
if (env.isHistoryEnabled()) {
205+
if (project.isHistoryEnabled()) {
206206
// Now remove the repositories associated with this project.
207207
List<RepositoryInfo> repos = env.getProjectRepositoriesMap().get(project);
208208
if (repos != null) {
@@ -302,11 +302,11 @@ private Project getProjectFromName(String projectNameParam) {
302302
public Response deleteHistoryCache(@Context HttpServletRequest request,
303303
@PathParam("project") String projectNameParam) {
304304

305-
if (!env.isHistoryEnabled()) {
305+
Project project = getProjectFromName(projectNameParam);
306+
if (!project.isHistoryEnabled()) {
306307
return Response.status(Response.Status.NO_CONTENT).build();
307308
}
308309

309-
Project project = getProjectFromName(projectNameParam);
310310
List<RepositoryInfo> repos = env.getProjectRepositoriesMap().get(project);
311311
if (repos == null || repos.isEmpty()) {
312312
LOGGER.log(Level.INFO, NO_REPO_LOG_MSG, project.getName());

opengrok-web/src/main/webapp/help.jsp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1616
1717
CDDL HEADER END
1818
19-
Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
19+
Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
2020
Portions Copyright 2011 Jens Elkner.
2121
Portions Copyright (c) 2018, 2020, Chris Fraire <cfraire@me.com>.
2222
Portions Copyright (c) 2022, Krystof Tulinger <k.tulinger@seznam.cz>.
@@ -184,14 +184,8 @@ A <dfn>Query</dfn> is a series of clauses. A clause may be prefixed by:
184184
<dt>path</dt>
185185
<dd>path of the source file (no need to use dividers, or if, then use "/" - Windows users, "\" is an escape key in Lucene query syntax! <br/>Please don't use "\", or replace it with "/").<br/>Also note that if you want just exact path, enclose it in "", e.g. "src/mypath", otherwise dividers will be removed and you get more hits.</dd>
186186

187-
<%
188-
if (PageConfig.get(request).getEnv().isHistoryEnabled()) {
189-
%>
190187
<dt>hist</dt>
191188
<dd>History log comments.</dd>
192-
<%
193-
}
194-
%>
195189

196190
<dt>type</dt>
197191
<dd>Type of analyzer used to scope down to certain file types (e.g. just C sources).<br/>Current mappings: <%=SearchHelper.getFileTypeDescriptions().toString()%></dd>

opengrok-web/src/main/webapp/list.jsp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1616
1717
CDDL HEADER END
1818
19-
Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
19+
Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
2020
Portions Copyright 2011 Jens Elkner.
2121
Portions Copyright (c) 2017-2020, Chris Fraire <cfraire@me.com>.
2222
@@ -62,7 +62,7 @@ org.opengrok.web.DirectoryListing"
6262
cfg.checkSourceRootExistence();
6363
6464
String rev = cfg.getRequestedRevision();
65-
if (!cfg.isDir() && rev.length() == 0) {
65+
if (!cfg.isDir() && rev.isEmpty()) {
6666
/*
6767
* Get the latest revision and redirect so that the revision number
6868
* appears in the URL.

0 commit comments

Comments
 (0)