TransportProtocol: Allow null Repository in canHandle()

This allows callers to determine if a URI is supported, before
worrying about the local repository.

Suggested-by: Dariusz Luksza <dariusz@luksza.org>
Change-Id: Ifc76a4ba841f2e2e7354bd51306b87b3b9d7f6ab
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
This commit is contained in:
Shawn O. Pearce 2011-03-14 10:48:58 -07:00 committed by Chris Aniszczyk
parent dffc50267e
commit f18e1fd1d1
10 changed files with 46 additions and 23 deletions

View File

@ -549,6 +549,6 @@ public void testFileProtocol() throws IllegalArgumentException,
public void testMissingPort() throws URISyntaxException { public void testMissingPort() throws URISyntaxException {
final String incorrectSshUrl = "ssh://some-host:/path/to/repository.git"; final String incorrectSshUrl = "ssh://some-host:/path/to/repository.git";
URIish u = new URIish(incorrectSshUrl); URIish u = new URIish(incorrectSshUrl);
assertFalse(TransportGitSsh.PROTO_SSH.canHandle(null, u, null)); assertFalse(TransportGitSsh.PROTO_SSH.canHandle(u));
} }
} }

View File

@ -542,8 +542,8 @@ public static Transport open(Repository local, URIish uri, String remoteName)
continue; continue;
} }
if (proto.canHandle(local, uri, remoteName)) if (proto.canHandle(uri, local, remoteName))
return proto.open(local, uri, remoteName); return proto.open(uri, local, remoteName);
} }
throw new NotSupportedException(MessageFormat.format(JGitText.get().URINotSupported, uri)); throw new NotSupportedException(MessageFormat.format(JGitText.get().URINotSupported, uri));

View File

@ -118,7 +118,7 @@ public Set<URIishField> getOptionalFields() {
return Collections.unmodifiableSet(EnumSet.of(URIishField.PASS)); return Collections.unmodifiableSet(EnumSet.of(URIishField.PASS));
} }
public Transport open(Repository local, URIish uri, String remoteName) public Transport open(URIish uri, Repository local, String remoteName)
throws NotSupportedException { throws NotSupportedException {
return new TransportAmazonS3(local, uri); return new TransportAmazonS3(local, uri);
} }

View File

@ -78,7 +78,7 @@ public Set<String> getSchemes() {
} }
@Override @Override
public boolean canHandle(Repository local, URIish uri, String remoteName) { public boolean canHandle(URIish uri, Repository local, String remoteName) {
if (uri.getPath() == null if (uri.getPath() == null
|| uri.getPort() > 0 || uri.getPort() > 0
|| uri.getUser() != null || uri.getUser() != null
@ -90,7 +90,7 @@ public boolean canHandle(Repository local, URIish uri, String remoteName) {
} }
@Override @Override
public Transport open(Repository local, URIish uri, String remoteName) public Transport open(URIish uri, Repository local, String remoteName)
throws NotSupportedException, TransportException { throws NotSupportedException, TransportException {
if ("bundle".equals(uri.getScheme())) { if ("bundle".equals(uri.getScheme())) {
File path = local.getFS().resolve(new File("."), uri.getPath()); File path = local.getFS().resolve(new File("."), uri.getPath());
@ -102,7 +102,7 @@ public Transport open(Repository local, URIish uri, String remoteName)
// resolve the path and figure out which type it is by testing // resolve the path and figure out which type it is by testing
// the target. // the target.
// //
return TransportLocal.PROTO_LOCAL.open(local, uri, remoteName); return TransportLocal.PROTO_LOCAL.open(uri, local, remoteName);
} }
}; };

View File

@ -96,7 +96,7 @@ public int getDefaultPort() {
return GIT_PORT; return GIT_PORT;
} }
public Transport open(Repository local, URIish uri, String remoteName) public Transport open(URIish uri, Repository local, String remoteName)
throws NotSupportedException { throws NotSupportedException {
return new TransportGitAnon(local, uri); return new TransportGitAnon(local, uri);
} }

View File

@ -116,7 +116,7 @@ public int getDefaultPort() {
} }
@Override @Override
public boolean canHandle(Repository local, URIish uri, String remoteName) { public boolean canHandle(URIish uri, Repository local, String remoteName) {
if (uri.getScheme() == null) { if (uri.getScheme() == null) {
// scp-style URI "host:path" does not have scheme. // scp-style URI "host:path" does not have scheme.
return uri.getHost() != null return uri.getHost() != null
@ -124,10 +124,10 @@ public boolean canHandle(Repository local, URIish uri, String remoteName) {
&& uri.getHost().length() != 0 && uri.getHost().length() != 0
&& uri.getPath().length() != 0; && uri.getPath().length() != 0;
} }
return super.canHandle(local, uri, remoteName); return super.canHandle(uri, local, remoteName);
} }
public Transport open(Repository local, URIish uri, String remoteName) public Transport open(URIish uri, Repository local, String remoteName)
throws NotSupportedException { throws NotSupportedException {
return new TransportGitSsh(local, uri); return new TransportGitSsh(local, uri);
} }

View File

@ -163,7 +163,7 @@ public int getDefaultPort() {
return 80; return 80;
} }
public Transport open(Repository local, URIish uri, String remoteName) public Transport open(URIish uri, Repository local, String remoteName)
throws NotSupportedException { throws NotSupportedException {
return new TransportHttp(local, uri); return new TransportHttp(local, uri);
} }
@ -192,7 +192,7 @@ public int getDefaultPort() {
return 21; return 21;
} }
public Transport open(Repository local, URIish uri, String remoteName) public Transport open(URIish uri, Repository local, String remoteName)
throws NotSupportedException { throws NotSupportedException {
return new TransportHttp(local, uri); return new TransportHttp(local, uri);
} }

View File

@ -104,7 +104,7 @@ public Set<String> getSchemes() {
} }
@Override @Override
public boolean canHandle(Repository local, URIish uri, String remoteName) { public boolean canHandle(URIish uri, Repository local, String remoteName) {
if (uri.getPath() == null if (uri.getPath() == null
|| uri.getPort() > 0 || uri.getPort() > 0
|| uri.getUser() != null || uri.getUser() != null
@ -116,7 +116,7 @@ public boolean canHandle(Repository local, URIish uri, String remoteName) {
} }
@Override @Override
public Transport open(Repository local, URIish uri, String remoteName) public Transport open(URIish uri, Repository local, String remoteName)
throws NoRemoteRepositoryException { throws NoRemoteRepositoryException {
// If the reference is to a local file, C Git behavior says // If the reference is to a local file, C Git behavior says
// assume this is a bundle, since repositories are directories. // assume this is a bundle, since repositories are directories.

View File

@ -138,17 +138,40 @@ public int getDefaultPort() {
* {@link #getOptionalFields()}, returning true only if all of the fields * {@link #getOptionalFields()}, returning true only if all of the fields
* match the specification. * match the specification.
* *
* @param local
* the local repository that will communicate with the other Git
* repository.
* @param uri * @param uri
* address of the Git repository; never null. * address of the Git repository; never null.
* @return true if this protocol can handle this URI; false otherwise.
*/
public boolean canHandle(URIish uri) {
return canHandle(uri, null, null);
}
/**
* Determine if this protocol can handle a particular URI.
* <p>
* Implementations should try to avoid looking at the local filesystem, but
* may look at implementation specific configuration options in the remote
* block of {@code local.getConfig()} using {@code remoteName} if the name
* is non-null.
* <p>
* The default implementation of this method matches the scheme against
* {@link #getSchemes()}, required fields against
* {@link #getRequiredFields()}, and optional fields against
* {@link #getOptionalFields()}, returning true only if all of the fields
* match the specification.
*
* @param uri
* address of the Git repository; never null.
* @param local
* the local repository that will communicate with the other Git
* repository. May be null if the caller is only asking about a
* specific URI and does not have a local Repository.
* @param remoteName * @param remoteName
* name of the remote, if the remote as configured in * name of the remote, if the remote as configured in
* {@code local}; otherwise null. * {@code local}; otherwise null.
* @return true if this protocol can handle this URI; false otherwise. * @return true if this protocol can handle this URI; false otherwise.
*/ */
public boolean canHandle(Repository local, URIish uri, String remoteName) { public boolean canHandle(URIish uri, Repository local, String remoteName) {
if (!getSchemes().isEmpty() && !getSchemes().contains(uri.getScheme())) if (!getSchemes().isEmpty() && !getSchemes().contains(uri.getScheme()))
return false; return false;
@ -213,11 +236,11 @@ public boolean canHandle(Repository local, URIish uri, String remoteName) {
* within {@code local.getConfig()} using the remote block named by the * within {@code local.getConfig()} using the remote block named by the
* {@code remoteName}, if the name is non-null. * {@code remoteName}, if the name is non-null.
* *
* @param uri
* address of the Git repository.
* @param local * @param local
* the local repository that will communicate with the other Git * the local repository that will communicate with the other Git
* repository. * repository.
* @param uri
* address of the Git repository.
* @param remoteName * @param remoteName
* name of the remote, if the remote as configured in * name of the remote, if the remote as configured in
* {@code local}; otherwise null. * {@code local}; otherwise null.
@ -227,7 +250,7 @@ public boolean canHandle(Repository local, URIish uri, String remoteName) {
* @throws TransportException * @throws TransportException
* the transport cannot open this URI. * the transport cannot open this URI.
*/ */
public abstract Transport open(Repository local, URIish uri, public abstract Transport open(URIish uri, Repository local,
String remoteName) String remoteName)
throws NotSupportedException, TransportException; throws NotSupportedException, TransportException;
} }

View File

@ -120,7 +120,7 @@ public int getDefaultPort() {
return 22; return 22;
} }
public Transport open(Repository local, URIish uri, String remoteName) public Transport open(URIish uri, Repository local, String remoteName)
throws NotSupportedException { throws NotSupportedException {
return new TransportSftp(local, uri); return new TransportSftp(local, uri);
} }