public Repository newRepository(ConfigGroup repoConfig, String branch) {
validate(repoConfig);
checkNotNull("branch", branch);
final GitRepository repo = new GitRepository(repoConfig.getName());
repo.setCurrentBranch(branch);
for (final ConfigItem item : repoConfig.getItems()) {
if (item instanceof SecureConfigItem) {
repo.addEnvironmentParameter(item.getName(), secureService.decrypt(item.getValue().toString()));
} else {
repo.addEnvironmentParameter(item.getName(), item.getValue());
}
}
if (!repo.isValid()) {
throw new IllegalStateException("Repository " + repoConfig.getName() + " not valid");
}
FileSystem fs = null;
URI uri = null;
try {
uri = URI.create(repo.getUri());
fs = ioService.newFileSystem(uri, new HashMap<String, Object>(repo.getEnvironment()) {{
if (!repo.getEnvironment().containsKey("origin")) {
put("init", true);
}
}});
} catch (final FileSystemAlreadyExistsException e) {
fs = ioService.getFileSystem(uri);
} catch (final Throwable ex) {
throw new RuntimeException(ex.getCause().getMessage(), ex);
}
org.uberfire.backend.vfs.Path defaultRoot = convert(fs.getRootDirectories().iterator().next());
Map<String, org.uberfire.backend.vfs.Path> branches = getBranches(fs);
if (branches.containsKey(branch)) {
defaultRoot = branches.get(branch);
}
repo.setBranches(branches);
repo.setRoot(defaultRoot);
final String[] uris = fs.toString().split("\\r?\\n");
final List<PublicURI> publicURIs = new ArrayList<PublicURI>(uris.length);
for (final String s : uris) {
final int protocolStart = s.indexOf("://");
final PublicURI publicURI;
if (protocolStart > 0) {
publicURI = new DefaultPublicURI(s.substring(0, protocolStart), s);
} else {
publicURI = new DefaultPublicURI(s);
}
publicURIs.add(publicURI);
}
repo.setPublicURIs(publicURIs);
return repo;
}