use of org.opensolaris.opengrok.history.RepositoryInfo in project OpenGrok by OpenGrok.
the class Indexer method prepareIndexer.
/*
* This is the first phase of the indexing where history cache is being
* generated for repositories (at least for those which support getting
* history per directory).
*
* PMD wants us to use length() > 0 && charAt(0) instead of startsWith()
* for performance. We prefer clarity over performance here, so silence it.
*/
@SuppressWarnings("PMD.SimplifyStartsWith")
public void prepareIndexer(RuntimeEnvironment env, boolean searchRepositories, boolean addProjects, String defaultProject, String configFilename, boolean refreshHistory, boolean listFiles, boolean createDict, List<String> subFiles, List<String> repositories, List<String> zapCache, boolean listRepoPaths) throws IndexerException, IOException {
if (env.getDataRootPath() == null) {
throw new IndexerException("ERROR: Please specify a DATA ROOT path");
}
if (env.getSourceRootFile() == null) {
throw new IndexerException("ERROR: please specify a SRC_ROOT with option -s !");
}
if (zapCache.isEmpty() && !env.validateExuberantCtags()) {
throw new IndexerException("Didn't find Exuberant Ctags");
}
if (zapCache == null) {
throw new IndexerException("Internal error, zapCache shouldn't be null");
}
if (searchRepositories || listRepoPaths || !zapCache.isEmpty()) {
LOGGER.log(Level.INFO, "Scanning for repositories...");
long start = System.currentTimeMillis();
if (refreshHistory == true) {
HistoryGuru.getInstance().addRepositories(env.getSourceRootPath());
}
long time = (System.currentTimeMillis() - start) / 1000;
LOGGER.log(Level.INFO, "Done scanning for repositories ({0}s)", time);
if (listRepoPaths || !zapCache.isEmpty()) {
List<RepositoryInfo> repos = env.getRepositories();
String prefix = env.getSourceRootPath();
if (listRepoPaths) {
if (repos.isEmpty()) {
System.out.println("No repositories found.");
return;
}
System.out.println("Repositories in " + prefix + ":");
for (RepositoryInfo info : env.getRepositories()) {
String dir = info.getDirectoryName();
System.out.println(dir.substring(prefix.length()));
}
}
if (!zapCache.isEmpty()) {
HashSet<String> toZap = new HashSet<>(zapCache.size() << 1);
boolean all = false;
for (String repo : zapCache) {
if ("*".equals(repo)) {
all = true;
break;
}
if (repo.startsWith(prefix)) {
repo = repo.substring(prefix.length());
}
toZap.add(repo);
}
if (all) {
toZap.clear();
for (RepositoryInfo info : env.getRepositories()) {
toZap.add(info.getDirectoryName().substring(prefix.length()));
}
}
try {
HistoryGuru.getInstance().removeCache(toZap);
} catch (HistoryException e) {
LOGGER.log(Level.WARNING, "Clearing history cache failed: {0}", e.getLocalizedMessage());
}
}
return;
}
}
if (addProjects) {
File[] files = env.getSourceRootFile().listFiles();
List<Project> projects = env.getProjects();
// Keep a copy of the old project list so that we can preserve
// the customization of existing projects.
Map<String, Project> oldProjects = new HashMap<>();
for (Project p : projects) {
oldProjects.put(p.getPath(), p);
}
projects.clear();
// Add a project for each top-level directory in source root.
for (File file : files) {
String name = file.getName();
String path = "/" + name;
if (oldProjects.containsKey(path)) {
// This is an existing object. Reuse the old project,
// possibly with customizations, instead of creating a
// new with default values.
projects.add(oldProjects.get(path));
} else if (!name.startsWith(".") && file.isDirectory()) {
// Found a new directory with no matching project, so
// create a new project with default properties.
Project p = new Project();
p.setName(name);
p.setPath(path);
p.setTabSize(env.getConfiguration().getTabSize());
projects.add(p);
}
}
// The projects should be sorted...
Collections.sort(projects, new Comparator<Project>() {
@Override
public int compare(Project p1, Project p2) {
String s1 = p1.getName();
String s2 = p2.getName();
int ret;
if (s1 == null) {
ret = (s2 == null) ? 0 : 1;
} else {
ret = s1.compareTo(s2);
}
return ret;
}
});
}
if (defaultProject != null) {
for (Project p : env.getProjects()) {
if (p.getPath().equals(defaultProject)) {
env.setDefaultProject(p);
break;
}
}
}
if (configFilename != null) {
LOGGER.log(Level.INFO, "Writing configuration to {0}", configFilename);
env.writeConfiguration(new File(configFilename));
LOGGER.info("Done...");
}
if (refreshHistory) {
if (repositories != null && !repositories.isEmpty()) {
LOGGER.log(Level.INFO, "Generating history cache for repositories: " + repositories.stream().collect(Collectors.joining(",")));
HistoryGuru.getInstance().createCache(repositories);
LOGGER.info("Done...");
} else {
LOGGER.log(Level.INFO, "Generating history cache for all repositories ...");
HistoryGuru.getInstance().createCache();
LOGGER.info("Done...");
}
}
if (listFiles) {
IndexDatabase.listAllFiles(subFiles);
}
if (createDict) {
IndexDatabase.listFrequentTokens(subFiles);
}
}
use of org.opensolaris.opengrok.history.RepositoryInfo in project OpenGrok by OpenGrok.
the class IndexerTest method testRFE2575.
@Test
public void testRFE2575() throws Exception {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setCtags(System.getProperty(ctagsProperty, "ctags"));
env.setSourceRoot(repository.getSourceRoot());
env.setDataRoot(repository.getDataRoot());
HistoryGuru.getInstance().addRepositories(repository.getSourceRoot());
List<RepositoryInfo> repos = env.getRepositories();
Repository r = null;
for (RepositoryInfo ri : repos) {
if (ri.getDirectoryName().equals(repository.getSourceRoot() + "/rfe2575")) {
r = RepositoryFactory.getRepository(ri);
break;
}
}
if (r != null && r.isWorking() && env.validateExuberantCtags()) {
Project project = new Project();
project.setPath("/rfe2575");
IndexDatabase idb = new IndexDatabase(project);
assertNotNull(idb);
MyIndexChangeListener listener = new MyIndexChangeListener();
idb.addIndexChangedListener(listener);
idb.update();
assertEquals(2, listener.files.size());
repository.purgeData();
RuntimeEnvironment.getInstance().setIndexVersionedFilesOnly(true);
idb = new IndexDatabase(project);
listener = new MyIndexChangeListener();
idb.addIndexChangedListener(listener);
idb.update();
assertEquals(1, listener.files.size());
RuntimeEnvironment.getInstance().setIndexVersionedFilesOnly(false);
} else {
System.out.println("Skipping test. Repository for rfe2575 not found or could not find a ctags or an sccs I could use in path.");
}
}
use of org.opensolaris.opengrok.history.RepositoryInfo in project OpenGrok by OpenGrok.
the class ProjectHelperTest method testAllowedGetRepositoryInfo.
/**
* Test of getRepositoryInfo method, of class ProjectHelper.
*/
@Test
public void testAllowedGetRepositoryInfo() {
Project p = new Project();
p.setName("allowed_grouped_repository_0_1");
List<RepositoryInfo> result = helper.getRepositoryInfo(p);
Assert.assertEquals(1, result.size());
Assert.assertEquals("allowed_grouped_repository_0_1_" + 0, result.get(0).getParent());
}
use of org.opensolaris.opengrok.history.RepositoryInfo in project OpenGrok by OpenGrok.
the class ProjectHelperTest method testUnAllowedGetRepositoryInfo.
/**
* Test of getRepositoryInfo method, of class ProjectHelper.
*/
@Test
public void testUnAllowedGetRepositoryInfo() {
Project p = new Project();
p.setName("repository_2_1");
List<RepositoryInfo> result = helper.getRepositoryInfo(p);
Assert.assertEquals("this project is not allowed", 0, result.size());
}
use of org.opensolaris.opengrok.history.RepositoryInfo in project OpenGrok by OpenGrok.
the class RuntimeEnvironment method generateProjectRepositoriesMap.
/**
* Generate a TreeMap of projects with corresponding repository information.
*
* Project with some repository information is considered as a repository
* otherwise it is just a simple project.
*/
private void generateProjectRepositoriesMap() throws IOException {
repository_map.clear();
for (RepositoryInfo r : getRepositories()) {
Project proj;
String repoPath;
repoPath = getPathRelativeToSourceRoot(new File(r.getDirectoryName()), 0);
if ((proj = Project.getProject(repoPath)) != null) {
List<RepositoryInfo> values = repository_map.get(proj);
if (values == null) {
values = new ArrayList<>();
repository_map.put(proj, values);
}
values.add(r);
}
}
}
Aggregations