浏览代码

URIish: fall back to host as humanish name

When we have a URI that contains an empty path component (that is
it only contains a "/") we want to fall back to the host as
humanish name.

This change is according to the behavior of upstream git, which
falls back on the hostname when guessing directory names for
newly cloned repositories (see [1] for the discussion).

[1] http://article.gmane.org/gmane.comp.version-control.git/274669

Change-Id: I44400c6ab72a2722d2155d53d63671bd867d6c44
Signed-off-by: Patrick Steinhardt <ps@pks.im>
tags/v4.1.0.201509280440-r
Patrick Steinhardt 9 年前
父节点
当前提交
9b1deadcb4

+ 58
- 0
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/URIishTest.java 查看文件

* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com> * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
* Copyright (C) 2013, Robin Stocker <robin@nibor.org> * Copyright (C) 2013, Robin Stocker <robin@nibor.org>
* Copyright (C) 2015, Patrick Steinhardt <ps@pks.im>
* and other copyright owners as documented in the project's IP log. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
assertEquals(u, new URIish(str)); assertEquals(u, new URIish(str));
} }


@Test
public void testSshProtoHostOnly() throws Exception {
final String str = "ssh://example.com/";
URIish u = new URIish(str);
assertEquals("ssh", u.getScheme());
assertTrue(u.isRemote());
assertEquals("/", u.getRawPath());
assertEquals("/", u.getPath());
assertEquals("example.com", u.getHost());
assertEquals(-1, u.getPort());
assertEquals("ssh://example.com/", u.toString());
assertEquals("ssh://example.com/", u.toASCIIString());
assertEquals("example.com", u.getHumanishName());
assertEquals(u, new URIish(str));
}

@Test
public void testSshProtoHostWithAuthentication() throws Exception {
final String str = "ssh://user:secret@pass@example.com/";
URIish u = new URIish(str);
assertEquals("ssh", u.getScheme());
assertTrue(u.isRemote());
assertEquals("/", u.getRawPath());
assertEquals("/", u.getPath());
assertEquals("example.com", u.getHost());
assertEquals(-1, u.getPort());
assertEquals("ssh://user@example.com/", u.toString());
assertEquals("ssh://user@example.com/", u.toASCIIString());
assertEquals("example.com", u.getHumanishName());
assertEquals("user", u.getUser());
assertEquals("secret@pass", u.getPass());
assertEquals(u, new URIish(str));
}

@Test
public void testSshProtoHostWithPort() throws Exception {
final String str = "ssh://example.com:2222/";
URIish u = new URIish(str);
assertEquals("ssh", u.getScheme());
assertTrue(u.isRemote());
assertEquals("/", u.getRawPath());
assertEquals("/", u.getPath());
assertEquals("example.com", u.getHost());
assertEquals(2222, u.getPort());
assertEquals("ssh://example.com:2222/", u.toString());
assertEquals("ssh://example.com:2222/", u.toASCIIString());
assertEquals("example.com", u.getHumanishName());
assertEquals(u, new URIish(str));
}

@Test @Test
public void testSshProtoWithUserAndPort() throws Exception { public void testSshProtoWithUserAndPort() throws Exception {
final String str = "ssh://user@example.com:33/some/p ath"; final String str = "ssh://user@example.com:33/some/p ath";
assertEquals("abc", humanishName); assertEquals("abc", humanishName);
} }


@Test
public void testGetEmptyHumanishNameWithAuthorityOnly() throws IllegalArgumentException,
URISyntaxException {
String humanishName = new URIish(GIT_SCHEME + "abc").getHumanishName();
assertEquals("abc", humanishName);
}

@Test @Test
public void testGetValidSlashHumanishName() public void testGetValidSlashHumanishName()
throws IllegalArgumentException, URISyntaxException { throws IllegalArgumentException, URISyntaxException {

+ 14
- 6
org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java 查看文件

* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
* Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com> * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
* Copyright (C) 2013, Robin Stocker <robin@nibor.org> * Copyright (C) 2013, Robin Stocker <robin@nibor.org>
* Copyright (C) 2015, Patrick Steinhardt <ps@pks.im>
* and other copyright owners as documented in the project's IP log. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
+ OPT_USER_PWD_P // + OPT_USER_PWD_P //
+ HOST_P // + HOST_P //
+ OPT_PORT_P // + OPT_PORT_P //
+ "(" // open a catpuring group the the user-home-dir part //$NON-NLS-1$
+ (USER_HOME_P + "?") // //$NON-NLS-1$
+ "[\\\\/])" // //$NON-NLS-1$
+ "(" // open a group capturing the user-home-dir-part //$NON-NLS-1$
+ (USER_HOME_P + "?") //$NON-NLS-1$
+ "[\\\\/])" //$NON-NLS-1$
+ ")?" // close the optional group containing hostname //$NON-NLS-1$ + ")?" // close the optional group containing hostname //$NON-NLS-1$
+ "(.+)?" // //$NON-NLS-1$
+ "(.+)?" //$NON-NLS-1$
+ "$"); //$NON-NLS-1$ + "$"); //$NON-NLS-1$


/** /**
* <td><code>/path/to/repo/</code></td> * <td><code>/path/to/repo/</code></td>
* </tr> * </tr>
* <tr> * <tr>
* <td><code>localhost</code></td>
* <td><code>ssh://localhost/</code></td>
* </tr>
* <tr>
* <td><code>/path//to</code></td> * <td><code>/path//to</code></td>
* <td>an empty string</td> * <td>an empty string</td>
* </tr> * </tr>
* @see #getPath * @see #getPath
*/ */
public String getHumanishName() throws IllegalArgumentException { public String getHumanishName() throws IllegalArgumentException {
if ("".equals(getPath()) || getPath() == null) //$NON-NLS-1$
throw new IllegalArgumentException();
String s = getPath(); String s = getPath();
if ("/".equals(s)) //$NON-NLS-1$
s = getHost();
if ("".equals(s) || s == null) //$NON-NLS-1$
throw new IllegalArgumentException();

String[] elements; String[] elements;
if ("file".equals(scheme) || LOCAL_FILE.matcher(s).matches()) //$NON-NLS-1$ if ("file".equals(scheme) || LOCAL_FILE.matcher(s).matches()) //$NON-NLS-1$
elements = s.split("[\\" + File.separatorChar + "/]"); //$NON-NLS-1$ //$NON-NLS-2$ elements = s.split("[\\" + File.separatorChar + "/]"); //$NON-NLS-1$ //$NON-NLS-2$

正在加载...
取消
保存