Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, 2021, Chris Fraire <[email protected]>.
*/
package org.opengrok.indexer.analysis;
Expand Down Expand Up @@ -596,7 +596,7 @@ public void populateDocument(Document doc, File file, String path, AbstractAnaly
doc.add(new SortedDocValuesField(QueryBuilder.FULLPATH,
new BytesRef(file.getAbsolutePath())));

if (RuntimeEnvironment.getInstance().isHistoryEnabled()) {
if (HistoryGuru.getInstance().repositorySupportsHistory(file)) {
populateDocumentHistory(doc, file);
}
doc.add(new Field(QueryBuilder.DATE, date, string_ft_stored_nanalyzed_norms));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2025, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
* Portions Copyright (c) 2020, Aleksandr Kirillov <[email protected]>.
*/
Expand Down Expand Up @@ -580,7 +580,7 @@ public Configuration() {
setHandleHistoryOfRenamedFiles(false);
setHistoryBasedReindex(true);
setHistoryCache(true);
setHistoryEnabled(true);
setHistoryEnabled(false);
setHitsPerPage(25);
setIgnoredNames(new IgnoredNames());
setIncludedNames(new Filter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
*/
package org.opengrok.indexer.history;
Expand Down Expand Up @@ -54,7 +54,6 @@
import org.opengrok.indexer.analysis.AnalyzerFactory;
import org.opengrok.indexer.analysis.AnalyzerGuru;
import org.opengrok.indexer.configuration.CommandTimeoutType;
import org.opengrok.indexer.configuration.Configuration;
import org.opengrok.indexer.configuration.Configuration.RemoteSCM;
import org.opengrok.indexer.configuration.OpenGrokThreadFactory;
import org.opengrok.indexer.configuration.PathAccepter;
Expand Down Expand Up @@ -630,6 +629,14 @@ public boolean hasHistory(File file) {
}
}

return repositorySupportsHistory(file);
}

/**
* @param file {@link File} object for a file under source root
* @return whether related {@link Repository} and settings allow for history retrieval
*/
public boolean repositorySupportsHistory(File file) {
Repository repo = getRepository(file);
if (repo == null) {
LOGGER.finest(() -> String.format("cannot find repository for '%s' to check history presence",
Expand All @@ -642,7 +649,7 @@ public boolean hasHistory(File file) {
}

// This should return true for Annotate view.
Configuration.RemoteSCM globalRemoteSupport = env.getRemoteScmSupported();
RemoteSCM globalRemoteSupport = env.getRemoteScmSupported();
boolean remoteSupported = ((globalRemoteSupport == RemoteSCM.ON)
|| (globalRemoteSupport == RemoteSCM.UIONLY)
|| (globalRemoteSupport == RemoteSCM.DIRBASED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
*/

/*
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.indexer.history;

import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
import org.opengrok.indexer.configuration.RuntimeEnvironment;
import org.opengrok.indexer.index.IndexDatabase;
import org.opengrok.indexer.logger.LoggerFactory;
import org.opengrok.indexer.search.QueryBuilder;
Expand All @@ -52,11 +51,11 @@ private LatestRevisionUtil() {

/**
* @param file file object corresponding to a file under source root
* @return last revision string for {@code file} or null
* @return last revision string for {@code file} or {@code null}
*/
@Nullable
public static String getLatestRevision(File file) {
if (!RuntimeEnvironment.getInstance().isHistoryEnabled()) {
if (!HistoryGuru.getInstance().repositorySupportsHistory(file)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,10 +1283,9 @@ private static void createAnnotationCache(File file, Document doc) {
}
}

private AbstractAnalyzer getAnalyzerFor(File file, String path)
throws IOException {
try (InputStream in = new BufferedInputStream(
new FileInputStream(file))) {
@VisibleForTesting
static AbstractAnalyzer getAnalyzerFor(File file, String path) throws IOException {
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
return AnalyzerGuru.getAnalyzer(in, path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,8 +1026,6 @@ public static String[] parseOptions(String[] argv) throws ParseException {
cfg = new Configuration();
}

cfg.setHistoryEnabled(false); // force user to turn on history capture

argv = optParser.parse(argv);

return argv;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.indexer.history;

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

env.setHistoryEnabled(true);

// This needs to be set before the call to env.setRepositories() below as it instantiates HistoryGuru.
env.setAnnotationCacheEnabled(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2018, 2020, Chris Fraire <[email protected]>.
* Portions Copyright (c) 2020, 2023, Ric Harris <[email protected]>.
*/
Expand Down Expand Up @@ -55,6 +55,7 @@
import org.apache.commons.lang3.time.DateUtils;
import org.eclipse.jgit.api.Git;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
Expand Down Expand Up @@ -90,6 +91,12 @@ class FileHistoryCacheTest {
private boolean savedIsHandleHistoryOfRenamedFiles;
private boolean savedIsTagsEnabled;

@BeforeAll
static void setUpClass() throws Exception {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setHistoryEnabled(true);
}

/**
* Set up the test environment with repositories and a cache instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class HistoryGuruTest {
@BeforeAll
static void setUpClass() throws Exception {
env = RuntimeEnvironment.getInstance();
env.setHistoryEnabled(true);
env.setAnnotationCacheEnabled(true);
savedNestingMaximum = env.getNestingMaximum();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -108,6 +109,12 @@ private void setUpTestRepository() throws IOException, URISyntaxException {
repositoryRoot = new File(repository.getSourceRoot(), "mercurial");
}

@BeforeAll
static void setUpClass() throws Exception {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setHistoryEnabled(true);
}

@BeforeEach
void setup() throws IOException, URISyntaxException {
setUpTestRepository();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/

/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.indexer.index;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.TextField;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.opengrok.indexer.analysis.AbstractAnalyzer;
import org.opengrok.indexer.analysis.AnalyzerGuru;
import org.opengrok.indexer.configuration.CommandTimeoutType;
import org.opengrok.indexer.configuration.RuntimeEnvironment;
import org.opengrok.indexer.history.HistoryGuru;
import org.opengrok.indexer.history.Repository;
import org.opengrok.indexer.history.RepositoryFactory;
import org.opengrok.indexer.search.QueryBuilder;
import org.opengrok.indexer.util.NullWriter;
import org.opengrok.indexer.util.TestRepository;

import java.io.File;
import java.io.Writer;
import java.net.URL;
import java.nio.file.Path;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;

class AnalyzerGuruDocumentTest {
private RuntimeEnvironment env;

private static TestRepository testRepository;

@BeforeEach
void setUpClass() throws Exception {
env = RuntimeEnvironment.getInstance();

testRepository = new TestRepository();
URL resourceURL = HistoryGuru.class.getResource("/repositories");
assertNotNull(resourceURL);
testRepository.create(resourceURL);

env.setSourceRoot(testRepository.getSourceRoot());
env.setDataRoot(testRepository.getDataRoot());
env.setHistoryEnabled(true);
env.setProjectsEnabled(true);
RepositoryFactory.initializeIgnoredNames(env);

// Restore the project and repository information.
env.setProjects(new HashMap<>());
env.setRepositories(testRepository.getSourceRoot());
HistoryGuru.getInstance().invalidateRepositories(env.getRepositories(), CommandTimeoutType.INDEXER);
env.generateProjectRepositoriesMap();
}

@AfterEach
void tearDownClass() throws Exception {
testRepository.destroy();
}

/**
* {@link AnalyzerGuru#populateDocument(Document, File, String, AbstractAnalyzer, Writer)} should populate
* the history of the document only if the repository related to the file allows for it.
*/
@ParameterizedTest
@ValueSource(booleans = {false, true})
void testPopulateDocumentHistory(boolean historyEnabled) throws Exception {
AnalyzerGuru analyzerGuru = new AnalyzerGuru();
Document doc = Mockito.mock(Document.class);
Path filePath = Path.of(env.getSourceRootPath(), "git", "main.c");
File file = filePath.toFile();
assertTrue(file.exists());
HistoryGuru histGuru = HistoryGuru.getInstance();
Repository repository = histGuru.getRepository(file);
assertNotNull(repository);
repository.setHistoryEnabled(historyEnabled);
String relativePath = env.getPathRelativeToSourceRoot(file);
analyzerGuru.populateDocument(doc, file, relativePath,
IndexDatabase.getAnalyzerFor(file, relativePath), new NullWriter());
ArgumentCaptor<TextField> argument = ArgumentCaptor.forClass(TextField.class);
verify(doc, atLeast(1)).add(argument.capture());
assertEquals(historyEnabled,
argument.getAllValues().stream().anyMatch(e -> e.name().equals(QueryBuilder.HIST)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.indexer.index;

Expand All @@ -36,6 +36,7 @@
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -81,6 +82,12 @@
class IndexerVsDeletedDocumentsTest {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();

@BeforeAll
static void setUpClass() {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setHistoryEnabled(true);
}

private static String getRandomString(int numChars) {
Random random = new Random();
final StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2025, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
*/
package org.opengrok.indexer.search.context;
Expand Down Expand Up @@ -64,9 +64,12 @@ class HistoryContextTest {

@BeforeAll
static void setUpClass() throws Exception {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setHistoryEnabled(true);

repositories = new TestRepository();
repositories.create(HistoryContextTest.class.getResource("/repositories"));
RuntimeEnvironment.getInstance().setRepositories(repositories.getSourceRoot());
env.setRepositories(repositories.getSourceRoot());
}

@AfterAll
Expand Down
Loading
Loading