Add support for single-slash URI
In bug 323571 it is mentioned that if you call 'toURI().toURL().toString()' on a java.io.File you cannot pass that string to jgit as an URIish. Problem is that the passed URI looks like 'file:/C:/a/b.txt' and that we where expecting double slashes after scheme':'. This fix adds support for this single-slash file URLs. Bug: 323571 Change-Id: I866a76a4fcd0c3b58e0d26a104fc4564e7ba5999 Signed-off-by: Christian Halstrick <christian.halstrick@sap.com>
This commit is contained in:
parent
467f3de0f5
commit
be2ddff6a7
|
@ -45,6 +45,8 @@
|
||||||
|
|
||||||
package org.eclipse.jgit.transport;
|
package org.eclipse.jgit.transport;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
@ -448,6 +450,41 @@ public void testUserPasswordAndPort() throws URISyntaxException {
|
||||||
assertEquals(u, new URIish(str));
|
assertEquals(u, new URIish(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testFileProtocol() throws IllegalArgumentException,
|
||||||
|
URISyntaxException, IOException {
|
||||||
|
// as defined by git docu
|
||||||
|
URIish u = new URIish("file:///a/b.txt");
|
||||||
|
assertEquals("file", u.getScheme());
|
||||||
|
assertFalse(u.isRemote());
|
||||||
|
assertNull(u.getHost());
|
||||||
|
assertNull(u.getPass());
|
||||||
|
assertEquals("/a/b.txt", u.getPath());
|
||||||
|
assertEquals(-1, u.getPort());
|
||||||
|
assertNull(u.getUser());
|
||||||
|
assertEquals("b.txt", u.getHumanishName());
|
||||||
|
|
||||||
|
File tmp = File.createTempFile("jgitUnitTest", ".tmp");
|
||||||
|
u = new URIish(tmp.toURI().toString());
|
||||||
|
assertEquals("file", u.getScheme());
|
||||||
|
assertFalse(u.isRemote());
|
||||||
|
assertNull(u.getHost());
|
||||||
|
assertNull(u.getPass());
|
||||||
|
assertTrue(u.getPath().contains("jgitUnitTest"));
|
||||||
|
assertEquals(-1, u.getPort());
|
||||||
|
assertNull(u.getUser());
|
||||||
|
assertTrue(u.getHumanishName().startsWith("jgitUnitTest"));
|
||||||
|
|
||||||
|
u = new URIish("file:/a/b.txt");
|
||||||
|
assertEquals("file", u.getScheme());
|
||||||
|
assertFalse(u.isRemote());
|
||||||
|
assertNull(u.getHost());
|
||||||
|
assertNull(u.getPass());
|
||||||
|
assertEquals("/a/b.txt", u.getPath());
|
||||||
|
assertEquals(-1, u.getPort());
|
||||||
|
assertNull(u.getUser());
|
||||||
|
assertEquals("b.txt", u.getHumanishName());
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
|
@ -143,7 +143,17 @@ public class URIish implements Serializable {
|
||||||
+ "$");
|
+ "$");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A pattern matching SCP URI's of the form user@host:path/to/repo.git
|
* A pattern matching a URI for the scheme 'file' which has only ':/' as
|
||||||
|
* separator between scheme and path. Standard file URIs have '://' as
|
||||||
|
* separator, but java.io.File.toURI() constructs those URIs.
|
||||||
|
*/
|
||||||
|
private static final Pattern SINGLE_SLASH_FILE_URI = Pattern.compile("^" //
|
||||||
|
+ "(file):(/(?!/)" //
|
||||||
|
+ PATH_P //
|
||||||
|
+ ")$");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A pattern matching a SCP URI's of the form user@host:path/to/repo.git
|
||||||
*/
|
*/
|
||||||
private static final Pattern SCP_URI = Pattern.compile("^" //
|
private static final Pattern SCP_URI = Pattern.compile("^" //
|
||||||
+ OPT_USER_PWD_P //
|
+ OPT_USER_PWD_P //
|
||||||
|
@ -173,7 +183,12 @@ public class URIish implements Serializable {
|
||||||
*/
|
*/
|
||||||
public URIish(String s) throws URISyntaxException {
|
public URIish(String s) throws URISyntaxException {
|
||||||
s = s.replace('\\', '/');
|
s = s.replace('\\', '/');
|
||||||
Matcher matcher = FULL_URI.matcher(s);
|
Matcher matcher = SINGLE_SLASH_FILE_URI.matcher(s);
|
||||||
|
if (matcher.matches()) {
|
||||||
|
scheme = matcher.group(1);
|
||||||
|
path = cleanLeadingSlashes(matcher.group(2), scheme);
|
||||||
|
} else {
|
||||||
|
matcher = FULL_URI.matcher(s);
|
||||||
if (matcher.matches()) {
|
if (matcher.matches()) {
|
||||||
scheme = matcher.group(1);
|
scheme = matcher.group(1);
|
||||||
user = matcher.group(2);
|
user = matcher.group(2);
|
||||||
|
@ -182,7 +197,8 @@ public URIish(String s) throws URISyntaxException {
|
||||||
if (matcher.group(5) != null)
|
if (matcher.group(5) != null)
|
||||||
port = Integer.parseInt(matcher.group(5));
|
port = Integer.parseInt(matcher.group(5));
|
||||||
path = cleanLeadingSlashes(
|
path = cleanLeadingSlashes(
|
||||||
n2e(matcher.group(6)) + n2e(matcher.group(7)), scheme);
|
n2e(matcher.group(6)) + n2e(matcher.group(7)),
|
||||||
|
scheme);
|
||||||
} else {
|
} else {
|
||||||
matcher = SCP_URI.matcher(s);
|
matcher = SCP_URI.matcher(s);
|
||||||
if (matcher.matches()) {
|
if (matcher.matches()) {
|
||||||
|
@ -200,6 +216,7 @@ public URIish(String s) throws URISyntaxException {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String n2e(String s) {
|
private String n2e(String s) {
|
||||||
if (s == null)
|
if (s == null)
|
||||||
|
|
Loading…
Reference in New Issue