diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Clone.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Clone.java index a136df561..3fe50d668 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Clone.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Clone.java @@ -93,15 +93,11 @@ protected void run() throws Exception { 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"); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/URIishTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/URIishTest.java index 2598fdc1f..d2d37a7ed 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/URIishTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/URIishTest.java @@ -1,4 +1,5 @@ /* + * Copyright (C) 2009, Mykola Nikishov * Copyright (C) 2008, Robin Rosenberg * Copyright (C) 2008, Shawn O. Pearce * 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 void testSshProtoWithUserPassAndPort() throws Exception { 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); + } + } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java index e1df85248..1f7bad183 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java @@ -1,4 +1,5 @@ /* + * Copyright (C) 2009, Mykola Nikishov * Copyright (C) 2008, Robin Rosenberg * Copyright (C) 2008, Shawn O. Pearce * and other copyright owners as documented in the project's IP log. @@ -56,6 +57,8 @@ * 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 @@ private String format(final boolean includePassword) { return r.toString(); } + + /** + * Get the "humanish" part of the path. Some examples of a 'humanish' part + * for a full path: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
PathHumanish part
/path/to/repo.gitrepo
/path/to/repo.git/
/path/to/repo/.git
/path/to/repo/
/path//toan empty string
+ * + * @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; + } + }