]> source.dussan.org Git - jgit.git/commitdiff
Method to get a 'humanish' name from a path 15/115/5
authorMykola Nikishov <mn@mn.com.ua>
Thu, 17 Dec 2009 23:03:51 +0000 (01:03 +0200)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Fri, 18 Dec 2009 23:47:06 +0000 (00:47 +0100)
Change-Id: Iec0688232bd59d4626111d77633109918e8e1df3
Signed-off-by: Mykola Nikishov <mn@mn.com.ua>
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Clone.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/URIishTest.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java

index a136df5618761b192824799c91728470232dd890..3fe50d668242c56e9f055208894e119de33d4e43 100644 (file)
@@ -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");
index 2598fdc1f53f1bd9ff1f5dcaee34b679fef90489..d2d37a7edcc1e640d6e2bbe7c21aecd769928aba 100644 (file)
@@ -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.
 
 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);
+       }
+
 }
index e1df85248eb1858600d3d8a7317c94fa53beadb2..1f7bad18386fa25406bf662eea1cfe0f60095bc7 100644 (file)
@@ -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;
+       }
+
 }