Skip to content

Commit 29816c3

Browse files
idodeclareVladimir Kotal
authored andcommitted
Add --disableRepository handling
1 parent d65161c commit 29816c3

File tree

5 files changed

+213
-45
lines changed

5 files changed

+213
-45
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@
6868
* nature of the web application, each thread will use the same instance of the
6969
* configuration object for each page request. Class and methods should have
7070
* package scope, but that didn't work with the XMLDecoder/XMLEncoder.
71-
*
72-
* This should be as close to POJO (https://en.wikipedia.org/wiki/Plain_old_Java_object) as possible.
71+
* <p>
72+
* This should be as close to a
73+
* <a href="https://en.wikipedia.org/wiki/Plain_old_Java_object">POJO</a> as
74+
* possible.
7375
*/
7476
public final class Configuration {
7577

@@ -187,6 +189,7 @@ public final class Configuration {
187189
private String webappLAF;
188190
private RemoteSCM remoteScmSupported;
189191
private boolean optimizeDatabase;
192+
private boolean quickContextScan;
190193

191194
private LuceneLockName luceneLocking = LuceneLockName.OFF;
192195
private boolean compressXref;
@@ -296,6 +299,8 @@ public final class Configuration {
296299

297300
private SuggesterConfig suggesterConfig = new SuggesterConfig();
298301

302+
private Set<String> disabledRepositories;
303+
299304
/*
300305
* types of handling history for remote SCM repositories:
301306
* ON - index history and display it in webapp
@@ -485,6 +490,7 @@ public Configuration() {
485490
setPluginDirectory(null);
486491
setPluginStack(new AuthorizationStack(AuthControlFlag.REQUIRED, "default stack"));
487492
setPrintProgress(false);
493+
setDisabledRepositories(new HashSet<>());
488494
setProjects(new ConcurrentHashMap<>());
489495
setQuickContextScan(true);
490496
//below can cause an outofmemory error, since it is defaulting to NO LIMIT
@@ -910,8 +916,6 @@ public boolean isAllowLeadingWildcard() {
910916
return allowLeadingWildcard;
911917
}
912918

913-
private boolean quickContextScan;
914-
915919
public boolean isQuickContextScan() {
916920
return quickContextScan;
917921
}
@@ -1259,6 +1263,14 @@ public void setSuggesterConfig(final SuggesterConfig config) {
12591263
this.suggesterConfig = config;
12601264
}
12611265

1266+
public Set<String> getDisabledRepositories() {
1267+
return disabledRepositories;
1268+
}
1269+
1270+
public void setDisabledRepositories(Set<String> disabledRepositories) {
1271+
this.disabledRepositories = disabledRepositories;
1272+
}
1273+
12621274
/**
12631275
* Write the current configuration to a file.
12641276
*

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,16 @@ public String setRepoCmd(String clazzName, String cmd) {
10171017
return cmd;
10181018
}
10191019

1020+
public void setRepoCmds(Map<String, String> cmds) {
1021+
Lock writeLock = configLock.writeLock();
1022+
writeLock.lock();
1023+
try {
1024+
configuration.setCmds(cmds);
1025+
} finally {
1026+
writeLock.unlock();
1027+
}
1028+
}
1029+
10201030
/**
10211031
* Sets the user page for the history listing.
10221032
*
@@ -1417,6 +1427,15 @@ public short getContextSurround() {
14171427
return (short) getConfigurationValue("contextSurround");
14181428
}
14191429

1430+
@SuppressWarnings("unchecked")
1431+
public Set<String> getDisabledRepositories() {
1432+
return (Set<String>) getConfigurationValue("disabledRepositories");
1433+
}
1434+
1435+
public void setDisabledRepositories(Set<String> disabledRepositories) {
1436+
setConfigurationValue("disabledRepositories", disabledRepositories);
1437+
}
1438+
14201439
/**
14211440
* Read an configuration file and set it as the current configuration.
14221441
*

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

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@
2727
import java.io.IOException;
2828
import java.lang.reflect.InvocationTargetException;
2929
import java.util.ArrayList;
30+
import java.util.HashMap;
3031
import java.util.List;
32+
import java.util.Locale;
33+
import java.util.Map;
34+
import java.util.Set;
3135
import java.util.logging.Level;
3236
import java.util.logging.Logger;
3337

38+
import org.opengrok.indexer.configuration.Configuration;
3439
import org.opengrok.indexer.configuration.RuntimeEnvironment;
40+
import org.opengrok.indexer.index.IgnoredNames;
3541
import org.opengrok.indexer.logger.LoggerFactory;
3642
import org.opengrok.indexer.util.ForbiddenSymlinkException;
3743

@@ -62,20 +68,43 @@ public final class RepositoryFactory {
6268
new SSCMRepository()
6369
};
6470

71+
private static final Map<String, Class<? extends Repository>> byName = new HashMap<>();
72+
73+
static {
74+
final String REPOSITORY = "Repository";
75+
for (Repository repository : repositories) {
76+
Class<? extends Repository> clazz = repository.getClass();
77+
String repoName = clazz.getSimpleName();
78+
byName.put(repoName, clazz);
79+
byName.put(repoName.toLowerCase(Locale.ROOT), clazz);
80+
if (repoName.endsWith(REPOSITORY)) {
81+
String shortName = repoName.substring(0, repoName.length() - REPOSITORY.length());
82+
if (shortName.length() > 0) {
83+
byName.put(shortName, clazz);
84+
byName.put(shortName.toLowerCase(Locale.ROOT), clazz);
85+
}
86+
}
87+
}
88+
}
89+
90+
/** Private to enforce static. */
6591
private RepositoryFactory() {
66-
// Factory class, should not be constructed
6792
}
6893

6994
/**
7095
* Get a list of all available repository handlers.
7196
*
72-
* @return a list which contains none-{@code null} values, only.
97+
* @return a list that contains non-{@code null} values only
7398
*/
7499
public static List<Class<? extends Repository>> getRepositoryClasses() {
75-
ArrayList<Class<? extends Repository>> list
76-
= new ArrayList<>(repositories.length);
100+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
101+
102+
ArrayList<Class<? extends Repository>> list = new ArrayList<>(repositories.length);
77103
for (int i = repositories.length - 1; i >= 0; i--) {
78-
list.add(repositories[i].getClass());
104+
Class<? extends Repository> clazz = repositories[i].getClass();
105+
if (isEnabled(clazz, env)) {
106+
list.add(clazz);
107+
}
79108
}
80109

81110
return list;
@@ -128,15 +157,18 @@ public static Repository getRepository(File file, boolean interactive, boolean i
128157
String relFile = env.getPathRelativeToSourceRoot(file);
129158

130159
Repository repo = null;
131-
for (Repository rep : repositories) {
132-
if ((!isNested || rep.isNestable()) && rep.isRepositoryFor(file, interactive)) {
133-
repo = rep.getClass().getDeclaredConstructor().newInstance();
160+
for (Repository referenceRepo : repositories) {
161+
Class<? extends Repository> clazz = referenceRepo.getClass();
162+
163+
if ((!isNested || referenceRepo.isNestable()) && isEnabled(clazz, env) &&
164+
referenceRepo.isRepositoryFor(file, interactive)) {
165+
repo = clazz.getDeclaredConstructor().newInstance();
134166

135167
if (env.isProjectsEnabled() && relFile.equals(File.separator)) {
136168
LOGGER.log(Level.WARNING, "{0} was detected as {1} repository however with directory " +
137169
"matching source root. This is invalid because projects are enabled. Ignoring this " +
138170
"repository.",
139-
new Object[]{file, rep.getType()});
171+
new Object[]{file, repo.getType()});
140172
return null;
141173
}
142174
repo.setDirectoryName(file);
@@ -220,22 +252,46 @@ public static Repository getRepository(RepositoryInfo info, boolean interactive)
220252
}
221253

222254
/**
223-
* Go through all repository types and add items to lists of ignored
224-
* files/directories. This way per-repository ignored entries are set
225-
* inside repository classes rather than globally in IgnoredFiles/Dirs.
226-
* Should be called after {@code setConfiguration()}.
227-
*
228-
* @param env runtime environment
255+
* Go through all supported repository types, and add their ignored items to
256+
* the environment's lists of ignored files/directories -- but skip any
257+
* repository types which are named in
258+
* {@link RuntimeEnvironment#getDisabledRepositories()} ()}. This way
259+
* per-repository ignored entries are set inside repository classes rather
260+
* than globally in IgnoredFiles/Dirs.
261+
* <p>
262+
* (Should be called after
263+
* {@link RuntimeEnvironment#setConfiguration(Configuration)}.)
229264
*/
230265
public static void initializeIgnoredNames(RuntimeEnvironment env) {
266+
IgnoredNames ignoredNames = env.getIgnoredNames();
231267
for (Repository repo : repositories) {
232-
for (String file : repo.getIgnoredFiles()) {
233-
env.getIgnoredNames().add("f:" + file);
268+
if (isEnabled(repo.getClass(), env)) {
269+
for (String file : repo.getIgnoredFiles()) {
270+
ignoredNames.add("f:" + file);
271+
}
272+
for (String dir : repo.getIgnoredDirs()) {
273+
ignoredNames.add("d:" + dir);
274+
}
234275
}
276+
}
277+
}
235278

236-
for (String dir : repo.getIgnoredDirs()) {
237-
env.getIgnoredNames().add("d:" + dir);
238-
}
279+
/**
280+
* Tries to match a supported repositories by name or nickname -- e.g.
281+
* {@code "CVSRepository"} or {@code "CVS"} or {@code "cvs"}.
282+
* @return a defined, class simple name (e.g. {@code "CVSRepository"} when
283+
* {@code "cvs"} is passed); or {@code null} if no match found
284+
*/
285+
public static String matchRepositoryByName(String name) {
286+
Class<? extends Repository> clazz = byName.get(name);
287+
if (clazz != null) {
288+
return clazz.getSimpleName();
239289
}
290+
return null;
291+
}
292+
293+
private static boolean isEnabled(Class<? extends Repository> clazz, RuntimeEnvironment env) {
294+
Set<String> disabledRepos = env.getDisabledRepositories();
295+
return disabledRepos == null || !disabledRepos.contains(clazz.getSimpleName());
240296
}
241297
}

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public final class Indexer {
121121
private static final HashSet<String> allowedSymlinks = new HashSet<>();
122122
private static final HashSet<String> canonicalRoots = new HashSet<>();
123123
private static final Set<String> defaultProjects = new TreeSet<>();
124+
private static final HashSet<String> disabledRepositories = new HashSet<>();
124125
private static RuntimeEnvironment env = null;
125126
private static String webappURI = null;
126127

@@ -168,6 +169,9 @@ public static void main(String[] argv) {
168169
pauseToAwaitProfiler();
169170
}
170171

172+
disabledRepositories.addAll(cfg.getDisabledRepositories());
173+
cfg.setDisabledRepositories(disabledRepositories);
174+
171175
env = RuntimeEnvironment.getInstance();
172176

173177
// Complete the configuration of repository types.
@@ -416,8 +420,8 @@ public static WebAddress parseWebAddress(String webAddr) {
416420
* @throws ParseException if parsing failed
417421
*/
418422
public static String[] parseOptions(String[] argv) throws ParseException {
419-
String[] usage = {HELP_OPT_1};
420-
String program = "opengrok.jar";
423+
final String[] usage = {HELP_OPT_1};
424+
final String program = "opengrok.jar";
421425
final String[] ON_OFF = {ON, OFF};
422426
final String[] REMOTE_REPO_CHOICES = {ON, OFF, DIRBASED, UIONLY};
423427
final String[] LUCENE_LOCKS = {ON, OFF, "simple", "native"};
@@ -465,10 +469,10 @@ public static String[] parseOptions(String[] argv) throws ParseException {
465469

466470
parser.on(
467471
"-A (.ext|prefix.):(-|analyzer)", "--analyzer", "/(\\.\\w+|\\w+\\.):(-|[a-zA-Z_0-9.]+)/",
468-
"Files with the named prefix/extension should be analyzed",
469-
"with the given analyzer, where 'analyzer' may be specified",
470-
"using a simple class name (RubyAnalyzer) or language name (C)",
471-
"(Note, analyzer specification is case-sensitive)",
472+
"Files with the named prefix/extension should be analyzed with the given",
473+
"analyzer, where 'analyzer' may be specified using a simple class name",
474+
"(e.g. RubyAnalyzer) or language name (e.g. C) and is case-sensitive.",
475+
"Option may be repeated.",
472476
" Ex: -A .foo:CAnalyzer",
473477
" will use the C analyzer for all files ending with .FOO",
474478
" Ex: -A bar.:Perl",
@@ -532,6 +536,22 @@ public static String[] parseOptions(String[] argv) throws ParseException {
532536
cfg.setScanningDepth((Integer) depth);
533537
});
534538

539+
parser.on("--disableRepository", "=type_name",
540+
"Disables operation of an OpenGrok-supported repository. Option may be",
541+
"repeated.",
542+
" Ex: --disableRepository git",
543+
" will disable the GitRepository",
544+
" Ex: --disableRepository MercurialRepository").Do(v -> {
545+
String repoType = (String) v;
546+
String repoSimpleType = RepositoryFactory.matchRepositoryByName(repoType);
547+
if (repoSimpleType == null) {
548+
System.err.println(String.format(
549+
"'--disableRepository %s' does not match a type and is ignored", v));
550+
} else {
551+
disabledRepositories.add(repoSimpleType);
552+
}
553+
});
554+
535555
parser.on("-e", "--economical",
536556
"To consume less disk space, OpenGrok will not generate and save",
537557
"hypertext cross-reference files but will generate on demand, which could",

0 commit comments

Comments
 (0)