Skip to content

Commit 982b7b2

Browse files
idodeclareVladimir Kotal
authored andcommitted
Fix --help {ctags,repos}
As exitWithHelp() is called before the runtime environment has had its configuration updated with parsed options, exitWithHelp() needs to force in some settings for ctags or repos help.
1 parent aeba307 commit 982b7b2

File tree

3 files changed

+67
-19
lines changed

3 files changed

+67
-19
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,27 @@ public static String getText() {
3737
builder.append("Enabled repositories:");
3838
builder.append(System.lineSeparator());
3939
builder.append(System.lineSeparator());
40-
4140
List<Class<? extends Repository>> clazzes = RepositoryFactory.getRepositoryClasses();
41+
appendClassesHelp(builder, clazzes);
42+
43+
List<Class<? extends Repository>> disabledClazzes =
44+
RepositoryFactory.getDisabledRepositoryClasses();
45+
if (!disabledClazzes.isEmpty()) {
46+
if (!clazzes.isEmpty()) {
47+
builder.append(System.lineSeparator());
48+
}
49+
builder.append("Disabled repositories:");
50+
builder.append(System.lineSeparator());
51+
builder.append(System.lineSeparator());
52+
appendClassesHelp(builder, disabledClazzes);
53+
}
54+
55+
return builder.toString();
56+
}
57+
58+
private static void appendClassesHelp(
59+
StringBuilder builder, List<Class<? extends Repository>> clazzes) {
60+
4261
clazzes.sort((o1, o2) -> o1.getSimpleName().compareToIgnoreCase(o2.getSimpleName()));
4362
for (Class<?> clazz : clazzes) {
4463
String simpleName = clazz.getSimpleName();
@@ -49,7 +68,6 @@ public static String getText() {
4968
}
5069
builder.append(System.lineSeparator());
5170
}
52-
return builder.toString();
5371
}
5472

5573
private static boolean toAka(StringBuilder builder, String repoSimpleName) {

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,30 @@ private RepositoryFactory() {
106106
* @return a list that contains non-{@code null} values only
107107
*/
108108
public static List<Class<? extends Repository>> getRepositoryClasses() {
109-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
110-
111109
ArrayList<Class<? extends Repository>> list = new ArrayList<>(repositories.length);
112110
for (int i = repositories.length - 1; i >= 0; i--) {
113111
Class<? extends Repository> clazz = repositories[i].getClass();
114-
if (isEnabled(clazz, env)) {
112+
if (isEnabled(clazz)) {
115113
list.add(clazz);
116114
}
117115
}
118-
116+
117+
return list;
118+
}
119+
120+
/**
121+
* Gets a list of all disabled repository handlers.
122+
* @return a list that contains non-{@code null} values only
123+
*/
124+
public static List<Class<? extends Repository>> getDisabledRepositoryClasses() {
125+
ArrayList<Class<? extends Repository>> list = new ArrayList<>();
126+
for (int i = repositories.length - 1; i >= 0; i--) {
127+
Class<? extends Repository> clazz = repositories[i].getClass();
128+
if (!isEnabled(clazz)) {
129+
list.add(clazz);
130+
}
131+
}
132+
119133
return list;
120134
}
121135

@@ -169,7 +183,7 @@ public static Repository getRepository(File file, boolean interactive, boolean i
169183
for (Repository referenceRepo : repositories) {
170184
Class<? extends Repository> clazz = referenceRepo.getClass();
171185

172-
if ((!isNested || referenceRepo.isNestable()) && isEnabled(clazz, env) &&
186+
if ((!isNested || referenceRepo.isNestable()) && isEnabled(clazz) &&
173187
referenceRepo.isRepositoryFor(file, interactive)) {
174188
repo = clazz.getDeclaredConstructor().newInstance();
175189

@@ -274,7 +288,7 @@ public static Repository getRepository(RepositoryInfo info, boolean interactive)
274288
public static void initializeIgnoredNames(RuntimeEnvironment env) {
275289
IgnoredNames ignoredNames = env.getIgnoredNames();
276290
for (Repository repo : repositories) {
277-
if (isEnabled(repo.getClass(), env)) {
291+
if (isEnabled(repo.getClass())) {
278292
for (String file : repo.getIgnoredFiles()) {
279293
ignoredNames.add("f:" + file);
280294
}
@@ -299,8 +313,8 @@ public static String matchRepositoryByName(String name) {
299313
return null;
300314
}
301315

302-
private static boolean isEnabled(Class<? extends Repository> clazz, RuntimeEnvironment env) {
303-
Set<String> disabledRepos = env.getDisabledRepositories();
316+
private static boolean isEnabled(Class<? extends Repository> clazz) {
317+
Set<String> disabledRepos = RuntimeEnvironment.getInstance().getDisabledRepositories();
304318
return disabledRepos == null || !disabledRepos.contains(clazz.getSimpleName());
305319
}
306320
}

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,17 @@ public static void main(String[] argv) {
154154

155155
try {
156156
argv = parseOptions(argv);
157+
158+
/*
159+
* Attend to disabledRepositories here in case exitWithHelp() will
160+
* need to report about repos.
161+
*/
162+
disabledRepositories.addAll(cfg.getDisabledRepositories());
163+
cfg.setDisabledRepositories(disabledRepositories);
164+
for (String repoName : disabledRepositories) {
165+
LOGGER.log(Level.FINEST, "Disabled {0}", repoName);
166+
}
167+
157168
if (help) {
158169
exitWithHelp();
159170
}
@@ -164,12 +175,6 @@ public static void main(String[] argv) {
164175
pauseToAwaitProfiler();
165176
}
166177

167-
disabledRepositories.addAll(cfg.getDisabledRepositories());
168-
cfg.setDisabledRepositories(disabledRepositories);
169-
for (String repoName : disabledRepositories) {
170-
LOGGER.log(Level.FINEST, "Disabled {0}", repoName);
171-
}
172-
173178
env = RuntimeEnvironment.getInstance();
174179

175180
// Complete the configuration of repository types.
@@ -410,11 +415,13 @@ public static String[] parseOptions(String[] argv) throws ParseException {
410415
OptionParser configure = OptionParser.scan(parser ->
411416
parser.on("-R configPath").Do(cfgFile -> {
412417
try {
418+
cfg = Configuration.read(new File((String) cfgFile));
419+
} catch (IOException e) {
413420
if (!preHelp) {
414-
cfg = Configuration.read(new File((String) cfgFile));
421+
die(e.getMessage());
422+
} else {
423+
System.err.println(String.format("Warning: failed to read -R %s", cfgFile));
415424
}
416-
} catch (IOException e) {
417-
die(e.getMessage());
418425
}
419426
}));
420427

@@ -1131,6 +1138,11 @@ private static void exitWithHelp() {
11311138
helpStream.print(ConfigurationHelp.getSamples());
11321139
break;
11331140
case CTAGS:
1141+
/*
1142+
* Force the environment's ctags, because this method is called
1143+
* before main() does the heavyweight setConfiguration().
1144+
*/
1145+
env.setCtags(cfg.getCtags());
11341146
helpStream.println("Ctags command-line:");
11351147
helpStream.println();
11361148
helpStream.println(getCtagsCommand());
@@ -1140,6 +1152,10 @@ private static void exitWithHelp() {
11401152
helpStream.println(AnalyzerGuruHelp.getUsage());
11411153
break;
11421154
case REPOS:
1155+
/*
1156+
* Force the environment's disabledRepositories (as above).
1157+
*/
1158+
env.setDisabledRepositories(cfg.getDisabledRepositories());
11431159
helpStream.println(RepositoriesHelp.getText());
11441160
break;
11451161
default:

0 commit comments

Comments
 (0)