String branch = repoConfig.getConfigItemValue( EnvironmentParameters.BRANCH );
if ( branch == null ) {
branch = "master";
}
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 );
}
Path defaultRoot = fs.getRootDirectories().iterator().next();
for ( final Path path : fs.getRootDirectories() ) {
String gitBranch = getBranchName( path );
if ( gitBranch.equals( branch ) ) {
defaultRoot = path;
break;
}
}
Set<String> branches = new HashSet<String>();
// collect all branches
for ( final Path path : fs.getRootDirectories() ) {
String gitBranch = getBranchName( path );
branches.add( gitBranch );
}
repo.setBranches( branches );
repo.setRoot( convert( 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;
}