Browse Source

Method to get a 'humanish' name from a path

Change-Id: Iec0688232bd59d4626111d77633109918e8e1df3
Signed-off-by: Mykola Nikishov <mn@mn.com.ua>
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
stable-0.7
Mykola Nikishov 15 years ago committed by Robin Rosenberg
parent
commit
21b6f3434e
  1. 12
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Clone.java
  2. 122
      org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/URIishTest.java
  3. 52
      org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java

12
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Clone.java

@ -93,15 +93,11 @@ class Clone extends AbstractFetchCommand {
final URIish uri = new URIish(sourceUri);
if (localName == null) {
String p = uri.getPath();
while (p.endsWith("/"))
p = p.substring(0, p.length() - 1);
final int s = p.lastIndexOf('/');
if (s < 0)
try {
localName = uri.getHumanishName();
} catch (IllegalArgumentException e) {
throw die("cannot guess local name from " + sourceUri);
localName = p.substring(s + 1);
if (localName.endsWith(".git"))
localName = localName.substring(0, localName.length() - 4);
}
}
if (gitdir == null)
gitdir = new File(localName, ".git");

122
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/URIishTest.java

@ -1,4 +1,5 @@
/*
* Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
* and other copyright owners as documented in the project's IP log.
@ -44,10 +45,14 @@
package org.eclipse.jgit.transport;
import java.net.URISyntaxException;
import junit.framework.TestCase;
public class URIishTest extends TestCase {
private static final String GIT_SCHEME = "git://";
public void testUnixFile() throws Exception {
final String str = "/home/m y";
URIish u = new URIish(str);
@ -244,4 +249,121 @@ public class URIishTest extends TestCase {
assertEquals(u.setPass(null).toPrivateString(), u.toString());
assertEquals(u, new URIish(str));
}
public void testGetNullHumanishName() {
try {
new URIish().getHumanishName();
fail("path must be not null");
} catch (IllegalArgumentException e) {
// expected
}
}
public void testGetEmptyHumanishName() throws URISyntaxException {
try {
new URIish(GIT_SCHEME).getHumanishName();
fail("empty path is useless");
} catch (IllegalArgumentException e) {
// expected
}
}
public void testGetAbsEmptyHumanishName() {
try {
new URIish().getHumanishName();
fail("empty path is useless");
} catch (IllegalArgumentException e) {
// expected
}
}
public void testGetValidWithEmptySlashDotGitHumanishName()
throws IllegalArgumentException, URISyntaxException {
String humanishName = new URIish("/a/b/.git").getHumanishName();
assertEquals("b", humanishName);
}
public void testGetWithSlashDotGitHumanishName() throws URISyntaxException {
assertEquals("", new URIish("/.git").getHumanishName());
}
public void testGetTwoSlashesDotGitHumanishName() throws URISyntaxException {
assertEquals("", new URIish("/.git").getHumanishName());
}
public void testGetValidHumanishName() throws IllegalArgumentException,
URISyntaxException {
String humanishName = new URIish(GIT_SCHEME + "abc").getHumanishName();
assertEquals("abc", humanishName);
}
public void testGetValidSlashHumanishName()
throws IllegalArgumentException, URISyntaxException {
String humanishName = new URIish(GIT_SCHEME + "abc/").getHumanishName();
assertEquals("abc", humanishName);
}
public void testGetSlashValidSlashHumanishName()
throws IllegalArgumentException, URISyntaxException {
String humanishName = new URIish("/abc/").getHumanishName();
assertEquals("abc", humanishName);
}
public void testGetSlashValidSlashDotGitSlashHumanishName()
throws IllegalArgumentException, URISyntaxException {
String humanishName = new URIish("/abc/.git").getHumanishName();
assertEquals("abc", humanishName);
}
public void testGetSlashSlashDotGitSlashHumanishName()
throws IllegalArgumentException, URISyntaxException {
final String humanishName = new URIish(GIT_SCHEME + "/abc//.git")
.getHumanishName();
assertEquals("may return an empty humanish name", "", humanishName);
}
public void testGetSlashesValidSlashHumanishName()
throws IllegalArgumentException, URISyntaxException {
String humanishName = new URIish("/a/b/c/").getHumanishName();
assertEquals("c", humanishName);
}
public void testGetValidDotGitHumanishName()
throws IllegalArgumentException, URISyntaxException {
String humanishName = new URIish(GIT_SCHEME + "abc.git")
.getHumanishName();
assertEquals("abc", humanishName);
}
public void testGetValidDotGitSlashHumanishName()
throws IllegalArgumentException, URISyntaxException {
String humanishName = new URIish(GIT_SCHEME + "abc.git/")
.getHumanishName();
assertEquals("abc", humanishName);
}
public void testGetValidWithSlashDotGitHumanishName()
throws IllegalArgumentException, URISyntaxException {
String humanishName = new URIish("/abc.git").getHumanishName();
assertEquals("abc", humanishName);
}
public void testGetValidWithSlashDotGitSlashHumanishName()
throws IllegalArgumentException, URISyntaxException {
String humanishName = new URIish("/abc.git/").getHumanishName();
assertEquals("abc", humanishName);
}
public void testGetValidWithSlashesDotGitHumanishName()
throws IllegalArgumentException, URISyntaxException {
String humanishName = new URIish("/a/b/c.git").getHumanishName();
assertEquals("c", humanishName);
}
public void testGetValidWithSlashesDotGitSlashHumanishName()
throws IllegalArgumentException, URISyntaxException {
String humanishName = new URIish("/a/b/c.git/").getHumanishName();
assertEquals("c", humanishName);
}
}

52
org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java

@ -1,4 +1,5 @@
/*
* Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
* and other copyright owners as documented in the project's IP log.
@ -56,6 +57,8 @@ import java.util.regex.Pattern;
* any special character is written as-is.
*/
public class URIish {
private static final String DOT_GIT = ".git";
private static final Pattern FULL_URI = Pattern
.compile("^(?:([a-z][a-z0-9+-]+)://(?:([^/]+?)(?::([^/]+?))?@)?(?:([^/]+?))?(?::(\\d+))?)?((?:[A-Za-z]:)?/.+)$");
@ -363,4 +366,53 @@ public class URIish {
return r.toString();
}
/**
* Get the "humanish" part of the path. Some examples of a 'humanish' part
* for a full path:
* <table>
* <tr>
* <th>Path</th>
* <th>Humanish part</th>
* </tr>
* <tr>
* <td><code>/path/to/repo.git</code></td>
* <td rowspan="4"><code>repo</code></td>
* </tr>
* <tr>
* <td><code>/path/to/repo.git/</code></td>
* </tr>
* <tr>
* <td><code>/path/to/repo/.git</code></td>
* </tr>
* <tr>
* <td><code>/path/to/repo/</code></td>
* </tr>
* <tr>
* <td><code>/path//to</code></td>
* <td>an empty string</td>
* </tr>
* </table>
*
* @return the "humanish" part of the path. May be an empty string. Never
* {@code null}.
* @throws IllegalArgumentException
* if it's impossible to determine a humanish part, or path is
* {@code null} or empty
* @see #getPath
*/
public String getHumanishName() throws IllegalArgumentException {
if ("".equals(getPath()) || getPath() == null)
throw new IllegalArgumentException();
String[] elements = getPath().split("/");
if (elements.length == 0)
throw new IllegalArgumentException();
String result = elements[elements.length - 1];
if (DOT_GIT.equals(result))
result = elements[elements.length - 2];
else if (result.endsWith(DOT_GIT))
result = result.substring(0, result.length() - DOT_GIT.length());
return result;
}
}

Loading…
Cancel
Save