Skip to content

Commit ad6b6aa

Browse files
author
Vladimir Kotal
committed
allow -S to specify search path
1 parent 30788c1 commit ad6b6aa

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.IOException;
3333
import java.nio.file.Path;
3434
import java.util.ArrayList;
35+
import java.util.Arrays;
3536
import java.util.Collections;
3637
import java.util.Date;
3738
import java.util.HashSet;
@@ -790,11 +791,11 @@ public void removeRepositories() {
790791
* Search through the directory for repositories and use the result to replace
791792
* the lists of repositories in both RuntimeEnvironment/Configuration and HistoryGuru.
792793
*
793-
* @param dir the root directory to start the search in
794+
* @param dir the directories to start the search in
794795
*/
795-
public void setRepositories(String dir) {
796+
public void setRepositories(String... dir) {
796797
List<RepositoryInfo> repos = new ArrayList<>(HistoryGuru.getInstance().
797-
addRepositories(new File[]{new File(dir)},
798+
addRepositories(Arrays.stream(dir).map(File::new).toArray(File[]::new),
798799
RuntimeEnvironment.getInstance().getIgnoredNames()));
799800
RuntimeEnvironment.getInstance().setRepositories(repos);
800801
}

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.text.ParseException;
3636
import java.util.ArrayList;
3737
import java.util.Arrays;
38+
import java.util.Collections;
3839
import java.util.HashMap;
3940
import java.util.HashSet;
4041
import java.util.List;
@@ -118,6 +119,7 @@ public final class Indexer {
118119
private static int status = 0;
119120

120121
private static final Set<String> repositories = new HashSet<>();
122+
private static Set<String> searchPaths = new HashSet<>();
121123
private static final HashSet<String> allowedSymlinks = new HashSet<>();
122124
private static final HashSet<String> canonicalRoots = new HashSet<>();
123125
private static final Set<String> defaultProjects = new TreeSet<>();
@@ -335,7 +337,16 @@ public static void main(String[] argv) {
335337
new Object[]{Info.getVersion(), Info.getRevision()});
336338

337339
// Create history cache first.
338-
getInstance().prepareIndexer(env, searchRepositories, addProjects,
340+
if (searchRepositories) {
341+
if (searchPaths.isEmpty()) {
342+
searchPaths.add(env.getSourceRootPath());
343+
} else {
344+
searchPaths = searchPaths.stream().
345+
map(t -> Paths.get(env.getSourceRootPath(), t).toString()).
346+
collect(Collectors.toSet());
347+
}
348+
}
349+
getInstance().prepareIndexer(env, searchPaths, addProjects,
339350
createDict, runIndex, subFiles, new ArrayList<>(repositories));
340351

341352
// prepareIndexer() populated the list of projects so now default projects can be set.
@@ -417,6 +428,8 @@ public static String[] parseOptions(String[] argv) throws ParseException {
417428
});
418429
});
419430

431+
searchPaths.clear();
432+
420433
// Limit usage lines to 72 characters for concise formatting.
421434

422435
optParser = OptionParser.Do(parser -> {
@@ -689,9 +702,16 @@ public static String[] parseOptions(String[] argv) throws ParseException {
689702
"are history-eligible; using --repository limits to only those specified.",
690703
"Option may be repeated.").Do(v -> repositories.add((String) v));
691704

692-
parser.on("-S", "--search",
693-
"Search for source repositories under -s,--source, and add them.").Do(v ->
694-
searchRepositories = true);
705+
parser.on("-S", "--search", "=[path/to/repository]",
706+
"Search for source repositories under -s,--source, and add them." +
707+
"Path (relative to the source root) to repository is optional. " +
708+
"Option may be repeated.").Do(v -> {
709+
searchRepositories = true;
710+
String repoPath = (String) v;
711+
if (!repoPath.isEmpty()) {
712+
searchPaths.add(repoPath);
713+
}
714+
});
695715

696716
parser.on("-s", "--source", "=/path/to/source/root",
697717
"The root directory of the source tree.").
@@ -873,8 +893,9 @@ public void prepareIndexer(RuntimeEnvironment env,
873893
List<String> subFiles,
874894
List<String> repositories) throws IndexerException, IOException {
875895

876-
prepareIndexer(env, searchRepositories, addProjects, createDict, true,
877-
subFiles, repositories);
896+
prepareIndexer(env,
897+
searchRepositories ? Collections.singleton(env.getSourceRootPath()) : Collections.emptySet(),
898+
addProjects, createDict, true, subFiles, repositories);
878899
}
879900

880901
/**
@@ -888,7 +909,7 @@ public void prepareIndexer(RuntimeEnvironment env,
888909
* for performance. We prefer clarity over performance here, so silence it.
889910
*
890911
* @param env runtime environment
891-
* @param searchRepositories if true, search for repositories
912+
* @param searchPaths list of paths in which to search for repositories
892913
* @param addProjects if true, add projects
893914
* @param createDict if true, create dictionary
894915
* @param createHistoryCache create history cache flag
@@ -899,7 +920,7 @@ public void prepareIndexer(RuntimeEnvironment env,
899920
*/
900921
@SuppressWarnings("PMD.SimplifyStartsWith")
901922
public void prepareIndexer(RuntimeEnvironment env,
902-
boolean searchRepositories,
923+
Set<String> searchPaths,
903924
boolean addProjects,
904925
boolean createDict,
905926
boolean createHistoryCache,
@@ -954,10 +975,10 @@ public void prepareIndexer(RuntimeEnvironment env,
954975
}
955976
}
956977

957-
if (searchRepositories) {
958-
LOGGER.log(Level.INFO, "Scanning for repositories...");
978+
if (!searchPaths.isEmpty()) {
979+
LOGGER.log(Level.INFO, "Scanning for repositories in {0}...", searchPaths);
959980
Statistics stats = new Statistics();
960-
env.setRepositories(env.getSourceRootPath());
981+
env.setRepositories(searchPaths.toArray(new String[0]));
961982
stats.report(LOGGER, String.format("Done scanning for repositories, found %d repositories",
962983
env.getRepositories().size()));
963984
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.io.FileWriter;
3434
import java.io.IOException;
3535
import java.io.StringWriter;
36+
import java.nio.file.Paths;
3637
import java.util.ArrayList;
3738
import java.util.Arrays;
3839
import java.util.Collections;
@@ -350,6 +351,25 @@ public void testRemoveFileOnFileChange() throws Exception {
350351
testrepo.destroy();
351352
}
352353

354+
@Test
355+
@ConditionalRun(RepositoryInstalled.MercurialInstalled.class)
356+
@ConditionalRun(RepositoryInstalled.GitInstalled.class)
357+
public void testSetRepositories() throws Exception {
358+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
359+
360+
TestRepository testrepo = new TestRepository();
361+
testrepo.create(HistoryGuru.class.getResourceAsStream("repositories.zip"));
362+
env.setSourceRoot(testrepo.getSourceRoot());
363+
364+
env.setRepositories(testrepo.getSourceRoot());
365+
assertEquals(9, env.getRepositories().size());
366+
367+
String[] repoNames = {"mercurial", "git"};
368+
env.setRepositories(Arrays.stream(repoNames).
369+
map(t -> Paths.get(env.getSourceRootPath(), t).toString()).distinct().toArray(String[]::new));
370+
assertEquals(2, env.getRepositories().size());
371+
}
372+
353373
@Test
354374
public void testXref() throws IOException {
355375
List<File> files = new ArrayList<>();

opengrok-web/src/test/java/org/opengrok/web/api/v1/controller/ProjectsControllerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public void testDelete() throws Exception {
291291
filter(p -> p.getName().equals("git")).
292292
collect(Collectors.toSet()).size());
293293

294-
// Run the indexer (ala 'indexpart') so that data directory is populated.
294+
// Run the indexer so that data directory is populated.
295295
ArrayList<String> subFiles = new ArrayList<>();
296296
subFiles.add("/git");
297297
subFiles.add("/mercurial");
@@ -301,7 +301,7 @@ public void testDelete() throws Exception {
301301
repos.add("/mercurial");
302302
repos.add("/svn");
303303
// This is necessary so that repositories in HistoryGuru get populated.
304-
// When 'indexpart' is run, this is called from setConfiguration() because
304+
// For per project reindex this is called from setConfiguration() because
305305
// of the -R option is present.
306306
HistoryGuru.getInstance().invalidateRepositories(
307307
env.getRepositories(), null, false);

0 commit comments

Comments
 (0)