Skip to content

Commit 87a275e

Browse files
authored
Allow @path syntax for both search paths and repository paths to be able to load values from a given file. (#3359)
fixes #3308
1 parent 5278970 commit 87a275e

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,10 @@ private Collection<RepositoryInfo> addRepositories(File[] files,
425425
continue;
426426
}
427427
if (repository == null) {
428+
if (depth > env.getScanningDepth()) {
429+
// we reached our search max depth, skip looking through the children
430+
continue;
431+
}
428432
// Not a repository, search its sub-dirs.
429433
if (pathAccepter.accept(file)) {
430434
File[] subFiles = file.listFiles();
@@ -433,7 +437,8 @@ private Collection<RepositoryInfo> addRepositories(File[] files,
433437
"Failed to get sub directories for ''{0}'', " +
434438
"check access permissions.",
435439
file.getAbsolutePath());
436-
} else if (depth <= env.getScanningDepth()) {
440+
} else {
441+
// Recursive call to scan next depth
437442
repoList.addAll(addRepositories(subFiles,
438443
allowedNesting, depth + 1, isNested));
439444
}

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@
2727
import java.io.File;
2828
import java.io.IOException;
2929
import java.io.PrintStream;
30+
import java.io.UncheckedIOException;
3031
import java.lang.reflect.Field;
3132
import java.lang.reflect.InvocationTargetException;
3233
import java.net.URI;
3334
import java.net.URISyntaxException;
35+
import java.nio.file.Files;
36+
import java.nio.file.Path;
3437
import java.nio.file.Paths;
3538
import java.text.ParseException;
3639
import java.util.ArrayList;
3740
import java.util.Arrays;
41+
import java.util.Collection;
3842
import java.util.Collections;
3943
import java.util.HashMap;
4044
import java.util.HashSet;
@@ -711,19 +715,24 @@ public static String[] parseOptions(String[] argv) throws ParseException {
711715
"with lots of renamed files. Default is off.").execute(v ->
712716
cfg.setHandleHistoryOfRenamedFiles((Boolean) v));
713717

714-
parser.on("--repository", "=path/to/repository",
718+
parser.on("--repository", "=[path/to/repository|@file_with_paths]",
715719
"Path (relative to the source root) to a repository for generating",
716720
"history (if -H,--history is on). By default all discovered repositories",
717721
"are history-eligible; using --repository limits to only those specified.",
718-
"Option may be repeated.").execute(v -> repositories.add((String) v));
722+
"File containing paths can be specified via @path syntax.",
723+
"Option may be repeated.")
724+
.execute(v -> handlePathParameter(repositories, ((String) v).trim()));
719725

720-
parser.on("-S", "--search", "=[path/to/repository]",
726+
parser.on("-S", "--search", "=[path/to/repository|@file_with_paths]",
721727
"Search for source repositories under -s,--source, and add them. Path",
722-
"(relative to the source root) is optional. Option may be repeated.").execute(v -> {
728+
"(relative to the source root) is optional. ",
729+
"File containing paths can be specified via @path syntax.",
730+
"Option may be repeated.")
731+
.execute(v -> {
723732
searchRepositories = true;
724-
String repoPath = (String) v;
725-
if (!repoPath.isEmpty()) {
726-
searchPaths.add(repoPath);
733+
String value = ((String) v).trim();
734+
if (!value.isEmpty()) {
735+
handlePathParameter(searchPaths, value);
727736
}
728737
});
729738

@@ -1143,6 +1152,24 @@ private static void pauseToAwaitProfiler() {
11431152
}
11441153
}
11451154

1155+
// Visible for testing
1156+
static void handlePathParameter(Collection<String> paramValueStore, String pathValue) {
1157+
if (pathValue.startsWith("@")) {
1158+
paramValueStore.addAll(loadPathsFromFile(pathValue.substring(1)));
1159+
} else {
1160+
paramValueStore.add(pathValue);
1161+
}
1162+
}
1163+
1164+
private static List<String> loadPathsFromFile(String filename) {
1165+
try {
1166+
return Files.readAllLines(Path.of(filename));
1167+
} catch (IOException e) {
1168+
LOGGER.log(Level.SEVERE, String.format("Could not load paths from %s", filename), e);
1169+
throw new UncheckedIOException(e);
1170+
}
1171+
}
1172+
11461173
private static void exitWithHelp() {
11471174
PrintStream helpStream = status != 0 ? System.err : System.out;
11481175
switch (helpMode) {

0 commit comments

Comments
 (0)