use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.
the class RepositoryFactory method getRepository.
/**
* Returns a repository for the given file, or null if no repository was
* found.
*
* Note that the operations performed by this method take quite a long time
* thanks to external commands being executed. For that reason, when run
* on multiple files, it should be parallelized (e.g. like it is done in
* {@code invalidateRepositories()}) and the commands run within should
* use interactive command timeout (as specified in {@code Configuration}).
*
* @param file File that might contain a repository
* @param cmdType command timeout type
* @param isNested a value indicating if a nestable {@link Repository} is required
* @return Correct repository for the given file or {@code null}
* @throws InstantiationException in case we cannot create the repository object
* @throws IllegalAccessException in case no permissions to repository file
* @throws NoSuchMethodException in case we cannot create the repository object
* @throws InvocationTargetException in case we cannot create the repository object
* @throws IOException when resolving repository path
* @throws ForbiddenSymlinkException when resolving repository path
*/
public static Repository getRepository(File file, CommandTimeoutType cmdType, boolean isNested) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, IOException, ForbiddenSymlinkException {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
String relFile = env.getPathRelativeToSourceRoot(file);
Repository repo = null;
for (Repository referenceRepo : repositories) {
Class<? extends Repository> clazz = referenceRepo.getClass();
if ((!isNested || referenceRepo.isNestable()) && isEnabled(clazz) && referenceRepo.isRepositoryFor(file, cmdType)) {
repo = clazz.getDeclaredConstructor().newInstance();
if (env.isProjectsEnabled() && relFile.equals(File.separator)) {
LOGGER.log(Level.WARNING, "{0} was detected as {1} repository however with directory " + "matching source root. This is invalid because projects are enabled. Ignoring this " + "repository.", new Object[] { file, repo.getType() });
return null;
}
repo.setDirectoryName(file);
if (!repo.isWorking()) {
LOGGER.log(Level.WARNING, "{0} not working (missing binaries?): {1}", new Object[] { repo.getClass().getSimpleName(), file.getPath() });
}
if (repo.getType() == null || repo.getType().length() == 0) {
repo.setType(repo.getClass().getSimpleName());
}
if (repo.getParent() == null || repo.getParent().length() == 0) {
try {
repo.setParent(repo.determineParent(cmdType));
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Failed to get parent for {0}: {1}", new Object[] { file.getAbsolutePath(), ex });
}
}
if (repo.getBranch() == null || repo.getBranch().length() == 0) {
try {
repo.setBranch(repo.determineBranch(cmdType));
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Failed to get branch for {0}: {1}", new Object[] { file.getAbsolutePath(), ex });
}
}
if (repo.getCurrentVersion() == null || repo.getCurrentVersion().length() == 0) {
try {
repo.setCurrentVersion(repo.determineCurrentVersion(cmdType));
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Failed to determineCurrentVersion for {0}: {1}", new Object[] { file.getAbsolutePath(), ex });
}
}
// revision, we need to prepare list of all tags in advance.
if (cmdType.equals(CommandTimeoutType.INDEXER) && env.isTagsEnabled() && repo.hasFileBasedTags()) {
repo.buildTagList(file, cmdType);
}
repo.fillFromProject();
break;
}
}
return repo;
}
use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.
the class RepositoryInfo method fillFromProject.
/**
* Fill configurable properties from associated project (if any) or Configuration.
*/
public void fillFromProject() {
Project proj = Project.getProject(getDirectoryNameRelative());
if (proj != null) {
setHistoryEnabled(proj.isHistoryEnabled());
setHandleRenamedFiles(proj.isHandleRenamedFiles());
setMergeCommitsEnabled(proj.isMergeCommitsEnabled());
} else {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
setHistoryEnabled(env.isHistoryEnabled());
setHandleRenamedFiles(env.isHandleHistoryOfRenamedFiles());
setMergeCommitsEnabled(env.isMergeCommitsEnabled());
}
}
use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.
the class BazaarRepository method getHistory.
@Override
History getHistory(File file, String sinceRevision) throws HistoryException {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
History result = new BazaarHistoryParser(this).parse(file, sinceRevision);
// We don't need to check if this repository supports tags, because we know it:-)
if (env.isTagsEnabled()) {
assignTagsInHistory(result);
}
return result;
}
use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.
the class MercurialRepository method getHistory.
History getHistory(File file, String sinceRevision, String tillRevision, Integer numCommits) throws HistoryException {
if (numCommits != null && numCommits <= 0) {
return null;
}
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
// Note that the filtering of revisions based on sinceRevision is done
// in the history log executor by passing appropriate options to
// the 'hg' executable.
// This is done only for directories since if getHistory() is used
// for file, the file is renamed and its complete history is fetched
// so no sinceRevision filter is needed.
// See findOriginalName() code for more details.
History result = new MercurialHistoryParser(this).parse(file, sinceRevision, tillRevision, numCommits);
// because we know it :-)
if (env.isTagsEnabled()) {
assignTagsInHistory(result);
}
return result;
}
use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.
the class PageConfigTest method testGetSortedFilesDirsFirst.
@SuppressWarnings("ResultOfMethodCallIgnored")
@EnabledOnOs({ OS.LINUX, OS.MAC, OS.SOLARIS, OS.AIX, OS.OTHER })
@Test
void testGetSortedFilesDirsFirst() throws IOException {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setListDirsFirst(true);
// Cannot spy/mock final class.
HttpServletRequest req = createRequest("/source", "/xref", "");
PageConfig pageConfig = PageConfig.get(req);
// Make sure the source root has just directories.
File sourceRootFile = new File(repository.getSourceRoot());
assertTrue(Arrays.stream(sourceRootFile.listFiles()).filter(File::isFile).collect(Collectors.toSet()).isEmpty());
// Create regular file under source root.
File file = new File(sourceRootFile, "foo.txt");
assertTrue(file.createNewFile());
assertTrue(file.isFile());
// Make sure the regular file is last.
List<String> entries = pageConfig.getSortedFiles(sourceRootFile.listFiles());
assertNotNull(entries);
assertFalse(entries.isEmpty());
int numEntries = entries.size();
assertEquals("foo.txt", entries.get(entries.size() - 1));
// Create symbolic link to non-existent target.
Path link = Path.of(sourceRootFile.getCanonicalPath(), "link");
Path target = Paths.get("/nonexistent");
Files.createSymbolicLink(link, target);
// Check the symlink was sorted as file.
entries = pageConfig.getSortedFiles(sourceRootFile.listFiles());
assertNotNull(entries);
assertFalse(entries.isEmpty());
assertEquals(numEntries + 1, entries.size());
assertEquals("link", entries.get(entries.size() - 1));
// Cleanup.
file.delete();
link.toFile().delete();
}
Aggregations