Created
October 22, 2019 16:53
-
-
Save rherrmann/89ee1d5a7966f923e5e7be5da9a893a7 to your computer and use it in GitHub Desktop.
Example using JGit's Transport::openFetch to test the connection to a remote repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************************************************************** | |
* Copyright (c) 2019 Rüdiger Herrmann | |
* All rights reserved. This program and the accompanying materials are made available under the | |
* terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at | |
* http://www.eclipse.org/legal/epl-v10.html | |
* | |
* Contributors: | |
* Rüdiger Herrmann - initial API and implementation | |
**************************************************************************************************/ | |
package com.codeaffine.gitplus.ui.internal.model; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.UncheckedIOException; | |
import org.eclipse.jgit.api.Git; | |
import org.eclipse.jgit.api.TransportConfigCallback; | |
import org.eclipse.jgit.api.errors.GitAPIException; | |
import org.eclipse.jgit.errors.NotSupportedException; | |
import org.eclipse.jgit.lib.Repository; | |
import org.eclipse.jgit.transport.FetchConnection; | |
import org.eclipse.jgit.transport.Transport; | |
import org.eclipse.jgit.transport.URIish; | |
import com.codeaffine.gitplus.ui.internal.util.io.Files; | |
public class RemoteConnectionValidator implements TransportOperation { | |
static final String TEMP_REPOSITORY_SUFFIX = ".tmp"; | |
static final String TEMP_REPOSITORY_PREFIX = "repository-"; | |
private static final int FIVE_SECONDS = 5; | |
private Repository repository; | |
private TransportConfigCallback transportConfigCallback; | |
@Override | |
public void setTransportConfigCallback( TransportConfigCallback transportConfigCallback ) { | |
this.transportConfigCallback = transportConfigCallback; | |
} | |
@Override | |
public GitRepository getRepository() { | |
return repository == null ? null : new GitRepository( repository ); | |
} | |
public boolean isValid( String remoteUrl ) { | |
boolean result = true; | |
try { | |
testConnection( remoteUrl ); | |
} catch( NotSupportedException notSuported ) { | |
try { | |
testConnectionWithLocalRepository( remoteUrl ); | |
} catch( Exception exception ) { | |
result = false; | |
} | |
} catch( Exception exception ) { | |
result = false; | |
} | |
return result; | |
} | |
private void testConnection( String remoteUrl ) throws Exception { | |
Transport transport = openTransport( remoteUrl ); | |
try { | |
configureTransport( transport ); | |
FetchConnection connection = transport.openFetch(); | |
connection.close(); | |
} finally { | |
transport.close(); | |
} | |
} | |
private void testConnectionWithLocalRepository( String remoteUrl ) throws Exception { | |
setupRepository(); | |
try { | |
testConnection( remoteUrl ); | |
} finally { | |
cleanupRepository(); | |
} | |
} | |
private Transport openTransport( String remoteUrl ) throws Exception { | |
URIish uri = new URIish( remoteUrl ); | |
return repository == null ? Transport.open( uri ) : Transport.open( repository, uri ); | |
} | |
private void configureTransport( Transport transport ) { | |
transport.setTimeout( FIVE_SECONDS ); | |
if( transportConfigCallback != null ) { | |
transportConfigCallback.configure( transport ); | |
} | |
} | |
private void setupRepository() { | |
try { | |
repository = Git.init().setDirectory( createTempDir() ).call().getRepository(); | |
} catch( GitAPIException gae ) { | |
throw new GitOperationException( gae ); | |
} | |
} | |
private void cleanupRepository() { | |
repository.close(); | |
Files.delete( repository.getWorkTree() ); | |
repository = null; | |
} | |
private static File createTempFile() { | |
try { | |
File result = File.createTempFile( TEMP_REPOSITORY_PREFIX, TEMP_REPOSITORY_SUFFIX ); | |
result.deleteOnExit(); | |
return Files.canonicalize( result ); | |
} catch( IOException ioe ) { | |
throw new UncheckedIOException( ioe ); | |
} | |
} | |
private static File createTempDir() { | |
File result = createTempFile(); | |
Files.delete( result ); | |
result.mkdir(); | |
result.deleteOnExit(); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment